diff --git a/lib/core.ts b/lib/core.ts index a19dc25..e1d0bdd 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -10,6 +10,7 @@ interface Internals { array: ArrayConstructor; promise: PromiseConstructor; bool: BooleanConstructor; + number: NumberConstructor; markSpecial(...funcs: Function[]): void; getEnv(func: Function): Environment | undefined; @@ -48,11 +49,12 @@ try { var Array = env.global.Array = internals.array; var Promise = env.global.Promise = internals.promise; var Boolean = env.global.Boolean = internals.bool; + var Number = env.global.Number = internals.number; env.setProto('object', Object.prototype); env.setProto('function', Function.prototype); env.setProto('array', Array.prototype); - env.setProto('bool', Boolean.prototype); + env.setProto('number', Number.prototype); (Object.prototype as any).__proto__ = null; @@ -62,7 +64,7 @@ try { run('values/symbol'); run('values/errors'); run('values/string'); - run('values/number'); + // run('values/number'); run('map'); run('set'); run('regex'); diff --git a/src/me/topchetoeu/jscript/engine/values/Values.java b/src/me/topchetoeu/jscript/engine/values/Values.java index 8d892e3..0d753d2 100644 --- a/src/me/topchetoeu/jscript/engine/values/Values.java +++ b/src/me/topchetoeu/jscript/engine/values/Values.java @@ -117,8 +117,8 @@ public class Values { public static double toNumber(Context ctx, Object obj) throws InterruptedException { var val = toPrimitive(ctx, obj, ConvertHint.VALUEOF); - if (val instanceof Number) return number(obj); - if (val instanceof Boolean) return ((Boolean)obj) ? 1 : 0; + if (val instanceof Number) return number(val); + if (val instanceof Boolean) return ((Boolean)val) ? 1 : 0; if (val instanceof String) { try { return Double.parseDouble((String)val); @@ -134,7 +134,7 @@ public class Values { if (val == NULL) return "null"; if (val instanceof Number) { - var d = number(obj); + var d = number(val); if (d == Double.NEGATIVE_INFINITY) return "-Infinity"; if (d == Double.POSITIVE_INFINITY) return "Infinity"; if (Double.isNaN(d)) return "NaN"; diff --git a/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java b/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java index 4d2c77e..843e78b 100644 --- a/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java +++ b/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java @@ -7,12 +7,14 @@ import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.NativeConstructor; public class BooleanPolyfill { + public static final BooleanPolyfill TRUE = new BooleanPolyfill(true); + public static final BooleanPolyfill FALSE = new BooleanPolyfill(false); + + public final boolean value; + @NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) { val = Values.toBoolean(val); - if (thisArg instanceof ObjectValue) { - ((ObjectValue)thisArg).defineProperty(ctx, "value", val); - return null; - } + if (thisArg instanceof ObjectValue) return (boolean)val ? TRUE : FALSE; else return val; } @Native(thisArg = true) public static String toString(Context ctx, Object thisArg) { @@ -21,4 +23,8 @@ public class BooleanPolyfill { @Native(thisArg = true) public static boolean valueOf(Context ctx, Object thisArg) { return Values.toBoolean(thisArg); } + + public BooleanPolyfill(boolean val) { + this.value = val; + } } diff --git a/src/me/topchetoeu/jscript/polyfills/Internals.java b/src/me/topchetoeu/jscript/polyfills/Internals.java index 9d3d8cd..0c13990 100644 --- a/src/me/topchetoeu/jscript/polyfills/Internals.java +++ b/src/me/topchetoeu/jscript/polyfills/Internals.java @@ -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; + @Native public final FunctionValue object, function, promise, array, bool, number; @Native public void markSpecial(FunctionValue ...funcs) { for (var func : funcs) { @@ -158,5 +158,6 @@ public class Internals { this.promise = targetEnv.wrappersProvider.getConstr(PromisePolyfill.class); this.array = targetEnv.wrappersProvider.getConstr(ArrayPolyfill.class); this.bool = targetEnv.wrappersProvider.getConstr(BooleanPolyfill.class); + this.number = targetEnv.wrappersProvider.getConstr(NumberPolyfill.class); } } diff --git a/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java b/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java new file mode 100644 index 0000000..c0c7e6a --- /dev/null +++ b/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java @@ -0,0 +1,38 @@ +package me.topchetoeu.jscript.polyfills; + +import me.topchetoeu.jscript.engine.Context; +import me.topchetoeu.jscript.engine.values.ObjectValue; +import me.topchetoeu.jscript.engine.values.Values; +import me.topchetoeu.jscript.interop.Native; +import me.topchetoeu.jscript.interop.NativeConstructor; + +public class NumberPolyfill { + @Native public static final double EPSILON = java.lang.Math.ulp(1.0); + @Native public static final double MAX_SAFE_INTEGER = 9007199254740991.; + @Native public static final double MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER; + // lmao big number go brrr + @Native public static final double MAX_VALUE = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.; + @Native public static final double MIN_VALUE = -MAX_VALUE; + @Native public static final double NaN = 0. / 0; + @Native public static final double NEGATIVE_INFINITY = -1. / 0; + @Native public static final double POSITIVE_INFINITY = 1. / 0; + + public final double value; + + @NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) throws InterruptedException { + val = Values.toNumber(ctx, val); + if (thisArg instanceof ObjectValue) return new NumberPolyfill((double)val); + else return val; + } + @Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException { + return Values.toString(ctx, Values.toNumber(ctx, thisArg)); + } + @Native(thisArg = true) public static double valueOf(Context ctx, Object thisArg) throws InterruptedException { + if (thisArg instanceof NumberPolyfill) return ((NumberPolyfill)thisArg).value; + else return Values.toNumber(ctx, thisArg); + } + + public NumberPolyfill(double val) { + this.value = val; + } +}