fix: remove unneeded LOAD_REGEX instruction

This commit is contained in:
TopchetoEU 2025-01-22 01:54:03 +02:00
parent 01e86b5e70
commit 582753440b
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
7 changed files with 9 additions and 29 deletions

View File

@ -31,7 +31,6 @@ public class Instruction {
LOAD_FUNC(0x30), LOAD_FUNC(0x30),
LOAD_ARR(0x31), LOAD_ARR(0x31),
LOAD_OBJ(0x32), LOAD_OBJ(0x32),
LOAD_REGEX(0x33),
LOAD_GLOB(0x38), LOAD_GLOB(0x38),
LOAD_INTRINSICS(0x39), LOAD_INTRINSICS(0x39),
@ -287,9 +286,6 @@ public class Instruction {
return new Instruction(Type.LOAD_MEMBER_STR, member); 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 // TODO: make this capturing a concern of the compiler
public static Instruction loadFunc(int id, String name, int[] captures) { public static Instruction loadFunc(int id, String name, int[] captures) {
var args = new Object[2 + captures.length]; var args = new Object[2 + captures.length];

View File

@ -15,7 +15,10 @@ public class RegexNode extends Node {
} }
@Override public void compile(CompileResult target, boolean pollute) { @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()); if (!pollute) target.add(Instruction.discard());
} }

View File

@ -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 { Error, RangeError, SyntaxError, TypeError } from "./values/errors.ts";
import { Boolean } from "./values/boolean.ts"; import { Boolean } from "./values/boolean.ts";
import { Function } from "./values/function.ts"; import { Function } from "./values/function.ts";
@ -87,5 +87,5 @@ setGlobalPrototypes({
type: TypeError.prototype, type: TypeError.prototype,
uint8: Uint8Array.prototype, uint8: Uint8Array.prototype,
int32: Int32Array.prototype, int32: Int32Array.prototype,
regex: RegExp,
}); });
setIntrinsic("regex", RegExp);

View File

@ -110,6 +110,7 @@ export interface Primordials {
now(): number; now(): number;
next(func: () => void): void; next(func: () => void): void;
schedule(func: () => void, delay: number): () => void; schedule(func: () => void, delay: number): () => void;
setIntrinsic(key: string, val: any): void;
} }
// prevent optimization to "undefined", which doesn't exist yet // prevent optimization to "undefined", which doesn't exist yet
@ -132,6 +133,7 @@ export const {
now, now,
next, next,
schedule, schedule,
setIntrinsic,
} = primordials; } = primordials;
export type regex = InstanceType<typeof regex>; export type regex = InstanceType<typeof regex>;

View File

@ -775,17 +775,13 @@ public class SimpleRepl {
setProto(args.env, env, Value.RANGE_ERR_PROTO, obj, "range"); 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.UINT8_ARR_PROTO, obj, "uint8");
setProto(args.env, env, Value.INT32_ARR_PROTO, obj, "int32"); 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; return Value.UNDEFINED;
})); }));
res.defineOwnField(env, "setIntrinsic", new NativeFunction(args -> { res.defineOwnField(env, "setIntrinsic", new NativeFunction(args -> {
var name = args.get(0).toString(env); var name = args.get(0).toString(env);
var val = args.get(1); var val = args.get(1);
Value.intrinsics(environment).put(name, val); Value.intrinsics(env).put(name, val);
return Value.UNDEFINED; return Value.UNDEFINED;
})); }));

View File

@ -219,20 +219,6 @@ public class InstructionRunner {
frame.codePtr++; frame.codePtr++;
return null; 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) { private static Value execDiscard(Environment env, Instruction instr, Frame frame) {
frame.pop(); frame.pop();
@ -529,7 +515,6 @@ public class InstructionRunner {
case LOAD_MEMBER: return execLoadMember(env, instr, frame); case LOAD_MEMBER: return execLoadMember(env, instr, frame);
case LOAD_MEMBER_INT: return execLoadMemberInt(env, instr, frame); case LOAD_MEMBER_INT: return execLoadMemberInt(env, instr, frame);
case LOAD_MEMBER_STR: return execLoadMemberStr(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_GLOB: return execLoadGlob(env, instr, frame);
case LOAD_INTRINSICS: return execLoadIntrinsics(env, instr, frame); case LOAD_INTRINSICS: return execLoadIntrinsics(env, instr, frame);
case LOAD_ERROR: return execLoadError(env, instr, frame); case LOAD_ERROR: return execLoadError(env, instr, frame);

View File

@ -48,8 +48,6 @@ public abstract class Value {
} }
} }
public static final Key<FunctionValue> REGEX_CONSTR = new Key<>();
public static final Key<Integer> MAX_STACK_COUNT = new Key<>(); public static final Key<Integer> MAX_STACK_COUNT = new Key<>();
public static final Key<Boolean> HIDE_STACK = new Key<>(); public static final Key<Boolean> HIDE_STACK = new Key<>();