refactor: move function pushMsg signatures in EventLoop

This commit is contained in:
TopchetoEU 2024-03-08 16:53:17 +02:00
parent 47b4dd3c15
commit b30f94de8f
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
4 changed files with 31 additions and 15 deletions

View File

@ -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

View File

@ -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<T> implements Comparable<Task<?>> {
@ -78,18 +76,6 @@ public class Engine implements EventLoop {
return this.thread != null;
}
public DataNotifier<Object> pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) {
return pushMsg(() -> {
return func.call(new Context(env), thisArg, args);
}, micro);
}
public DataNotifier<Object> 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() {
}
}

View File

@ -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<EventLoop> KEY = new Key<>();
@ -20,4 +22,16 @@ public interface EventLoop {
public default DataNotifier<Void> pushMsg(Runnable runnable, boolean micro) {
return pushMsg(() -> { runnable.run(); return null; }, micro);
}
public default DataNotifier<Object> 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<Object> 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);
}
}

View File

@ -1,6 +1,17 @@
package me.topchetoeu.jscript.runtime;
import java.util.List;
public interface Extensions {
public static Extensions EMPTY = new Extensions() {
@Override public <T> void add(Key<T> key, T obj) { }
@Override public boolean remove(Key<?> key) { return false; }
@Override public <T> T get(Key<T> key) { return null; }
@Override public boolean has(Key<?> key) { return false; }
@Override public Iterable<Key<?>> keys() { return List.of(); }
};
<T> T get(Key<T> key);
<T> void add(Key<T> key, T obj);
Iterable<Key<?>> keys();
@ -35,4 +46,9 @@ public interface Extensions {
add((Key<Object>)key, (Object)source.get(key));
}
}
public static Extensions wrap(Extensions ext) {
if (ext == null) return EMPTY;
else return ext;
}
}