From 9c65bacbac62c448fb04e57129a7f00ddf8ab253 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 28 Sep 2023 09:38:51 +0300 Subject: [PATCH] fix: type name can f itself --- lib/core.ts | 7 +---- .../topchetoeu/jscript/interop/InitType.java | 7 +++++ .../jscript/interop/NativeInit.java | 12 ++++++++ .../interop/NativeWrapperProvider.java | 30 +++++++++++++++---- .../jscript/interop/OverloadFunction.java | 5 ++++ .../jscript/polyfills/ArrayPolyfill.java | 7 +++++ .../polyfills/AsyncFunctionPolyfill.java | 2 -- .../jscript/polyfills/BooleanPolyfill.java | 6 ++++ .../jscript/polyfills/ErrorPolyfill.java | 30 +++++-------------- .../jscript/polyfills/FunctionPolyfill.java | 8 +++++ .../jscript/polyfills/GeneratorPolyfill.java | 1 + .../jscript/polyfills/Internals.java | 3 -- .../jscript/polyfills/MapPolyfill.java | 1 + .../jscript/polyfills/NumberPolyfill.java | 7 +++++ .../jscript/polyfills/ObjectPolyfill.java | 7 +++++ .../jscript/polyfills/PromisePolyfill.java | 7 +++++ .../jscript/polyfills/RangeErrorPolyfill.java | 20 +++++++++++++ .../jscript/polyfills/RegExpPolyfill.java | 1 + .../jscript/polyfills/SetPolyfill.java | 1 + .../jscript/polyfills/StringPolyfill.java | 7 +++++ .../jscript/polyfills/SymbolPolyfill.java | 7 +++++ .../polyfills/SyntaxErrorPolyfill.java | 20 +++++++++++++ .../jscript/polyfills/TypeErrorPolyfill.java | 20 +++++++++++++ 23 files changed, 178 insertions(+), 38 deletions(-) create mode 100644 src/me/topchetoeu/jscript/interop/InitType.java create mode 100644 src/me/topchetoeu/jscript/interop/NativeInit.java create mode 100644 src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java create mode 100644 src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java create mode 100644 src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java diff --git a/lib/core.ts b/lib/core.ts index fa09628..d1795dd 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -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; diff --git a/src/me/topchetoeu/jscript/interop/InitType.java b/src/me/topchetoeu/jscript/interop/InitType.java new file mode 100644 index 0000000..89793f8 --- /dev/null +++ b/src/me/topchetoeu/jscript/interop/InitType.java @@ -0,0 +1,7 @@ +package me.topchetoeu.jscript.interop; + +public enum InitType { + CONSTRUCTOR, + PROTOTYPE, + NAMESPACE, +} \ No newline at end of file diff --git a/src/me/topchetoeu/jscript/interop/NativeInit.java b/src/me/topchetoeu/jscript/interop/NativeInit.java new file mode 100644 index 0000000..66d13fe --- /dev/null +++ b/src/me/topchetoeu/jscript/interop/NativeInit.java @@ -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(); +} diff --git a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java index 0098016..7affa94 100644 --- a/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java +++ b/src/me/topchetoeu/jscript/interop/NativeWrapperProvider.java @@ -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); diff --git a/src/me/topchetoeu/jscript/interop/OverloadFunction.java b/src/me/topchetoeu/jscript/interop/OverloadFunction.java index bf0f20c..5f54ceb 100644 --- a/src/me/topchetoeu/jscript/interop/OverloadFunction.java +++ b/src/me/topchetoeu/jscript/interop/OverloadFunction.java @@ -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); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java b/src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java index a2901a7..7f9edde 100644 --- a/src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java b/src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java index 951c6c5..b568cf6 100644 --- a/src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java @@ -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; diff --git a/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java b/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java index 843e78b..026dad2 100644 --- a/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java b/src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java index 2e694a8..a466de9 100644 --- a/src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java b/src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java index e46e674..9d5a625 100644 --- a/src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java b/src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java index db176ed..cf4b817 100644 --- a/src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java @@ -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; diff --git a/src/me/topchetoeu/jscript/polyfills/Internals.java b/src/me/topchetoeu/jscript/polyfills/Internals.java index 643ab8f..dded47a 100644 --- a/src/me/topchetoeu/jscript/polyfills/Internals.java +++ b/src/me/topchetoeu/jscript/polyfills/Internals.java @@ -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; diff --git a/src/me/topchetoeu/jscript/polyfills/MapPolyfill.java b/src/me/topchetoeu/jscript/polyfills/MapPolyfill.java index f747164..1791c81 100644 --- a/src/me/topchetoeu/jscript/polyfills/MapPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/MapPolyfill.java @@ -15,6 +15,7 @@ import me.topchetoeu.jscript.interop.NativeGetter; public class MapPolyfill { private LinkedHashMap 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); } diff --git a/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java b/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java index a3859eb..a9e30c6 100644 --- a/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java b/src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java index 51c12e9..1dca001 100644 --- a/src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java b/src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java index 741f052..574a894 100644 --- a/src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java b/src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java new file mode 100644 index 0000000..99bfa82 --- /dev/null +++ b/src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java @@ -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"); + } +} \ No newline at end of file diff --git a/src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java b/src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java index 373bff1..1fb885c 100644 --- a/src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/SetPolyfill.java b/src/me/topchetoeu/jscript/polyfills/SetPolyfill.java index 9fd9bbd..c6bc7f3 100644 --- a/src/me/topchetoeu/jscript/polyfills/SetPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/SetPolyfill.java @@ -15,6 +15,7 @@ import me.topchetoeu.jscript.interop.NativeGetter; public class SetPolyfill { private LinkedHashSet 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); } diff --git a/src/me/topchetoeu/jscript/polyfills/StringPolyfill.java b/src/me/topchetoeu/jscript/polyfills/StringPolyfill.java index cab4021..de56291 100644 --- a/src/me/topchetoeu/jscript/polyfills/StringPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/StringPolyfill.java @@ -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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java b/src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java index 899702c..6dfb33e 100644 --- a/src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java @@ -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 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"); + } } diff --git a/src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java b/src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java new file mode 100644 index 0000000..27c6ed1 --- /dev/null +++ b/src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java @@ -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"); + } +} \ No newline at end of file diff --git a/src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java b/src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java new file mode 100644 index 0000000..5814f69 --- /dev/null +++ b/src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java @@ -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"); + } +} \ No newline at end of file