From f712fb09aed0913d185afde46d53b9b5dbc6df59 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 22 Jan 2025 20:32:55 +0200 Subject: [PATCH] refactor: merge TYPEOF instruction into OPERATION --- .../me/topchetoeu/j2s/common/Instruction.java | 10 +--- .../me/topchetoeu/j2s/common/Operation.java | 51 ++++++++++--------- .../values/operations/TypeofNode.java | 5 +- .../j2s/runtime/InstructionRunner.java | 17 ++----- 4 files changed, 34 insertions(+), 49 deletions(-) diff --git a/common/src/main/java/me/topchetoeu/j2s/common/Instruction.java b/common/src/main/java/me/topchetoeu/j2s/common/Instruction.java index 59487dd..73438ae 100644 --- a/common/src/main/java/me/topchetoeu/j2s/common/Instruction.java +++ b/common/src/main/java/me/topchetoeu/j2s/common/Instruction.java @@ -51,8 +51,7 @@ public class Instruction { STORE_MEMBER_INT(0x4A), STORE_MEMBER_STR(0x4B), - TYPEOF(0x53), - OPERATION(0x54), + OPERATION(0x50), GLOB_GET(0x60), GLOB_SET(0x61), @@ -333,13 +332,6 @@ public class Instruction { return new Instruction(Type.DISCARD); } - public static Instruction typeof() { - return new Instruction(Type.TYPEOF); - } - public static Instruction typeof(String varName) { - return new Instruction(Type.TYPEOF, varName); - } - public static Instruction operation(Operation op) { return new Instruction(Type.OPERATION, op); } diff --git a/common/src/main/java/me/topchetoeu/j2s/common/Operation.java b/common/src/main/java/me/topchetoeu/j2s/common/Operation.java index 10285fa..846d3ec 100644 --- a/common/src/main/java/me/topchetoeu/j2s/common/Operation.java +++ b/common/src/main/java/me/topchetoeu/j2s/common/Operation.java @@ -3,36 +3,37 @@ package me.topchetoeu.j2s.common; import java.util.HashMap; public enum Operation { - INSTANCEOF(1, 2), - IN(2, 2), + TYPEOF(0x10, 1), + INSTANCEOF(0x11, 2), + IN(0x12, 2), - MULTIPLY(3, 2), - DIVIDE(4, 2), - MODULO(5, 2), - ADD(6, 2), - SUBTRACT(7, 2), + MULTIPLY(0x20, 2), + DIVIDE(0x21, 2), + MODULO(0x22, 2), + ADD(0x23, 2), + SUBTRACT(0x24, 2), - USHIFT_RIGHT(8, 2), - SHIFT_RIGHT(9, 2), - SHIFT_LEFT(10, 2), + USHIFT_RIGHT(0x30, 2), + SHIFT_RIGHT(0x31, 2), + SHIFT_LEFT(0x32, 2), - GREATER(11, 2), - LESS(12, 2), - GREATER_EQUALS(13, 2), - LESS_EQUALS(14, 2), - LOOSE_EQUALS(15, 2), - LOOSE_NOT_EQUALS(16, 2), - EQUALS(17, 2), - NOT_EQUALS(18, 2), + GREATER(0x40, 2), + LESS(0x41, 2), + GREATER_EQUALS(0x42, 2), + LESS_EQUALS(0x43, 2), + LOOSE_EQUALS(0x44, 2), + LOOSE_NOT_EQUALS(0x45, 2), + EQUALS(0x46, 2), + NOT_EQUALS(0x47, 2), - AND(19, 2), - OR(20, 2), - XOR(21, 2), + AND(0x50, 2), + OR(0x51, 2), + XOR(0x52, 2), - NEG(23, 1), - POS(24, 1), - NOT(25, 1), - INVERSE(26, 1); + NEG(0x60, 1), + POS(0x61, 1), + NOT(0x62, 1), + INVERSE(0x63, 1); private static final HashMap operations = new HashMap<>(); diff --git a/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/operations/TypeofNode.java b/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/operations/TypeofNode.java index eb4338b..17065fe 100644 --- a/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/operations/TypeofNode.java +++ b/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/operations/TypeofNode.java @@ -2,6 +2,7 @@ package me.topchetoeu.j2s.compilation.values.operations; import me.topchetoeu.j2s.common.Instruction; import me.topchetoeu.j2s.common.Location; +import me.topchetoeu.j2s.common.Operation; import me.topchetoeu.j2s.compilation.CompileResult; import me.topchetoeu.j2s.compilation.JavaScript; import me.topchetoeu.j2s.compilation.Node; @@ -20,14 +21,14 @@ public class TypeofNode extends Node { @Override public void compile(CompileResult target, boolean pollute) { if (value instanceof VariableNode varNode) { target.add(VariableNode.toGet(target, varNode.loc(), varNode.name, true, true)); - if (pollute) target.add(Instruction.typeof()); + if (pollute) target.add(Instruction.operation(Operation.TYPEOF)); else target.add(Instruction.discard()); return; } value.compile(target, pollute); - if (pollute) target.add(Instruction.typeof()); + if (pollute) target.add(Instruction.operation(Operation.TYPEOF)); } public TypeofNode(Location loc, Node value) { diff --git a/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java b/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java index 7d33795..e7e9388 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java @@ -235,18 +235,6 @@ public class InstructionRunner { return null; } - private static Value execTypeof(Environment env, Instruction instr, Frame frame) { - String name = instr.get(0); - Value obj; - - if (name != null) obj = Value.global(env).getMember(env, name); - else obj = frame.pop(); - - frame.push(obj.type()); - - frame.codePtr++; - return null; - } private static Value execNop(Environment env, Instruction instr, Frame frame) { frame.codePtr++; return null; @@ -355,6 +343,10 @@ public class InstructionRunner { case INSTANCEOF: res = BoolValue.of(stack[ptr - 1].isInstanceOf(env, stack[ptr].getMember(env, StringValue.of("prototype")))); break; + case TYPEOF: + res = stack[ptr++].type(); + frame.stackPtr++; + break; default: return null; } @@ -481,7 +473,6 @@ public class InstructionRunner { case STORE_MEMBER_INT: return execStoreMemberInt(env, instr, frame); case STORE_VAR: return execStoreVar(env, instr, frame); - case TYPEOF: return execTypeof(env, instr, frame); case DELETE: return execDelete(env, instr, frame); case JMP: return execJmp(env, instr, frame);