Skip to content

Commit

Permalink
Upgrade to Esprima 3.0.0-beta-6 (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Aug 26, 2022
1 parent e1e8649 commit 2196309
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 87 deletions.
6 changes: 3 additions & 3 deletions Jint.Benchmark/EngineConstructionBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class EngineConstructionBenchmark

public EngineConstructionBenchmark()
{
var parser = new JavaScriptParser("return [].length + ''.length");
_program = parser.ParseScript();
var parser = new JavaScriptParser();
_program = parser.ParseScript("return [].length + ''.length");
}

[Benchmark]
Expand All @@ -22,4 +22,4 @@ public double BuildEngine()
return engine.Evaluate(_program).AsNumber();
}
}
}
}
4 changes: 2 additions & 2 deletions Jint.Repl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static void Main(string[] args)
}

var script = File.ReadAllText(filename);
engine.Evaluate(script);
engine.Evaluate(script, "repl");
return;
}

Expand All @@ -45,7 +45,7 @@ private static void Main(string[] args)
Console.WriteLine();

var defaultColor = Console.ForegroundColor;
var parserOptions = new ParserOptions("repl")
var parserOptions = new ParserOptions
{
Tolerant = true,
AdaptRegexp = true
Expand Down
4 changes: 2 additions & 2 deletions Jint.Tests.Test262/Test262ModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public Module LoadModule(Engine engine, ResolvedSpecifier resolved)
code = stream.ReadToEnd();
}

var parserOptions = new ParserOptions(resolved.Uri?.LocalPath!)
var parserOptions = new ParserOptions
{
AdaptRegexp = true,
Tolerant = true
};

module = new JavaScriptParser(code, parserOptions).ParseModule();
module = new JavaScriptParser(parserOptions).ParseModule(code, source: resolved.Uri?.LocalPath!);
}
catch (ParserException ex)
{
Expand Down
8 changes: 4 additions & 4 deletions Jint.Tests.Test262/Test262Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private Engine BuildTestExecutor(Test262File file)
cfg.EnableModules(new Test262ModuleLoader(State.Test262Stream.Options.FileSystem, relativePath));
});

if (file.Flags.Contains("raw"))
if (file.Flags.IndexOf("raw") != -1)
{
// nothing should be loaded
return engine;
Expand All @@ -39,8 +39,8 @@ private Engine BuildTestExecutor(Test262File file)
}

var options = new ParserOptions { AdaptRegexp = true, Tolerant = false };
var parser = new JavaScriptParser(args.At(0).AsString(), options);
var script = parser.ParseScript();
var parser = new JavaScriptParser(options);
var script = parser.ParseScript(args.At(0).AsString());

