fix: pass environment to compiler via simple environment wrapper

This commit is contained in:
TopchetoEU 2023-12-27 13:58:58 +02:00
parent 579f09c837
commit aaf9a6fa45
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 45 additions and 11 deletions

View File

@ -23,6 +23,7 @@ import me.topchetoeu.jscript.filesystem.MemoryFilesystem;
import me.topchetoeu.jscript.filesystem.Mode; import me.topchetoeu.jscript.filesystem.Mode;
import me.topchetoeu.jscript.filesystem.PhysicalFilesystem; import me.topchetoeu.jscript.filesystem.PhysicalFilesystem;
import me.topchetoeu.jscript.filesystem.RootFilesystem; import me.topchetoeu.jscript.filesystem.RootFilesystem;
import me.topchetoeu.jscript.lib.EnvironmentLib;
import me.topchetoeu.jscript.lib.Internals; import me.topchetoeu.jscript.lib.Internals;
import me.topchetoeu.jscript.modules.ModuleRepo; import me.topchetoeu.jscript.modules.ModuleRepo;
import me.topchetoeu.jscript.permissions.PermissionsManager; import me.topchetoeu.jscript.permissions.PermissionsManager;
@ -151,10 +152,13 @@ public class Main {
).await(); ).await();
System.out.println("Loaded typescript!"); System.out.println("Loaded typescript!");
var typescript = tsEnv.global.get(new Context(engine, bsEnv), "ts");
var libs = new ArrayValue(null, Reading.resourceToString("js/lib.d.ts"));
engine.pushMsg( engine.pushMsg(
false, bsEnv, 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(new Context(engine, bsEnv), "ts"), environment, new ArrayValue(null, Reading.resourceToString("js/lib.d.ts")) typescript, new EnvironmentLib(environment), libs
).await(); ).await();
} }
catch (EngineException e) { catch (EngineException e) {

View File

@ -16,6 +16,7 @@ import me.topchetoeu.jscript.engine.values.ArrayValue;
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.engine.values.Values;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.lib.EnvironmentLib;
import me.topchetoeu.jscript.mapping.SourceMap; import me.topchetoeu.jscript.mapping.SourceMap;
public class Context extends ExtensionStack { public class Context extends ExtensionStack {
@ -42,7 +43,7 @@ public class Context extends ExtensionStack {
public FunctionValue compile(Filename filename, String raw) { public FunctionValue compile(Filename filename, String raw) {
var env = environment(); var env = environment();
var result = Environment.compileFunc(this).call(this, null, raw, filename.toString(), env); var result = Environment.compileFunc(this).call(this, null, raw, filename.toString(), new EnvironmentLib(env));
var function = (FunctionValue)Values.getMember(this, result, "function"); var function = (FunctionValue)Values.getMember(this, result, "function");
if (!has(DebugContext.ENV_KEY)) return function; if (!has(DebugContext.ENV_KEY)) return function;

View File

@ -13,14 +13,12 @@ import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Symbol; import me.topchetoeu.jscript.engine.values.Symbol;
import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeWrapperProvider; import me.topchetoeu.jscript.interop.NativeWrapperProvider;
import me.topchetoeu.jscript.parsing.Parsing; import me.topchetoeu.jscript.parsing.Parsing;
// TODO: Remove hardcoded extensions form environment // TODO: Remove hardcoded extensions form environment
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public class Environment implements Extensions { public class Environment implements Extensions {
private static int nextId = 0;
public static final HashMap<String, Symbol> symbols = new HashMap<>(); public static final HashMap<String, Symbol> symbols = new HashMap<>();
@ -48,8 +46,6 @@ public class Environment implements Extensions {
public GlobalScope global; public GlobalScope global;
public WrappersProvider wrappers; public WrappersProvider wrappers;
@Native public int id = ++nextId;
@Override public <T> void add(Symbol key, T obj) { @Override public <T> void add(Symbol key, T obj) {
data.put(key, obj); data.put(key, obj);
} }
@ -73,7 +69,7 @@ public class Environment implements Extensions {
var filename = Values.toString(ctx, args[1]); var filename = Values.toString(ctx, args[1]);
var isDebug = Values.toBoolean(args[2]); var isDebug = Values.toBoolean(args[2]);
var env = Values.wrapper(args[2], Environment.class); var env = Values.wrapper(Values.getMember(ctx, args[2], Symbol.get("env")), Environment.class);
var res = new ObjectValue(); var res = new ObjectValue();
var target = Parsing.compile(env, Filename.parse(filename), source); var target = Parsing.compile(env, Filename.parse(filename), source);

View File

@ -43,7 +43,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (get.thisArg() && !member || !get.thisArg() && !memberMatch) continue; if (get.thisArg() && !member || !get.thisArg() && !memberMatch) continue;
Object name = get.value(); Object name = get.value();
if (((String)name).startsWith("@@")) name = Symbol.get(((String)name).substring(2)); if (name.toString().startsWith("@@")) name = Symbol.get(name.toString().substring(2));
else if (name.equals("")) name = method.getName(); else if (name.equals("")) name = method.getName();
var prop = target.properties.get(name); var prop = target.properties.get(name);
@ -60,7 +60,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (set.thisArg() && !member || !set.thisArg() && !memberMatch) continue; if (set.thisArg() && !member || !set.thisArg() && !memberMatch) continue;
Object name = set.value(); Object name = set.value();
if (((String)name).startsWith("@@")) name = Symbol.get(((String)name).substring(2)); if (name.toString().startsWith("@@")) name = Symbol.get(name.toString().substring(2));
else if (name.equals("")) name = method.getName(); else if (name.equals("")) name = method.getName();
var prop = target.properties.get(name); var prop = target.properties.get(name);
@ -83,7 +83,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (nat != null) { if (nat != null) {
Object name = nat.value(); Object name = nat.value();
if (((String)name).startsWith("@@")) name = Symbol.get(((String)name).substring(2)); if (name.toString().startsWith("@@")) name = Symbol.get(name.toString().substring(2));
else if (name.equals("")) name = field.getName(); else if (name.equals("")) name = field.getName();
var getter = OverloadFunction.of("get " + name, Overload.getterFromField(field)); var getter = OverloadFunction.of("get " + name, Overload.getterFromField(field));
@ -99,7 +99,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (nat != null) { if (nat != null) {
Object name = nat.value(); Object name = nat.value();
if (((String)name).startsWith("@@")) name = Symbol.get(((String)name).substring(2)); if (name.toString().startsWith("@@")) name = Symbol.get(name.toString().substring(2));
else if (name.equals("")) name = cl.getName(); else if (name.equals("")) name = cl.getName();
var getter = new NativeFunction("get " + name, (ctx, thisArg, args) -> cl); var getter = new NativeFunction("get " + name, (ctx, thisArg, args) -> cl);

View File

@ -0,0 +1,33 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeSetter;
@Native("Environment")
public class EnvironmentLib {
private Environment env;
@NativeGetter("@@env") public Environment env() { return env; }
@NativeGetter public int id() {
return env.hashCode();
}
@NativeGetter public ObjectValue global() {
return env.global.obj;
}
@NativeGetter public FunctionValue compile() {
return Environment.compileFunc(env);
}
@NativeSetter public void compile(FunctionValue func) {
env.add(Environment.COMPILE_FUNC, func);
}
public EnvironmentLib(Environment env) {
this.env = env;
}
}