From aaf9a6fa4589d9a1e87db6c39c6986eca50d2c96 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 27 Dec 2023 13:58:58 +0200 Subject: [PATCH] fix: pass environment to compiler via simple environment wrapper --- src/me/topchetoeu/jscript/Main.java | 6 +++- src/me/topchetoeu/jscript/engine/Context.java | 3 +- .../jscript/engine/Environment.java | 6 +--- .../interop/NativeWrapperProvider.java | 8 ++--- .../jscript/lib/EnvironmentLib.java | 33 +++++++++++++++++++ 5 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/me/topchetoeu/jscript/lib/EnvironmentLib.java diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index 7d12cac..252114f 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -23,6 +23,7 @@ import me.topchetoeu.jscript.filesystem.MemoryFilesystem; import me.topchetoeu.jscript.filesystem.Mode; import me.topchetoeu.jscript.filesystem.PhysicalFilesystem; import me.topchetoeu.jscript.filesystem.RootFilesystem; +import me.topchetoeu.jscript.lib.EnvironmentLib; import me.topchetoeu.jscript.lib.Internals; import me.topchetoeu.jscript.modules.ModuleRepo; import me.topchetoeu.jscript.permissions.PermissionsManager; @@ -151,10 +152,13 @@ public class Main { ).await(); 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( false, bsEnv, 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(); } catch (EngineException e) { diff --git a/src/me/topchetoeu/jscript/engine/Context.java b/src/me/topchetoeu/jscript/engine/Context.java index 4924971..361d9e2 100644 --- a/src/me/topchetoeu/jscript/engine/Context.java +++ b/src/me/topchetoeu/jscript/engine/Context.java @@ -16,6 +16,7 @@ import me.topchetoeu.jscript.engine.values.ArrayValue; import me.topchetoeu.jscript.engine.values.FunctionValue; import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.exceptions.EngineException; +import me.topchetoeu.jscript.lib.EnvironmentLib; import me.topchetoeu.jscript.mapping.SourceMap; public class Context extends ExtensionStack { @@ -42,7 +43,7 @@ public class Context extends ExtensionStack { public FunctionValue compile(Filename filename, String raw) { 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"); if (!has(DebugContext.ENV_KEY)) return function; diff --git a/src/me/topchetoeu/jscript/engine/Environment.java b/src/me/topchetoeu/jscript/engine/Environment.java index 5a58248..a8202f3 100644 --- a/src/me/topchetoeu/jscript/engine/Environment.java +++ b/src/me/topchetoeu/jscript/engine/Environment.java @@ -13,14 +13,12 @@ import me.topchetoeu.jscript.engine.values.ObjectValue; import me.topchetoeu.jscript.engine.values.Symbol; import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.exceptions.EngineException; -import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.NativeWrapperProvider; import me.topchetoeu.jscript.parsing.Parsing; // TODO: Remove hardcoded extensions form environment @SuppressWarnings("unchecked") public class Environment implements Extensions { - private static int nextId = 0; public static final HashMap symbols = new HashMap<>(); @@ -48,8 +46,6 @@ public class Environment implements Extensions { public GlobalScope global; public WrappersProvider wrappers; - @Native public int id = ++nextId; - @Override public void add(Symbol key, T obj) { data.put(key, obj); } @@ -73,7 +69,7 @@ public class Environment implements Extensions { var filename = Values.toString(ctx, args[1]); 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 target = Parsing.compile(env, Filename.parse(filename), source); diff --git a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java index fa7be76..4aa409f 100644 --- a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java +++ b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java @@ -43,7 +43,7 @@ public class NativeWrapperProvider implements WrappersProvider { if (get.thisArg() && !member || !get.thisArg() && !memberMatch) continue; 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(); var prop = target.properties.get(name); @@ -60,7 +60,7 @@ public class NativeWrapperProvider implements WrappersProvider { if (set.thisArg() && !member || !set.thisArg() && !memberMatch) continue; 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(); var prop = target.properties.get(name); @@ -83,7 +83,7 @@ public class NativeWrapperProvider implements WrappersProvider { if (nat != null) { 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(); var getter = OverloadFunction.of("get " + name, Overload.getterFromField(field)); @@ -99,7 +99,7 @@ public class NativeWrapperProvider implements WrappersProvider { if (nat != null) { 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(); var getter = new NativeFunction("get " + name, (ctx, thisArg, args) -> cl); diff --git a/src/me/topchetoeu/jscript/lib/EnvironmentLib.java b/src/me/topchetoeu/jscript/lib/EnvironmentLib.java new file mode 100644 index 0000000..aa15d7d --- /dev/null +++ b/src/me/topchetoeu/jscript/lib/EnvironmentLib.java @@ -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; + } +}