refactor: move parse to Contetx

This commit is contained in:
TopchetoEU 2023-09-09 18:09:20 +03:00
parent 0e04459fe7
commit 1ce5fc9d99
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 12 additions and 16 deletions

View File

@ -96,7 +96,7 @@ public class Main {
}); });
env.global.define("go", ctx -> { env.global.define("go", ctx -> {
try { 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); return func.call(ctx);
} }
catch (IOException e) { catch (IOException e) {

View File

@ -1,9 +1,18 @@
package me.topchetoeu.jscript.engine; 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 class Context {
public final FunctionContext function; public final FunctionContext function;
public final MessageContext message; 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) { public Context(FunctionContext funcCtx, MessageContext msgCtx) {
this.function = funcCtx; this.function = funcCtx;
this.message = msgCtx; this.message = msgCtx;

View File

@ -1,14 +1,11 @@
package me.topchetoeu.jscript.engine; package me.topchetoeu.jscript.engine;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.LinkedBlockingDeque;
import me.topchetoeu.jscript.engine.values.FunctionValue; 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.Awaitable;
import me.topchetoeu.jscript.events.DataNotifier; import me.topchetoeu.jscript.events.DataNotifier;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.parsing.Parsing;
public class Engine { public class Engine {
private class UncompiledFunction extends FunctionValue { private class UncompiledFunction extends FunctionValue {
@ -19,7 +16,7 @@ public class Engine {
@Override @Override
public Object call(Context ctx, Object thisArg, Object ...args) throws InterruptedException { public Object call(Context ctx, Object thisArg, Object ...args) throws InterruptedException {
ctx = new Context(this.ctx, ctx.message); 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) { public UncompiledFunction(FunctionContext ctx, String filename, String raw) {
@ -47,20 +44,15 @@ public class Engine {
private static int nextId = 0; private static int nextId = 0;
// private Map<DataKey<?>, Object> callCtxVals = new HashMap<>();
// private NativeTypeRegister typeRegister;
private Thread thread; private Thread thread;
private LinkedBlockingDeque<Task> macroTasks = new LinkedBlockingDeque<>(); private LinkedBlockingDeque<Task> macroTasks = new LinkedBlockingDeque<>();
private LinkedBlockingDeque<Task> microTasks = new LinkedBlockingDeque<>(); private LinkedBlockingDeque<Task> microTasks = new LinkedBlockingDeque<>();
public final int id = ++nextId; public final int id = ++nextId;
// public NativeTypeRegister typeRegister() { return typeRegister; }
private void runTask(Task task) throws InterruptedException { private void runTask(Task task) throws InterruptedException {
try { 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) { catch (InterruptedException e) {
task.notifier.error(new RuntimeException(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); 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() { // public Engine() {
// this.typeRegister = new NativeTypeRegister(); // this.typeRegister = new NativeTypeRegister();
// } // }