From 99cef983898116315810d6c5a1e4c5f96cffda8e Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:10:07 +0300 Subject: [PATCH] feat: implement access to intrinsics --- src/java/me/topchetoeu/jscript/common/Instruction.java | 6 +++++- .../me/topchetoeu/jscript/runtime/InstructionRunner.java | 6 ++++++ src/java/me/topchetoeu/jscript/runtime/values/Value.java | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/java/me/topchetoeu/jscript/common/Instruction.java b/src/java/me/topchetoeu/jscript/common/Instruction.java index 478fddc..2e7f5b2 100644 --- a/src/java/me/topchetoeu/jscript/common/Instruction.java +++ b/src/java/me/topchetoeu/jscript/common/Instruction.java @@ -36,7 +36,8 @@ public class Instruction { LOAD_ARR(0x31), LOAD_OBJ(0x32), LOAD_GLOB(0x33), - LOAD_REGEX(0x34), + LOAD_INTRINSICS(0x34), + LOAD_REGEX(0x35), LOAD_VAR(0x40), LOAD_MEMBER(0x41), @@ -334,6 +335,9 @@ public class Instruction { public static Instruction loadGlob() { return new Instruction(Type.LOAD_GLOB); } + public static Instruction loadIntrinsics(String key) { + return new Instruction(Type.LOAD_INTRINSICS, key); + } public static Instruction loadMember() { return new Instruction(Type.LOAD_MEMBER); } diff --git a/src/java/me/topchetoeu/jscript/runtime/InstructionRunner.java b/src/java/me/topchetoeu/jscript/runtime/InstructionRunner.java index 9a7829e..a0bd64e 100644 --- a/src/java/me/topchetoeu/jscript/runtime/InstructionRunner.java +++ b/src/java/me/topchetoeu/jscript/runtime/InstructionRunner.java @@ -157,6 +157,11 @@ public class InstructionRunner { frame.codePtr++; return null; } + private static Value execLoadIntrinsics(Environment env, Instruction instr, Frame frame) { + frame.push(Value.intrinsics(env).get((String)instr.get(0))); + frame.codePtr++; + return null; + } private static Value execLoadArr(Environment env, Instruction instr, Frame frame) { var res = new ArrayValue(); res.setSize(instr.get(0)); @@ -382,6 +387,7 @@ public class InstructionRunner { case LOAD_MEMBER: return execLoadMember(env, instr, frame); case LOAD_REGEX: return execLoadRegEx(env, instr, frame); case LOAD_GLOB: return execLoadGlob(env, instr, frame); + case LOAD_INTRINSICS: return execLoadIntrinsics(env, instr, frame); case LOAD_ARGS: return execLoadArgs(env, instr, frame); case LOAD_THIS: return execLoadThis(env, instr, frame); diff --git a/src/java/me/topchetoeu/jscript/runtime/values/Value.java b/src/java/me/topchetoeu/jscript/runtime/values/Value.java index ed90d04..7f322f0 100644 --- a/src/java/me/topchetoeu/jscript/runtime/values/Value.java +++ b/src/java/me/topchetoeu/jscript/runtime/values/Value.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -65,6 +66,7 @@ public abstract class Value { public static final Key TYPE_ERR_PROTO = Key.of(); public static final Key RANGE_ERR_PROTO = Key.of(); public static final Key GLOBAL = Key.of(); + public static final Key> INTRINSICS = Key.of(); public static final VoidValue UNDEFINED = new VoidValue("undefined", new StringValue("undefined")); public static final VoidValue NULL = new VoidValue("null", new StringValue("object")); @@ -680,4 +682,7 @@ public abstract class Value { public static final ObjectValue global(Environment env) { return env.initFrom(GLOBAL, () -> new ObjectValue()); } + public static final Map intrinsics(Environment env) { + return env.initFrom(INTRINSICS, () -> new HashMap<>()); + } }