fix: type name can f itself

This commit is contained in:
TopchetoEU 2023-09-28 09:38:51 +03:00
parent 0dacaaeb4c
commit 9c65bacbac
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
23 changed files with 178 additions and 38 deletions

View File

@ -54,7 +54,6 @@ interface Internals {
var env: Environment = arguments[0], internals: Internals = arguments[1];
try {
const values = {
Object: env.global.Object = internals.object,
Function: env.global.Function = internals.function,
@ -86,16 +85,12 @@ try {
env.setProto('rangeErr', env.global.RangeError.prototype);
env.setProto('typeErr', env.global.TypeError.prototype);
env.setProto('syntaxErr', env.global.SyntaxError.prototype);
(env.global.Object.prototype as any).__proto__ = null;
internals.getEnv(run)?.setProto('array', Array.prototype);
globalThis.log = (...args) => internals.apply(internals.log, internals, args);
for (const key in values) {
(values as any)[key].prototype[env.symbol('Symbol.typeName')] = key;
log();
}
run('timeout');
env.global.log = log;

View File

@ -0,0 +1,7 @@
package me.topchetoeu.jscript.interop;
public enum InitType {
CONSTRUCTOR,
PROTOTYPE,
NAMESPACE,
}

View File

@ -0,0 +1,12 @@
package me.topchetoeu.jscript.interop;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface NativeInit {
InitType value();
}

View File

@ -33,7 +33,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (!(val instanceof OverloadFunction)) target.defineProperty(null, name, val = new OverloadFunction(name.toString()));
((OverloadFunction)val).overloads.add(Overload.fromMethod(method, nat.thisArg()));
((OverloadFunction)val).add(Overload.fromMethod(method, nat.thisArg()));
}
else {
if (get != null) {
@ -50,7 +50,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (prop != null && prop.getter instanceof OverloadFunction) getter = (OverloadFunction)prop.getter;
else getter = new OverloadFunction("get " + name);
getter.overloads.add(Overload.fromMethod(method, get.thisArg()));
getter.add(Overload.fromMethod(method, get.thisArg()));
target.defineProperty(null, name, getter, setter, true, true);
}
if (set != null) {
@ -67,7 +67,7 @@ public class NativeWrapperProvider implements WrappersProvider {
if (prop != null && prop.setter instanceof OverloadFunction) setter = (OverloadFunction)prop.setter;
else setter = new OverloadFunction("set " + name);
setter.overloads.add(Overload.fromMethod(method, set.thisArg()));
setter.add(Overload.fromMethod(method, set.thisArg()));
target.defineProperty(null, name, getter, setter, true, true);
}
}
@ -83,8 +83,8 @@ public class NativeWrapperProvider implements WrappersProvider {
if (((String)name).startsWith("@@")) name = env.symbol(((String)name).substring(2));
else if (name.equals("")) name = field.getName();
var getter = new OverloadFunction("get " + name).add(Overload.getterFromField(field));
var setter = new OverloadFunction("set " + name).add(Overload.setterFromField(field));
var getter = OverloadFunction.of("get " + name, Overload.getterFromField(field));
var setter = OverloadFunction.of("set " + name, Overload.setterFromField(field));
target.defineProperty(null, name, getter, setter, true, false);
}
}
@ -115,6 +115,13 @@ public class NativeWrapperProvider implements WrappersProvider {
public static ObjectValue makeProto(Environment ctx, Class<?> clazz) {
var res = new ObjectValue();
for (var overload : clazz.getDeclaredMethods()) {
var init = overload.getAnnotation(NativeInit.class);
if (init == null || init.value() != InitType.PROTOTYPE) continue;
try { overload.invoke(null, ctx, res); }
catch (ReflectiveOperationException e) { e.printStackTrace(); }
}
applyMethods(ctx, true, res, clazz);
applyFields(ctx, true, res, clazz);
applyClasses(ctx, true, res, clazz);
@ -140,6 +147,12 @@ public class NativeWrapperProvider implements WrappersProvider {
if (constr == null) continue;
((OverloadFunction)func).add(Overload.fromMethod(overload, constr.thisArg()));
}
for (var overload : clazz.getDeclaredMethods()) {
var init = overload.getAnnotation(NativeInit.class);
if (init == null || init.value() != InitType.CONSTRUCTOR) continue;
try { overload.invoke(null, ctx, func); }
catch (ReflectiveOperationException e) { e.printStackTrace(); }
}
if (((OverloadFunction)func).overloads.size() == 0) {
func = new NativeFunction(clazz.getName(), (a, b, c) -> { throw EngineException.ofError("This constructor is not invokable."); });
@ -162,6 +175,13 @@ public class NativeWrapperProvider implements WrappersProvider {
public static ObjectValue makeNamespace(Environment ctx, Class<?> clazz) {
ObjectValue res = new ObjectValue();
for (var overload : clazz.getDeclaredMethods()) {
var init = overload.getAnnotation(NativeInit.class);
if (init == null || init.value() != InitType.NAMESPACE) continue;
try { overload.invoke(null, ctx, res); }
catch (ReflectiveOperationException e) { e.printStackTrace(); }
}
applyMethods(ctx, false, res, clazz);
applyFields(ctx, false, res, clazz);
applyClasses(ctx, false, res, clazz);

View File

@ -113,4 +113,9 @@ public class OverloadFunction extends FunctionValue {
public OverloadFunction(String name) {
super(name, 0);
}
public static OverloadFunction of(String name, Overload overload) {
if (overload == null) return null;
else return new OverloadFunction(name).add(overload);
}
}

View File

@ -4,13 +4,16 @@ import java.util.Iterator;
import java.util.Stack;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit;
import me.topchetoeu.jscript.interop.NativeSetter;
public class ArrayPolyfill {
@ -347,4 +350,8 @@ public class ArrayPolyfill {
return res;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Array");
}
}

View File

@ -7,13 +7,11 @@ import me.topchetoeu.jscript.engine.values.CodeFunction;
import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.NativeFunction;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.Native;
public class AsyncFunctionPolyfill extends FunctionValue {
public final FunctionValue factory;
public static class AsyncHelper {
@Native("@@Symbol.typeName") public final String name = "AsyncFunction";
public PromisePolyfill promise = new PromisePolyfill();
public CodeFrame frame;

View File

@ -1,10 +1,13 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class BooleanPolyfill {
public static final BooleanPolyfill TRUE = new BooleanPolyfill(true);
@ -27,4 +30,7 @@ public class BooleanPolyfill {
public BooleanPolyfill(boolean val) {
this.value = val;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Boolean");
}
}

View File

@ -1,35 +1,16 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class ErrorPolyfill {
public static class SyntaxErrorPolyfill extends ErrorPolyfill {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "SyntaxError");
return target;
}
}
public static class TypeErrorPolyfill extends ErrorPolyfill {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "TypeError");
return target;
}
}
public static class RangeErrorPolyfill extends ErrorPolyfill {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "RangeError");
return target;
}
}
private static String toString(Context ctx, Object cause, Object name, Object message, ArrayValue stack) throws InterruptedException {
if (name == null) name = "";
else name = Values.toString(ctx, name).trim();
@ -78,4 +59,9 @@ public class ErrorPolyfill {
return target;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Error");
target.defineProperty(null, "name", "Error");
}
}

View File

@ -1,11 +1,15 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.NativeFunction;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeInit;
public class FunctionPolyfill {
@Native(thisArg = true) public static Object apply(Context ctx, FunctionValue func, Object thisArg, ArrayValue args) throws InterruptedException {
@ -40,4 +44,8 @@ public class FunctionPolyfill {
@Native public static FunctionValue generator(FunctionValue func) {
return new GeneratorPolyfill(func);
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Function");
}
}

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.frame.CodeFrame;
import me.topchetoeu.jscript.engine.frame.Runners;
import me.topchetoeu.jscript.engine.values.CodeFunction;

View File

@ -10,9 +10,6 @@ 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.interop.Native;
import me.topchetoeu.jscript.polyfills.ErrorPolyfill.RangeErrorPolyfill;
import me.topchetoeu.jscript.polyfills.ErrorPolyfill.SyntaxErrorPolyfill;
import me.topchetoeu.jscript.polyfills.ErrorPolyfill.TypeErrorPolyfill;
public class Internals {
public final Environment targetEnv;

View File

@ -15,6 +15,7 @@ import me.topchetoeu.jscript.interop.NativeGetter;
public class MapPolyfill {
private LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
@Native("@@Symbol.typeName") public final String name = "Map";
@Native("@@Symbol.iterator") public ObjectValue iterator(Context ctx) throws InterruptedException {
return this.entries(ctx);
}

View File

@ -1,10 +1,13 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class NumberPolyfill {
@Native public static final double EPSILON = java.lang.Math.ulp(1.0);
@ -49,4 +52,8 @@ public class NumberPolyfill {
public NumberPolyfill(double val) {
this.value = val;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Number");
}
}

View File

@ -1,14 +1,17 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.FunctionValue;
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.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class ObjectPolyfill {
@Native public static ObjectValue assign(Context ctx, ObjectValue dst, Object... src) throws InterruptedException {
@ -209,4 +212,8 @@ public class ObjectPolyfill {
// else if (arg instanceof Symbol) return SymbolPolyfill.constructor(ctx, thisArg, arg);
else return arg;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Object");
}
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Map;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.MessageContext;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.FunctionValue;
@ -13,7 +14,9 @@ import me.topchetoeu.jscript.engine.values.NativeWrapper;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeInit;
public class PromisePolyfill {
private static class Handle {
@ -338,4 +341,8 @@ public class PromisePolyfill {
public PromisePolyfill() {
this(STATE_PENDING, null);
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Promise");
}
}

View File

@ -0,0 +1,20 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class RangeErrorPolyfill extends ErrorPolyfill {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "RangeError");
return target;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "RangeError");
target.defineProperty(null, "name", "RangeError");
}
}

View File

@ -62,6 +62,7 @@ public class RegExpPolyfill {
@Native public final boolean hasIndices;
@Native public final boolean global;
@Native public final boolean sticky;
@Native("@@Symbol.typeName") public final String name = "RegExp";
@NativeGetter public boolean ignoreCase() { return (flags & Pattern.CASE_INSENSITIVE) != 0; }
@NativeGetter public boolean multiline() { return (flags & Pattern.MULTILINE) != 0; }

View File

@ -15,6 +15,7 @@ import me.topchetoeu.jscript.interop.NativeGetter;
public class SetPolyfill {
private LinkedHashSet<Object> set = new LinkedHashSet<>();
@Native("@@Symbol.typeName") public final String name = "Set";
@Native("@@Symbol.iterator") public ObjectValue iterator(Context ctx) throws InterruptedException {
return this.values(ctx);
}

View File

@ -3,14 +3,17 @@ package me.topchetoeu.jscript.polyfills;
import java.util.regex.Pattern;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ArrayValue;
import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit;
// TODO: implement index wrapping properly
public class StringPolyfill {
@ -250,4 +253,8 @@ public class StringPolyfill {
public StringPolyfill(String val) {
this.value = val;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "String");
}
}

View File

@ -4,13 +4,16 @@ import java.util.HashMap;
import java.util.Map;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
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.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit;
public class SymbolPolyfill {
private static final Map<String, Symbol> symbols = new HashMap<>();
@ -59,4 +62,8 @@ public class SymbolPolyfill {
public SymbolPolyfill(Symbol val) {
this.value = val;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "Symbol");
}
}

View File

@ -0,0 +1,20 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class SyntaxErrorPolyfill extends ErrorPolyfill {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "SyntaxError");
return target;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "SyntaxError");
target.defineProperty(null, "name", "SyntaxError");
}
}

View File

@ -0,0 +1,20 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit;
public class TypeErrorPolyfill extends ErrorPolyfill {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "TypeError");
return target;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {
target.defineProperty(null, env.symbol("Symbol.typeName"), "TypeError");
target.defineProperty(null, "name", "TypeError");
}
}