From 39eb6ffac51e3ed949aae6bd0e2a1429acbc31a4 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sun, 24 Nov 2024 12:48:30 +0200 Subject: [PATCH] fix: do variable inits properly --- .../topchetoeu/jscript/compilation/FunctionNode.java | 2 +- .../jscript/compilation/VariableDeclareNode.java | 2 +- .../jscript/compilation/control/ForInNode.java | 2 +- .../jscript/compilation/control/TryNode.java | 2 +- .../jscript/compilation/values/VariableNode.java | 12 +++--------- .../values/operations/VariableAssignNode.java | 4 ++-- 6 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java index 38da50a..30878d7 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java @@ -38,7 +38,7 @@ public abstract class FunctionNode extends Node { var index = scope.define(param.name); target.add(Instruction.loadArg(i++)); - target.add(index.index().toInit()); + target.add(index.index().toSet(false)); } // if (selfName != null && !scope.has(selfName, false)) { diff --git a/src/main/java/me/topchetoeu/jscript/compilation/VariableDeclareNode.java b/src/main/java/me/topchetoeu/jscript/compilation/VariableDeclareNode.java index de32404..4ef5076 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/VariableDeclareNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/VariableDeclareNode.java @@ -27,7 +27,7 @@ public class VariableDeclareNode extends Node { for (var entry : values) { if (entry.value != null) { entry.value.compile(target, true); - target.add(VariableNode.toInit(target, loc(), entry.var.name)); + target.add(VariableNode.toSet(target, loc(), entry.var.name, false, true)); } } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/ForInNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/ForInNode.java index 17de917..082ef6d 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/ForInNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/ForInNode.java @@ -34,7 +34,7 @@ public class ForInNode extends Node { int mid = target.temp(); target.add(Instruction.loadMember("value")).setLocation(binding.loc()); - target.add(VariableNode.toInit(target, loc(), binding.name)); + target.add(VariableNode.toSet(target, loc(), binding.name, false, true)); target.setLocationAndDebug(object.loc(), BreakpointType.STEP_OVER); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java index f9e8846..acce411 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/TryNode.java @@ -43,7 +43,7 @@ public class TryNode extends Node { if (captureName != null) { var catchVar = target.scope.defineCatch(captureName); target.add(Instruction.loadError()); - target.add(catchVar.index().toInit()); + target.add(catchVar.index().toSet(false)); catchBody.compile(target, false); target.scope.undefineCatch(); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java index ca32e06..f8b3016 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java @@ -20,7 +20,7 @@ public class VariableNode extends Node implements ChangeTarget { } @Override public void afterAssign(CompileResult target, boolean pollute) { - target.add(VariableNode.toSet(target, loc(), name, pollute)); + target.add(VariableNode.toSet(target, loc(), name, pollute, false)); } @Override public void compile(CompileResult target, boolean pollute) { @@ -40,17 +40,11 @@ public class VariableNode extends Node implements ChangeTarget { return toGet(target, loc, name, true, false); } - public static Instruction toInit(CompileResult target, Location loc, String name) { - var i = target.scope.get(name, false); - - if (i != null) return i.index().toInit(); - else return Instruction.globSet(name, false, true); - } - public static Instruction toSet(CompileResult target, Location loc, String name, boolean keep) { + public static Instruction toSet(CompileResult target, Location loc, String name, boolean keep, boolean init) { var i = target.scope.get(name, false); if (i != null) return i.index().toSet(keep); - else return Instruction.globSet(name, keep, false); + else return Instruction.globSet(name, keep, init); } public VariableNode(Location loc, String name) { diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/VariableAssignNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/VariableAssignNode.java index 9f11b5b..940a27c 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/VariableAssignNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/VariableAssignNode.java @@ -18,11 +18,11 @@ public class VariableAssignNode extends Node { target.add(VariableNode.toGet(target, loc(), name)); FunctionNode.compileWithName(value, target, true, name); target.add(Instruction.operation(operation)); - target.add(VariableNode.toSet(target, loc(), name, pollute)); + target.add(VariableNode.toSet(target, loc(), name, pollute, false)); } else { FunctionNode.compileWithName(value, target, true, name); - target.add(VariableNode.toSet(target, loc(), name, pollute)); + target.add(VariableNode.toSet(target, loc(), name, pollute, false)); } }