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

Fix block-scoped function registrations #859

Merged
merged 1 commit into from
Apr 9, 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
4 changes: 0 additions & 4 deletions Jint.Tests.Test262/Jint.Tests.Test262.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@
<PackageReference Include="xunit.runner.console" Version="2.4.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<None Remove="harness\**" />
<None Remove="test\**" />
</ItemGroup>
</Project>
7 changes: 1 addition & 6 deletions Jint.Tests/Runtime/CallStackTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit;

namespace Jint.Tests.Runtime
{
Expand Down
5 changes: 0 additions & 5 deletions Jint.Tests/Runtime/Debugger/DebugHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using Jint.Native.Object;
using Jint.Runtime.Debugger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Jint.Tests.Runtime.Debugger
Expand Down
27 changes: 27 additions & 0 deletions Jint.Tests/Runtime/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,32 @@ public void ArrowFunctionShouldBeExtensible()
assert(a.foo === 'bar');
");
}

[Fact]
public void BlockScopeFunctionShouldWork()
{
const string script = @"
function execute(doc, args){
var i = doc;
{
function doSomething() {
return 'ayende';
}

i.Name = doSomething();
}
}
";

var engine = new Engine(options =>
{
options.Strict();
});
engine.Execute(script);

var obj = engine.Execute("var obj = {}; execute(obj); return obj;").GetCompletionValue().AsObject();

Assert.Equal("ayende", obj.Get("Name").AsString());
}
}
}
6 changes: 3 additions & 3 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ internal JsValue ResolveThisBinding()
ExceptionHelper.ThrowSyntaxError(this, $"Identifier '{fn}' has already been declared");
}

var fo = Function.CreateFunctionObject(f, env);
var fo = Function.InstantiateFunctionObject(f, env);
envRec.CreateGlobalFunctionBinding(fn, fo, canBeDeleted: false);
}

