Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle recursive engine invocations #837

Merged
merged 1 commit into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jint.Tests.Test262/Test262Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected void RunTestCode(string code, bool strict)
var parser = new JavaScriptParser(args.At(0).AsString(), options);
var script = parser.ParseScript(strict);

var value = engine.Execute(script, false).GetCompletionValue();
var value = engine.Execute(script).GetCompletionValue();

return value;
}), true, true, true));
Expand Down
13 changes: 13 additions & 0 deletions Jint.Tests/Runtime/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,19 @@ public void ShouldReuseOptions()
Assert.Equal(1, Convert.ToInt32(engine2.GetValue("x").ToObject()));
}

[Fact]
public void RecursiveCallStack()
{
var engine = new Engine();
Func<string, object> evaluateCode = code => engine.Execute(code).GetCompletionValue();
var evaluateCodeValue = JsValue.FromObject(engine, evaluateCode);

engine.SetValue("evaluateCode", evaluateCodeValue);
var result = (int) engine.Execute(@"evaluateCode('678 + 711')").GetCompletionValue().AsNumber();

Assert.Equal(1389, result);
}

private class Wrapper
{
public Testificate Test { get; set; }
Expand Down
29 changes: 16 additions & 13 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,8 @@ public Engine Execute(string source, ParserOptions parserOptions)

public Engine Execute(Script script)
{
return Execute(script, true);
}

internal Engine Execute(Script script, bool resetState)
{
if (resetState)
{
ResetConstraints();
ResetLastStatement();
ResetCallStack();
}
ResetConstraints();
ResetLastStatement();

using (new StrictModeScope(_isStrict || script.Strict))
{
Expand All @@ -368,11 +359,23 @@ internal Engine Execute(Script script, bool resetState)
GlobalEnvironment);

var list = new JintStatementList(this, null, script.Body);

var result = list.Execute();

Completion result;
try
{
result = list.Execute();
}
catch
{
// unhandled exception
ResetCallStack();
throw;
}

if (result.Type == CompletionType.Throw)
{
var ex = new JavaScriptException(result.GetValueOrDefault()).SetCallstack(this, result.Location);
ResetCallStack();
throw ex;
}

Expand Down
2 changes: 2 additions & 0 deletions Jint/Runtime/CallStack/JintCallStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public CallStackElement Pop()
return item;
}

public int Count => _stack._size;

public void Clear()
{
_stack.Clear();
Expand Down