fix: a plethora of loop off by one and null issues
This commit is contained in:
parent
274a925ff8
commit
66440a9649
@ -27,12 +27,10 @@ public class DoWhileNode extends Node {
|
|||||||
@Override public void compile(CompileResult target, boolean pollute) {
|
@Override public void compile(CompileResult target, boolean pollute) {
|
||||||
int start = target.size();
|
int start = target.size();
|
||||||
var end = new DeferredIntSupplier();
|
var end = new DeferredIntSupplier();
|
||||||
var mid = new DeferredIntSupplier();
|
|
||||||
|
|
||||||
LabelContext.pushLoop(target.env, loc(), label, end, start);
|
LabelContext.pushLoop(target.env, loc(), label, end, start);
|
||||||
body.compile(target, false, BreakpointType.STEP_OVER);
|
body.compile(target, false, BreakpointType.STEP_OVER);
|
||||||
|
|
||||||
mid.set(target.size());
|
|
||||||
condition.compile(target, true, BreakpointType.STEP_OVER);
|
condition.compile(target, true, BreakpointType.STEP_OVER);
|
||||||
int endI = target.size();
|
int endI = target.size();
|
||||||
end.set(endI + 1);
|
end.set(endI + 1);
|
||||||
|
@ -52,7 +52,7 @@ public class ForInNode extends Node {
|
|||||||
target.add(Instruction.discard());
|
target.add(Instruction.discard());
|
||||||
target.set(mid, Instruction.jmpIfNot(endI - mid + 1));
|
target.set(mid, Instruction.jmpIfNot(endI - mid + 1));
|
||||||
|
|
||||||
end.set(endI);
|
end.set(endI + 1);
|
||||||
LabelContext.popLoop(target.env, label);
|
LabelContext.popLoop(target.env, label);
|
||||||
|
|
||||||
if (pollute) target.add(Instruction.pushUndefined());
|
if (pollute) target.add(Instruction.pushUndefined());
|
||||||
|
@ -12,14 +12,13 @@ import me.topchetoeu.jscript.compilation.JavaScript;
|
|||||||
import me.topchetoeu.jscript.compilation.LabelContext;
|
import me.topchetoeu.jscript.compilation.LabelContext;
|
||||||
import me.topchetoeu.jscript.compilation.Node;
|
import me.topchetoeu.jscript.compilation.Node;
|
||||||
import me.topchetoeu.jscript.compilation.VariableDeclareNode;
|
import me.topchetoeu.jscript.compilation.VariableDeclareNode;
|
||||||
import me.topchetoeu.jscript.compilation.values.operations.DiscardNode;
|
|
||||||
|
|
||||||
public class ForNode extends Node {
|
public class ForNode extends Node {
|
||||||
public final Node declaration, assignment, condition, body;
|
public final Node declaration, assignment, condition, body;
|
||||||
public final String label;
|
public final String label;
|
||||||
|
|
||||||
@Override public void resolve(CompileResult target) {
|
@Override public void resolve(CompileResult target) {
|
||||||
declaration.resolve(target);
|
if (declaration != null) declaration.resolve(target);
|
||||||
body.resolve(target);
|
body.resolve(target);
|
||||||
}
|
}
|
||||||
@Override public void compileFunctions(CompileResult 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);
|
if (assignment != null) assignment.compile(target, false, BreakpointType.STEP_OVER);
|
||||||
int endI = target.size();
|
int endI = target.size();
|
||||||
|
|
||||||
end.set(endI);
|
end.set(endI + 1);
|
||||||
LabelContext.popLoop(target.env, label);
|
LabelContext.popLoop(target.env, label);
|
||||||
|
|
||||||
target.add(Instruction.jmp(start - endI));
|
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());
|
if (pollute) target.add(Instruction.pushUndefined());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +66,7 @@ public class ForNode extends Node {
|
|||||||
var n = Parsing.skipEmpty(src, i);
|
var n = Parsing.skipEmpty(src, i);
|
||||||
|
|
||||||
if (!src.is(i + n, ";")) return ParseRes.failed();
|
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) {
|
private static ParseRes<Node> parseCondition(Source src, int i) {
|
||||||
var n = Parsing.skipEmpty(src, i);
|
var n = Parsing.skipEmpty(src, i);
|
||||||
|
@ -38,8 +38,8 @@ public class WhileNode extends Node {
|
|||||||
end.set(endI + 1);
|
end.set(endI + 1);
|
||||||
LabelContext.popLoop(target.env, label);
|
LabelContext.popLoop(target.env, label);
|
||||||
|
|
||||||
target.add(Instruction.jmp(start - end.getAsInt()));
|
target.add(Instruction.jmp(start - endI));
|
||||||
target.set(mid, Instruction.jmpIfNot(end.getAsInt() - mid + 1));
|
target.set(mid, Instruction.jmpIfNot(endI - mid + 1));
|
||||||
if (pollute) target.add(Instruction.pushUndefined());
|
if (pollute) target.add(Instruction.pushUndefined());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user