From 604b752be67f24eb090f0deb4ab317b00fd9729f Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 4 Oct 2023 08:49:20 +0300 Subject: [PATCH] feat: add global functions --- lib/tsconfig.json | 19 ------- src/me/topchetoeu/jscript/Main.java | 8 --- src/me/topchetoeu/jscript/engine/Data.java | 10 ++-- .../me/topchetoeu/jscript/js}/lib.d.ts | 2 +- .../jscript/polyfills/Internals.java | 52 +++++++++++++++---- 5 files changed, 48 insertions(+), 43 deletions(-) delete mode 100644 lib/tsconfig.json rename {lib => src/me/topchetoeu/jscript/js}/lib.d.ts (99%) diff --git a/lib/tsconfig.json b/lib/tsconfig.json deleted file mode 100644 index 64939d9..0000000 --- a/lib/tsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "files": [ - "lib.d.ts", - "core.ts" - ], - "compilerOptions": { - "outFile": "../bin/me/topchetoeu/jscript/js/core.js", - // "declarationDir": "", - // "declarationDir": "bin/me/topchetoeu/jscript/dts", - "target": "ES5", - "lib": [], - "module": "None", - "stripInternal": true, - "downlevelIteration": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - } -} diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index fe9625e..b72caea 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -8,7 +8,6 @@ import java.nio.file.Files; import java.nio.file.Path; import me.topchetoeu.jscript.engine.Message; -import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Engine; import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.values.NativeFunction; @@ -87,13 +86,6 @@ public class Main { return null; }), null); - // engine.pushMsg( - // false, - // new Context(builderEnv, new MessageContext(engine)), - // "core.js", resourceToString("js/core.js"), - // null, env, new Internals(env) - // ).toObservable().on(valuePrinter); - task = engine.start(); var reader = new Thread(() -> { try { diff --git a/src/me/topchetoeu/jscript/engine/Data.java b/src/me/topchetoeu/jscript/engine/Data.java index 49d18b7..1db4ba8 100644 --- a/src/me/topchetoeu/jscript/engine/Data.java +++ b/src/me/topchetoeu/jscript/engine/Data.java @@ -41,13 +41,15 @@ public class Data implements Iterable, ?>> { } public boolean has(DataKey key) { return data.containsKey(key); } - public Data increase(DataKey key, int n, int start) { - return set(key, get(key, start) + n); + public int increase(DataKey key, int n, int start) { + int res; + set(key, res = get(key, start) + n); + return res; } - public Data increase(DataKey key, int n) { + public int increase(DataKey key, int n) { return increase(key, n, 0); } - public Data increase(DataKey key) { + public int increase(DataKey key) { return increase(key, 1, 0); } diff --git a/lib/lib.d.ts b/src/me/topchetoeu/jscript/js/lib.d.ts similarity index 99% rename from lib/lib.d.ts rename to src/me/topchetoeu/jscript/js/lib.d.ts index 5ce6aab..df95570 100644 --- a/lib/lib.d.ts +++ b/src/me/topchetoeu/jscript/js/lib.d.ts @@ -350,7 +350,7 @@ interface Object { toString(): string; hasOwnProperty(key: any): boolean; } -interface ObjectConstructor extends Function { +interface ObjectConstructor { (arg: string): String; (arg: number): Number; (arg: boolean): Boolean; diff --git a/src/me/topchetoeu/jscript/polyfills/Internals.java b/src/me/topchetoeu/jscript/polyfills/Internals.java index 24386bd..fd808ad 100644 --- a/src/me/topchetoeu/jscript/polyfills/Internals.java +++ b/src/me/topchetoeu/jscript/polyfills/Internals.java @@ -3,26 +3,29 @@ package me.topchetoeu.jscript.polyfills; import java.util.HashMap; import me.topchetoeu.jscript.engine.Context; +import me.topchetoeu.jscript.engine.DataKey; import me.topchetoeu.jscript.engine.Environment; +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 HashMap threads = new HashMap<>(); - private int i = 0; + private static final DataKey> THREADS = new DataKey<>(); + private static final DataKey I = new DataKey<>(); - @Native public FunctionValue bind(FunctionValue func, Object thisArg) throws InterruptedException { + @Native public static FunctionValue bind(FunctionValue func, Object thisArg) throws InterruptedException { return FunctionPolyfill.bind(func, thisArg); } - @Native public void log(Context ctx, Object ...args) throws InterruptedException { + @Native public static void log(Context ctx, Object ...args) throws InterruptedException { for (var arg : args) { Values.printValue(ctx, arg); } System.out.println(); } - @Native public int setTimeout(Context ctx, FunctionValue func, int delay, Object ...args) { + @Native public static int setTimeout(Context ctx, FunctionValue func, int delay, Object ...args) { var thread = new Thread(() -> { var ms = (long)delay; var ns = (int)((delay - ms) * 10000000); @@ -36,11 +39,12 @@ public class Internals { }); thread.start(); + int i = ctx.env.data.increase(I, 1, 0); + var threads = ctx.env.data.add(THREADS, new HashMap<>()); threads.put(++i, thread); - return i; } - @Native public int setInterval(Context ctx, FunctionValue func, int delay, Object ...args) { + @Native public static int setInterval(Context ctx, FunctionValue func, int delay, Object ...args) { var thread = new Thread(() -> { var ms = (long)delay; var ns = (int)((delay - ms) * 10000000); @@ -56,22 +60,32 @@ public class Internals { }); thread.start(); + int i = ctx.env.data.increase(I, 1, 0); + var threads = ctx.env.data.add(THREADS, new HashMap<>()); threads.put(++i, thread); - return i; } - @Native public void clearTimeout(Context ctx, int i) { + @Native public static void clearTimeout(Context ctx, int i) { + var threads = ctx.env.data.add(THREADS, new HashMap<>()); + var thread = threads.remove(i); if (thread != null) thread.interrupt(); } - @Native public void clearInterval(Context ctx, int i) { + @Native public static void clearInterval(Context ctx, int i) { clearTimeout(ctx, i); } + @Native public static double parseInt(Context ctx, String val) throws InterruptedException { + return NumberPolyfill.parseInt(ctx, val); + } + @Native public static double parseFloat(Context ctx, String val) throws InterruptedException { + return NumberPolyfill.parseFloat(ctx, val); + } + public void apply(Environment env) { var wp = env.wrappersProvider; - var glob = env.global; + var glob = env.global = new GlobalScope(NativeWrapperProvider.makeNamespace(env, Internals.class)); glob.define(null, "Object", false, wp.getConstr(ObjectPolyfill.class)); glob.define(null, "Function", false, wp.getConstr(FunctionPolyfill.class)); @@ -92,6 +106,22 @@ public class Internals { glob.define(null, "TypeError", false, wp.getConstr(TypeErrorPolyfill.class)); glob.define(null, "RangeError", false, wp.getConstr(RangeErrorPolyfill.class)); + env.setProto("object", wp.getProto(ObjectPolyfill.class)); + env.setProto("function", wp.getProto(FunctionPolyfill.class)); + env.setProto("array", wp.getProto(ArrayPolyfill.class)); + + env.setProto("bool", wp.getProto(BooleanPolyfill.class)); + env.setProto("number", wp.getProto(NumberPolyfill.class)); + env.setProto("string", wp.getProto(StringPolyfill.class)); + env.setProto("symbol", wp.getProto(SymbolPolyfill.class)); + + env.setProto("error", wp.getProto(ErrorPolyfill.class)); + env.setProto("syntaxErr", wp.getProto(SyntaxErrorPolyfill.class)); + env.setProto("typeErr", wp.getProto(TypeErrorPolyfill.class)); + env.setProto("rangeErr", wp.getProto(RangeErrorPolyfill.class)); + + wp.getProto(ObjectPolyfill.class).setPrototype(null, null); + System.out.println("Loaded polyfills!"); } }