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

Don't use BlockStatement to model function body #771

Merged
merged 2 commits into from
Aug 4, 2020
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
32 changes: 32 additions & 0 deletions Jint.Tests/Runtime/ConstTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Xunit;

namespace Jint.Tests.Runtime
{
public class ConstTests
{
private readonly Engine _engine;

public ConstTests()
{
_engine = new Engine()
.SetValue("log", new Action<object>(Console.WriteLine))
.SetValue("assert", new Action<bool>(Assert.True))
.SetValue("equal", new Action<object, object>(Assert.Equal));
}

[Fact]
public void ConstInsideIife()
{
_engine.Execute(@"
(function(){
const testVariable = 'test';
function render() {
log(testVariable);
}
render();
})();
");
}
}
}
2 changes: 1 addition & 1 deletion Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,7 @@ public void ShouldBeAbleToJsonStringifyClrObjects()

engine.Execute("var jsObj = { 'key1' :'value1', 'key2' : 'value2' }");

engine.SetValue("netObj", new Dictionary<string, object>()
engine.SetValue("netObj", new Dictionary<string, object>
{
{"key1", "value1"},
{"key2", "value2"},
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/ArrowFunctionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override JsValue Call(JsValue thisArg, JsValue[] arguments)
arguments,
localEnv);

var result = _function.Body.Execute();
var result = _function.Execute();

var value = result.GetValueOrDefault().Clone();

Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Function/ScriptFunctionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public override JsValue Call(JsValue thisArgument, JsValue[] arguments)
arguments,
localEnv);

var result = _function.Body.Execute();
var result = _function.Execute();
var value = result.GetValueOrDefault().Clone();
argumentsInstance?.FunctionWasCalled();

Expand Down
28 changes: 13 additions & 15 deletions Jint/Runtime/Interpreter/JintFunctionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Esprima.Ast;
using Jint.Native;
using Jint.Native.Function;
using Jint.Runtime.Interpreter.Statements;
using Jint.Runtime.Interpreter.Expressions;

namespace Jint.Runtime.Interpreter
{
Expand All @@ -14,8 +14,9 @@ internal sealed class JintFunctionDefinition
{
private readonly Engine _engine;

private JintStatement _body;

private JintExpression _bodyExpression;
private JintStatementList _bodyStatementList;

public readonly string Name;
public readonly bool Strict;
public readonly IFunction Function;
Expand Down Expand Up @@ -47,21 +48,18 @@ internal sealed class JintFunctionDefinition
}
}

public JintStatement Body
internal Completion Execute()
{
get
if (Function.Expression)
{
if (_body != null)
{
return _body;
}

_body = Function.Expression
? (JintStatement) new JintReturnStatement(_engine, new ReturnStatement((Expression) Function.Body))
: new JintBlockStatement(_engine, (BlockStatement) Function.Body);

return _body;
_bodyExpression ??= JintExpression.Build(_engine, (Expression) Function.Body);
var jsValue = _bodyExpression?.GetValue() ?? Undefined.Instance;
return new Completion(CompletionType.Return, jsValue, null, Function.Body.Location);
}

var blockStatement = (BlockStatement) Function.Body;
_bodyStatementList ??= new JintStatementList(_engine, blockStatement, blockStatement.Body);
return _bodyStatementList.Execute();
}

internal State Initialize(Engine engine, FunctionInstance functionInstance)
Expand Down