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 cb4387c..8746478 100644 --- a/common/src/main/java/me/topchetoeu/j2s/common/Instruction.java +++ b/common/src/main/java/me/topchetoeu/j2s/common/Instruction.java @@ -31,7 +31,6 @@ public class Instruction { LOAD_FUNC(0x30), LOAD_ARR(0x31), LOAD_OBJ(0x32), - LOAD_REGEX(0x33), LOAD_GLOB(0x38), LOAD_INTRINSICS(0x39), @@ -287,9 +286,6 @@ public class Instruction { return new Instruction(Type.LOAD_MEMBER_STR, member); } - public static Instruction loadRegex(String pattern, String flags) { - return new Instruction(Type.LOAD_REGEX, pattern, flags); - } // TODO: make this capturing a concern of the compiler public static Instruction loadFunc(int id, String name, int[] captures) { var args = new Object[2 + captures.length]; diff --git a/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/RegexNode.java b/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/RegexNode.java index fe0fbd7..01dceae 100644 --- a/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/RegexNode.java +++ b/compilation/src/main/java/me/topchetoeu/j2s/compilation/values/RegexNode.java @@ -15,7 +15,10 @@ public class RegexNode extends Node { } @Override public void compile(CompileResult target, boolean pollute) { - target.add(Instruction.loadRegex(pattern, flags)); + target.add(Instruction.loadIntrinsics("regex")); + target.add(Instruction.pushValue(pattern)); + target.add(Instruction.pushValue(flags)); + target.add(Instruction.call(2, false)); if (!pollute) target.add(Instruction.discard()); } diff --git a/lib/src/stdlib/_entry.ts b/lib/src/stdlib/_entry.ts index b330028..6aab2a8 100644 --- a/lib/src/stdlib/_entry.ts +++ b/lib/src/stdlib/_entry.ts @@ -1,4 +1,4 @@ -import { object, setGlobalPrototypes, target } from "./primordials.ts"; +import { object, setGlobalPrototypes, setIntrinsic, target } from "./primordials.ts"; import { Error, RangeError, SyntaxError, TypeError } from "./values/errors.ts"; import { Boolean } from "./values/boolean.ts"; import { Function } from "./values/function.ts"; @@ -87,5 +87,5 @@ setGlobalPrototypes({ type: TypeError.prototype, uint8: Uint8Array.prototype, int32: Int32Array.prototype, - regex: RegExp, }); +setIntrinsic("regex", RegExp); \ No newline at end of file diff --git a/lib/src/stdlib/primordials.ts b/lib/src/stdlib/primordials.ts index 9813bb0..7c9c9ea 100644 --- a/lib/src/stdlib/primordials.ts +++ b/lib/src/stdlib/primordials.ts @@ -110,6 +110,7 @@ export interface Primordials { now(): number; next(func: () => void): void; schedule(func: () => void, delay: number): () => void; + setIntrinsic(key: string, val: any): void; } // prevent optimization to "undefined", which doesn't exist yet @@ -132,6 +133,7 @@ export const { now, next, schedule, + setIntrinsic, } = primordials; export type regex = InstanceType; \ No newline at end of file diff --git a/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java b/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java index 7807517..15e1108 100644 --- a/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java +++ b/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java @@ -775,17 +775,13 @@ public class SimpleRepl { setProto(args.env, env, Value.RANGE_ERR_PROTO, obj, "range"); setProto(args.env, env, Value.UINT8_ARR_PROTO, obj, "uint8"); setProto(args.env, env, Value.INT32_ARR_PROTO, obj, "int32"); - var val = obj.getMember(args.env, "regex"); - if (val instanceof FunctionValue func) { - env.add(Value.REGEX_CONSTR, func); - } return Value.UNDEFINED; })); res.defineOwnField(env, "setIntrinsic", new NativeFunction(args -> { var name = args.get(0).toString(env); var val = args.get(1); - Value.intrinsics(environment).put(name, val); + Value.intrinsics(env).put(name, val); return Value.UNDEFINED; })); 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 d9f6621..3fdf0d1 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java @@ -219,20 +219,6 @@ public class InstructionRunner { frame.codePtr++; return null; } - private static Value execLoadRegEx(Environment env, Instruction instr, Frame frame) { - if (env.hasNotNull(Value.REGEX_CONSTR)) { - frame.push(env.get(Value.REGEX_CONSTR).constructNoSelf(env, - StringValue.of(instr.get(0)), - StringValue.of(instr.get(1)) - )); - } - else { - throw EngineException.ofSyntax("Regex is not supported"); - } - - frame.codePtr++; - return null; - } private static Value execDiscard(Environment env, Instruction instr, Frame frame) { frame.pop(); @@ -529,7 +515,6 @@ public class InstructionRunner { case LOAD_MEMBER: return execLoadMember(env, instr, frame); case LOAD_MEMBER_INT: return execLoadMemberInt(env, instr, frame); case LOAD_MEMBER_STR: return execLoadMemberStr(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_ERROR: return execLoadError(env, instr, frame); diff --git a/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/Value.java b/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/Value.java index ed5da41..cb653cc 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/Value.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/Value.java @@ -48,8 +48,6 @@ public abstract class Value { } } - public static final Key REGEX_CONSTR = new Key<>(); - public static final Key MAX_STACK_COUNT = new Key<>(); public static final Key HIDE_STACK = new Key<>();