From 1ce5fc9d993fe96e3b618e5f290673e54d5e2c7b Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 9 Sep 2023 18:09:20 +0300 Subject: [PATCH] refactor: move parse to Contetx --- src/me/topchetoeu/jscript/Main.java | 2 +- src/me/topchetoeu/jscript/engine/Context.java | 9 +++++++++ src/me/topchetoeu/jscript/engine/Engine.java | 17 ++--------------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index 9de34f3..09657b8 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -96,7 +96,7 @@ public class Main { }); env.global.define("go", ctx -> { try { - var func = engine.compile(ctx, "do.js", new String(Files.readAllBytes(Path.of("do.js")))); + var func = ctx.compile("do.js", new String(Files.readAllBytes(Path.of("do.js")))); return func.call(ctx); } catch (IOException e) { diff --git a/src/me/topchetoeu/jscript/engine/Context.java b/src/me/topchetoeu/jscript/engine/Context.java index adcc37a..417b115 100644 --- a/src/me/topchetoeu/jscript/engine/Context.java +++ b/src/me/topchetoeu/jscript/engine/Context.java @@ -1,9 +1,18 @@ package me.topchetoeu.jscript.engine; +import me.topchetoeu.jscript.engine.values.FunctionValue; +import me.topchetoeu.jscript.engine.values.Values; +import me.topchetoeu.jscript.parsing.Parsing; + public class Context { public final FunctionContext function; public final MessageContext message; + public FunctionValue compile(String filename, String raw) throws InterruptedException { + var res = Values.toString(this, function.compile.call(this, null, raw, filename)); + return Parsing.compile(function, filename, res); + } + public Context(FunctionContext funcCtx, MessageContext msgCtx) { this.function = funcCtx; this.message = msgCtx; diff --git a/src/me/topchetoeu/jscript/engine/Engine.java b/src/me/topchetoeu/jscript/engine/Engine.java index e98e000..3e0b388 100644 --- a/src/me/topchetoeu/jscript/engine/Engine.java +++ b/src/me/topchetoeu/jscript/engine/Engine.java @@ -1,14 +1,11 @@ package me.topchetoeu.jscript.engine; -import java.util.Map; import java.util.concurrent.LinkedBlockingDeque; import me.topchetoeu.jscript.engine.values.FunctionValue; -import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.events.Awaitable; import me.topchetoeu.jscript.events.DataNotifier; import me.topchetoeu.jscript.exceptions.EngineException; -import me.topchetoeu.jscript.parsing.Parsing; public class Engine { private class UncompiledFunction extends FunctionValue { @@ -19,7 +16,7 @@ public class Engine { @Override public Object call(Context ctx, Object thisArg, Object ...args) throws InterruptedException { ctx = new Context(this.ctx, ctx.message); - return compile(ctx, filename, raw).call(ctx, thisArg, args); + return ctx.compile(filename, raw).call(ctx, thisArg, args); } public UncompiledFunction(FunctionContext ctx, String filename, String raw) { @@ -47,20 +44,15 @@ public class Engine { private static int nextId = 0; - // private Map, Object> callCtxVals = new HashMap<>(); - // private NativeTypeRegister typeRegister; private Thread thread; - private LinkedBlockingDeque macroTasks = new LinkedBlockingDeque<>(); private LinkedBlockingDeque microTasks = new LinkedBlockingDeque<>(); public final int id = ++nextId; - // public NativeTypeRegister typeRegister() { return typeRegister; } - private void runTask(Task task) throws InterruptedException { try { - task.notifier.next(task.func.call(new Context(null, new MessageContext(this)), task.thisArg, task.args)); + task.notifier.next(task.func.call(new Context(null, task.ctx), task.thisArg, task.args)); } catch (InterruptedException e) { task.notifier.error(new RuntimeException(e)); @@ -120,11 +112,6 @@ public class Engine { return pushMsg(micro, ctx.message, new UncompiledFunction(ctx.function, filename, raw), thisArg, args); } - public FunctionValue compile(Context ctx, String filename, String raw) throws InterruptedException { - var res = Values.toString(ctx, ctx.function.compile.call(ctx, null, raw, filename)); - return Parsing.compile(ctx.function, filename, res); - } - // public Engine() { // this.typeRegister = new NativeTypeRegister(); // }