Core library reprogramming #5

Merged
TopchetoEU merged 23 commits from TopchetoEU/corelib-reprogramming into master 2023-10-04 05:50:26 +00:00
5 changed files with 57 additions and 10 deletions
Showing only changes of commit f21cdc831c - Show all commits

View File

@ -10,6 +10,7 @@ interface Internals {
array: ArrayConstructor; array: ArrayConstructor;
promise: PromiseConstructor; promise: PromiseConstructor;
bool: BooleanConstructor; bool: BooleanConstructor;
number: NumberConstructor;
markSpecial(...funcs: Function[]): void; markSpecial(...funcs: Function[]): void;
getEnv(func: Function): Environment | undefined; getEnv(func: Function): Environment | undefined;
@ -48,11 +49,12 @@ try {
var Array = env.global.Array = internals.array; var Array = env.global.Array = internals.array;
var Promise = env.global.Promise = internals.promise; var Promise = env.global.Promise = internals.promise;
var Boolean = env.global.Boolean = internals.bool; var Boolean = env.global.Boolean = internals.bool;
var Number = env.global.Number = internals.number;
env.setProto('object', Object.prototype); env.setProto('object', Object.prototype);
env.setProto('function', Function.prototype); env.setProto('function', Function.prototype);
env.setProto('array', Array.prototype); env.setProto('array', Array.prototype);
env.setProto('bool', Boolean.prototype); env.setProto('number', Number.prototype);
(Object.prototype as any).__proto__ = null; (Object.prototype as any).__proto__ = null;
@ -62,7 +64,7 @@ try {
run('values/symbol'); run('values/symbol');
run('values/errors'); run('values/errors');
run('values/string'); run('values/string');
run('values/number'); // run('values/number');
run('map'); run('map');
run('set'); run('set');
run('regex'); run('regex');

View File

@ -117,8 +117,8 @@ public class Values {
public static double toNumber(Context ctx, Object obj) throws InterruptedException { public static double toNumber(Context ctx, Object obj) throws InterruptedException {
var val = toPrimitive(ctx, obj, ConvertHint.VALUEOF); var val = toPrimitive(ctx, obj, ConvertHint.VALUEOF);
if (val instanceof Number) return number(obj); if (val instanceof Number) return number(val);
if (val instanceof Boolean) return ((Boolean)obj) ? 1 : 0; if (val instanceof Boolean) return ((Boolean)val) ? 1 : 0;
if (val instanceof String) { if (val instanceof String) {
try { try {
return Double.parseDouble((String)val); return Double.parseDouble((String)val);
@ -134,7 +134,7 @@ public class Values {
if (val == NULL) return "null"; if (val == NULL) return "null";
if (val instanceof Number) { if (val instanceof Number) {
var d = number(obj); var d = number(val);
if (d == Double.NEGATIVE_INFINITY) return "-Infinity"; if (d == Double.NEGATIVE_INFINITY) return "-Infinity";
if (d == Double.POSITIVE_INFINITY) return "Infinity"; if (d == Double.POSITIVE_INFINITY) return "Infinity";
if (Double.isNaN(d)) return "NaN"; if (Double.isNaN(d)) return "NaN";

View File

@ -7,12 +7,14 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
public class BooleanPolyfill { 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) { @NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) {
val = Values.toBoolean(val); val = Values.toBoolean(val);
if (thisArg instanceof ObjectValue) { if (thisArg instanceof ObjectValue) return (boolean)val ? TRUE : FALSE;
((ObjectValue)thisArg).defineProperty(ctx, "value", val);
return null;
}
else return val; else return val;
} }
@Native(thisArg = true) public static String toString(Context ctx, Object thisArg) { @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) { @Native(thisArg = true) public static boolean valueOf(Context ctx, Object thisArg) {
return Values.toBoolean(thisArg); return Values.toBoolean(thisArg);
} }
public BooleanPolyfill(boolean val) {
this.value = val;
}
} }

View File

@ -14,7 +14,7 @@ import me.topchetoeu.jscript.interop.Native;
public class Internals { public class Internals {
public final Environment targetEnv; 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) { @Native public void markSpecial(FunctionValue ...funcs) {
for (var func : funcs) { for (var func : funcs) {
@ -158,5 +158,6 @@ public class Internals {
this.promise = targetEnv.wrappersProvider.getConstr(PromisePolyfill.class); this.promise = targetEnv.wrappersProvider.getConstr(PromisePolyfill.class);
this.array = targetEnv.wrappersProvider.getConstr(ArrayPolyfill.class); this.array = targetEnv.wrappersProvider.getConstr(ArrayPolyfill.class);
this.bool = targetEnv.wrappersProvider.getConstr(BooleanPolyfill.class); this.bool = targetEnv.wrappersProvider.getConstr(BooleanPolyfill.class);
this.number = targetEnv.wrappersProvider.getConstr(NumberPolyfill.class);
} }
} }

View File

@ -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;
}
}