ES6 Support Groundwork + Fixes #26

Merged
TopchetoEU merged 49 commits from ES6 into master 2024-09-05 14:26:07 +00:00
9 changed files with 31 additions and 92 deletions
Showing only changes of commit 7ab78b9cea - Show all commits

View File

@ -22,9 +22,12 @@ public class CompoundNode extends Node {
@Override public void compile(CompileResult target, boolean pollute, BreakpointType type) { @Override public void compile(CompileResult target, boolean pollute, BreakpointType type) {
List<Node> statements = new ArrayList<Node>(); List<Node> statements = new ArrayList<Node>();
var subtarget = target.subtarget();
subtarget.add(() -> Instruction.stackAlloc(subtarget.scope.allocCount()));
for (var stm : this.statements) { for (var stm : this.statements) {
if (stm instanceof FunctionStatementNode) { if (stm instanceof FunctionStatementNode func) {
stm.compile(target, false); func.compile(subtarget, false);
} }
else statements.add(stm); else statements.add(stm);
} }
@ -34,10 +37,13 @@ public class CompoundNode extends Node {
for (var i = 0; i < statements.size(); i++) { for (var i = 0; i < statements.size(); i++) {
var stm = statements.get(i); var stm = statements.get(i);
if (i != statements.size() - 1) stm.compile(target, false, BreakpointType.STEP_OVER); if (i != statements.size() - 1) stm.compile(subtarget, false, BreakpointType.STEP_OVER);
else stm.compile(target, polluted = pollute, 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) { if (!polluted && pollute) {
target.add(Instruction.pushUndefined()); target.add(Instruction.pushUndefined());
} }

View File

@ -66,7 +66,6 @@ public abstract class FunctionNode extends Node {
subtarget.length = args.length; subtarget.length = args.length;
subtarget.scope.end(); subtarget.scope.end();
funcScope.end(); funcScope.end();
subtarget.add(Instruction.ret()).setLocation(end);
if (pollute) compileLoadFunc(target, funcScope.getCaptureIndices(), name); if (pollute) compileLoadFunc(target, funcScope.getCaptureIndices(), name);

View File

@ -288,8 +288,6 @@ public class JavaScript {
target.add(Instruction.throwSyntax(e)).setLocation(stm.loc()); target.add(Instruction.throwSyntax(e)).setLocation(stm.loc());
} }
target.add(Instruction.ret()).setLocation(stm.loc());
return target; return target;
} }

View File

@ -44,13 +44,7 @@ public class ForInNode extends Node {
var end = new DeferredIntSupplier(); var end = new DeferredIntSupplier();
LabelContext.pushLoop(target.env, loc(), label, end, start); 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); body.compile(target, false, BreakpointType.STEP_OVER);
subtarget.scope.end();
subtarget.add(Instruction.stackFree(subtarget.scope.allocCount()));
LabelContext.popLoop(target.env, label); LabelContext.popLoop(target.env, label);
int endI = target.size(); int endI = target.size();

View File

@ -30,16 +30,9 @@ public class ForNode extends Node {
int mid = target.temp(); int mid = target.temp();
var end = new DeferredIntSupplier(); var end = 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);
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()));
LabelContext.popLoop(target.env, label); LabelContext.popLoop(target.env, label);
// int beforeAssign = target.size(); // int beforeAssign = target.size();

View File

@ -51,13 +51,7 @@ public class ForOfNode extends Node {
var end = new DeferredIntSupplier(); var end = new DeferredIntSupplier();
LabelContext.pushLoop(target.env, loc(), label, end, start); 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); body.compile(target, false, BreakpointType.STEP_OVER);
subtarget.scope.end();
subtarget.add(Instruction.stackFree(subtarget.scope.allocCount()));
LabelContext.popLoop(target.env, label); LabelContext.popLoop(target.env, label);
int endI = target.size(); int endI = target.size();

View File

@ -29,15 +29,7 @@ public class IfNode extends Node {
var end = new DeferredIntSupplier(); var end = new DeferredIntSupplier();
LabelContext.getBreak(target.env).push(loc(), label, end); LabelContext.getBreak(target.env).push(loc(), label, end);
body.compile(target, false, BreakpointType.STEP_OVER);
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()));
LabelContext.getBreak(target.env).pop(label); LabelContext.getBreak(target.env).pop(label);
int endI = target.size(); int endI = target.size();
@ -50,25 +42,11 @@ public class IfNode extends Node {
var end = new DeferredIntSupplier(); var end = new DeferredIntSupplier();
LabelContext.getBreak(target.env).push(loc(), label, end); LabelContext.getBreak(target.env).push(loc(), label, end);
body.compile(target, false, BreakpointType.STEP_OVER);
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()));
int mid = target.temp(); int mid = target.temp();
var elseTarget = target.subtarget(); body.compile(target, false, BreakpointType.STEP_OVER);
elseTarget.add(() -> Instruction.stackAlloc(elseTarget.scope.allocCount()));
body.compile(elseTarget, false, BreakpointType.STEP_OVER);
elseTarget.scope.end();
elseTarget.add(Instruction.stackFree(elseTarget.scope.allocCount()));
LabelContext.getBreak(target.env).pop(label); LabelContext.getBreak(target.env).pop(label);
int endI = target.size(); int endI = target.size();

