From b27b0171cc44ed9b0e59e6749019bf40e8e82000 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sun, 31 Mar 2024 13:42:33 +0300 Subject: [PATCH 1/2] Add more Acornima friendly type aliases --- Directory.Build.props | 7 +- Jint.Benchmark/EngineComparisonBenchmark.cs | 4 +- Jint.Benchmark/EngineConstructionBenchmark.cs | 2 +- Jint.Benchmark/Jint.Benchmark.csproj | 1 + Jint.Tests.CommonScripts/ConcurrencyTest.cs | 2 +- .../ShadowRealmTests.cs | 2 +- Jint.Tests.Test262/Test262Test.cs | 4 +- Jint.Tests/Parser/JavascriptParserTests.cs | 305 +++++++++--------- Jint.Tests/Runtime/Debugger/EvaluateTests.cs | 2 +- Jint.Tests/Runtime/EngineTests.cs | 4 +- Jint/Engine.Ast.cs | 6 +- Jint/Engine.cs | 8 +- Jint/EsprimaExtensions.cs | 4 +- Jint/JsValueExtensions.cs | 2 +- Jint/Native/Function/ClassDefinition.cs | 2 +- Jint/Native/Function/EvalFunction.cs | 4 +- .../Function/FunctionInstance.Dynamic.cs | 4 +- Jint/Native/JsTypedArray.cs | 2 +- Jint/Native/ShadowRealm/ShadowRealm.cs | 4 +- Jint/Runtime/Debugger/DebugHandler.cs | 4 +- .../Environments/FunctionEnvironment.cs | 2 +- .../BindingPatternAssignmentExpression.cs | 18 +- .../Expressions/JintAssignmentExpression.cs | 2 +- .../Expressions/JintLiteralExpression.cs | 14 +- .../Statements/JintForInForOfStatement.cs | 6 +- .../Statements/JintVariableDeclaration.cs | 6 +- Jint/Runtime/Modules/ModuleBuilder.cs | 6 +- Jint/Runtime/Modules/ModuleFactory.cs | 6 +- Jint/Runtime/TypeConverter.cs | 2 +- 29 files changed, 220 insertions(+), 215 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 81faa74db7..ae0709f9b1 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -30,8 +30,13 @@ - + + + + + + diff --git a/Jint.Benchmark/EngineComparisonBenchmark.cs b/Jint.Benchmark/EngineComparisonBenchmark.cs index b3c1ce178a..9dba692148 100644 --- a/Jint.Benchmark/EngineComparisonBenchmark.cs +++ b/Jint.Benchmark/EngineComparisonBenchmark.cs @@ -38,7 +38,7 @@ public class EngineComparisonBenchmark [GlobalSetup] public void Setup() { - var javaScriptParser = new JavaScriptParser(); + var parser = new Parser(); foreach (var fileName in _files.Keys.ToList()) { var script = File.ReadAllText($"Scripts/{fileName}.js"); @@ -47,7 +47,7 @@ public void Setup() script = _dromaeoHelpers + Environment.NewLine + Environment.NewLine + script; } _files[fileName] = script; - _parsedScripts[fileName] = javaScriptParser.ParseScript(script, strict: true); + _parsedScripts[fileName] = parser.ParseScript(script, strict: true); } } diff --git a/Jint.Benchmark/EngineConstructionBenchmark.cs b/Jint.Benchmark/EngineConstructionBenchmark.cs index 709f17a024..0b7ce7937a 100644 --- a/Jint.Benchmark/EngineConstructionBenchmark.cs +++ b/Jint.Benchmark/EngineConstructionBenchmark.cs @@ -12,7 +12,7 @@ public class EngineConstructionBenchmark [GlobalSetup] public void GlobalSetup() { - var parser = new JavaScriptParser(); + var parser = new Parser(); _program = parser.ParseScript("([].length + ''.length)"); _simple = parser.ParseScript("1"); new Engine().Evaluate(_program); diff --git a/Jint.Benchmark/Jint.Benchmark.csproj b/Jint.Benchmark/Jint.Benchmark.csproj index 223d04007d..116978076f 100644 --- a/Jint.Benchmark/Jint.Benchmark.csproj +++ b/Jint.Benchmark/Jint.Benchmark.csproj @@ -34,5 +34,6 @@ + \ No newline at end of file diff --git a/Jint.Tests.CommonScripts/ConcurrencyTest.cs b/Jint.Tests.CommonScripts/ConcurrencyTest.cs index 826ab95ca8..4fdf505353 100644 --- a/Jint.Tests.CommonScripts/ConcurrencyTest.cs +++ b/Jint.Tests.CommonScripts/ConcurrencyTest.cs @@ -11,7 +11,7 @@ public void ConcurrentEnginesCanUseSameAst(bool prepared) var scriptContents = SunSpiderTests.GetEmbeddedFile("babel-standalone.js"); var script = prepared ? Engine.PrepareScript(scriptContents) - : new JavaScriptParser().ParseScript(scriptContents); + : new Parser().ParseScript(scriptContents); Parallel.ForEach(Enumerable.Range(0, 3), x => { diff --git a/Jint.Tests.PublicInterface/ShadowRealmTests.cs b/Jint.Tests.PublicInterface/ShadowRealmTests.cs index 60a85cbe54..3d6199bece 100644 --- a/Jint.Tests.PublicInterface/ShadowRealmTests.cs +++ b/Jint.Tests.PublicInterface/ShadowRealmTests.cs @@ -83,7 +83,7 @@ public void CanReuseScriptWithShadowRealm() var shadowRealm2 = engine.Intrinsics.ShadowRealm.Construct(); shadowRealm2.SetValue("message", "realm 2"); - var parser = new Esprima.JavaScriptParser(); + var parser = new Parser(); var script = parser.ParseScript("(function hello() {return \"hello \" + message})();"); // Act & Assert diff --git a/Jint.Tests.Test262/Test262Test.cs b/Jint.Tests.Test262/Test262Test.cs index 811a6c1921..153daa8684 100644 --- a/Jint.Tests.Test262/Test262Test.cs +++ b/Jint.Tests.Test262/Test262Test.cs @@ -38,7 +38,7 @@ private Engine BuildTestExecutor(Test262File file) } var options = new ParserOptions { RegExpParseMode = RegExpParseMode.AdaptToInterpreted, Tolerant = false }; - var parser = new JavaScriptParser(options); + var parser = new Parser(options); var script = parser.ParseScript(args.At(0).AsString()); return engine.Evaluate(script); @@ -93,7 +93,7 @@ private static void ExecuteTest(Engine engine, Test262File file) } else { - engine.Execute(new JavaScriptParser().ParseScript(file.Program, source: file.FileName)); + engine.Execute(new Parser().ParseScript(file.Program, source: file.FileName)); } } diff --git a/Jint.Tests/Parser/JavascriptParserTests.cs b/Jint.Tests/Parser/JavascriptParserTests.cs index 8c00b4e938..2d570d7fa2 100644 --- a/Jint.Tests/Parser/JavascriptParserTests.cs +++ b/Jint.Tests/Parser/JavascriptParserTests.cs @@ -1,182 +1,181 @@ using Jint.Runtime; -namespace Jint.Tests.Parser +namespace Jint.Tests.Parsing; + +public class JavascriptParserTests { - public class JavascriptParserTests + [Fact] + public void ShouldParseThis() + { + var program = new Parser().ParseScript("this"); + var body = program.Body; + + Assert.Single(body); + Assert.Equal(NodeType.ThisExpression, body.First().As().Expression.Type); + } + + [Fact] + public void ShouldParseNull() + { + var program = new Parser().ParseScript("null"); + var body = program.Body; + + Assert.Single(body); + Assert.Equal(NodeType.Literal, body.First().As().Expression.Type); + Assert.Equal(null, body.First().As().Expression.As().Value); + Assert.Equal("null", body.First().As().Expression.As().Raw); + } + + [Fact] + public void ShouldParseNumeric() { - [Fact] - public void ShouldParseThis() - { - var program = new JavaScriptParser().ParseScript("this"); - var body = program.Body; - - Assert.Single(body); - Assert.Equal(NodeType.ThisExpression, body.First().As().Expression.Type); - } - - [Fact] - public void ShouldParseNull() - { - var program = new JavaScriptParser().ParseScript("null"); - var body = program.Body; - - Assert.Single(body); - Assert.Equal(NodeType.Literal, body.First().As().Expression.Type); - Assert.Equal(null, body.First().As().Expression.As().Value); - Assert.Equal("null", body.First().As().Expression.As().Raw); - } - - [Fact] - public void ShouldParseNumeric() - { - var code = @" + var code = @" 42 "; - var program = new JavaScriptParser().ParseScript(code); - var body = program.Body; - - Assert.Single(body); - Assert.Equal(NodeType.Literal, body.First().As().Expression.Type); - Assert.Equal(42d, body.First().As().Expression.As().Value); - Assert.Equal("42", body.First().As().Expression.As().Raw); - } - - [Fact] - public void ShouldParseBinaryExpression() - { - BinaryExpression binary; - - var program = new JavaScriptParser().ParseScript("(1 + 2 ) * 3"); - var body = program.Body; - - Assert.Single(body); - Assert.NotNull(binary = body.First().As().Expression.As()); - Assert.Equal(3d, binary.Right.As().Value); - Assert.Equal(BinaryOperator.Times, binary.Operator); - Assert.Equal(1d, binary.Left.As().Left.As().Value); - Assert.Equal(2d, binary.Left.As().Right.As().Value); - Assert.Equal(BinaryOperator.Plus, binary.Left.As().Operator); - } - - [Theory] - [InlineData(0, "0")] - [InlineData(42, "42")] - [InlineData(0.14, "0.14")] - [InlineData(3.14159, "3.14159")] - [InlineData(6.02214179e+23, "6.02214179e+23")] - [InlineData(1.492417830e-10, "1.492417830e-10")] - [InlineData(0, "0x0")] - [InlineData(0, "0x0;")] - [InlineData(0xabc, "0xabc")] - [InlineData(0xdef, "0xdef")] - [InlineData(0X1A, "0X1A")] - [InlineData(0x10, "0x10")] - [InlineData(0x100, "0x100")] - [InlineData(0X04, "0X04")] - [InlineData(02, "02")] - [InlineData(10, "012")] - [InlineData(10, "0012")] - [InlineData(1.189008226412092e+38, "0x5973772948c653ac1971f1576e03c4d4")] - [InlineData(18446744073709552000d, "0xffffffffffffffff")] - public void ShouldParseNumericLiterals(object expected, string code) - { - Literal literal; - - var program = new JavaScriptParser().ParseScript(code); - var body = program.Body; - - Assert.Single(body); - Assert.NotNull(literal = body.First().As().Expression.As()); - Assert.Equal(Convert.ToDouble(expected), Convert.ToDouble(literal.Value)); - } - - [Theory] - [InlineData("Hello", @"'Hello'")] - [InlineData("\n\r\t\v\b\f\\\'\"\0", @"'\n\r\t\v\b\f\\\'\""\0'")] - [InlineData("\u0061", @"'\u0061'")] - [InlineData("\x61", @"'\x61'")] - [InlineData("Hello\nworld", @"'Hello\nworld'")] - [InlineData("Hello\\\nworld", @"'Hello\\\nworld'")] - public void ShouldParseStringLiterals(string expected, string code) - { - Literal literal; - - var program = new JavaScriptParser().ParseScript(code); - var body = program.Body; - - Assert.Single(body); - Assert.NotNull(literal = body.First().As().Expression.As()); - Assert.Equal(expected, literal.Value); - } - - [Theory] - [InlineData(@"{ x + var program = new Parser().ParseScript(code); + var body = program.Body; + + Assert.Single(body); + Assert.Equal(NodeType.Literal, body.First().As().Expression.Type); + Assert.Equal(42d, body.First().As().Expression.As().Value); + Assert.Equal("42", body.First().As().Expression.As().Raw); + } + + [Fact] + public void ShouldParseBinaryExpression() + { + BinaryExpression binary; + + var program = new Parser().ParseScript("(1 + 2 ) * 3"); + var body = program.Body; + + Assert.Single(body); + Assert.NotNull(binary = body.First().As().Expression.As()); + Assert.Equal(3d, binary.Right.As().Value); + Assert.Equal(BinaryOperator.Times, binary.Operator); + Assert.Equal(1d, binary.Left.As().Left.As().Value); + Assert.Equal(2d, binary.Left.As().Right.As().Value); + Assert.Equal(BinaryOperator.Plus, binary.Left.As().Operator); + } + + [Theory] + [InlineData(0, "0")] + [InlineData(42, "42")] + [InlineData(0.14, "0.14")] + [InlineData(3.14159, "3.14159")] + [InlineData(6.02214179e+23, "6.02214179e+23")] + [InlineData(1.492417830e-10, "1.492417830e-10")] + [InlineData(0, "0x0")] + [InlineData(0, "0x0;")] + [InlineData(0xabc, "0xabc")] + [InlineData(0xdef, "0xdef")] + [InlineData(0X1A, "0X1A")] + [InlineData(0x10, "0x10")] + [InlineData(0x100, "0x100")] + [InlineData(0X04, "0X04")] + [InlineData(02, "02")] + [InlineData(10, "012")] + [InlineData(10, "0012")] + [InlineData(1.189008226412092e+38, "0x5973772948c653ac1971f1576e03c4d4")] + [InlineData(18446744073709552000d, "0xffffffffffffffff")] + public void ShouldParseNumericLiterals(object expected, string code) + { + Literal literal; + + var program = new Parser().ParseScript(code); + var body = program.Body; + + Assert.Single(body); + Assert.NotNull(literal = body.First().As().Expression.As()); + Assert.Equal(Convert.ToDouble(expected), Convert.ToDouble(literal.Value)); + } + + [Theory] + [InlineData("Hello", @"'Hello'")] + [InlineData("\n\r\t\v\b\f\\\'\"\0", @"'\n\r\t\v\b\f\\\'\""\0'")] + [InlineData("\u0061", @"'\u0061'")] + [InlineData("\x61", @"'\x61'")] + [InlineData("Hello\nworld", @"'Hello\nworld'")] + [InlineData("Hello\\\nworld", @"'Hello\\\nworld'")] + public void ShouldParseStringLiterals(string expected, string code) + { + Literal literal; + + var program = new Parser().ParseScript(code); + var body = program.Body; + + Assert.Single(body); + Assert.NotNull(literal = body.First().As().Expression.As()); + Assert.Equal(expected, literal.Value); + } + + [Theory] + [InlineData(@"{ x ++y }")] - [InlineData(@"{ x + [InlineData(@"{ x --y }")] - [InlineData(@"var x /* comment */; + [InlineData(@"var x /* comment */; { var x = 14, y = 3 z; }")] - [InlineData(@"while (true) { continue + [InlineData(@"while (true) { continue there; }")] - [InlineData(@"while (true) { continue // Comment + [InlineData(@"while (true) { continue // Comment there; }")] - [InlineData(@"while (true) { continue /* Multiline + [InlineData(@"while (true) { continue /* Multiline Comment */there; }")] - [InlineData(@"while (true) { break + [InlineData(@"while (true) { break there; }")] - [InlineData(@"while (true) { break // Comment + [InlineData(@"while (true) { break // Comment there; }")] - [InlineData(@"while (true) { break /* Multiline + [InlineData(@"while (true) { break /* Multiline Comment */there; }")] - [InlineData(@"(function(){ return + [InlineData(@"(function(){ return x; })")] - [InlineData(@"(function(){ return // Comment + [InlineData(@"(function(){ return // Comment x; })")] - [InlineData(@"(function(){ return/* Multiline + [InlineData(@"(function(){ return/* Multiline Comment */x; })")] - [InlineData(@"{ throw error + [InlineData(@"{ throw error error; }")] - [InlineData(@"{ throw error// Comment + [InlineData(@"{ throw error// Comment error; }")] - [InlineData(@"{ throw error/* Multiline + [InlineData(@"{ throw error/* Multiline Comment */error; }")] - public void ShouldInsertSemicolons(string code) - { - new JavaScriptParser().ParseScript(code); - } + public void ShouldInsertSemicolons(string code) + { + new Parser().ParseScript(code); + } - [Fact] - public void ShouldProvideLocationForMultiLinesStringLiterals() - { - const string Code = @"'\ + [Fact] + public void ShouldProvideLocationForMultiLinesStringLiterals() + { + const string Code = @"'\ \ ' "; - var program = new JavaScriptParser(new ParserOptions()).ParseScript(Code); - var expr = program.Body.First().As().Expression; - Assert.Equal(1, expr.Location.Start.Line); - Assert.Equal(0, expr.Location.Start.Column); - Assert.Equal(3, expr.Location.End.Line); - Assert.Equal(1, expr.Location.End.Column); - } - - [Fact] - public void ShouldThrowErrorForInvalidLeftHandOperation() - { - Assert.Throws(() => new Engine().Execute("~ (WE0=1)--- l('1');")); - } - - - [Theory] - [InlineData("....")] - [InlineData("while")] - [InlineData("var")] - [InlineData("-.-")] - public void ShouldThrowParserExceptionForInvalidCode(string code) - { - Assert.Throws(() => new JavaScriptParser().ParseScript(code)); - } + var program = new Parser(new ParserOptions()).ParseScript(Code); + var expr = program.Body.First().As().Expression; + Assert.Equal(1, expr.Location.Start.Line); + Assert.Equal(0, expr.Location.Start.Column); + Assert.Equal(3, expr.Location.End.Line); + Assert.Equal(1, expr.Location.End.Column); + } + + [Fact] + public void ShouldThrowErrorForInvalidLeftHandOperation() + { + Assert.Throws(() => new Engine().Execute("~ (WE0=1)--- l('1');")); + } + + + [Theory] + [InlineData("....")] + [InlineData("while")] + [InlineData("var")] + [InlineData("-.-")] + public void ShouldThrowParseErrorExceptionForInvalidCode(string code) + { + Assert.Throws(() => new Parser().ParseScript(code)); } } diff --git a/Jint.Tests/Runtime/Debugger/EvaluateTests.cs b/Jint.Tests/Runtime/Debugger/EvaluateTests.cs index 7d85fa0176..2d5c771602 100644 --- a/Jint.Tests/Runtime/Debugger/EvaluateTests.cs +++ b/Jint.Tests/Runtime/Debugger/EvaluateTests.cs @@ -72,7 +72,7 @@ function test(x) { var exception = Assert.Throws(() => engine.Debugger.Evaluate("this is a syntax error")); - Assert.IsType(exception.InnerException); + Assert.IsType(exception.InnerException); }); } diff --git a/Jint.Tests/Runtime/EngineTests.cs b/Jint.Tests/Runtime/EngineTests.cs index 5d8ed87d39..56bb960092 100644 --- a/Jint.Tests/Runtime/EngineTests.cs +++ b/Jint.Tests/Runtime/EngineTests.cs @@ -1046,7 +1046,7 @@ public void ShouldGetParseErrorLocation() { engine.Evaluate("1.2+ new", "jQuery.js"); } - catch (ParserException e) + catch (ParseErrorException e) { Assert.Equal(1, e.LineNumber); Assert.Equal(9, e.Column); @@ -1314,7 +1314,7 @@ public void ShouldNotAllowDuplicateProtoProperty() { var code = "if({ __proto__: [], __proto__:[] } instanceof Array) {}"; - Exception ex = Assert.Throws(() => _engine.Execute(code, new ParserOptions { Tolerant = false })); + Exception ex = Assert.Throws(() => _engine.Execute(code, new ParserOptions { Tolerant = false })); Assert.Contains("Duplicate __proto__ fields are not allowed in object literals", ex.Message); ex = Assert.Throws(() => _engine.Execute($"eval('{code}')")); diff --git a/Jint/Engine.Ast.cs b/Jint/Engine.Ast.cs index 51f4e781bc..5629a9bd85 100644 --- a/Jint/Engine.Ast.cs +++ b/Jint/Engine.Ast.cs @@ -25,7 +25,7 @@ public static Script PrepareScript(string script, string? source = null, bool st AllowReturnOutsideFunction = true, OnNodeCreated = astAnalyzer.NodeVisitor }; - return new JavaScriptParser(options).ParseScript(script, source, strict); + return new Parser(options).ParseScript(script, source, strict); } /// @@ -42,7 +42,7 @@ public static Module PrepareModule(string script, string? source = null) OnNodeCreated = astAnalyzer.NodeVisitor }; - return new JavaScriptParser(options).ParseModule(script, source); + return new Parser(options).ParseModule(script, source); } [StructLayout(LayoutKind.Auto)] @@ -86,7 +86,7 @@ public void NodeVisitor(Node node) var constantValue = JintLiteralExpression.ConvertToJsValue(literal); node.AssociatedData = constantValue is not null ? new JintConstantExpression(literal, constantValue) : null; - if (node.AssociatedData is null && literal.TokenType == TokenType.RegularExpression && _options.CompileRegex) + if (node.AssociatedData is null && literal.TokenType == TokenKind.RegularExpression && _options.CompileRegex) { var regExpLiteral = (RegExpLiteral) literal; var regExpParseResult = regExpLiteral.ParseResult; diff --git a/Jint/Engine.cs b/Jint/Engine.cs index 3c48afbdf5..0d51fa2dbc 100644 --- a/Jint/Engine.cs +++ b/Jint/Engine.cs @@ -30,7 +30,7 @@ public sealed partial class Engine : IDisposable private static readonly Options _defaultEngineOptions = new(); private readonly ParserOptions _defaultParserOptions; - private readonly JavaScriptParser _defaultParser; + private readonly Parser _defaultParser; private readonly ExecutionContextStack _executionContexts; private JsValue _completionValue = JsValue.Undefined; @@ -150,7 +150,7 @@ private Engine(Options? options, Action? configure) RegexTimeout = Options.Constraints.RegexTimeout }; - _defaultParser = new JavaScriptParser(_defaultParserOptions); + _defaultParser = new Parser(_defaultParserOptions); } private void Reset() @@ -349,7 +349,7 @@ public JsValue Evaluate(string code, string source, ParserOptions parserOptions) { var parser = ReferenceEquals(_defaultParserOptions, parserOptions) ? _defaultParser - : new JavaScriptParser(parserOptions); + : new Parser(parserOptions); var script = parser.ParseScript(code, source, _isStrict); @@ -381,7 +381,7 @@ public Engine Execute(string code, string source, ParserOptions parserOptions) { var parser = ReferenceEquals(_defaultParserOptions, parserOptions) ? _defaultParser - : new JavaScriptParser(parserOptions); + : new Parser(parserOptions); var script = parser.ParseScript(code, source, _isStrict); diff --git a/Jint/EsprimaExtensions.cs b/Jint/EsprimaExtensions.cs index 8f322eb7c9..f29d7a6587 100644 --- a/Jint/EsprimaExtensions.cs +++ b/Jint/EsprimaExtensions.cs @@ -37,7 +37,7 @@ internal static JsValue TryGetKey(this T expression, Engine engine, bool reso JsValue key; if (expression is Literal literal) { - key = literal.TokenType == TokenType.NullLiteral ? JsValue.Null : LiteralKeyToString(literal); + key = literal.TokenType == TokenKind.NullLiteral ? JsValue.Null : LiteralKeyToString(literal); } else if (!resolveComputed && expression is Identifier identifier) { @@ -287,7 +287,7 @@ internal static void BindingInitialization( var catchEnvRecord = (DeclarativeEnvironment) env; catchEnvRecord.CreateMutableBindingAndInitialize(identifier.Name, canBeDeleted: false, value); } - else if (expression is BindingPattern bindingPattern) + else if (expression is DestructuringPattern bindingPattern) { BindingPatternAssignmentExpression.ProcessPatterns( context, diff --git a/Jint/JsValueExtensions.cs b/Jint/JsValueExtensions.cs index 610fbeb0d0..6d98503ec6 100644 --- a/Jint/JsValueExtensions.cs +++ b/Jint/JsValueExtensions.cs @@ -661,7 +661,7 @@ internal static BigInteger ToBigInteger(this JsValue value, Engine engine) { return TypeConverter.ToBigInt(value); } - catch (ParserException ex) + catch (ParseErrorException ex) { ExceptionHelper.ThrowSyntaxError(engine.Realm, ex.Message); return default; diff --git a/Jint/Native/Function/ClassDefinition.cs b/Jint/Native/Function/ClassDefinition.cs index 1b4b2286c0..39a7fb2db0 100644 --- a/Jint/Native/Function/ClassDefinition.cs +++ b/Jint/Native/Function/ClassDefinition.cs @@ -24,7 +24,7 @@ static ClassDefinition() // generate missing constructor AST only once static MethodDefinition CreateConstructorMethodDefinition(string source) { - var script = new JavaScriptParser().ParseScript(source); + var script = new Parser().ParseScript(source); var classDeclaration = (ClassDeclaration) script.Body[0]; return (MethodDefinition) classDeclaration.Body.Body[0]; } diff --git a/Jint/Native/Function/EvalFunction.cs b/Jint/Native/Function/EvalFunction.cs index b724ae8882..230a32cb10 100644 --- a/Jint/Native/Function/EvalFunction.cs +++ b/Jint/Native/Function/EvalFunction.cs @@ -11,7 +11,7 @@ public sealed class EvalFunction : Function private static readonly JsString _functionName = new("eval"); private static readonly ParserOptions _parserOptions = ParserOptions.Default with { Tolerant = true }; - private readonly JavaScriptParser _parser = new(_parserOptions); + private readonly Parser _parser = new(_parserOptions); internal EvalFunction( Engine engine, @@ -79,7 +79,7 @@ internal JsValue PerformEval(JsValue x, Realm callerRealm, bool strictCaller, bo { script = _parser.ParseScript(x.ToString(), strict: strictCaller); } - catch (ParserException e) + catch (ParseErrorException e) { if (string.Equals(e.Description, Messages.InvalidLHSInAssignment, StringComparison.Ordinal)) { diff --git a/Jint/Native/Function/FunctionInstance.Dynamic.cs b/Jint/Native/Function/FunctionInstance.Dynamic.cs index d3d82800b5..4910bf8ec3 100644 --- a/Jint/Native/Function/FunctionInstance.Dynamic.cs +++ b/Jint/Native/Function/FunctionInstance.Dynamic.cs @@ -142,14 +142,14 @@ internal Function CreateDynamicFunction( } } - JavaScriptParser parser = new(new ParserOptions + Parser parser = new(new ParserOptions { Tolerant = false, RegexTimeout = _engine.Options.Constraints.RegexTimeout }); function = (IFunction) parser.ParseScript(functionExpression, source: null, _engine._isStrict).Body[0]; } - catch (ParserException ex) + catch (ParseErrorException ex) { ExceptionHelper.ThrowSyntaxError(_engine.ExecutionContext.Realm, ex.Message); } diff --git a/Jint/Native/JsTypedArray.cs b/Jint/Native/JsTypedArray.cs index 64a246669d..4dabe73d1d 100644 --- a/Jint/Native/JsTypedArray.cs +++ b/Jint/Native/JsTypedArray.cs @@ -319,7 +319,7 @@ private void IntegerIndexedElementSet(double index, JsValue value) DoIntegerIndexedElementSet((int) index, numValue); } } - catch (ParserException ex) + catch (ParseErrorException ex) { ExceptionHelper.ThrowSyntaxError(_engine.Realm, ex.Message); } diff --git a/Jint/Native/ShadowRealm/ShadowRealm.cs b/Jint/Native/ShadowRealm/ShadowRealm.cs index d4fba7fe54..e505276e6f 100644 --- a/Jint/Native/ShadowRealm/ShadowRealm.cs +++ b/Jint/Native/ShadowRealm/ShadowRealm.cs @@ -19,7 +19,7 @@ namespace Jint.Native.ShadowRealm; public sealed class ShadowRealm : ObjectInstance #pragma warning restore MA0049 { - private readonly JavaScriptParser _parser; + private readonly Parser _parser; internal readonly Realm _shadowRealm; private readonly ExecutionContext _executionContext; @@ -111,7 +111,7 @@ internal JsValue PerformShadowRealmEval(string sourceText, Realm callerRealm) { script = _parser.ParseScript(sourceText, source: null, _engine._isStrict); } - catch (ParserException e) + catch (ParseErrorException e) { if (string.Equals(e.Description, Messages.InvalidLHSInAssignment, StringComparison.Ordinal)) { diff --git a/Jint/Runtime/Debugger/DebugHandler.cs b/Jint/Runtime/Debugger/DebugHandler.cs index bb5f345a81..9c0034cca1 100644 --- a/Jint/Runtime/Debugger/DebugHandler.cs +++ b/Jint/Runtime/Debugger/DebugHandler.cs @@ -125,13 +125,13 @@ public JsValue Evaluate(Script script) public JsValue Evaluate(string source, ParserOptions? options = null) { options ??= new ParserOptions(); - var parser = new JavaScriptParser(options); + var parser = new Parser(options); try { var script = parser.ParseScript(source, "evaluation"); return Evaluate(script); } - catch (ParserException ex) + catch (ParseErrorException ex) { throw new DebugEvaluationException("An error occurred during debugger expression parsing", ex); } diff --git a/Jint/Runtime/Environments/FunctionEnvironment.cs b/Jint/Runtime/Environments/FunctionEnvironment.cs index 9dde14bffa..45b6283a7f 100644 --- a/Jint/Runtime/Environments/FunctionEnvironment.cs +++ b/Jint/Runtime/Environments/FunctionEnvironment.cs @@ -299,7 +299,7 @@ private void HandleRestElementArray( { SetItemSafely(restIdentifier.Name, array, initiallyEmpty); } - else if (restElement.Argument is BindingPattern bindingPattern) + else if (restElement.Argument is DestructuringPattern bindingPattern) { SetFunctionParameter(context, bindingPattern, new [] { array }, 0, initiallyEmpty); } diff --git a/Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs b/Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs index f2d3417ec9..808c443df8 100644 --- a/Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs @@ -8,13 +8,13 @@ namespace Jint.Runtime.Interpreter.Expressions { internal sealed class BindingPatternAssignmentExpression : JintExpression { - private readonly BindingPattern _pattern; + private readonly DestructuringPattern _pattern; private JintExpression _right = null!; private bool _initialized; public BindingPatternAssignmentExpression(AssignmentExpression expression) : base(expression) { - _pattern = (BindingPattern) expression.Left; + _pattern = (DestructuringPattern) expression.Left; } protected override object EvaluateInternal(EvaluationContext context) @@ -42,7 +42,7 @@ protected override object EvaluateInternal(EvaluationContext context) internal static JsValue ProcessPatterns( EvaluationContext context, - BindingPattern pattern, + DestructuringPattern pattern, JsValue argument, Environment? environment, bool checkPatternPropertyReference = true) @@ -157,7 +157,7 @@ private static JsValue HandleArrayPattern( AssignToReference(engine, reference, value, environment); } - else if (left is BindingPattern bindingPattern) + else if (left is DestructuringPattern bindingPattern) { JsValue value; if (arrayOperations != null) @@ -215,7 +215,7 @@ private static JsValue HandleArrayPattern( { AssignToIdentifier(engine, leftIdentifier.Name, array, environment, checkReference); } - else if (restElement.Argument is BindingPattern bp) + else if (restElement.Argument is DestructuringPattern bp) { ProcessPatterns(context, bp, array, environment); } @@ -256,7 +256,7 @@ private static JsValue HandleArrayPattern( AssignToIdentifier(engine, leftIdentifier.Name, value, environment, checkReference); } - else if (assignmentPattern.Left is BindingPattern bp) + else if (assignmentPattern.Left is DestructuringPattern bp) { ProcessPatterns(context, bp, value, environment); } @@ -334,7 +334,7 @@ private static JsValue HandleObjectPattern( value = completion; } - if (assignmentPattern.Left is BindingPattern bp) + if (assignmentPattern.Left is DestructuringPattern bp) { ProcessPatterns(context, bp, value, environment); continue; @@ -349,7 +349,7 @@ private static JsValue HandleObjectPattern( AssignToIdentifier(context.Engine, target!.Name, value, environment, checkReference); } - else if (p.Value is BindingPattern bindingPattern) + else if (p.Value is DestructuringPattern bindingPattern) { var value = source.Get(sourceKey); ProcessPatterns(context, bindingPattern, value, environment); @@ -378,7 +378,7 @@ private static JsValue HandleObjectPattern( source.CopyDataProperties(rest, processedProperties); AssignToIdentifier(context.Engine, leftIdentifier.Name, rest, environment, checkReference); } - else if (restElement.Argument is BindingPattern bp) + else if (restElement.Argument is DestructuringPattern bp) { ProcessPatterns(context, bp, argument, environment); } diff --git a/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs b/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs index 570cb0f513..90c022e393 100644 --- a/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs @@ -29,7 +29,7 @@ internal static JintExpression Build(AssignmentExpression expression) { if (expression.Operator == Operator.Assign) { - if (expression.Left is BindingPattern) + if (expression.Left is DestructuringPattern) { return new BindingPatternAssignmentExpression(expression); } diff --git a/Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs b/Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs index e0ad08ab8a..d05ee8598d 100644 --- a/Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/JintLiteralExpression.cs @@ -28,11 +28,11 @@ internal static JintExpression Build(Literal expression) { switch (literal.TokenType) { - case TokenType.BooleanLiteral: + case TokenKind.BooleanLiteral: return literal.BooleanValue!.Value ? JsBoolean.True : JsBoolean.False; - case TokenType.NullLiteral: + case TokenKind.NullLiteral: return JsValue.Null; - case TokenType.NumericLiteral: + case TokenKind.NumericLiteral: { // unbox only once var numericValue = (double) literal.Value!; @@ -42,11 +42,11 @@ internal static JintExpression Build(Literal expression) ? JsNumber.Create(intValue) : JsNumber.Create(numericValue); } - case TokenType.StringLiteral: + case TokenKind.StringLiteral: return JsString.Create((string) literal.Value!); - case TokenType.BigIntLiteral: + case TokenKind.BigIntLiteral: return JsBigInt.Create((BigInteger) literal.Value!); - case TokenType.RegularExpression: + case TokenKind.RegularExpression: break; } @@ -65,7 +65,7 @@ public override JsValue GetValue(EvaluationContext context) private JsValue ResolveValue(EvaluationContext context) { var expression = (Literal) _expression; - if (expression.TokenType == TokenType.RegularExpression) + if (expression.TokenType == TokenKind.RegularExpression) { var regExpLiteral = (RegExpLiteral) expression; var regExpParseResult = regExpLiteral.ParseResult; diff --git a/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs b/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs index 62b1c9fe32..97d3588ac6 100644 --- a/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs +++ b/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs @@ -19,7 +19,7 @@ internal sealed class JintForInForOfStatement : JintStatement private ProbablyBlockStatement _body; private JintExpression? _expr; - private BindingPattern? _assignmentPattern; + private DestructuringPattern? _assignmentPattern; private JintExpression _right = null!; private List? _tdzNames; private bool _destructuring; @@ -59,7 +59,7 @@ protected override void Initialize(EvaluationContext context) id.GetBoundNames(_tdzNames); } - if (id is BindingPattern bindingPattern) + if (id is DestructuringPattern bindingPattern) { _destructuring = true; _assignmentPattern = bindingPattern; @@ -70,7 +70,7 @@ protected override void Initialize(EvaluationContext context) _expr = new JintIdentifierExpression(identifier); } } - else if (_leftNode is BindingPattern bindingPattern) + else if (_leftNode is DestructuringPattern bindingPattern) { _destructuring = true; _assignmentPattern = bindingPattern; diff --git a/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs b/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs index f20ee82c41..6c93be17b5 100644 --- a/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs +++ b/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs @@ -11,7 +11,7 @@ internal sealed class JintVariableDeclaration : JintStatement configure) return new global::Esprima.Ast.Module(NodeList.Create(Array.Empty())); } - var javaScriptParser = new JavaScriptParser(_options); + var parser = new Parser(_options); try { var source = _sourceRaw.Count == 1 ? _sourceRaw[0] : string.Join(Environment.NewLine, _sourceRaw); - return javaScriptParser.ParseModule(source, _specifier); + return parser.ParseModule(source, _specifier); } - catch (ParserException ex) + catch (ParseErrorException ex) { var location = SourceLocation.From(Position.From(ex.LineNumber, ex.Column), Position.From(ex.LineNumber, ex.Column), _specifier); ExceptionHelper.ThrowSyntaxError(_engine.Realm, $"Error while loading module: error in module '{_specifier}': {ex.Error}", location); diff --git a/Jint/Runtime/Modules/ModuleFactory.cs b/Jint/Runtime/Modules/ModuleFactory.cs index 0c7f09a2f0..b5ea01bca3 100644 --- a/Jint/Runtime/Modules/ModuleFactory.cs +++ b/Jint/Runtime/Modules/ModuleFactory.cs @@ -17,7 +17,7 @@ public static class ModuleFactory /// if is not null. If /// is null, the modules location source will be null as well. /// - /// Is thrown if the provided can not be parsed. + /// Is thrown if the provided can not be parsed. /// Is thrown if an error occured when parsing . public static Module BuildSourceTextModule(Engine engine, ResolvedSpecifier resolved, string code) { @@ -25,9 +25,9 @@ public static Module BuildSourceTextModule(Engine engine, ResolvedSpecifier reso Esprima.Ast.Module module; try { - module = new JavaScriptParser().ParseModule(code, source); + module = new Parser().ParseModule(code, source); } - catch (ParserException ex) + catch (ParseErrorException ex) { ExceptionHelper.ThrowSyntaxError(engine.Realm, $"Error while loading module: error in module '{source}': {ex.Error}"); module = null; diff --git a/Jint/Runtime/TypeConverter.cs b/Jint/Runtime/TypeConverter.cs index 1c7f8b455c..4e99835914 100644 --- a/Jint/Runtime/TypeConverter.cs +++ b/Jint/Runtime/TypeConverter.cs @@ -587,7 +587,7 @@ internal static BigInteger StringToBigInt(string str) { if (!TryStringToBigInt(str, out var result)) { - throw new ParserException(" Cannot convert " + str + " to a BigInt"); + throw new ParseErrorException(" Cannot convert " + str + " to a BigInt"); } return result; From 22698adc3f3ffd70bd6e23532ed65431a5692204 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Sun, 31 Mar 2024 13:46:40 +0300 Subject: [PATCH 2/2] cleanup --- Jint/EsprimaExtensions.cs | 8 ++------ Jint/Runtime/Environments/FunctionEnvironment.cs | 4 ++-- ...s => DestructuringPatternAssignmentExpression.cs} | 12 ++++++------ .../Expressions/JintAssignmentExpression.cs | 2 +- .../Statements/JintForInForOfStatement.cs | 10 +++++----- .../Statements/JintVariableDeclaration.cs | 8 ++++---- 6 files changed, 20 insertions(+), 24 deletions(-) rename Jint/Runtime/Interpreter/Expressions/{BindingPatternAssignmentExpression.cs => DestructuringPatternAssignmentExpression.cs} (97%) diff --git a/Jint/EsprimaExtensions.cs b/Jint/EsprimaExtensions.cs index f29d7a6587..754f221785 100644 --- a/Jint/EsprimaExtensions.cs +++ b/Jint/EsprimaExtensions.cs @@ -287,13 +287,9 @@ internal static void BindingInitialization( var catchEnvRecord = (DeclarativeEnvironment) env; catchEnvRecord.CreateMutableBindingAndInitialize(identifier.Name, canBeDeleted: false, value); } - else if (expression is DestructuringPattern bindingPattern) + else if (expression is DestructuringPattern pattern) { - BindingPatternAssignmentExpression.ProcessPatterns( - context, - bindingPattern, - value, - env); + DestructuringPatternAssignmentExpression.ProcessPatterns(context, pattern, value, env); } } diff --git a/Jint/Runtime/Environments/FunctionEnvironment.cs b/Jint/Runtime/Environments/FunctionEnvironment.cs index 45b6283a7f..a36ce49a12 100644 --- a/Jint/Runtime/Environments/FunctionEnvironment.cs +++ b/Jint/Runtime/Environments/FunctionEnvironment.cs @@ -299,9 +299,9 @@ private void HandleRestElementArray( { SetItemSafely(restIdentifier.Name, array, initiallyEmpty); } - else if (restElement.Argument is DestructuringPattern bindingPattern) + else if (restElement.Argument is DestructuringPattern pattern) { - SetFunctionParameter(context, bindingPattern, new [] { array }, 0, initiallyEmpty); + SetFunctionParameter(context, pattern, [array], 0, initiallyEmpty); } else { diff --git a/Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs b/Jint/Runtime/Interpreter/Expressions/DestructuringPatternAssignmentExpression.cs similarity index 97% rename from Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs rename to Jint/Runtime/Interpreter/Expressions/DestructuringPatternAssignmentExpression.cs index 808c443df8..c0d4018199 100644 --- a/Jint/Runtime/Interpreter/Expressions/BindingPatternAssignmentExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/DestructuringPatternAssignmentExpression.cs @@ -6,13 +6,13 @@ namespace Jint.Runtime.Interpreter.Expressions { - internal sealed class BindingPatternAssignmentExpression : JintExpression + internal sealed class DestructuringPatternAssignmentExpression : JintExpression { private readonly DestructuringPattern _pattern; private JintExpression _right = null!; private bool _initialized; - public BindingPatternAssignmentExpression(AssignmentExpression expression) : base(expression) + public DestructuringPatternAssignmentExpression(AssignmentExpression expression) : base(expression) { _pattern = (DestructuringPattern) expression.Left; } @@ -157,7 +157,7 @@ private static JsValue HandleArrayPattern( AssignToReference(engine, reference, value, environment); } - else if (left is DestructuringPattern bindingPattern) + else if (left is DestructuringPattern dp) { JsValue value; if (arrayOperations != null) @@ -169,7 +169,7 @@ private static JsValue HandleArrayPattern( iterator!.TryIteratorStep(out var temp); value = temp; } - ProcessPatterns(context, bindingPattern, value, environment); + ProcessPatterns(context, dp, value, environment); } else if (left is RestElement restElement) { @@ -349,10 +349,10 @@ private static JsValue HandleObjectPattern( AssignToIdentifier(context.Engine, target!.Name, value, environment, checkReference); } - else if (p.Value is DestructuringPattern bindingPattern) + else if (p.Value is DestructuringPattern dp) { var value = source.Get(sourceKey); - ProcessPatterns(context, bindingPattern, value, environment); + ProcessPatterns(context, dp, value, environment); } else if (p.Value is MemberExpression memberExpression) { diff --git a/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs b/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs index 90c022e393..2c782c7bd8 100644 --- a/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs +++ b/Jint/Runtime/Interpreter/Expressions/JintAssignmentExpression.cs @@ -31,7 +31,7 @@ internal static JintExpression Build(AssignmentExpression expression) { if (expression.Left is DestructuringPattern) { - return new BindingPatternAssignmentExpression(expression); + return new DestructuringPatternAssignmentExpression(expression); } return new SimpleAssignmentExpression(expression); diff --git a/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs b/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs index 97d3588ac6..6357b0802c 100644 --- a/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs +++ b/Jint/Runtime/Interpreter/Statements/JintForInForOfStatement.cs @@ -59,10 +59,10 @@ protected override void Initialize(EvaluationContext context) id.GetBoundNames(_tdzNames); } - if (id is DestructuringPattern bindingPattern) + if (id is DestructuringPattern pattern) { _destructuring = true; - _assignmentPattern = bindingPattern; + _assignmentPattern = pattern; } else { @@ -70,10 +70,10 @@ protected override void Initialize(EvaluationContext context) _expr = new JintIdentifierExpression(identifier); } } - else if (_leftNode is DestructuringPattern bindingPattern) + else if (_leftNode is DestructuringPattern pattern) { _destructuring = true; - _assignmentPattern = bindingPattern; + _assignmentPattern = pattern; } else if (_leftNode is MemberExpression memberExpression) { @@ -232,7 +232,7 @@ private Completion BodyEvaluation( } else { - nextValue = BindingPatternAssignmentExpression.ProcessPatterns( + nextValue = DestructuringPatternAssignmentExpression.ProcessPatterns( context, _assignmentPattern!, nextValue, diff --git a/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs b/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs index 6c93be17b5..eeaad7cd85 100644 --- a/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs +++ b/Jint/Runtime/Interpreter/Statements/JintVariableDeclaration.cs @@ -30,11 +30,11 @@ protected override void Initialize(EvaluationContext context) JintExpression? left = null; JintExpression? init = null; - DestructuringPattern? bindingPattern = null; + DestructuringPattern? pattern = null; if (declaration.Id is DestructuringPattern bp) { - bindingPattern = bp; + pattern = bp; } else { @@ -50,7 +50,7 @@ protected override void Initialize(EvaluationContext context) _declarations[i] = new ResolvedDeclaration { Left = left, - LeftPattern = bindingPattern, + LeftPattern = pattern, LeftIdentifierExpression = leftIdentifier, EvalOrArguments = leftIdentifier?.HasEvalOrArguments == true, Init = init @@ -89,7 +89,7 @@ protected override Completion ExecuteInternal(EvaluationContext context) var value = declaration.Init.GetValue(context); - BindingPatternAssignmentExpression.ProcessPatterns( + DestructuringPatternAssignmentExpression.ProcessPatterns( context, declaration.LeftPattern, value,