fix: scope gets polluted by arguments with named function expressions

This commit is contained in:
TopchetoEU 2024-09-05 13:29:20 +03:00
parent d7353e19ed
commit 8f13ff3e0b
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 11 additions and 10 deletions

View File

@ -14,7 +14,7 @@ public class FunctionArrowNode extends FunctionNode {
@Override public String name() { return null; }
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
var id = target.addChild(compileBody(target, false, name, null));
var id = target.addChild(compileBody(target, name, null));
target.add(_i -> Instruction.loadFunc(id, true, false, true, null, captures(id, target)));
}

View File

@ -23,7 +23,7 @@ public abstract class FunctionNode extends Node {
return ((FunctionScope)target.children.get(id).scope).getCaptureIndices();
}
public final CompileResult compileBody(Environment env, FunctionScope scope, boolean lastReturn, boolean hasArgs, String _name, String selfName) {
public final CompileResult compileBody(Environment env, FunctionScope scope, boolean lastReturn, String _name, String selfName) {
var name = this.name() != null ? this.name() : _name;
env = env.child()
@ -31,7 +31,7 @@ public abstract class FunctionNode extends Node {
.remove(LabelContext.CONTINUE_CTX);
return new CompileResult(env, scope, params.params.size(), target -> {
if (hasArgs || params.params.size() > 0) target.add(Instruction.loadArgs(true));
// if (params.params.size() > 0) target.add(Instruction.loadArgs(true));
// if (hasArgs) {
// var argsVar = scope.defineStrict(new Variable("arguments", true), loc());
@ -39,6 +39,7 @@ public abstract class FunctionNode extends Node {
// }
if (params.params.size() > 0) {
target.add(Instruction.loadArgs(true));
if (params.params.size() > 1) target.add(Instruction.dup(params.params.size() - 1));
var i = 0;
@ -70,13 +71,13 @@ public abstract class FunctionNode extends Node {
if (params.restName != null) {
if (scope.has(params.restName, false)) throw new SyntaxException(params.restLocation, "Duplicate parameter name not allowed");
var restVar = scope.defineParam(new Variable(params.restName, false), params.restLocation);
var restVar = scope.define(new Variable(params.restName, false), params.restLocation);
target.add(Instruction.loadRestArgs(params.params.size()));
target.add(_i -> Instruction.storeVar(restVar.index()));
}
if (selfName != null && !scope.has(name, false)) {
var i = scope.defineParam(new Variable(selfName, true), end);
var i = scope.defineSpecial(new Variable(selfName, true), end);
target.add(Instruction.loadCallee());
target.add(_i -> Instruction.storeVar(i.index(), false));
@ -92,8 +93,8 @@ public abstract class FunctionNode extends Node {
scope.finish();
});
}
public final CompileResult compileBody(CompileResult parent, boolean hasArgs, String name, String selfName) {
return compileBody(parent.env, new FunctionScope(parent.scope), false, hasArgs, name, selfName);
public final CompileResult compileBody(CompileResult parent, String name, String selfName) {
return compileBody(parent.env, new FunctionScope(parent.scope), false, name, selfName);
}
public abstract void compile(CompileResult target, boolean pollute, String name, BreakpointType bp);

View File

@ -16,7 +16,7 @@ public class FunctionStatementNode extends FunctionNode {
}
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
var id = target.addChild(compileBody(target, true, name, null));
var id = target.addChild(compileBody(target, name, null));
target.add(_i -> Instruction.loadFunc(id, true, true, false, name, captures(id, target)));
target.add(VariableNode.toSet(target, end, this.name, pollute, true));
}

View File

@ -10,7 +10,7 @@ public class FunctionValueNode extends FunctionNode {
@Override public String name() { return name; }
@Override public void compile(CompileResult target, boolean pollute, String name, BreakpointType bp) {
var id = target.addChild(compileBody(target, true, name, null));
var id = target.addChild(compileBody(target, name, null));
target.add(_i -> Instruction.loadFunc(id, true, true, false, name, captures(id, target)));
}

View File

@ -324,7 +324,7 @@ public final class JavaScript {
public static CompileResult compile(Environment env, Node ...statements) {
var func = new FunctionValueNode(null, null, new Parameters(List.of()), new CompoundNode(null, statements), null);
var res = func.compileBody(env, new FunctionScope(true), true, true, null, null);
var res = func.compileBody(env, new FunctionScope(true), true, null, null);
res.buildTask.run();
return res;
}