From 1e982cd2effb4290ffd53d8e7eee6a595d71cbec Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 24 Jan 2025 00:22:15 +0200 Subject: [PATCH] some minor restructuring --- lib/src/stdlib/primordials.ts | 2 +- lib/src/stdlib/values/function.ts | 4 ++++ .../java/me/topchetoeu/j2s/runtime/Frame.java | 17 ++++++++++------- .../j2s/runtime/InstructionRunner.java | 1 + .../me/topchetoeu/j2s/runtime/values/Value.java | 3 --- .../j2s/runtime/values/objects/ObjectValue.java | 3 +-- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/src/stdlib/primordials.ts b/lib/src/stdlib/primordials.ts index 455feb9..29f223c 100644 --- a/lib/src/stdlib/primordials.ts +++ b/lib/src/stdlib/primordials.ts @@ -72,7 +72,7 @@ export interface BufferPrimordials { export interface FunctionPrimordials { invokeType(args: IArguments, self: any): "new" | "call"; invokeTypeInfer(): "new" | "call"; - target(): Function | null | undefined; + target(level?: number): Function | null | undefined; setConstructable(func: Function, flag: boolean): void; setCallable(func: Function, flag: boolean): void; invoke(func: Function, self: any, args: any[]): any; diff --git a/lib/src/stdlib/values/function.ts b/lib/src/stdlib/values/function.ts index cf7e630..f8af144 100644 --- a/lib/src/stdlib/values/function.ts +++ b/lib/src/stdlib/values/function.ts @@ -58,6 +58,10 @@ export const Function = (() => { public static compile(src: string, filename?: string) { return compile(String(src), filename); } + + public static newTarget() { + return func.target(1); + } } func.setCallable(Function, true); diff --git a/runtime/src/main/java/me/topchetoeu/j2s/runtime/Frame.java b/runtime/src/main/java/me/topchetoeu/j2s/runtime/Frame.java index b84cea4..26d442a 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/Frame.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/Frame.java @@ -7,7 +7,7 @@ import java.util.concurrent.CancellationException; import me.topchetoeu.j2s.common.Environment; import me.topchetoeu.j2s.common.Instruction; import me.topchetoeu.j2s.common.Key; -import me.topchetoeu.j2s.runtime.debug.DebugContext; +import me.topchetoeu.j2s.runtime.debug.DebugHandler; import me.topchetoeu.j2s.runtime.exceptions.EngineException; import me.topchetoeu.j2s.runtime.values.Value; import me.topchetoeu.j2s.runtime.values.functions.CodeFunction; @@ -17,6 +17,9 @@ import me.topchetoeu.j2s.runtime.values.primitives.numbers.IntValue; public final class Frame { public static final Key> KEY = new Key<>(); + public static final Key MAX_STACK_COUNT = new Key<>(); + public static final Key HIDE_STACK = new Key<>(); + public static final EngineException STACK_OVERFLOW; static { STACK_OVERFLOW = EngineException.ofRange("Stack overflow!"); @@ -120,7 +123,7 @@ public final class Frame { public final Stack tryStack = new Stack<>(); public final CodeFunction function; public final Environment env; - private final DebugContext dbg; + private final DebugHandler dbg; public Value getVar(int i) { if (i < 0) return captures[~i][0]; @@ -206,7 +209,7 @@ public final class Frame { returnValue = InstructionRunner.exec(env, instr, this); } catch (EngineException e) { - error = e.add(env, function.name, dbg.getMapOrEmpty(function).toLocation(codePtr, true)); + error = e.add(env, function.name, dbg.getMapOrEmpty(env, function).toLocation(codePtr)); } } } @@ -214,7 +217,7 @@ public final class Frame { catch (EngineException e) { error = e; } catch (RuntimeException e) { if (!(e instanceof CancellationException)) { - System.out.println(dbg.getMapOrEmpty(function).toLocation(codePtr, true)); + System.out.println(dbg.getMapOrEmpty(env, function).toLocation(codePtr)); } throw e; } @@ -352,10 +355,10 @@ public final class Frame { public void onPush() { get(env).push(this); - DebugContext.get(env).onFramePush(env, this); + DebugHandler.get(env).onFramePush(env, this); } public void onPop() { - DebugContext.get(env).onFramePop(env, this); + DebugHandler.get(env).onFramePop(env, this); get(env).pop(); } @@ -376,7 +379,7 @@ public final class Frame { public Frame(Environment env, boolean isNew, Value target, Value self, Value[] args, CodeFunction func) { this.env = env; - this.dbg = DebugContext.get(env); + this.dbg = DebugHandler.get(env); this.function = func; this.isNew = isNew; this.target = target; 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 e7e9388..e71cb41 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/InstructionRunner.java @@ -14,6 +14,7 @@ import me.topchetoeu.j2s.runtime.values.primitives.numbers.NumberValue; public class InstructionRunner { private static Value execReturn(Environment env, Instruction instr, Frame frame) { + frame.codePtr++; return frame.pop(); } private static Value execThrow(Environment env, Instruction instr, Frame 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 cb653cc..13adc3e 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,9 +48,6 @@ public abstract class Value { } } - public static final Key MAX_STACK_COUNT = new Key<>(); - public static final Key HIDE_STACK = new Key<>(); - public static final Key BOOL_PROTO = new Key<>(); public static final Key NUMBER_PROTO = new Key<>(); public static final Key STRING_PROTO = new Key<>(); diff --git a/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/objects/ObjectValue.java b/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/objects/ObjectValue.java index 47064f2..11fe7d8 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/objects/ObjectValue.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/objects/ObjectValue.java @@ -44,6 +44,7 @@ public class ObjectValue extends Value { private HashMap symbolFields = new HashMap<>(); private HashMap properties = new HashMap<>(); private HashMap symbolProperties = new HashMap<>(); + private State state = State.NORMAL; private LinkedHashMap keys = new LinkedHashMap<>(); private LinkedHashMap symbols = new LinkedHashMap<>(); @@ -72,8 +73,6 @@ public class ObjectValue extends Value { @Override public NumberValue toNumber(Environment env) { return toPrimitive(env).toNumber(env); } @Override public StringValue type() { return StringValue.of("object"); } - private State state = State.NORMAL; - @Override public State getState() { return state; } public final void preventExtensions() {