View File

@ -14,9 +14,9 @@ import me.topchetoeu.jscript.compilation.LabelContext;
import me.topchetoeu.jscript.compilation.Node; import me.topchetoeu.jscript.compilation.Node;
public class TryNode extends Node { public class TryNode extends Node {
public final Node tryBody; public final CompoundNode tryBody;
public final Node catchBody; public final CompoundNode catchBody;
public final Node finallyBody; public final CompoundNode finallyBody;
public final String captureName; public final String captureName;
public final String label; public final String label;
@ -34,43 +34,26 @@ public class TryNode extends Node {
LabelContext.getBreak(target.env).push(loc(), label, endSuppl); LabelContext.getBreak(target.env).push(loc(), label, endSuppl);
{ tryBody.compile(target, false);
var subtarget = target.subtarget(); target.add(Instruction.tryEnd());
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()));
}
if (catchBody != null) { if (catchBody != null) {
catchStart = target.size() - start; catchStart = target.size() - start;
var subtarget = target.subtarget(); if (captureName != null) {
var decN = captureName != null ? 1 : 0; 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()); target.add(Instruction.tryEnd());
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));
} }
if (finallyBody != null) { if (finallyBody != null) {
finallyStart = target.size() - start; finallyStart = target.size() - start;
finallyBody.compile(target, false);
var subtarget = target.subtarget();
finallyBody.compile(subtarget, false);
subtarget.add(Instruction.tryEnd());
} }
LabelContext.getBreak(target.env).pop(label); LabelContext.getBreak(target.env).pop(label);
@ -83,7 +66,7 @@ public class TryNode extends Node {
if (pollute) target.add(Instruction.pushUndefined()); 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); super(loc);
this.tryBody = tryBody; this.tryBody = tryBody;
this.catchBody = catchBody; this.catchBody = catchBody;
@ -109,7 +92,7 @@ public class TryNode extends Node {
n += Parsing.skipEmpty(src, i + n); n += Parsing.skipEmpty(src, i + n);
String capture = null; String capture = null;
Node catchBody = null, finallyBody = null; CompoundNode catchBody = null, finallyBody = null;
if (Parsing.isIdentifier(src, i + n, "catch")) { if (Parsing.isIdentifier(src, i + n, "catch")) {
n += 5; n += 5;

View File

@ -28,13 +28,7 @@ public class WhileNode extends Node {
LabelContext.pushLoop(target.env, loc(), label, end, start); 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); body.compile(target, false, BreakpointType.STEP_OVER);
subtarget.scope.end();
subtarget.add(Instruction.stackFree(subtarget.scope.allocCount()));
LabelContext.popLoop(target.env, label); LabelContext.popLoop(target.env, label);
var endI = target.size(); var endI = target.size();