feat: add global functions
This commit is contained in:
parent
6c7fe6deaf
commit
604b752be6
@ -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,
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
@ -41,13 +41,15 @@ public class Data implements Iterable<Entry<DataKey<?>, ?>> {
|
||||
}
|
||||
public boolean has(DataKey<?> key) { return data.containsKey(key); }
|
||||
|
||||
public Data increase(DataKey<Integer> key, int n, int start) {
|
||||
return set(key, get(key, start) + n);
|
||||
public int increase(DataKey<Integer> key, int n, int start) {
|
||||
int res;
|
||||
set(key, res = get(key, start) + n);
|
||||
return res;
|
||||
}
|
||||
public Data increase(DataKey<Integer> key, int n) {
|
||||
public int increase(DataKey<Integer> key, int n) {
|
||||
return increase(key, n, 0);
|
||||
}
|
||||
public Data increase(DataKey<Integer> key) {
|
||||
public int increase(DataKey<Integer> key) {
|
||||
return increase(key, 1, 0);
|
||||
}
|
||||
|
||||
|
@ -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;
|
@ -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<Integer, Thread> threads = new HashMap<>();
|
||||
private int i = 0;
|
||||
private static final DataKey<HashMap<Integer, Thread>> THREADS = new DataKey<>();
|
||||
private static final DataKey<Integer> 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!");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user