EngineTests.ScriptPreparation.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Text.RegularExpressions;
  2. using Jint.Native;
  3. using Jint.Runtime.Interpreter;
  4. using Jint.Runtime.Interpreter.Expressions;
  5. using Jint.Runtime.Interpreter.Statements;
  6. namespace Jint.Tests.Runtime;
  7. public partial class EngineTests
  8. {
  9. [Fact]
  10. public void ScriptPreparationAcceptsReturnOutsideOfFunctions()
  11. {
  12. var preparedScript = Engine.PrepareScript("return 1;");
  13. Assert.IsType<ReturnStatement>(preparedScript.Program.Body[0]);
  14. }
  15. [Fact]
  16. public void CanPreCompileRegex()
  17. {
  18. var script = Engine.PrepareScript("var x = /[cgt]/ig; var y = /[cgt]/ig; 'g'.match(x).length;");
  19. var declaration = Assert.IsType<VariableDeclaration>(script.Program.Body[0]);
  20. var init = Assert.IsType<RegExpLiteral>(declaration.Declarations[0].Init);
  21. Assert.Equal("[cgt]", init.Value.ToString());
  22. Assert.Equal(RegexOptions.Compiled, init.Value.Options & RegexOptions.Compiled);
  23. Assert.Equal(1, _engine.Evaluate(script));
  24. }
  25. [Fact]
  26. public void ScriptPreparationFoldsConstants()
  27. {
  28. var preparedScript = Engine.PrepareScript("return 1 + 2;");
  29. var returnStatement = Assert.IsType<ReturnStatement>(preparedScript.Program.Body[0]);
  30. var constant = Assert.IsType<JintConstantExpression>(returnStatement.Argument?.UserData);
  31. Assert.Equal(3, constant.GetValue(null!));
  32. Assert.Equal(3, _engine.Evaluate(preparedScript));
  33. }
  34. [Fact]
  35. public void ScriptPreparationOptimizesNegatingUnaryExpression()
  36. {
  37. var preparedScript = Engine.PrepareScript("-1");
  38. var expression = Assert.IsType<NonSpecialExpressionStatement>(preparedScript.Program.Body[0]);
  39. var unaryExpression = Assert.IsType<NonUpdateUnaryExpression>(expression.Expression);
  40. var constant = Assert.IsType<JintConstantExpression>(unaryExpression.UserData);
  41. Assert.Equal(-1, constant.GetValue(null!));
  42. Assert.Equal(-1, _engine.Evaluate(preparedScript));
  43. }
  44. [Fact]
  45. public void ScriptPreparationOptimizesConstantReturn()
  46. {
  47. var preparedScript = Engine.PrepareScript("return false;");
  48. var statement = Assert.IsType<ReturnStatement>(preparedScript.Program.Body[0]);
  49. var returnStatement = Assert.IsType<ConstantStatement>(statement.UserData);
  50. var builtStatement = JintStatement.Build(statement);
  51. Assert.Same(returnStatement, builtStatement);
  52. var result = builtStatement.Execute(new EvaluationContext(_engine)).Value;
  53. Assert.Equal(JsBoolean.False, result);
  54. }
  55. [Fact]
  56. public void CompiledRegexShouldProduceSameResultAsNonCompiled()
  57. {
  58. const string Script = """JSON.stringify(/(.*?)a(?!(a+)b\2c)\2(.*)/.exec("baaabaac"))""";
  59. var nonCompiled = _engine.Evaluate(Script);
  60. var compiled = _engine.Evaluate(Engine.PrepareScript(Script));
  61. Assert.Equal(nonCompiled, compiled);
  62. }
  63. }