refactor: improve Engine API

This commit is contained in:
TopchetoEU 2023-12-24 14:26:42 +02:00
parent 93973c12b1
commit 078d7ed95f
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
7 changed files with 402 additions and 403 deletions

View File

@ -57,7 +57,7 @@ public class Main {
var file = Path.of(arg); var file = Path.of(arg);
var raw = Files.readString(file); var raw = Files.readString(file);
var res = engine.pushMsg( var res = engine.pushMsg(
false, new Context(engine, environment), false, environment,
Filename.fromFile(file.toFile()), Filename.fromFile(file.toFile()),
raw, null raw, null
).await(); ).await();
@ -73,7 +73,7 @@ public class Main {
if (raw == null) break; if (raw == null) break;
var res = engine.pushMsg( var res = engine.pushMsg(
false, new Context(engine, environment), false, environment,
new Filename("jscript", "repl/" + i + ".js"), new Filename("jscript", "repl/" + i + ".js"),
raw, null raw, null
).await(); ).await();
@ -135,18 +135,16 @@ public class Main {
bsEnv.stackVisible = false; bsEnv.stackVisible = false;
engine.pushMsg( engine.pushMsg(
false, new Context(engine, tsEnv), false, tsEnv,
new Filename("jscript", "ts.js"), new Filename("jscript", "ts.js"),
Reading.resourceToString("js/ts.js"), null Reading.resourceToString("js/ts.js"), null
).await(); ).await();
System.out.println("Loaded typescript!"); System.out.println("Loaded typescript!");
var ctx = new Context(engine, bsEnv);
engine.pushMsg( engine.pushMsg(
false, ctx, false, bsEnv,
new Filename("jscript", "bootstrap.js"), Reading.resourceToString("js/bootstrap.js"), null, new Filename("jscript", "bootstrap.js"), Reading.resourceToString("js/bootstrap.js"), null,
tsEnv.global.get(ctx, "ts"), environment, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts")) tsEnv.global.get(new Context(engine, bsEnv), "ts"), environment, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts"))
).await(); ).await();
} }
catch (EngineException e) { catch (EngineException e) {

View File

@ -119,7 +119,6 @@ public class Context {
} }
public Context(Engine engine, Environment env) { public Context(Engine engine, Environment env) {
this(engine); this(engine);
this.pushEnv(env); if (env != null) this.pushEnv(env);
} }
} }

View File

@ -141,13 +141,13 @@ public class Engine implements DebugController {
return this.thread != null; return this.thread != null;
} }
public Awaitable<Object> pushMsg(boolean micro, Context ctx, FunctionValue func, Object thisArg, Object ...args) { public Awaitable<Object> pushMsg(boolean micro, Environment env, FunctionValue func, Object thisArg, Object ...args) {
var msg = new Task(ctx == null ? new Context(this) : ctx, func, thisArg, args, micro); var msg = new Task(new Context(this, env), func, thisArg, args, micro);
tasks.add(msg); tasks.add(msg);
return msg.notifier; return msg.notifier;
} }
public Awaitable<Object> pushMsg(boolean micro, Context ctx, Filename filename, String raw, Object thisArg, Object ...args) { public Awaitable<Object> pushMsg(boolean micro, Environment env, Filename filename, String raw, Object thisArg, Object ...args) {
return pushMsg(micro, ctx, new UncompiledFunction(filename, raw), thisArg, args); return pushMsg(micro, env, new UncompiledFunction(filename, raw), thisArg, args);
} }
@Override @Override

View File

@ -53,7 +53,6 @@ public class Environment implements PermissionsProvider {
res.defineProperty(ctx, "function", target.func(env)); res.defineProperty(ctx, "function", target.func(env));
res.defineProperty(ctx, "mapChain", new ArrayValue()); res.defineProperty(ctx, "mapChain", new ArrayValue());
if (isDebug) { if (isDebug) {
res.defineProperty(ctx, "breakpoints", ArrayValue.of(ctx, target.breakpoints.stream().map(Location::toString).collect(Collectors.toList()))); res.defineProperty(ctx, "breakpoints", ArrayValue.of(ctx, target.breakpoints.stream().map(Location::toString).collect(Collectors.toList())));
} }
@ -128,4 +127,7 @@ public class Environment implements PermissionsProvider {
this.wrappers = nativeConverter; this.wrappers = nativeConverter;
this.global = global; this.global = global;
} }
public Environment() {
this(null, null, null);
}
} }

