diff --git a/src/main/java/me/topchetoeu/jscript/common/Instruction.java b/src/main/java/me/topchetoeu/jscript/common/Instruction.java index 838049c..d846072 100644 --- a/src/main/java/me/topchetoeu/jscript/common/Instruction.java +++ b/src/main/java/me/topchetoeu/jscript/common/Instruction.java @@ -64,7 +64,8 @@ public class Instruction { GLOB_DEF(0x62), STACK_ALLOC(0x70), - STACK_FREE(0x71); + STACK_REALLOC(0x71), + STACK_FREE(0x72); private static final HashMap types = new HashMap<>(); public final int numeric; @@ -456,11 +457,14 @@ public class Instruction { return new Instruction(Type.OPERATION, op); } - public static Instruction stackAlloc(int i) { - return new Instruction(Type.STACK_ALLOC, i); + public static Instruction stackAlloc(int n) { + return new Instruction(Type.STACK_ALLOC, n); } - public static Instruction stackFree(int i) { - return new Instruction(Type.STACK_FREE, i); + public static Instruction stackRealloc(int n) { + return new Instruction(Type.STACK_REALLOC, n); + } + public static Instruction stackFree(int n) { + return new Instruction(Type.STACK_FREE, n); } @Override public String toString() { diff --git a/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java b/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java index 585a389..67badc2 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java @@ -505,7 +505,15 @@ public class InstructionRunner { int n = instr.get(0); for (var i = 0; i < n; i++) frame.locals.add(new Value[] { Value.UNDEFINED }); - + + frame.codePtr++; + return null; + } + private static Value execStackRealloc(Environment env, Instruction instr, Frame frame) { + int n = instr.get(0); + + for (var i = frame.locals.size() - n; i < frame.locals.size(); i++) frame.locals.set(i, new Value[] { frame.locals.get(i)[0] }); + frame.codePtr++; return null; } @@ -576,6 +584,7 @@ public class InstructionRunner { case GLOB_SET: return exexGlobSet(env, instr, frame); case STACK_ALLOC: return execStackAlloc(env, instr, frame); + case STACK_REALLOC: return execStackRealloc(env, instr, frame); case STACK_FREE: return execStackFree(env, instr, frame); default: throw EngineException.ofSyntax("Invalid instruction " + instr.type.name() + ".");