diff --git a/src/java/me/topchetoeu/jscript/compilation/CompoundNode.java b/src/java/me/topchetoeu/jscript/compilation/CompoundNode.java index 4db2815..a45f58b 100644 --- a/src/java/me/topchetoeu/jscript/compilation/CompoundNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/CompoundNode.java @@ -22,9 +22,12 @@ public class CompoundNode extends Node { @Override public void compile(CompileResult target, boolean pollute, BreakpointType type) { List statements = new ArrayList(); + var subtarget = target.subtarget(); + subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); + for (var stm : this.statements) { - if (stm instanceof FunctionStatementNode) { - stm.compile(target, false); + if (stm instanceof FunctionStatementNode func) { + func.compile(subtarget, false); } else statements.add(stm); } @@ -34,10 +37,13 @@ public class CompoundNode extends Node { for (var i = 0; i < statements.size(); i++) { var stm = statements.get(i); - if (i != statements.size() - 1) stm.compile(target, false, BreakpointType.STEP_OVER); - else stm.compile(target, polluted = pollute, BreakpointType.STEP_OVER); + if (i != statements.size() - 1) stm.compile(subtarget, false, BreakpointType.STEP_OVER); + else stm.compile(subtarget, polluted = pollute, BreakpointType.STEP_OVER); } + subtarget.scope.end(); + subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); + if (!polluted && pollute) { target.add(Instruction.pushUndefined()); } diff --git a/src/java/me/topchetoeu/jscript/compilation/FunctionNode.java b/src/java/me/topchetoeu/jscript/compilation/FunctionNode.java index daca4a5..ed54d94 100644 --- a/src/java/me/topchetoeu/jscript/compilation/FunctionNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/FunctionNode.java @@ -66,7 +66,6 @@ public abstract class FunctionNode extends Node { subtarget.length = args.length; subtarget.scope.end(); funcScope.end(); - subtarget.add(Instruction.ret()).setLocation(end); if (pollute) compileLoadFunc(target, funcScope.getCaptureIndices(), name); diff --git a/src/java/me/topchetoeu/jscript/compilation/JavaScript.java b/src/java/me/topchetoeu/jscript/compilation/JavaScript.java index ffa5208..96f2bbb 100644 --- a/src/java/me/topchetoeu/jscript/compilation/JavaScript.java +++ b/src/java/me/topchetoeu/jscript/compilation/JavaScript.java @@ -288,8 +288,6 @@ public class JavaScript { target.add(Instruction.throwSyntax(e)).setLocation(stm.loc()); } - target.add(Instruction.ret()).setLocation(stm.loc()); - return target; } diff --git a/src/java/me/topchetoeu/jscript/compilation/control/ForInNode.java b/src/java/me/topchetoeu/jscript/compilation/control/ForInNode.java index 12a52e6..e2c2735 100644 --- a/src/java/me/topchetoeu/jscript/compilation/control/ForInNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/control/ForInNode.java @@ -44,13 +44,7 @@ public class ForInNode extends Node { var end = new DeferredIntSupplier(); LabelContext.pushLoop(target.env, loc(), label, end, start); - var subtarget = target.subtarget(); - subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); - body.compile(target, false, BreakpointType.STEP_OVER); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); LabelContext.popLoop(target.env, label); int endI = target.size(); diff --git a/src/java/me/topchetoeu/jscript/compilation/control/ForNode.java b/src/java/me/topchetoeu/jscript/compilation/control/ForNode.java index bf361f4..8a7c3d3 100644 --- a/src/java/me/topchetoeu/jscript/compilation/control/ForNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/control/ForNode.java @@ -30,16 +30,9 @@ public class ForNode extends Node { int mid = target.temp(); var end = new DeferredIntSupplier(); + LabelContext.pushLoop(target.env, loc(), label, end, start); - - var subtarget = target.subtarget(); - subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); - - body.compile(subtarget, false, BreakpointType.STEP_OVER); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); - + body.compile(target, false, BreakpointType.STEP_OVER); LabelContext.popLoop(target.env, label); // int beforeAssign = target.size(); diff --git a/src/java/me/topchetoeu/jscript/compilation/control/ForOfNode.java b/src/java/me/topchetoeu/jscript/compilation/control/ForOfNode.java index 514a255..87ffb45 100644 --- a/src/java/me/topchetoeu/jscript/compilation/control/ForOfNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/control/ForOfNode.java @@ -51,13 +51,7 @@ public class ForOfNode extends Node { var end = new DeferredIntSupplier(); LabelContext.pushLoop(target.env, loc(), label, end, start); - var subtarget = target.subtarget(); - subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); - body.compile(target, false, BreakpointType.STEP_OVER); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); LabelContext.popLoop(target.env, label); int endI = target.size(); diff --git a/src/java/me/topchetoeu/jscript/compilation/control/IfNode.java b/src/java/me/topchetoeu/jscript/compilation/control/IfNode.java index e4045a4..d6a07f5 100644 --- a/src/java/me/topchetoeu/jscript/compilation/control/IfNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/control/IfNode.java @@ -29,15 +29,7 @@ public class IfNode extends Node { var end = new DeferredIntSupplier(); LabelContext.getBreak(target.env).push(loc(), label, end); - - var subtarget = target.subtarget(); - subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); - - body.compile(subtarget, false, BreakpointType.STEP_OVER); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); - + body.compile(target, false, BreakpointType.STEP_OVER); LabelContext.getBreak(target.env).pop(label); int endI = target.size(); @@ -50,25 +42,11 @@ public class IfNode extends Node { var end = new DeferredIntSupplier(); LabelContext.getBreak(target.env).push(loc(), label, end); - - var bodyTarget = target.subtarget(); - bodyTarget.add(() -> Instruction.stackAlloc(bodyTarget.scope.allocCount())); - - body.compile(bodyTarget, false, BreakpointType.STEP_OVER); - - bodyTarget.scope.end(); - bodyTarget.add(Instruction.stackFree(bodyTarget.scope.allocCount())); + body.compile(target, false, BreakpointType.STEP_OVER); int mid = target.temp(); - var elseTarget = target.subtarget(); - elseTarget.add(() -> Instruction.stackAlloc(elseTarget.scope.allocCount())); - - body.compile(elseTarget, false, BreakpointType.STEP_OVER); - - elseTarget.scope.end(); - elseTarget.add(Instruction.stackFree(elseTarget.scope.allocCount())); - + body.compile(target, false, BreakpointType.STEP_OVER); LabelContext.getBreak(target.env).pop(label); int endI = target.size(); diff --git a/src/java/me/topchetoeu/jscript/compilation/control/TryNode.java b/src/java/me/topchetoeu/jscript/compilation/control/TryNode.java index 52d6723..04e4650 100644 --- a/src/java/me/topchetoeu/jscript/compilation/control/TryNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/control/TryNode.java @@ -14,9 +14,9 @@ import me.topchetoeu.jscript.compilation.LabelContext; import me.topchetoeu.jscript.compilation.Node; public class TryNode extends Node { - public final Node tryBody; - public final Node catchBody; - public final Node finallyBody; + public final CompoundNode tryBody; + public final CompoundNode catchBody; + public final CompoundNode finallyBody; public final String captureName; public final String label; @@ -34,43 +34,26 @@ public class TryNode extends Node { LabelContext.getBreak(target.env).push(loc(), label, endSuppl); - { - var subtarget = target.subtarget(); - subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); - - tryBody.compile(subtarget, false); - subtarget.add(Instruction.tryEnd()); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); - } + tryBody.compile(target, false); + target.add(Instruction.tryEnd()); if (catchBody != null) { catchStart = target.size() - start; - var subtarget = target.subtarget(); - var decN = captureName != null ? 1 : 0; + if (captureName != null) { + var subtarget = target.subtarget(); + subtarget.scope.defineStrict(captureName, false, catchBody.loc()); + catchBody.compile(subtarget, false); + subtarget.scope.end(); + } + else catchBody.compile(target, false); - if (captureName != null) subtarget.scope.defineStrict(captureName, false, catchBody.loc()); - - var _subtarget = subtarget; - - subtarget.add(() -> Instruction.stackAlloc(_subtarget.scope.allocCount() - decN)); - - catchBody.compile(subtarget, false); - - subtarget.add(Instruction.tryEnd()); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount() - decN)); + target.add(Instruction.tryEnd()); } if (finallyBody != null) { finallyStart = target.size() - start; - - var subtarget = target.subtarget(); - finallyBody.compile(subtarget, false); - subtarget.add(Instruction.tryEnd()); + finallyBody.compile(target, false); } LabelContext.getBreak(target.env).pop(label); @@ -83,7 +66,7 @@ public class TryNode extends Node { if (pollute) target.add(Instruction.pushUndefined()); } - public TryNode(Location loc, String label, Node tryBody, Node catchBody, Node finallyBody, String captureName) { + public TryNode(Location loc, String label, CompoundNode tryBody, CompoundNode catchBody, CompoundNode finallyBody, String captureName) { super(loc); this.tryBody = tryBody; this.catchBody = catchBody; @@ -109,7 +92,7 @@ public class TryNode extends Node { n += Parsing.skipEmpty(src, i + n); String capture = null; - Node catchBody = null, finallyBody = null; + CompoundNode catchBody = null, finallyBody = null; if (Parsing.isIdentifier(src, i + n, "catch")) { n += 5; diff --git a/src/java/me/topchetoeu/jscript/compilation/control/WhileNode.java b/src/java/me/topchetoeu/jscript/compilation/control/WhileNode.java index fda05e1..55787df 100644 --- a/src/java/me/topchetoeu/jscript/compilation/control/WhileNode.java +++ b/src/java/me/topchetoeu/jscript/compilation/control/WhileNode.java @@ -28,13 +28,7 @@ public class WhileNode extends Node { LabelContext.pushLoop(target.env, loc(), label, end, start); - var subtarget = target.subtarget(); - subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount())); - body.compile(target, false, BreakpointType.STEP_OVER); - - subtarget.scope.end(); - subtarget.add(Instruction.stackFree(subtarget.scope.allocCount())); LabelContext.popLoop(target.env, label); var endI = target.size();