refactor: don't require ctx in frame.push

This commit is contained in:
TopchetoEU 2024-01-06 18:23:34 +02:00
parent e4c9a8756e
commit d8585a20bf
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 28 additions and 28 deletions

View File

@ -103,7 +103,7 @@ public class CodeFrame {
public boolean jumpFlag = false, popTryFlag = false; public boolean jumpFlag = false, popTryFlag = false;
private Location prevLoc = null; private Location prevLoc = null;
public ObjectValue getLocalScope(Context ctx, boolean props) { public ObjectValue getLocalScope(boolean props) {
var names = new String[scope.locals.length]; var names = new String[scope.locals.length];
for (int i = 0; i < scope.locals.length; i++) { for (int i = 0; i < scope.locals.length; i++) {
@ -118,7 +118,7 @@ public class CodeFrame {
return new ScopeValue(scope.locals, names); return new ScopeValue(scope.locals, names);
} }
public ObjectValue getCaptureScope(Context ctx, boolean props) { public ObjectValue getCaptureScope(boolean props) {
var names = new String[scope.captures.length]; var names = new String[scope.captures.length];
for (int i = 0; i < scope.captures.length; i++) { for (int i = 0; i < scope.captures.length; i++) {
@ -129,7 +129,7 @@ public class CodeFrame {
return new ScopeValue(scope.captures, names); return new ScopeValue(scope.captures, names);
} }
public ObjectValue getValStackScope(Context ctx) { public ObjectValue getValStackScope() {
return new ObjectValue() { return new ObjectValue() {
@Override @Override
protected Object getField(Context ctx, Object key) { protected Object getField(Context ctx, Object key) {
@ -181,7 +181,7 @@ public class CodeFrame {
return res; return res;
} }
public void push(Context ctx, Object val) { public void push(Object val) {
if (stack.length <= stackPtr) { if (stack.length <= stackPtr) {
var newStack = new Object[stack.length * 2]; var newStack = new Object[stack.length * 2];
System.arraycopy(stack, 0, newStack, 0, stack.length); System.arraycopy(stack, 0, newStack, 0, stack.length);
@ -191,7 +191,7 @@ public class CodeFrame {
} }
public Object next(Object value, Object returnValue, EngineException error) { public Object next(Object value, Object returnValue, EngineException error) {
if (value != Values.NO_RETURN) push(ctx, value); if (value != Values.NO_RETURN) push(value);
Instruction instr = null; Instruction instr = null;
if (codePtr >= 0 && codePtr < function.body.length) instr = function.body[codePtr]; if (codePtr >= 0 && codePtr < function.body.length) instr = function.body[codePtr];

View File

@ -32,7 +32,7 @@ public class Runners {
var func = frame.pop(); var func = frame.pop();
var thisArg = frame.pop(); var thisArg = frame.pop();
frame.push(ctx, Values.call(ctx, func, thisArg, callArgs)); frame.push(Values.call(ctx, func, thisArg, callArgs));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
@ -41,7 +41,7 @@ public class Runners {
var callArgs = frame.take(instr.get(0)); var callArgs = frame.take(instr.get(0));
var funcObj = frame.pop(); var funcObj = frame.pop();
frame.push(ctx, Values.callNew(ctx, funcObj, callArgs)); frame.push(Values.callNew(ctx, funcObj, callArgs));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
@ -64,7 +64,7 @@ public class Runners {
if (!(obj instanceof ObjectValue)) throw EngineException.ofType("Property apply target must be an object."); if (!(obj instanceof ObjectValue)) throw EngineException.ofType("Property apply target must be an object.");
Values.object(obj).defineProperty(ctx, name, Values.function(getter), Values.function(setter), false, false); Values.object(obj).defineProperty(ctx, name, Values.function(getter), Values.function(setter), false, false);
frame.push(ctx, obj); frame.push(obj);
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
@ -74,10 +74,10 @@ public class Runners {
if (!Values.isPrimitive(type)) { if (!Values.isPrimitive(type)) {
var proto = Values.getMember(ctx, type, "prototype"); var proto = Values.getMember(ctx, type, "prototype");
frame.push(ctx, Values.isInstanceOf(ctx, obj, proto)); frame.push(Values.isInstanceOf(ctx, obj, proto));
} }
else { else {
frame.push(ctx, false); frame.push(false);
} }
frame.codePtr++; frame.codePtr++;
@ -89,13 +89,13 @@ public class Runners {
var members = Values.getMembers(ctx, val, false, false); var members = Values.getMembers(ctx, val, false, false);
Collections.reverse(members); Collections.reverse(members);
frame.push(ctx, null); frame.push(null);
for (var el : members) { for (var el : members) {
if (el instanceof Symbol) continue; if (el instanceof Symbol) continue;
var obj = new ObjectValue(); var obj = new ObjectValue();
obj.defineProperty(ctx, "value", el); obj.defineProperty(ctx, "value", el);
frame.push(ctx, obj); frame.push(obj);
} }
frame.codePtr++; frame.codePtr++;
@ -122,45 +122,45 @@ public class Runners {
int count = instr.get(0); int count = instr.get(0);
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
frame.push(ctx, frame.peek(count - 1)); frame.push(frame.peek(count - 1));
} }
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadUndefined(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadUndefined(Context ctx, Instruction instr, CodeFrame frame) {
frame.push(ctx, null); frame.push(null);
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadValue(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadValue(Context ctx, Instruction instr, CodeFrame frame) {
frame.push(ctx, instr.get(0)); frame.push(instr.get(0));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadVar(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadVar(Context ctx, Instruction instr, CodeFrame frame) {
var i = instr.get(0); var i = instr.get(0);
if (i instanceof String) frame.push(ctx, ctx.environment.global.get(ctx, (String)i)); if (i instanceof String) frame.push(ctx.environment.global.get(ctx, (String)i));
else frame.push(ctx, frame.scope.get((int)i).get(ctx)); else frame.push(frame.scope.get((int)i).get(ctx));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadObj(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadObj(Context ctx, Instruction instr, CodeFrame frame) {
frame.push(ctx, new ObjectValue()); frame.push(new ObjectValue());
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadGlob(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadGlob(Context ctx, Instruction instr, CodeFrame frame) {
frame.push(ctx, ctx.environment.global.obj); frame.push(ctx.environment.global.obj);
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadArr(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadArr(Context ctx, Instruction instr, CodeFrame frame) {
var res = new ArrayValue(); var res = new ArrayValue();
res.setSize(instr.get(0)); res.setSize(instr.get(0));
frame.push(ctx, res); frame.push(res);
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
@ -174,7 +174,7 @@ public class Runners {
var func = new CodeFunction(ctx.environment, "", Engine.functions.get(id), captures); var func = new CodeFunction(ctx.environment, "", Engine.functions.get(id), captures);
frame.push(ctx, func); frame.push(func);
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
@ -184,7 +184,7 @@ public class Runners {
var obj = frame.pop(); var obj = frame.pop();
try { try {
frame.push(ctx, Values.getMember(ctx, obj, key)); frame.push(Values.getMember(ctx, obj, key));
} }
catch (IllegalArgumentException e) { catch (IllegalArgumentException e) {
throw EngineException.ofType(e.getMessage()); throw EngineException.ofType(e.getMessage());
@ -193,12 +193,12 @@ public class Runners {
return Values.NO_RETURN; return Values.NO_RETURN;
} }
public static Object execLoadKeyMember(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadKeyMember(Context ctx, Instruction instr, CodeFrame frame) {
frame.push(ctx, instr.get(0)); frame.push(instr.get(0));
return execLoadMember(ctx, instr, frame); return execLoadMember(ctx, instr, frame);
} }
public static Object execLoadRegEx(Context ctx, Instruction instr, CodeFrame frame) { public static Object execLoadRegEx(Context ctx, Instruction instr, CodeFrame frame) {
if (ctx.hasNotNull(Environment.REGEX_CONSTR)) { if (ctx.hasNotNull(Environment.REGEX_CONSTR)) {
frame.push(ctx, Values.callNew(ctx, ctx.get(Environment.REGEX_CONSTR), instr.get(0), instr.get(1))); frame.push(Values.callNew(ctx, ctx.get(Environment.REGEX_CONSTR), instr.get(0), instr.get(1)));
} }
else { else {
throw EngineException.ofSyntax("Regex is not supported."); throw EngineException.ofSyntax("Regex is not supported.");
@ -218,7 +218,7 @@ public class Runners {
var obj = frame.pop(); var obj = frame.pop();
if (!Values.setMember(ctx, obj, key, val)) throw EngineException.ofSyntax("Can't set member '" + key + "'."); if (!Values.setMember(ctx, obj, key, val)) throw EngineException.ofSyntax("Can't set member '" + key + "'.");
if ((boolean)instr.get(0)) frame.push(ctx, val); if ((boolean)instr.get(0)) frame.push(val);
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
@ -264,7 +264,7 @@ public class Runners {
var obj = frame.pop(); var obj = frame.pop();
var index = frame.pop(); var index = frame.pop();
frame.push(ctx, Values.hasMember(ctx, obj, index, false)); frame.push(Values.hasMember(ctx, obj, index, false));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }
@ -280,7 +280,7 @@ public class Runners {
} }
else obj = frame.pop(); else obj = frame.pop();
frame.push(ctx, Values.type(obj)); frame.push(Values.type(obj));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
@ -305,7 +305,7 @@ public class Runners {
for (var i = op.operands - 1; i >= 0; i--) args[i] = frame.pop(); for (var i = op.operands - 1; i >= 0; i--) args[i] = frame.pop();
frame.push(ctx, Values.operation(ctx, op, args)); frame.push(Values.operation(ctx, op, args));
frame.codePtr++; frame.codePtr++;
return Values.NO_RETURN; return Values.NO_RETURN;
} }