View File

@ -483,7 +483,7 @@ public class SimpleDebugger implements Debugger {
env.global = new GlobalScope(codeFrame.local); env.global = new GlobalScope(codeFrame.local);
var ctx = new Context(engine).pushEnv(env); var ctx = new Context(engine).pushEnv(env);
var awaiter = engine.pushMsg(false, ctx, new Filename("jscript", "eval"), code, codeFrame.frame.thisArg, codeFrame.frame.args); var awaiter = engine.pushMsg(false, ctx.environment(), new Filename("jscript", "eval"), code, codeFrame.frame.thisArg, codeFrame.frame.args);
engine.run(true); engine.run(true);

View File

@ -51,7 +51,7 @@ public class Internals {
} }
catch (InterruptedException e) { return; } catch (InterruptedException e) { return; }
ctx.engine.pushMsg(false, ctx, func, null, args); ctx.engine.pushMsg(false, ctx.environment(), func, null, args);
}); });
thread.start(); thread.start();
@ -71,7 +71,7 @@ public class Internals {
} }
catch (InterruptedException e) { return; } catch (InterruptedException e) { return; }
ctx.engine.pushMsg(false, ctx, func, null, args); ctx.engine.pushMsg(false, ctx.environment(), func, null, args);
} }
}); });
thread.start(); thread.start();

View File

@ -253,7 +253,7 @@ import me.topchetoeu.jscript.interop.Native;
this.val = val; this.val = val;
this.state = STATE_FULFILLED; this.state = STATE_FULFILLED;
ctx.engine.pushMsg(true, ctx, new NativeFunction((_ctx, _thisArg, _args) -> { ctx.engine.pushMsg(true, ctx.environment(), new NativeFunction((_ctx, _thisArg, _args) -> {
for (var handle : handles) { for (var handle : handles) {
handle.fulfilled.call(handle.ctx, null, val); handle.fulfilled.call(handle.ctx, null, val);
} }
@ -288,7 +288,7 @@ import me.topchetoeu.jscript.interop.Native;
this.val = val; this.val = val;
this.state = STATE_REJECTED; this.state = STATE_REJECTED;
ctx.engine.pushMsg(true, ctx, new NativeFunction((_ctx, _thisArg, _args) -> { ctx.engine.pushMsg(true, ctx.environment(), new NativeFunction((_ctx, _thisArg, _args) -> {
for (var handle : handles) handle.rejected.call(handle.ctx, null, val); for (var handle : handles) handle.rejected.call(handle.ctx, null, val);
if (!handled) { if (!handled) {
Values.printError(new EngineException(val).setCtx(ctx.environment(), ctx.engine), "(in promise)"); Values.printError(new EngineException(val).setCtx(ctx.environment(), ctx.engine), "(in promise)");
@ -305,9 +305,9 @@ import me.topchetoeu.jscript.interop.Native;
} }
private void handle(Context ctx, FunctionValue fulfill, FunctionValue reject) { private void handle(Context ctx, FunctionValue fulfill, FunctionValue reject) {
if (state == STATE_FULFILLED) ctx.engine.pushMsg(true, ctx, fulfill, null, val); if (state == STATE_FULFILLED) ctx.engine.pushMsg(true, ctx.environment(), fulfill, null, val);
else if (state == STATE_REJECTED) { else if (state == STATE_REJECTED) {
ctx.engine.pushMsg(true, ctx, reject, null, val); ctx.engine.pushMsg(true, ctx.environment(), reject, null, val);
handled = true; handled = true;
} }
else handles.add(new Handle(ctx, fulfill, reject)); else handles.add(new Handle(ctx, fulfill, reject));