From b30f94de8f00240e881248c457cbbd85cdbd8c5c Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:53:17 +0200 Subject: [PATCH] refactor: move function pushMsg signatures in EventLoop --- gradle.properties | 2 +- .../me/topchetoeu/jscript/runtime/Engine.java | 14 -------------- .../me/topchetoeu/jscript/runtime/EventLoop.java | 14 ++++++++++++++ .../topchetoeu/jscript/runtime/Extensions.java | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/gradle.properties b/gradle.properties index ff3f81e..42cef0b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ project_group = me.topchetoeu project_name = jscript -project_version = 0.9.7-beta +project_version = 0.9.8-beta main_class = me.topchetoeu.jscript.utils.JScriptRepl diff --git a/src/java/me/topchetoeu/jscript/runtime/Engine.java b/src/java/me/topchetoeu/jscript/runtime/Engine.java index 2a13a58..9e2d6d1 100644 --- a/src/java/me/topchetoeu/jscript/runtime/Engine.java +++ b/src/java/me/topchetoeu/jscript/runtime/Engine.java @@ -2,11 +2,9 @@ package me.topchetoeu.jscript.runtime; import java.util.concurrent.PriorityBlockingQueue; -import me.topchetoeu.jscript.common.Filename; import me.topchetoeu.jscript.common.ResultRunnable; import me.topchetoeu.jscript.common.events.DataNotifier; import me.topchetoeu.jscript.runtime.exceptions.InterruptException; -import me.topchetoeu.jscript.runtime.values.FunctionValue; public class Engine implements EventLoop { private static class Task implements Comparable> { @@ -78,18 +76,6 @@ public class Engine implements EventLoop { return this.thread != null; } - public DataNotifier pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) { - return pushMsg(() -> { - return func.call(new Context(env), thisArg, args); - }, micro); - } - public DataNotifier pushMsg(boolean micro, Environment env, Filename filename, String raw, Object thisArg, Object ...args) { - return pushMsg(() -> { - var ctx = new Context(env); - return ctx.compile(filename, raw).call(new Context(env), thisArg, args); - }, micro); - } - public Engine() { } } diff --git a/src/java/me/topchetoeu/jscript/runtime/EventLoop.java b/src/java/me/topchetoeu/jscript/runtime/EventLoop.java index f3f013d..68920d2 100644 --- a/src/java/me/topchetoeu/jscript/runtime/EventLoop.java +++ b/src/java/me/topchetoeu/jscript/runtime/EventLoop.java @@ -1,8 +1,10 @@ package me.topchetoeu.jscript.runtime; +import me.topchetoeu.jscript.common.Filename; import me.topchetoeu.jscript.common.ResultRunnable; import me.topchetoeu.jscript.common.events.DataNotifier; import me.topchetoeu.jscript.runtime.exceptions.EngineException; +import me.topchetoeu.jscript.runtime.values.FunctionValue; public interface EventLoop { public static final Key KEY = new Key<>(); @@ -20,4 +22,16 @@ public interface EventLoop { public default DataNotifier pushMsg(Runnable runnable, boolean micro) { return pushMsg(() -> { runnable.run(); return null; }, micro); } + + public default DataNotifier pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) { + return pushMsg(() -> { + return func.call(new Context(env), thisArg, args); + }, micro); + } + public default DataNotifier pushMsg(boolean micro, Environment env, Filename filename, String raw, Object thisArg, Object ...args) { + return pushMsg(() -> { + var ctx = new Context(env); + return ctx.compile(filename, raw).call(new Context(env), thisArg, args); + }, micro); + } } diff --git a/src/java/me/topchetoeu/jscript/runtime/Extensions.java b/src/java/me/topchetoeu/jscript/runtime/Extensions.java index c89dd71..d824790 100644 --- a/src/java/me/topchetoeu/jscript/runtime/Extensions.java +++ b/src/java/me/topchetoeu/jscript/runtime/Extensions.java @@ -1,6 +1,17 @@ package me.topchetoeu.jscript.runtime; +import java.util.List; + public interface Extensions { + public static Extensions EMPTY = new Extensions() { + @Override public void add(Key key, T obj) { } + @Override public boolean remove(Key key) { return false; } + + @Override public T get(Key key) { return null; } + @Override public boolean has(Key key) { return false; } + @Override public Iterable> keys() { return List.of(); } + }; + T get(Key key); void add(Key key, T obj); Iterable> keys(); @@ -35,4 +46,9 @@ public interface Extensions { add((Key)key, (Object)source.get(key)); } } + + public static Extensions wrap(Extensions ext) { + if (ext == null) return EMPTY; + else return ext; + } }