From 926b9c17d88f5f1bc0e99fa23efee7bb83144d8e Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 6 Oct 2023 18:42:35 +0300 Subject: [PATCH] feat: readd JSON to lib --- src/me/topchetoeu/jscript/engine/WrappersProvider.java | 1 + .../topchetoeu/jscript/interop/NativeWrapperProvider.java | 5 +++++ src/me/topchetoeu/jscript/lib/Internals.java | 8 ++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/me/topchetoeu/jscript/engine/WrappersProvider.java b/src/me/topchetoeu/jscript/engine/WrappersProvider.java index dd27353..4c594ba 100644 --- a/src/me/topchetoeu/jscript/engine/WrappersProvider.java +++ b/src/me/topchetoeu/jscript/engine/WrappersProvider.java @@ -5,5 +5,6 @@ import me.topchetoeu.jscript.engine.values.ObjectValue; public interface WrappersProvider { public ObjectValue getProto(Class obj); + public ObjectValue getNamespace(Class obj); public FunctionValue getConstr(Class obj); } diff --git a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java index 7affa94..3a12212 100644 --- a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java +++ b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java @@ -13,6 +13,7 @@ import me.topchetoeu.jscript.exceptions.EngineException; public class NativeWrapperProvider implements WrappersProvider { private final HashMap, FunctionValue> constructors = new HashMap<>(); private final HashMap, ObjectValue> prototypes = new HashMap<>(); + private final HashMap, ObjectValue> namespaces = new HashMap<>(); private final Environment env; private static void applyMethods(Environment env, boolean member, ObjectValue target, Class clazz) { @@ -229,6 +230,10 @@ public class NativeWrapperProvider implements WrappersProvider { initType(clazz, constructors.get(clazz), prototypes.get(clazz)); return prototypes.get(clazz); } + public ObjectValue getNamespace(Class clazz) { + if (!namespaces.containsKey(clazz)) namespaces.put(clazz, makeNamespace(env, clazz)); + return namespaces.get(clazz); + } public FunctionValue getConstr(Class clazz) { initType(clazz, constructors.get(clazz), prototypes.get(clazz)); return constructors.get(clazz); diff --git a/src/me/topchetoeu/jscript/lib/Internals.java b/src/me/topchetoeu/jscript/lib/Internals.java index 893c54c..e09d58e 100644 --- a/src/me/topchetoeu/jscript/lib/Internals.java +++ b/src/me/topchetoeu/jscript/lib/Internals.java @@ -9,7 +9,6 @@ import me.topchetoeu.jscript.engine.scope.GlobalScope; import me.topchetoeu.jscript.engine.values.FunctionValue; import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.interop.Native; -import me.topchetoeu.jscript.interop.NativeWrapperProvider; public class Internals { private static final DataKey> THREADS = new DataKey<>(); @@ -82,10 +81,11 @@ public class Internals { public void apply(Environment env) { var wp = env.wrappersProvider; - var glob = env.global = new GlobalScope(NativeWrapperProvider.makeNamespace(env, Internals.class)); + var glob = env.global = new GlobalScope(wp.getNamespace(Internals.class)); + + glob.define(null, "Math", false, wp.getNamespace(MathLib.class)); + glob.define(null, "JSON", false, wp.getNamespace(JSONLib.class)); - glob.define(null, "Math", false, NativeWrapperProvider.makeNamespace(env, MathLib.class)); - glob.define(null, "Date", false, wp.getConstr(DateLib.class)); glob.define(null, "Object", false, wp.getConstr(ObjectLib.class)); glob.define(null, "Function", false, wp.getConstr(FunctionLib.class));