refactor: merge TYPEOF instruction into OPERATION

This commit is contained in:
TopchetoEU 2025-01-22 20:32:55 +02:00
parent 6355a48c6b
commit f712fb09ae
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
4 changed files with 34 additions and 49 deletions

View File

@ -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);
}

View File

@ -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<Integer, Operation> operations = new HashMap<>();

View File

@ -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) {

View File

@ -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);