From 8f13ff3e0b3925098cbeb0dfa044d19331aa9e47 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:29:20 +0300 Subject: [PATCH] fix: scope gets polluted by arguments with named function expressions --- .../jscript/compilation/FunctionArrowNode.java | 2 +- .../jscript/compilation/FunctionNode.java | 13 +++++++------ .../jscript/compilation/FunctionStatementNode.java | 2 +- .../jscript/compilation/FunctionValueNode.java | 2 +- .../topchetoeu/jscript/compilation/JavaScript.java | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java index 7aaeb93..f816f3f 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java @@ -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))); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java index 876f21a..ea13605 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java @@ -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); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionStatementNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionStatementNode.java index b90307a..550730a 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionStatementNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionStatementNode.java @@ -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)); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionValueNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionValueNode.java index 2b60022..db22d72 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionValueNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionValueNode.java @@ -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))); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java b/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java index c8bf4ef..94dfcd2 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java @@ -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; }