From 66440a964932f3d0f57bba98213830b0bc1cd910 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 13 Dec 2024 02:28:36 +0200 Subject: [PATCH] fix: a plethora of loop off by one and null issues --- .../jscript/compilation/control/DoWhileNode.java | 2 -- .../jscript/compilation/control/ForInNode.java | 2 +- .../topchetoeu/jscript/compilation/control/ForNode.java | 9 ++++----- .../jscript/compilation/control/WhileNode.java | 4 ++-- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/DoWhileNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/DoWhileNode.java index d50d19f..abf42ee 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/DoWhileNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/DoWhileNode.java @@ -27,12 +27,10 @@ public class DoWhileNode extends Node { @Override public void compile(CompileResult target, boolean pollute) { int start = target.size(); var end = new DeferredIntSupplier(); - var mid = new DeferredIntSupplier(); LabelContext.pushLoop(target.env, loc(), label, end, start); body.compile(target, false, BreakpointType.STEP_OVER); - mid.set(target.size()); condition.compile(target, true, BreakpointType.STEP_OVER); int endI = target.size(); end.set(endI + 1); 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 77a210d..d620bd9 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/ForInNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/ForInNode.java @@ -52,7 +52,7 @@ public class ForInNode extends Node { target.add(Instruction.discard()); target.set(mid, Instruction.jmpIfNot(endI - mid + 1)); - end.set(endI); + end.set(endI + 1); LabelContext.popLoop(target.env, label); if (pollute) target.add(Instruction.pushUndefined()); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/ForNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/ForNode.java index 32d0c59..25837ef 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/ForNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/ForNode.java @@ -12,14 +12,13 @@ import me.topchetoeu.jscript.compilation.JavaScript; import me.topchetoeu.jscript.compilation.LabelContext; import me.topchetoeu.jscript.compilation.Node; import me.topchetoeu.jscript.compilation.VariableDeclareNode; -import me.topchetoeu.jscript.compilation.values.operations.DiscardNode; public class ForNode extends Node { public final Node declaration, assignment, condition, body; public final String label; @Override public void resolve(CompileResult target) { - declaration.resolve(target); + if (declaration != null) declaration.resolve(target); body.resolve(target); } @Override public void compileFunctions(CompileResult target) { @@ -46,11 +45,11 @@ public class ForNode extends Node { if (assignment != null) assignment.compile(target, false, BreakpointType.STEP_OVER); int endI = target.size(); - end.set(endI); + end.set(endI + 1); LabelContext.popLoop(target.env, label); target.add(Instruction.jmp(start - endI)); - if (mid >= 0) target.set(mid, Instruction.jmpIfNot(endI - mid + 1)); + if (condition != null) target.set(mid, Instruction.jmpIfNot(endI - mid + 1)); if (pollute) target.add(Instruction.pushUndefined()); } @@ -67,7 +66,7 @@ public class ForNode extends Node { var n = Parsing.skipEmpty(src, i); if (!src.is(i + n, ";")) return ParseRes.failed(); - else return ParseRes.res(new DiscardNode(src.loc(i), null), n + 1); + else return ParseRes.res(null, n + 1); } private static ParseRes parseCondition(Source src, int i) { var n = Parsing.skipEmpty(src, i); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/WhileNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/WhileNode.java index a341cce..b687f2a 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/WhileNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/WhileNode.java @@ -38,8 +38,8 @@ public class WhileNode extends Node { end.set(endI + 1); LabelContext.popLoop(target.env, label); - target.add(Instruction.jmp(start - end.getAsInt())); - target.set(mid, Instruction.jmpIfNot(end.getAsInt() - mid + 1)); + target.add(Instruction.jmp(start - endI)); + target.set(mid, Instruction.jmpIfNot(endI - mid + 1)); if (pollute) target.add(Instruction.pushUndefined()); }