return engine.Evaluate(script);
}), true, true, true));
Expand Down Expand Up @@ -87,7 +87,7 @@ private static void ExecuteTest(Engine engine, Test262File file)
}
else
{
engine.Execute(new JavaScriptParser(file.Program, new ParserOptions(file.FileName)).ParseScript());
engine.Execute(new JavaScriptParser().ParseScript(file.Program, source: file.FileName));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Jint.Tests.Test262/TestHarness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private static partial Task InitializeCustomState()
foreach (var file in State.HarnessFiles)
{
var source = file.Program;
State.Sources[Path.GetFileName(file.FileName)] = new JavaScriptParser(source, new ParserOptions(file.FileName)).ParseScript();
State.Sources[Path.GetFileName(file.FileName)] = new JavaScriptParser().ParseScript(source, source: file.FileName);
}

return Task.CompletedTask;
Expand Down
30 changes: 15 additions & 15 deletions Jint.Tests/Parser/JavascriptParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class JavascriptParserTests
[Fact]
public void ShouldParseThis()
{
var program = new JavaScriptParser("this").ParseScript();
var program = new JavaScriptParser().ParseScript("this");
var body = program.Body;

Assert.Single(body);
Expand All @@ -19,7 +19,7 @@ public void ShouldParseThis()
[Fact]
public void ShouldParseNull()
{
var program = new JavaScriptParser("null").ParseScript();
var program = new JavaScriptParser().ParseScript("null");
var body = program.Body;

Assert.Single(body);
Expand All @@ -31,10 +31,10 @@ public void ShouldParseNull()
[Fact]
public void ShouldParseNumeric()
{
var program = new JavaScriptParser(
@"
var code = @"
42
").ParseScript();
";
var program = new JavaScriptParser().ParseScript(code);
var body = program.Body;

Assert.Single(body);
Expand All @@ -48,7 +48,7 @@ public void ShouldParseBinaryExpression()
{
BinaryExpression binary;

var program = new JavaScriptParser("(1 + 2 ) * 3").ParseScript();
var program = new JavaScriptParser().ParseScript("(1 + 2 ) * 3");
var body = program.Body;

Assert.Single(body);
Expand Down Expand Up @@ -80,11 +80,11 @@ public void ShouldParseBinaryExpression()
[InlineData(10, "0012")]
[InlineData(1.189008226412092e+38, "0x5973772948c653ac1971f1576e03c4d4")]
[InlineData(18446744073709552000d, "0xffffffffffffffff")]
public void ShouldParseNumericLiterals(object expected, string source)
public void ShouldParseNumericLiterals(object expected, string code)
{
Literal literal;

var program = new JavaScriptParser(source).ParseScript();
var program = new JavaScriptParser().ParseScript(code);
var body = program.Body;

Assert.Single(body);
Expand All @@ -99,11 +99,11 @@ public void ShouldParseNumericLiterals(object expected, string source)
[InlineData("\x61", @"'\x61'")]
[InlineData("Hello\nworld", @"'Hello\nworld'")]
[InlineData("Hello\\\nworld", @"'Hello\\\nworld'")]
public void ShouldParseStringLiterals(string expected, string source)
public void ShouldParseStringLiterals(string expected, string code)
{
Literal literal;

var program = new JavaScriptParser(source).ParseScript();
var program = new JavaScriptParser().ParseScript(code);
var body = program.Body;

Assert.Single(body);
Expand Down Expand Up @@ -144,19 +144,19 @@ public void ShouldParseStringLiterals(string expected, string source)
[InlineData(@"{ throw error/* Multiline
Comment */error; }")]

public void ShouldInsertSemicolons(string source)
public void ShouldInsertSemicolons(string code)
{
new JavaScriptParser(source).ParseScript();
new JavaScriptParser().ParseScript(code);
}

[Fact]
public void ShouldProvideLocationForMultiLinesStringLiterals()
{
var source = @"'\
const string Code = @"'\
\
'
";
var program = new JavaScriptParser(source, new ParserOptions()).ParseScript();
var program = new JavaScriptParser(new ParserOptions()).ParseScript(Code);
var expr = program.Body.First().As<ExpressionStatement>().Expression;
Assert.Equal(1, expr.Location.Start.Line);
Assert.Equal(0, expr.Location.Start.Column);
Expand All @@ -178,7 +178,7 @@ public void ShouldThrowErrorForInvalidLeftHandOperation()
[InlineData("-.-")]
public void ShouldThrowParserExceptionForInvalidCode(string code)
{
Assert.Throws<ParserException>(() => new JavaScriptParser(code).ParseScript());
Assert.Throws<ParserException>(() => new JavaScriptParser().ParseScript(code));
}
}
}
6 changes: 3 additions & 3 deletions Jint.Tests/Runtime/Debugger/BreakPointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ public void BreakPointBreaksInCorrectSource()

// We need to specify the source to the parser.
// And we need locations too (Jint specifies that in its default options)
engine.Execute(script1, new ParserOptions("script1"));
engine.Execute(script1, "script1");
Assert.False(didBreak);

engine.Execute(script2, new ParserOptions("script2"));
engine.Execute(script2, "script2");
Assert.False(didBreak);

// Note that it's actually script3 that executes the function in script2
// and triggers the breakpoint
engine.Execute(script3, new ParserOptions("script3"));
engine.Execute(script3, "script3");
Assert.True(didBreak);
}

Expand Down
2 changes: 1 addition & 1 deletion Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ public void ShouldGetParseErrorLocation()
var engine = new Engine();
try
{
engine.Evaluate("1.2+ new", new ParserOptions(source: "jQuery.js"));
engine.Evaluate("1.2+ new", "jQuery.js");
}
catch (ParserException e)
{
Expand Down
33 changes: 17 additions & 16 deletions Jint.Tests/Runtime/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void CanProduceCorrectStackTraceForInternalError()
var b = function(v) {
return a(v);
}
", new ParserOptions("custom.js"));
", "custom.js");

var e = Assert.Throws<JavaScriptException>(() => engine.Execute("var x = b(7);", new ParserOptions("main.js")));
var e = Assert.Throws<JavaScriptException>(() => engine.Execute("var x = b(7);", "main.js"));
Assert.Equal("Cannot read property 'yyy' of undefined", e.Message);
Assert.Equal(3, e.Location.Start.Line);
Assert.Equal(15, e.Location.Start.Column);
Expand All @@ -92,9 +92,9 @@ public void CanProduceCorrectStackTraceForScriptError()
var b = function(v) {
return a(v);
}
", new ParserOptions("custom.js"));
", "custom.js");

var e = Assert.Throws<JavaScriptException>(() => engine.Execute("var x = b(7);", new ParserOptions("main.js")));
var e = Assert.Throws<JavaScriptException>(() => engine.Execute("var x = b(7);", "main.js"));
Assert.Equal("Error thrown from script", e.Message);
Assert.Equal(3, e.Location.Start.Line);
Assert.Equal(8, e.Location.Start.Column);
Expand All @@ -119,9 +119,9 @@ public void ErrorObjectHasTheStackTraceImmediately()
var b = function(v) {
return a(v);
}
", new ParserOptions("custom.js"));
", "custom.js");

var e = engine.Evaluate(@"b(7)", new ParserOptions("main.js")).AsString();
var e = engine.Evaluate(@"b(7)", "main.js").AsString();

var stack = e;
EqualIgnoringNewLineDifferences(@" at a (v) custom.js:3:10
Expand All @@ -146,9 +146,9 @@ public void ThrownErrorObjectHasStackTraceInCatch()
var b = function(v) {
return a(v);
}
", new ParserOptions("custom.js"));
", "custom.js");

var e = engine.Evaluate(@"b(7)", new ParserOptions("main.js")).AsString();
var e = engine.Evaluate(@"b(7)", "main.js").AsString();

var stack = e;
EqualIgnoringNewLineDifferences(@" at a (v) custom.js:4:11
Expand All @@ -174,9 +174,9 @@ public void GeneratedErrorHasStackTraceInCatch()
var b = function(v) {
return a(v);
}
", new ParserOptions("custom.js"));
", "custom.js");

var e = engine.Evaluate(@"b(7)", new ParserOptions("main.js")).AsString();
var e = engine.Evaluate(@"b(7)", "main.js").AsString();

var stack = e;
EqualIgnoringNewLineDifferences(@" at a (v) custom.js:4:13
Expand Down Expand Up @@ -293,12 +293,12 @@ public void StackTraceCollectedForImmediatelyInvokedFunctionExpression()
return item;
})(getItem);";

var parserOptions = new ParserOptions("get-item.js")
var parserOptions = new ParserOptions
{
AdaptRegexp = true,
Tolerant = true
};
var ex = Assert.Throws<JavaScriptException>(() => engine.Execute(script, parserOptions));
var ex = Assert.Throws<JavaScriptException>(() => engine.Execute(script, "get-item.js", parserOptions));

const string expected = @"Error: Cannot read property '5' of null
at getItem (items, itemIndex) get-item.js:2:22
Expand Down Expand Up @@ -382,9 +382,9 @@ public void ErrorsHaveCorrectConstructor(string type)
[Fact]
public void CallStackWorksWithRecursiveCalls()
{
static ParserOptions CreateParserOptions(string filePath)
static ParserOptions CreateParserOptions()
{
return new ParserOptions(filePath)
return new ParserOptions
{
AdaptRegexp = true,
Tolerant = true
Expand All @@ -405,12 +405,13 @@ static ParserOptions CreateParserOptions(string filePath)
nuм -= 3;",
_ => throw new FileNotFoundException($"File '{path}' not exist.", path)
};
engine.Execute(content, CreateParserOptions(path));
engine.Execute(content, path, CreateParserOptions());
}));
engine.Execute(
@"var num = 5;
executeFile(""first-file.js"");",
CreateParserOptions("main-file.js")
"main-file.js",
CreateParserOptions()
);
});

Expand Down
4 changes: 2 additions & 2 deletions Jint.Tests/Runtime/NullPropagation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public void NullPropagationShouldNotAffectOperators()
this.has_emptyfield_not_null = this.EmptyField !== null;
";

var wrapperScript = string.Format(@"function ExecutePatchScript(docInner){{ (function(doc){{ {0} }}).apply(docInner); }};", script);
var wrapperScript = $@"function ExecutePatchScript(docInner){{ (function(doc){{ {script} }}).apply(docInner); }};";

engine.Execute(wrapperScript, new ParserOptions("main.js"));
engine.Execute(wrapperScript, "main.js");

engine.Invoke("ExecutePatchScript", jsObject);

Expand Down
44 changes: 30 additions & 14 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ namespace Jint
{
public sealed partial class Engine : IDisposable
{
private static readonly ParserOptions DefaultParserOptions = new("<anonymous>")
{
AdaptRegexp = true,
Tolerant = true
};
private readonly JavaScriptParser _defaultParser = new(ParserOptions.Default);

private readonly ExecutionContextStack _executionContexts;
private JsValue _completionValue = JsValue.Undefined;
Expand Down Expand Up @@ -242,22 +238,42 @@ public void ResetCallStack()
CallStack.Clear();
}

public JsValue Evaluate(string source)
=> Evaluate(source, DefaultParserOptions);
public JsValue Evaluate(string code)
=> Evaluate(code, "<anonymous>", ParserOptions.Default);

public JsValue Evaluate(string code, string source)
=> Evaluate(code, source, ParserOptions.Default);

public JsValue Evaluate(string code, ParserOptions parserOptions)
=> Evaluate(code, "<anonymous>", parserOptions);

public JsValue Evaluate(string code, string source, ParserOptions parserOptions)
{
var parser = ReferenceEquals(ParserOptions.Default, parserOptions)
? _defaultParser
: new JavaScriptParser(parserOptions);

public JsValue Evaluate(string source, ParserOptions parserOptions)
=> Evaluate(new JavaScriptParser(source, parserOptions).ParseScript());
var script = parser.ParseScript(code, source);

return Evaluate(script);
}

public JsValue Evaluate(Script script)
=> Execute(script)._completionValue;

public Engine Execute(string source)
=> Execute(source, DefaultParserOptions);
public Engine Execute(string code, string? source = null)
=> Execute(code, source ?? "<anonymous>", ParserOptions.Default);

public Engine Execute(string source, ParserOptions parserOptions)
public Engine Execute(string code, ParserOptions parserOptions)
=> Execute(code, "<anonymous>", parserOptions);

public Engine Execute(string code, string source, ParserOptions parserOptions)
{
var parser = new JavaScriptParser(source, parserOptions);
var script = parser.ParseScript();
var parser = ReferenceEquals(ParserOptions.Default, parserOptions)
? _defaultParser
: new JavaScriptParser(parserOptions);

var script = parser.ParseScript(code, source);

return Execute(script);
}
Expand Down
Loading

0 comments on commit 2196309

Please sign in to comment.