Expand Down Expand Up @@ -966,7 +966,7 @@ internal JsValue ResolveThisBinding()
foreach (var f in functionsToInitialize)
{
var fn = f.Id.Name;
var fo = Function.CreateFunctionObject(f, lexEnv);
var fo = Function.InstantiateFunctionObject(f, lexEnv);
varEnvRec.SetMutableBinding(fn, fo, strict: false);
}
}
Expand Down Expand Up @@ -1137,7 +1137,7 @@ private ArgumentsInstance CreateUnmappedArgumentsObject(JsValue[] argumentsList)
foreach (var f in functionsToInitialize)
{
var fn = f.Id.Name;
var fo = Function.CreateFunctionObject(f, lexEnv);
var fo = Function.InstantiateFunctionObject(f, lexEnv);
if (varEnvRec is GlobalEnvironmentRecord ger)
{
ger.CreateGlobalFunctionBinding(fn, fo, canBeDeleted: true);
Expand Down
8 changes: 7 additions & 1 deletion Jint/EsprimaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,20 @@ internal static void GetBoundNames(this Node? parameter, List<string> target)
return;
}

if (parameter is VariableDeclaration variableDeclaration)
{
variableDeclaration.GetBoundNames(target);
return;
}

while (true)
{
if (parameter is Identifier identifier)
{
target.Add(identifier.Name!);
return;
}

if (parameter is RestElement restElement)
{
parameter = restElement.Argument;
Expand Down
19 changes: 9 additions & 10 deletions Jint/HoistingScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,33 @@ namespace Jint
treeWalker._lexicalNames);
}

public static List<VariableDeclaration> GetLexicalDeclarations(BlockStatement statement)
public static List<Declaration> GetLexicalDeclarations(BlockStatement statement)
{
List<VariableDeclaration> lexicalDeclarations = null ;
List<Declaration> lexicalDeclarations = null ;
ref readonly var statementListItems = ref statement.Body;
for (var i = 0; i < statementListItems.Count; i++)
{
var node = statementListItems[i];
if (node.Type != Nodes.VariableDeclaration)
if (node.Type != Nodes.VariableDeclaration && node.Type != Nodes.FunctionDeclaration)
{
continue;
}

var rootVariable = (VariableDeclaration) node;
if (rootVariable.Kind == VariableDeclarationKind.Var)
if (node is VariableDeclaration { Kind: VariableDeclarationKind.Var })
{
continue;
}

lexicalDeclarations ??= new List<VariableDeclaration>();
lexicalDeclarations.Add(rootVariable);
lexicalDeclarations ??= new List<Declaration>();
lexicalDeclarations.Add((Declaration) node);
}

return lexicalDeclarations;
}

public static List<VariableDeclaration> GetLexicalDeclarations(SwitchCase statement)
public static List<Declaration> GetLexicalDeclarations(SwitchCase statement)
{
List<VariableDeclaration> lexicalDeclarations = null ;
List<Declaration> lexicalDeclarations = null ;
ref readonly var statementListItems = ref statement.Consequent;
for (var i = 0; i < statementListItems.Count; i++)
{
Expand All @@ -101,7 +100,7 @@ public static List<VariableDeclaration> GetLexicalDeclarations(SwitchCase statem
continue;
}

lexicalDeclarations ??= new List<VariableDeclaration>();
lexicalDeclarations ??= new List<Declaration>();
lexicalDeclarations.Add(rootVariable);
}

Expand Down
6 changes: 2 additions & 4 deletions Jint/Native/Function/FunctionConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
}

/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2
/// https://tc39.es/ecma262/#sec-runtime-semantics-instantiatefunctionobject
/// </summary>
/// <param name="functionDeclaration"></param>
/// <returns></returns>
public FunctionInstance CreateFunctionObject(FunctionDeclaration functionDeclaration, LexicalEnvironment env)
internal FunctionInstance InstantiateFunctionObject(FunctionDeclaration functionDeclaration, LexicalEnvironment env)
{
var functionObject = new ScriptFunctionInstance(
Engine,
Expand Down
4 changes: 1 addition & 3 deletions Jint/Runtime/Debugger/DebugScope.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using Jint.Native;
using Jint.Runtime.Environments;

Expand Down
3 changes: 1 addition & 2 deletions Jint/Runtime/Debugger/DebugScopes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Jint.Native;
using Jint.Runtime.Environments;
using Jint.Runtime.Environments;
using System.Collections;
using System.Collections.Generic;

Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Environments/LexicalEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Jint.Runtime.Environments
/// </summary>
public sealed class LexicalEnvironment
{
private readonly Engine _engine;
internal readonly Engine _engine;
internal EnvironmentRecord _record;
internal LexicalEnvironment _outer;

Expand Down
21 changes: 11 additions & 10 deletions Jint/Runtime/Interpreter/JintStatementList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ public Completion Execute()
/// </summary>
internal static void BlockDeclarationInstantiation(
LexicalEnvironment env,
List<VariableDeclaration> lexicalDeclarations)
List<Declaration> declarations)
{
var envRec = env._record;
var boundNames = new List<string>();
for (var i = 0; i < lexicalDeclarations.Count; i++)
for (var i = 0; i < declarations.Count; i++)
{
var variableDeclaration = lexicalDeclarations[i];
var d = declarations[i];
boundNames.Clear();
variableDeclaration.GetBoundNames(boundNames);
d.GetBoundNames(boundNames);
for (var j = 0; j < boundNames.Count; j++)
{
var dn = boundNames[j];
if (variableDeclaration.Kind == VariableDeclarationKind.Const)
if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you couldn't resist

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rider/R# made me do it 😇

{
envRec.CreateImmutableBinding(dn, strict: true);
}
Expand All @@ -135,11 +135,12 @@ public Completion Execute()
}
}

/* If d is a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, then
* Let fn be the sole element of the BoundNames of d.
* Let fo be the result of performing InstantiateFunctionObject for d with argument env.
* Perform envRec.InitializeBinding(fn, fo).
*/
if (d is FunctionDeclaration functionDeclaration)
{
var fn = functionDeclaration.Id!.Name;
var fo = env._engine.Function.InstantiateFunctionObject(functionDeclaration, env);
envRec.InitializeBinding(fn, fo);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Interpreter/Statements/JintBlockStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Jint.Runtime.Interpreter.Statements
internal sealed class JintBlockStatement : JintStatement<BlockStatement>
{
private JintStatementList _statementList;
private List<VariableDeclaration> _lexicalDeclarations;
private List<Declaration> _lexicalDeclarations;

public JintBlockStatement(Engine engine, BlockStatement blockStatement) : base(engine, blockStatement)
{
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Interpreter/Statements/JintSwitchBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private sealed class JintSwitchCase
{
internal readonly JintStatementList Consequent;
internal readonly JintExpression Test;
internal readonly List<VariableDeclaration> LexicalDeclarations;
internal readonly List<Declaration> LexicalDeclarations;

public JintSwitchCase(Engine engine, SwitchCase switchCase)
{
Expand Down