diff --git a/Jint.Benchmark/EngineComparisonBenchmark.cs b/Jint.Benchmark/EngineComparisonBenchmark.cs index dae5305baf..d7e2c87028 100644 --- a/Jint.Benchmark/EngineComparisonBenchmark.cs +++ b/Jint.Benchmark/EngineComparisonBenchmark.cs @@ -40,7 +40,7 @@ public class EngineComparisonBenchmark [GlobalSetup] public void Setup() { - var javaScriptParser = new Parser(Engine.s_defaultParserOptions); + var javaScriptParser = new Parser(Engine.BaseParserOptions); foreach (var fileName in _files.Keys.ToList()) { var script = File.ReadAllText($"Scripts/{fileName}.js"); diff --git a/Jint.Benchmark/EngineConstructionBenchmark.cs b/Jint.Benchmark/EngineConstructionBenchmark.cs index bd15e903a1..98db900818 100644 --- a/Jint.Benchmark/EngineConstructionBenchmark.cs +++ b/Jint.Benchmark/EngineConstructionBenchmark.cs @@ -14,7 +14,7 @@ public class EngineConstructionBenchmark [GlobalSetup] public void GlobalSetup() { - var parser = new Parser(Engine.s_defaultParserOptions); + var parser = new Parser(Engine.BaseParserOptions); _program = parser.ParseScript("([].length + ''.length)"); _simple = parser.ParseScript("1"); new Engine().Evaluate(_program); diff --git a/Jint.Tests/Runtime/EngineTests.cs b/Jint.Tests/Runtime/EngineTests.cs index f5c8e4fa2b..cbb9830f3e 100644 --- a/Jint.Tests/Runtime/EngineTests.cs +++ b/Jint.Tests/Runtime/EngineTests.cs @@ -1298,8 +1298,8 @@ public void ShouldExecuteDromaeoBase64() public void ShouldExecuteKnockoutWithoutErrorWhetherTolerantOrIntolerant() { var content = GetEmbeddedFile("knockout-3.4.0.js"); - _engine.Execute(content, Engine.s_defaultParserOptions with { Tolerant = true }); - _engine.Execute(content, Engine.s_defaultParserOptions with { Tolerant = false }); + _engine.Execute(content, Engine.BaseParserOptions with { Tolerant = true }); + _engine.Execute(content, Engine.BaseParserOptions with { Tolerant = false }); } [Fact] @@ -1316,7 +1316,7 @@ public void ShouldNotAllowDuplicateProtoProperty() { var code = "if({ __proto__: [], __proto__:[] } instanceof Array) {}"; - Exception ex = Assert.Throws(() => _engine.Execute(code, Engine.s_defaultParserOptions with { Tolerant = false })); + Exception ex = Assert.Throws(() => _engine.Execute(code, Engine.BaseParserOptions with { Tolerant = false })); Assert.Contains("Duplicate __proto__ fields are not allowed in object literals", ex.Message); ex = Assert.Throws(() => _engine.Execute($"eval('{code}')")); @@ -2946,7 +2946,7 @@ public void ExecuteWithSourceShouldTriggerBeforeEvaluateEvent() public void ExecuteWithParserOptionsShouldTriggerBeforeEvaluateEvent() { TestBeforeEvaluateEvent( - (engine, code) => engine.Execute(code, Engine.s_defaultParserOptions), + (engine, code) => engine.Execute(code, Engine.BaseParserOptions), expectedSource: "" ); } @@ -2955,7 +2955,7 @@ public void ExecuteWithParserOptionsShouldTriggerBeforeEvaluateEvent() public void ExecuteWithSourceAndParserOptionsShouldTriggerBeforeEvaluateEvent() { TestBeforeEvaluateEvent( - (engine, code) => engine.Execute(code, "mysource", Engine.s_defaultParserOptions), + (engine, code) => engine.Execute(code, "mysource", Engine.BaseParserOptions), expectedSource: "mysource" ); } @@ -2982,7 +2982,7 @@ public void EvaluateWithSourceShouldTriggerBeforeEvaluateEvent() public void EvaluateWithParserOptionsShouldTriggerBeforeEvaluateEvent() { TestBeforeEvaluateEvent( - (engine, code) => engine.Evaluate(code, Engine.s_defaultParserOptions), + (engine, code) => engine.Evaluate(code, Engine.BaseParserOptions), expectedSource: "" ); } @@ -2991,7 +2991,7 @@ public void EvaluateWithParserOptionsShouldTriggerBeforeEvaluateEvent() public void EvaluateWithSourceAndParserOptionsShouldTriggerBeforeEvaluateEvent() { TestBeforeEvaluateEvent( - (engine, code) => engine.Evaluate(code, "mysource", Engine.s_defaultParserOptions), + (engine, code) => engine.Evaluate(code, "mysource", Engine.BaseParserOptions), expectedSource: "mysource" ); } diff --git a/Jint.Tests/Runtime/ErrorTests.cs b/Jint.Tests/Runtime/ErrorTests.cs index 8e85eebb8d..fc9cabf0d8 100644 --- a/Jint.Tests/Runtime/ErrorTests.cs +++ b/Jint.Tests/Runtime/ErrorTests.cs @@ -295,7 +295,7 @@ public void StackTraceCollectedForImmediatelyInvokedFunctionExpression() return item; })(getItem);"; - var parserOptions = Engine.s_defaultParserOptions with + var parserOptions = Engine.BaseParserOptions with { Tolerant = true }; @@ -385,7 +385,7 @@ public void CallStackWorksWithRecursiveCalls() { static ParserOptions CreateParserOptions() { - return Engine.s_defaultParserOptions with + return Engine.BaseParserOptions with { Tolerant = true }; diff --git a/Jint/Engine.Ast.cs b/Jint/Engine.Ast.cs index fc8f16f471..3f4545c65d 100644 --- a/Jint/Engine.Ast.cs +++ b/Jint/Engine.Ast.cs @@ -13,12 +13,6 @@ namespace Jint; public partial class Engine { - internal static readonly ParserOptions s_defaultParserOptions = ParserOptions.Default with - { - EcmaVersion = EcmaVersion.Experimental, - RegExpParseMode = RegExpParseMode.AdaptToInterpreted, - }; - /// /// Prepares a script for the engine that includes static analysis data to speed up execution during run-time. /// @@ -28,7 +22,7 @@ public partial class Engine public static Script PrepareScript(string script, string? source = null, bool strict = false) { var astAnalyzer = new AstAnalyzer(new ScriptPreparationOptions()); - var options = s_defaultParserOptions with + var options = BaseParserOptions with { AllowReturnOutsideFunction = true, OnNode = astAnalyzer.NodeVisitor @@ -46,7 +40,7 @@ public static Script PrepareScript(string script, string? source = null, bool st public static Module PrepareModule(string script, string? source = null) { var astAnalyzer = new AstAnalyzer(new ScriptPreparationOptions()); - var options = s_defaultParserOptions with + var options = BaseParserOptions with { OnNode = astAnalyzer.NodeVisitor }; diff --git a/Jint/Engine.cs b/Jint/Engine.cs index ed9ae1308a..40e68e26a1 100644 --- a/Jint/Engine.cs +++ b/Jint/Engine.cs @@ -29,6 +29,12 @@ namespace Jint [DebuggerTypeProxy(typeof(EngineDebugView))] public sealed partial class Engine : IDisposable { + internal static readonly ParserOptions BaseParserOptions = ParserOptions.Default with + { + EcmaVersion = EcmaVersion.Experimental, + RegExpParseMode = RegExpParseMode.AdaptToInterpreted, + }; + private static readonly Options _defaultEngineOptions = new(); private readonly ParserOptions _defaultParserOptions; @@ -146,7 +152,7 @@ private Engine(Options? options, Action? configure) CallStack = new JintCallStack(Options.Constraints.MaxRecursionDepth >= 0); _stackGuard = new StackGuard(this); - _defaultParserOptions = s_defaultParserOptions with + _defaultParserOptions = BaseParserOptions with { AllowReturnOutsideFunction = true, RegexTimeout = Options.Constraints.RegexTimeout diff --git a/Jint/Native/Function/ClassDefinition.cs b/Jint/Native/Function/ClassDefinition.cs index 32b8b7ae6f..dbe9f92aa5 100644 --- a/Jint/Native/Function/ClassDefinition.cs +++ b/Jint/Native/Function/ClassDefinition.cs @@ -23,7 +23,7 @@ internal sealed class ClassDefinition static ClassDefinition() { - var parser = new Parser(Engine.s_defaultParserOptions); + var parser = new Parser(Engine.BaseParserOptions); // generate missing constructor AST only once static MethodDefinition CreateConstructorMethodDefinition(Parser parser, string source) diff --git a/Jint/Native/Function/EvalFunction.cs b/Jint/Native/Function/EvalFunction.cs index 28831d688e..89049423ef 100644 --- a/Jint/Native/Function/EvalFunction.cs +++ b/Jint/Native/Function/EvalFunction.cs @@ -12,7 +12,7 @@ public sealed class EvalFunction : Function { private static readonly JsString _functionName = new("eval"); - private static readonly ParserOptions _parserOptions = Engine.s_defaultParserOptions with + private static readonly ParserOptions _parserOptions = Engine.BaseParserOptions with { AllowNewTargetOutsideFunction = true, AllowSuperOutsideMethod = true, diff --git a/Jint/Native/Function/FunctionInstance.Dynamic.cs b/Jint/Native/Function/FunctionInstance.Dynamic.cs index 4869dc713e..98061f106a 100644 --- a/Jint/Native/Function/FunctionInstance.Dynamic.cs +++ b/Jint/Native/Function/FunctionInstance.Dynamic.cs @@ -144,7 +144,7 @@ internal Function CreateDynamicFunction( } } - Parser parser = new Parser(Engine.s_defaultParserOptions with + Parser parser = new Parser(Engine.BaseParserOptions with { RegexTimeout = _engine.Options.Constraints.RegexTimeout }); diff --git a/Jint/Native/ShadowRealm/ShadowRealm.cs b/Jint/Native/ShadowRealm/ShadowRealm.cs index 55e0f08aac..4d7b1834fb 100644 --- a/Jint/Native/ShadowRealm/ShadowRealm.cs +++ b/Jint/Native/ShadowRealm/ShadowRealm.cs @@ -27,7 +27,7 @@ public sealed class ShadowRealm : ObjectInstance internal ShadowRealm(Engine engine, ExecutionContext executionContext, Realm shadowRealm) : base(engine) { - _parser = new(Engine.s_defaultParserOptions with + _parser = new(Engine.BaseParserOptions with { RegexTimeout = engine.Options.Constraints.RegexTimeout }); diff --git a/Jint/Runtime/Debugger/DebugHandler.cs b/Jint/Runtime/Debugger/DebugHandler.cs index 2858b7ddaf..e92360b13f 100644 --- a/Jint/Runtime/Debugger/DebugHandler.cs +++ b/Jint/Runtime/Debugger/DebugHandler.cs @@ -126,7 +126,7 @@ public JsValue Evaluate(Script script) /// public JsValue Evaluate(string source, ParserOptions? options = null) { - options ??= Engine.s_defaultParserOptions; + options ??= Engine.BaseParserOptions; var parser = new Parser(options); try { diff --git a/Jint/Runtime/Modules/ModuleBuilder.cs b/Jint/Runtime/Modules/ModuleBuilder.cs index db123bf291..e0ee014e04 100644 --- a/Jint/Runtime/Modules/ModuleBuilder.cs +++ b/Jint/Runtime/Modules/ModuleBuilder.cs @@ -19,7 +19,7 @@ internal ModuleBuilder(Engine engine, string specifier) { _engine = engine; _specifier = specifier; - _options = Engine.s_defaultParserOptions with + _options = Engine.BaseParserOptions with { RegexTimeout = engine.Options.Constraints.RegexTimeout }; diff --git a/Jint/Runtime/Modules/ModuleFactory.cs b/Jint/Runtime/Modules/ModuleFactory.cs index 2da19b6f94..59aa4eae6a 100644 --- a/Jint/Runtime/Modules/ModuleFactory.cs +++ b/Jint/Runtime/Modules/ModuleFactory.cs @@ -26,7 +26,7 @@ public static Module BuildSourceTextModule(Engine engine, ResolvedSpecifier reso Acornima.Ast.Module module; try { - module = new Parser(Engine.s_defaultParserOptions).ParseModule(code, source); + module = new Parser(Engine.BaseParserOptions).ParseModule(code, source); } catch (ParseErrorException ex) {