From 754648fbf655ef76a8eea8c9c5094a28f4a22cc4 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 23 Nov 2024 20:08:01 +0200 Subject: [PATCH] regress: remove ES6 instructions --- .../jscript/common/Instruction.java | 178 +++++++++--------- 1 file changed, 85 insertions(+), 93 deletions(-) diff --git a/src/main/java/me/topchetoeu/jscript/common/Instruction.java b/src/main/java/me/topchetoeu/jscript/common/Instruction.java index 60251f9..f5cabb0 100644 --- a/src/main/java/me/topchetoeu/jscript/common/Instruction.java +++ b/src/main/java/me/topchetoeu/jscript/common/Instruction.java @@ -17,10 +17,7 @@ public class Instruction { TRY_END(0x06), CALL(0x10), - @Deprecated - CALL_MEMBER(0x11), CALL_NEW(0x12), - CALL_SUPER(0x13), JMP_IF(0x18), JMP_IFN(0x19), JMP(0x1A), @@ -40,11 +37,12 @@ public class Instruction { LOAD_GLOB(0x38), LOAD_INTRINSICS(0x39), - LOAD_ARGS(0x3A), - LOAD_REST_ARGS(0x3B), - LOAD_CALLEE(0x3C), - LOAD_THIS(0x3D), - LOAD_ERROR(0x3E), + LOAD_ARG(0x3A), + LOAD_ARGS_N(0x3B), + LOAD_ARGS(0x3C), + LOAD_CALLED(0x3D), + LOAD_THIS(0x3E), + LOAD_ERROR(0x3F), LOAD_VAR(0x40), LOAD_MEMBER(0x41), @@ -61,16 +59,10 @@ public class Instruction { KEYS(0x52), TYPEOF(0x53), OPERATION(0x54), - EXTEND(0x55), GLOB_GET(0x60), GLOB_SET(0x61), - GLOB_DEF(0x62), - - // CAP_INIT(0x70), - VAR_INIT(0x71), - CAP_FREE(0x72), - VAR_FREE(0x73); + GLOB_DEF(0x62); private static final HashMap types = new HashMap<>(); public final int numeric; @@ -88,8 +80,19 @@ public class Instruction { } } public static enum BreakpointType { + /** + * A debugger should never stop at such instruction, unless a breakpoint has been set on it + */ NONE, + /** + * Debuggers should pause at instructions marked with this breakpoint type + * after any step command + */ STEP_OVER, + /** + * Debuggers should pause at instructions marked with this breakpoint type + * only after a step-in command + */ STEP_IN; public boolean shouldStepIn() { @@ -108,27 +111,6 @@ public class Instruction { if (i >= params.length || i < 0) return null; return (T)params[i]; } - @SuppressWarnings("unchecked") - public T get(int i, T defaultVal) { - if (i >= params.length || i < 0) return defaultVal; - return (T)params[i]; - } - public boolean match(Object ...args) { - if (args.length != params.length) return false; - for (int i = 0; i < args.length; i++) { - var a = params[i]; - var b = args[i]; - if (a == null || b == null) { - if (!(a == null && b == null)) return false; - } - if (!a.equals(b)) return false; - } - return true; - } - public boolean is(int i, Object arg) { - if (params.length <= i) return false; - return params[i].equals(arg); - } // public void write(DataOutputStream writer) throws IOException { // var rawType = type.numeric; @@ -253,60 +235,79 @@ public class Instruction { // } // } + /** + * Signals the start of a protected context + * @param catchStart The point to witch to jump if an error has been caught + * @param finallyStart The point to witch to jump after either the try or catch bodies have exited + * @param end The point to which to jump after exiting the whole protected context + */ public static Instruction tryStart(int catchStart, int finallyStart, int end) { return new Instruction(Type.TRY_START, catchStart, finallyStart, end); } + /** + * Signifies that the current protected section (try, catch or finally) has ended + */ public static Instruction tryEnd() { return new Instruction(Type.TRY_END); } + /** + * Throws the top stack value + */ public static Instruction throwInstr() { return new Instruction(Type.THROW); } + /** + * Converts the given exception to a runtime syntax error and throws it + */ public static Instruction throwSyntax(SyntaxException err) { return new Instruction(Type.THROW_SYNTAX, err.getMessage()); } + /** + * Converts the given exception to a runtime syntax error and throws it + */ public static Instruction throwSyntax(String err) { return new Instruction(Type.THROW_SYNTAX, err); } + /** + * Converts the given exception to a runtime syntax error and throws it + */ public static Instruction throwSyntax(Location loc, String err) { return new Instruction(Type.THROW_SYNTAX, new SyntaxException(loc, err).getMessage()); } + /** + * Performs a JS object property deletion. + * Operands: + * 1. Object to manipulate + * 2. Key to delete + */ public static Instruction delete() { return new Instruction(Type.DELETE); } + /** + * Returns the top stack value + */ public static Instruction ret() { return new Instruction(Type.RETURN); } + /** + * A special NOP instruction telling any debugger to pause + */ public static Instruction debug() { return new Instruction(Type.NOP, "debug"); } + /** + * Does nothing. May be used for metadata or implementation-specific instructions that don't alter the behavior + */ public static Instruction nop(Object ...params) { return new Instruction(Type.NOP, params); } - public static Instruction call(int argn, boolean hasSelf, String name) { - return new Instruction(Type.CALL, argn, hasSelf, name); - } public static Instruction call(int argn, boolean hasSelf) { - return call(argn, hasSelf, ""); - } - @Deprecated - public static Instruction callMember(int argn, String name) { - return new Instruction(Type.CALL_MEMBER, argn, name); - } - @Deprecated - public static Instruction callMember(int argn) { - return new Instruction(Type.CALL_MEMBER, argn, ""); - } - public static Instruction callNew(int argn, String name) { - return new Instruction(Type.CALL_NEW, argn, name); + return new Instruction(Type.CALL, argn, hasSelf); } public static Instruction callNew(int argn) { - return new Instruction(Type.CALL_NEW, argn, ""); - } - public static Instruction callSuper(int argn) { - return new Instruction(Type.CALL_SUPER, argn); + return new Instruction(Type.CALL_NEW, argn); } public static Instruction jmp(int offset) { @@ -362,14 +363,30 @@ public class Instruction { public static Instruction loadThis() { return new Instruction(Type.LOAD_THIS); } - public static Instruction loadArgs(boolean real) { - return new Instruction(Type.LOAD_ARGS, real); + /** + * Loads the given argument + * @param i The index of the argument to load. If -1, will get the index from the stack instead + */ + public static Instruction loadArg(int i) { + return new Instruction(Type.LOAD_ARG, i); } - public static Instruction loadRestArgs(int offset) { - return new Instruction(Type.LOAD_REST_ARGS, offset); + /** + * Pushes the amount of arguments to the stack + */ + public static Instruction loadArgsN() { + return new Instruction(Type.LOAD_ARGS_N); } - public static Instruction loadCallee() { - return new Instruction(Type.LOAD_CALLEE); + /** + * Pushes the arguments object to the stack + */ + public static Instruction loadArgs() { + return new Instruction(Type.LOAD_ARGS); + } + /** + * Loads a reference to the function being called + */ + public static Instruction loadCalled() { + return new Instruction(Type.LOAD_CALLED); } public static Instruction loadGlob() { return new Instruction(Type.LOAD_GLOB); @@ -394,17 +411,11 @@ public class Instruction { return new Instruction(Type.LOAD_REGEX, pattern, flags); } // TODO: make this capturing a concern of the compiler - public static Instruction loadFunc(int id, boolean callable, boolean constructible, boolean captureThis, boolean noThis, String name, int[] captures) { - if (name == null) name = ""; - - var args = new Object[6 + captures.length]; + public static Instruction loadFunc(int id, String name, int[] captures) { + var args = new Object[2 + captures.length]; args[0] = id; args[1] = name; - args[2] = callable; - args[3] = constructible; - args[4] = captureThis; - args[5] = noThis; - for (var i = 0; i < captures.length; i++) args[i + 6] = captures[i]; + for (var i = 0; i < captures.length; i++) args[i + 2] = captures[i]; return new Instruction(Type.LOAD_FUNC, args); } public static Instruction loadObj() { @@ -463,36 +474,17 @@ public class Instruction { return new Instruction(Type.KEYS, own, onlyEnumerable); } - public static Instruction defProp(boolean setter, boolean enumerable) { - return new Instruction(Type.DEF_PROP, setter, enumerable); + public static Instruction defProp(boolean setter) { + return new Instruction(Type.DEF_PROP, setter); } - public static Instruction defField(boolean enumerable) { - return new Instruction(Type.DEF_FIELD, enumerable); - } - public static Instruction extend() { - return new Instruction(Type.EXTEND); + public static Instruction defField() { + return new Instruction(Type.DEF_FIELD); } public static Instruction operation(Operation op) { return new Instruction(Type.OPERATION, op); } - public static Instruction capFree(int i) { - return new Instruction(Type.CAP_FREE, i); - } - public static Instruction varFree(int i) { - return new Instruction(Type.VAR_FREE, i); - } - public static Instruction varInit(int i, boolean force) { - return new Instruction(Type.VAR_INIT, i, force); - } - // public static Instruction stackAlloc(int start, int n) { - // return new Instruction(Type.STACK_ALLOC, start, start + n); - // } - // public static Instruction stackRealloc(int start, int n) { - // return new Instruction(Type.STACK_REALLOC, start, start + n); - // } - @Override public String toString() { var res = type.toString();