fix: a plethora of loop off by one and null issues

This commit is contained in:
TopchetoEU 2024-12-13 02:28:36 +02:00
parent 274a925ff8
commit 66440a9649
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
4 changed files with 7 additions and 10 deletions

View File

@ -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);

View File

@ -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());

View File

@ -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<Node> parseCondition(Source src, int i) {
var n = Parsing.skipEmpty(src, i);

View File

@ -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());
}