feat: readd JSON to lib

This commit is contained in:
TopchetoEU 2023-10-06 18:42:35 +03:00
parent fc705e7383
commit 926b9c17d8
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 10 additions and 4 deletions

View File

@ -5,5 +5,6 @@ import me.topchetoeu.jscript.engine.values.ObjectValue;
public interface WrappersProvider { public interface WrappersProvider {
public ObjectValue getProto(Class<?> obj); public ObjectValue getProto(Class<?> obj);
public ObjectValue getNamespace(Class<?> obj);
public FunctionValue getConstr(Class<?> obj); public FunctionValue getConstr(Class<?> obj);
} }

View File

@ -13,6 +13,7 @@ import me.topchetoeu.jscript.exceptions.EngineException;
public class NativeWrapperProvider implements WrappersProvider { public class NativeWrapperProvider implements WrappersProvider {
private final HashMap<Class<?>, FunctionValue> constructors = new HashMap<>(); private final HashMap<Class<?>, FunctionValue> constructors = new HashMap<>();
private final HashMap<Class<?>, ObjectValue> prototypes = new HashMap<>(); private final HashMap<Class<?>, ObjectValue> prototypes = new HashMap<>();
private final HashMap<Class<?>, ObjectValue> namespaces = new HashMap<>();
private final Environment env; private final Environment env;
private static void applyMethods(Environment env, boolean member, ObjectValue target, Class<?> clazz) { 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)); initType(clazz, constructors.get(clazz), prototypes.get(clazz));
return 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) { public FunctionValue getConstr(Class<?> clazz) {
initType(clazz, constructors.get(clazz), prototypes.get(clazz)); initType(clazz, constructors.get(clazz), prototypes.get(clazz));
return constructors.get(clazz); return constructors.get(clazz);

View File

@ -9,7 +9,6 @@ import me.topchetoeu.jscript.engine.scope.GlobalScope;
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.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeWrapperProvider;
public class Internals { public class Internals {
private static final DataKey<HashMap<Integer, Thread>> THREADS = new DataKey<>(); private static final DataKey<HashMap<Integer, Thread>> THREADS = new DataKey<>();
@ -82,10 +81,11 @@ public class Internals {
public void apply(Environment env) { public void apply(Environment env) {
var wp = env.wrappersProvider; 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, "Date", false, wp.getConstr(DateLib.class));
glob.define(null, "Object", false, wp.getConstr(ObjectLib.class)); glob.define(null, "Object", false, wp.getConstr(ObjectLib.class));
glob.define(null, "Function", false, wp.getConstr(FunctionLib.class)); glob.define(null, "Function", false, wp.getConstr(FunctionLib.class));