feat: implement symbols in java

This commit is contained in:
2023-09-26 11:29:49 +03:00
parent e16c0fedb1
commit 6c71911575
6 changed files with 77 additions and 54 deletions

View File

@@ -14,7 +14,7 @@ import me.topchetoeu.jscript.interop.Native;
public class Internals {
public final Environment targetEnv;
@Native public final FunctionValue object, function, promise, array, bool, number, string, map, set;
@Native public final FunctionValue object, function, promise, array, bool, number, string, symbol, map, set;
@Native public void markSpecial(FunctionValue ...funcs) {
for (var func : funcs) {
@@ -72,7 +72,7 @@ public class Internals {
return stringFromChars(res);
}
@Native public Symbol symbol(String str) {
@Native public Symbol getSymbol(String str) {
return new Symbol(str);
}
@Native public String symbolToString(Symbol str) {
@@ -160,6 +160,7 @@ public class Internals {
this.bool = targetEnv.wrappersProvider.getConstr(BooleanPolyfill.class);
this.number = targetEnv.wrappersProvider.getConstr(NumberPolyfill.class);
this.string = targetEnv.wrappersProvider.getConstr(StringPolyfill.class);
this.symbol = targetEnv.wrappersProvider.getConstr(SymbolPolyfill.class);
this.map = targetEnv.wrappersProvider.getConstr(MapPolyfill.class);
this.set = targetEnv.wrappersProvider.getConstr(SetPolyfill.class);
}

View File

@@ -0,0 +1,69 @@
package me.topchetoeu.jscript.polyfills;
import java.util.HashMap;
import java.util.Map;
import me.topchetoeu.jscript.engine.Context;
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.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeGetter;
public class SymbolPolyfill {
private static final Map<String, Symbol> symbols = new HashMap<>();
@Native("@@Symbol.typeName") public final String name = "Symbol";
@NativeGetter public static Symbol typeName(Context ctx) { return ctx.env.symbol("Symbol.typeName"); }
@NativeGetter public static Symbol replace(Context ctx) { return ctx.env.symbol("Symbol.replace"); }
@NativeGetter public static Symbol match(Context ctx) { return ctx.env.symbol("Symbol.match"); }
@NativeGetter public static Symbol matchAll(Context ctx) { return ctx.env.symbol("Symbol.matchAll"); }
@NativeGetter public static Symbol split(Context ctx) { return ctx.env.symbol("Symbol.split"); }
@NativeGetter public static Symbol search(Context ctx) { return ctx.env.symbol("Symbol.search"); }
@NativeGetter public static Symbol iterator(Context ctx) { return ctx.env.symbol("Symbol.iterator"); }
@NativeGetter public static Symbol asyncIterator(Context ctx) { return ctx.env.symbol("Symbol.asyncIterator"); }
public final Symbol value;
private static Symbol passThis(String funcName, Object val) {
if (val instanceof SymbolPolyfill) return ((SymbolPolyfill)val).value;
else if (val instanceof Symbol) return (Symbol)val;
else throw EngineException.ofType(String.format("'%s' may only be called upon object and primitve symbols.", funcName));
}
@NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) throws InterruptedException {
if (thisArg instanceof ObjectValue) throw EngineException.ofType("Symbol constructor may not be called with new.");
if (val == null) return new Symbol("");
else return new Symbol(Values.toString(ctx, val));
}
@Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException {
return passThis("toString", thisArg).value;
}
@Native(thisArg = true) public static Symbol valueOf(Context ctx, Object thisArg) throws InterruptedException {
return passThis("valueOf", thisArg);
}
@Native public static String fromCharCode(int ...val) {
char[] arr = new char[val.length];
for (var i = 0; i < val.length; i++) arr[i] = (char)val[i];
return new String(arr);
}
@Native("for") public static Symbol _for(String key) {
if (symbols.containsKey(key)) return symbols.get(key);
else {
var sym = new Symbol(key);
symbols.put(key, sym);
return sym;
}
}
@Native public static String keyFor(Symbol sym) {
return sym.value;
}
public SymbolPolyfill(Symbol val) {
this.value = val;
}
}