refactor: rename polyfills to libs

This commit is contained in:
TopchetoEU 2023-10-06 11:57:26 +03:00
parent 952a4d631d
commit a17ec737b7
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
24 changed files with 603 additions and 748 deletions

View File

@ -15,7 +15,7 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.events.Observer; import me.topchetoeu.jscript.events.Observer;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.SyntaxException; import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.polyfills.Internals; import me.topchetoeu.jscript.lib.Internals;
public class Main { public class Main {
static Thread task; static Thread task;

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.Iterator; import java.util.Iterator;
import java.util.Stack; import java.util.Stack;
@ -16,7 +16,7 @@ import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
import me.topchetoeu.jscript.interop.NativeSetter; import me.topchetoeu.jscript.interop.NativeSetter;
public class ArrayPolyfill { public class ArrayLib {
@NativeGetter(thisArg = true) public static int length(Context ctx, ArrayValue thisArg) throws InterruptedException { @NativeGetter(thisArg = true) public static int length(Context ctx, ArrayValue thisArg) throws InterruptedException {
return thisArg.size(); return thisArg.size();
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.frame.CodeFrame; import me.topchetoeu.jscript.engine.frame.CodeFrame;
@ -8,11 +8,11 @@ import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.NativeFunction; import me.topchetoeu.jscript.engine.values.NativeFunction;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
public class AsyncFunctionPolyfill extends FunctionValue { public class AsyncFunctionLib extends FunctionValue {
public final FunctionValue factory; public final FunctionValue factory;
public static class AsyncHelper { public static class AsyncHelper {
public PromisePolyfill promise = new PromisePolyfill(); public PromiseLib promise = new PromiseLib();
public CodeFrame frame; public CodeFrame frame;
private boolean awaiting = false; private boolean awaiting = false;
@ -40,7 +40,7 @@ public class AsyncFunctionPolyfill extends FunctionValue {
ctx.message.popFrame(frame); ctx.message.popFrame(frame);
if (awaiting) { if (awaiting) {
PromisePolyfill.then(ctx, frame.pop(), new NativeFunction(this::fulfill), new NativeFunction(this::reject)); PromiseLib.then(ctx, frame.pop(), new NativeFunction(this::fulfill), new NativeFunction(this::reject));
} }
} }
@ -69,7 +69,7 @@ public class AsyncFunctionPolyfill extends FunctionValue {
return handler.promise; return handler.promise;
} }
public AsyncFunctionPolyfill(FunctionValue factory) { public AsyncFunctionLib(FunctionValue factory) {
super(factory.name, factory.length); super(factory.name, factory.length);
this.factory = factory; this.factory = factory;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.Map; import java.util.Map;
@ -12,14 +12,14 @@ import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
public class AsyncGeneratorPolyfill extends FunctionValue { public class AsyncGeneratorLib extends FunctionValue {
public final FunctionValue factory; public final FunctionValue factory;
public static class AsyncGenerator { public static class AsyncGenerator {
@Native("@@Symbol.typeName") public final String name = "AsyncGenerator"; @Native("@@Symbol.typeName") public final String name = "AsyncGenerator";
private int state = 0; private int state = 0;
private boolean done = false; private boolean done = false;
private PromisePolyfill currPromise; private PromiseLib currPromise;
public CodeFrame frame; public CodeFrame frame;
private void next(Context ctx, Object inducedValue, Object inducedReturn, Object inducedError) throws InterruptedException { private void next(Context ctx, Object inducedValue, Object inducedReturn, Object inducedError) throws InterruptedException {
@ -58,7 +58,7 @@ public class AsyncGeneratorPolyfill extends FunctionValue {
ctx.message.popFrame(frame); ctx.message.popFrame(frame);
if (state == 1) { if (state == 1) {
PromisePolyfill.then(ctx, frame.pop(), new NativeFunction(this::fulfill), new NativeFunction(this::reject)); PromiseLib.then(ctx, frame.pop(), new NativeFunction(this::fulfill), new NativeFunction(this::reject));
} }
else if (state == 2) { else if (state == 2) {
var obj = new ObjectValue(); var obj = new ObjectValue();
@ -85,21 +85,21 @@ public class AsyncGeneratorPolyfill extends FunctionValue {
} }
@Native @Native
public PromisePolyfill next(Context ctx, Object ...args) throws InterruptedException { public PromiseLib next(Context ctx, Object ...args) throws InterruptedException {
this.currPromise = new PromisePolyfill(); this.currPromise = new PromiseLib();
if (args.length == 0) next(ctx, Runners.NO_RETURN, Runners.NO_RETURN, Runners.NO_RETURN); if (args.length == 0) next(ctx, Runners.NO_RETURN, Runners.NO_RETURN, Runners.NO_RETURN);
else next(ctx, args[0], Runners.NO_RETURN, Runners.NO_RETURN); else next(ctx, args[0], Runners.NO_RETURN, Runners.NO_RETURN);
return this.currPromise; return this.currPromise;
} }
@Native("throw") @Native("throw")
public PromisePolyfill _throw(Context ctx, Object error) throws InterruptedException { public PromiseLib _throw(Context ctx, Object error) throws InterruptedException {
this.currPromise = new PromisePolyfill(); this.currPromise = new PromiseLib();
next(ctx, Runners.NO_RETURN, Runners.NO_RETURN, error); next(ctx, Runners.NO_RETURN, Runners.NO_RETURN, error);
return this.currPromise; return this.currPromise;
} }
@Native("return") @Native("return")
public PromisePolyfill _return(Context ctx, Object value) throws InterruptedException { public PromiseLib _return(Context ctx, Object value) throws InterruptedException {
this.currPromise = new PromisePolyfill(); this.currPromise = new PromiseLib();
next(ctx, Runners.NO_RETURN, value, Runners.NO_RETURN); next(ctx, Runners.NO_RETURN, value, Runners.NO_RETURN);
return this.currPromise; return this.currPromise;
} }
@ -127,7 +127,7 @@ public class AsyncGeneratorPolyfill extends FunctionValue {
return handler; return handler;
} }
public AsyncGeneratorPolyfill(FunctionValue factory) { public AsyncGeneratorLib(FunctionValue factory) {
super(factory.name, factory.length); super(factory.name, factory.length);
this.factory = factory; this.factory = factory;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -9,9 +9,9 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class BooleanPolyfill { public class BooleanLib {
public static final BooleanPolyfill TRUE = new BooleanPolyfill(true); public static final BooleanLib TRUE = new BooleanLib(true);
public static final BooleanPolyfill FALSE = new BooleanPolyfill(false); public static final BooleanLib FALSE = new BooleanLib(false);
public final boolean value; public final boolean value;
@ -27,7 +27,7 @@ public class BooleanPolyfill {
return Values.toBoolean(thisArg); return Values.toBoolean(thisArg);
} }
public BooleanPolyfill(boolean val) { public BooleanLib(boolean val) {
this.value = val; this.value = val;
} }
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) { @NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {

View File

@ -1,302 +1,269 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.Calendar; import java.util.Calendar;
import java.util.TimeZone; import java.util.TimeZone;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.Native;
public class DateLib {
public class Date { private Calendar normal;
private Calendar normal; private Calendar utc;
private Calendar utc;
private void updateUTC() {
public Date(long timestamp) { if (utc == null || normal == null) return;
normal = Calendar.getInstance(); utc.setTimeInMillis(normal.getTimeInMillis());
utc = Calendar.getInstance(); }
normal.setTimeInMillis(timestamp); private void updateNormal() {
utc.setTimeZone(TimeZone.getTimeZone("UTC")); if (utc == null || normal == null) return;
utc.setTimeInMillis(timestamp); normal.setTimeInMillis(utc.getTimeInMillis());
} }
private void invalidate() {
@Native normal = utc = null;
public Date() { }
this(new java.util.Date().getTime());
} @Native
public static double now() {
private void updateUTC() { return new DateLib().getTime();
if (utc == null || normal == null) return; }
utc.setTimeInMillis(normal.getTimeInMillis());
} @Native
private void updateNormal() { public double getYear() {
if (utc == null || normal == null) return; if (normal == null) return Double.NaN;
normal.setTimeInMillis(utc.getTimeInMillis()); return normal.get(Calendar.YEAR) - 1900;
} }
private void invalidate() { @Native
normal = utc = null; public double setYear(Context ctx, double real) throws InterruptedException {
} if (real >= 0 && real <= 99) real = real + 1900;
if (Double.isNaN(real)) invalidate();
@Native else normal.set(Calendar.YEAR, (int)real);
public static double now() { updateUTC();
return new Date().getTime(); return getTime();
} }
@Native @Native
public double getYear() { public double getFullYear() {
if (normal == null) return Double.NaN; if (normal == null) return Double.NaN;
return normal.get(Calendar.YEAR) - 1900; return normal.get(Calendar.YEAR);
} }
@Native @Native
public double setYear(Context ctx, Object val) throws InterruptedException { public double getMonth() {
var real = Values.toNumber(ctx, val); if (normal == null) return Double.NaN;
if (real >= 0 && real <= 99) real = real + 1900; return normal.get(Calendar.MONTH);
if (Double.isNaN(real)) invalidate(); }
else normal.set(Calendar.YEAR, (int)real); @Native
updateUTC(); public double getDate() {
return getTime(); if (normal == null) return Double.NaN;
} return normal.get(Calendar.DAY_OF_MONTH);
}
@Native @Native
public double getFullYear() { public double getDay() {
if (normal == null) return Double.NaN; if (normal == null) return Double.NaN;
return normal.get(Calendar.YEAR); return normal.get(Calendar.DAY_OF_WEEK);
} }
@Native @Native
public double getMonth() { public double getHours() {
if (normal == null) return Double.NaN; if (normal == null) return Double.NaN;
return normal.get(Calendar.MONTH); return normal.get(Calendar.HOUR_OF_DAY);
} }
@Native @Native
public double getDate() { public double getMinutes() {
if (normal == null) return Double.NaN; if (normal == null) return Double.NaN;
return normal.get(Calendar.DAY_OF_MONTH); return normal.get(Calendar.MINUTE);
} }
@Native @Native
public double getDay() { public double getSeconds() {
if (normal == null) return Double.NaN; if (normal == null) return Double.NaN;
return normal.get(Calendar.DAY_OF_WEEK); return normal.get(Calendar.SECOND);
} }
@Native @Native
public double getHours() { public double getMilliseconds() {
if (normal == null) return Double.NaN; if (normal == null) return Double.NaN;
return normal.get(Calendar.HOUR_OF_DAY); return normal.get(Calendar.MILLISECOND);
} }
@Native
public double getMinutes() { @Native
if (normal == null) return Double.NaN; public double getUTCFullYear() {
return normal.get(Calendar.MINUTE); if (utc == null) return Double.NaN;
} return utc.get(Calendar.YEAR);
@Native }
public double getSeconds() { @Native
if (normal == null) return Double.NaN; public double getUTCMonth() {
return normal.get(Calendar.SECOND); if (utc == null) return Double.NaN;
} return utc.get(Calendar.MONTH);
@Native }
public double getMilliseconds() { @Native
if (normal == null) return Double.NaN; public double getUTCDate() {
return normal.get(Calendar.MILLISECOND); if (utc == null) return Double.NaN;
} return utc.get(Calendar.DAY_OF_MONTH);
}
@Native @Native
public double getUTCFullYear() { public double getUTCDay() {
if (utc == null) return Double.NaN; if (utc == null) return Double.NaN;
return utc.get(Calendar.YEAR); return utc.get(Calendar.DAY_OF_WEEK);
} }
@Native @Native
public double getUTCMonth() { public double getUTCHours() {
if (utc == null) return Double.NaN; if (utc == null) return Double.NaN;
return utc.get(Calendar.MONTH); return utc.get(Calendar.HOUR_OF_DAY);
} }
@Native @Native
public double getUTCDate() { public double getUTCMinutes() {
if (utc == null) return Double.NaN; if (utc == null) return Double.NaN;
return utc.get(Calendar.DAY_OF_MONTH); return utc.get(Calendar.MINUTE);
} }
@Native @Native
public double getUTCDay() { public double getUTCSeconds() {
if (utc == null) return Double.NaN; if (utc == null) return Double.NaN;
return utc.get(Calendar.DAY_OF_WEEK); return utc.get(Calendar.SECOND);
} }
@Native @Native
public double getUTCHours() { public double getUTCMilliseconds() {
if (utc == null) return Double.NaN; if (utc == null) return Double.NaN;
return utc.get(Calendar.HOUR_OF_DAY); return utc.get(Calendar.MILLISECOND);
} }
@Native
public double getUTCMinutes() { @Native
if (utc == null) return Double.NaN; public double setFullYear(Context ctx, double real) throws InterruptedException {
return utc.get(Calendar.MINUTE); if (Double.isNaN(real)) invalidate();
} else normal.set(Calendar.YEAR, (int)real);
@Native updateUTC();
public double getUTCSeconds() { return getTime();
if (utc == null) return Double.NaN; }
return utc.get(Calendar.SECOND); @Native
} public double setMonth(Context ctx, double real) throws InterruptedException {
@Native if (Double.isNaN(real)) invalidate();
public double getUTCMilliseconds() { else normal.set(Calendar.MONTH, (int)real);
if (utc == null) return Double.NaN; updateUTC();
return utc.get(Calendar.MILLISECOND); return getTime();
} }
@Native
@Native public double setDate(Context ctx, double real) throws InterruptedException {
public double setFullYear(Context ctx, Object val) throws InterruptedException { if (Double.isNaN(real)) invalidate();
var real = Values.toNumber(ctx, val); else normal.set(Calendar.DAY_OF_MONTH, (int)real);
if (Double.isNaN(real)) invalidate(); updateUTC();
else normal.set(Calendar.YEAR, (int)real); return getTime();
updateUTC(); }
return getTime(); @Native
} public double setDay(Context ctx, double real) throws InterruptedException {
@Native if (Double.isNaN(real)) invalidate();
public double setMonth(Context ctx, Object val) throws InterruptedException { else normal.set(Calendar.DAY_OF_WEEK, (int)real);
var real = Values.toNumber(ctx, val); updateUTC();
if (Double.isNaN(real)) invalidate(); return getTime();
else normal.set(Calendar.MONTH, (int)real); }
updateUTC(); @Native
return getTime(); public double setHours(Context ctx, double real) throws InterruptedException {
} if (Double.isNaN(real)) invalidate();
@Native else normal.set(Calendar.HOUR_OF_DAY, (int)real);
public double setDate(Context ctx, Object val) throws InterruptedException { updateUTC();
var real = Values.toNumber(ctx, val); return getTime();
if (Double.isNaN(real)) invalidate(); }
else normal.set(Calendar.DAY_OF_MONTH, (int)real); @Native
updateUTC(); public double setMinutes(Context ctx, double real) throws InterruptedException {
return getTime(); if (Double.isNaN(real)) invalidate();
} else normal.set(Calendar.MINUTE, (int)real);
@Native updateUTC();
public double setDay(Context ctx, Object val) throws InterruptedException { return getTime();
var real = Values.toNumber(ctx, val); }
if (Double.isNaN(real)) invalidate(); @Native
else normal.set(Calendar.DAY_OF_WEEK, (int)real); public double setSeconds(Context ctx, double real) throws InterruptedException {
updateUTC(); if (Double.isNaN(real)) invalidate();
return getTime(); else normal.set(Calendar.SECOND, (int)real);
} updateUTC();
@Native return getTime();
public double setHours(Context ctx, Object val) throws InterruptedException { }
var real = Values.toNumber(ctx, val); @Native
if (Double.isNaN(real)) invalidate(); public double setMilliseconds(Context ctx, double real) throws InterruptedException {
else normal.set(Calendar.HOUR_OF_DAY, (int)real); if (Double.isNaN(real)) invalidate();
updateUTC(); else normal.set(Calendar.MILLISECOND, (int)real);
return getTime(); updateUTC();
} return getTime();
@Native }
public double setMinutes(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val); @Native
if (Double.isNaN(real)) invalidate(); public double setUTCFullYear(Context ctx, double real) throws InterruptedException {
else normal.set(Calendar.MINUTE, (int)real); if (Double.isNaN(real)) invalidate();
updateUTC(); else utc.set(Calendar.YEAR, (int)real);
return getTime(); updateNormal();
} return getTime();
@Native }
public double setSeconds(Context ctx, Object val) throws InterruptedException { @Native
var real = Values.toNumber(ctx, val); public double setUTCMonth(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate(); if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.SECOND, (int)real); else utc.set(Calendar.MONTH, (int)real);
updateUTC(); updateNormal();
return getTime(); return getTime();
} }
@Native @Native
public double setMilliseconds(Context ctx, Object val) throws InterruptedException { public double setUTCDate(Context ctx, double real) throws InterruptedException {
var real = Values.toNumber(ctx, val); if (Double.isNaN(real)) invalidate();
if (Double.isNaN(real)) invalidate(); else utc.set(Calendar.DAY_OF_MONTH, (int)real);
else normal.set(Calendar.MILLISECOND, (int)real); updateNormal();
updateUTC(); return getTime();
return getTime(); }
} @Native
public double setUTCDay(Context ctx, double real) throws InterruptedException {
@Native if (Double.isNaN(real)) invalidate();
public double setUTCFullYear(Context ctx, Object val) throws InterruptedException { else utc.set(Calendar.DAY_OF_WEEK, (int)real);
var real = Values.toNumber(ctx, val); updateNormal();
if (Double.isNaN(real)) invalidate(); return getTime();
else utc.set(Calendar.YEAR, (int)real); }
updateNormal(); @Native
return getTime(); public double setUTCHours(Context ctx, double real) throws InterruptedException {
} if (Double.isNaN(real)) invalidate();
@Native else utc.set(Calendar.HOUR_OF_DAY, (int)real);
public double setUTCMonth(Context ctx, Object val) throws InterruptedException { updateNormal();
var real = Values.toNumber(ctx, val); return getTime();
if (Double.isNaN(real)) invalidate(); }
else utc.set(Calendar.MONTH, (int)real); @Native
updateNormal(); public double setUTCMinutes(Context ctx, double real) throws InterruptedException {
return getTime(); if (Double.isNaN(real)) invalidate();
} else utc.set(Calendar.MINUTE, (int)real);
@Native updateNormal();
public double setUTCDate(Context ctx, Object val) throws InterruptedException { return getTime();
var real = Values.toNumber(ctx, val); }
if (Double.isNaN(real)) invalidate(); @Native
else utc.set(Calendar.DAY_OF_MONTH, (int)real); public double setUTCSeconds(Context ctx, double real) throws InterruptedException {
updateNormal(); if (Double.isNaN(real)) invalidate();
return getTime(); else utc.set(Calendar.SECOND, (int)real);
} updateNormal();
@Native return getTime();
public double setUTCDay(Context ctx, Object val) throws InterruptedException { }
var real = Values.toNumber(ctx, val); @Native
if (Double.isNaN(real)) invalidate(); public double setUTCMilliseconds(Context ctx, double real) throws InterruptedException {
else utc.set(Calendar.DAY_OF_WEEK, (int)real); if (Double.isNaN(real)) invalidate();
updateNormal(); else utc.set(Calendar.MILLISECOND, (int)real);
return getTime(); updateNormal();
} return getTime();
@Native }
public double setUTCHours(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val); @Native
if (Double.isNaN(real)) invalidate(); public double getTime() {
else utc.set(Calendar.HOUR_OF_DAY, (int)real); if (utc == null) return Double.NaN;
updateNormal(); return utc.getTimeInMillis();
return getTime(); }
} @Native
@Native public double getTimezoneOffset() {
public double setUTCMinutes(Context ctx, Object val) throws InterruptedException { if (normal == null) return Double.NaN;
var real = Values.toNumber(ctx, val); return normal.getTimeZone().getRawOffset() / 60000;
if (Double.isNaN(real)) invalidate(); }
else utc.set(Calendar.MINUTE, (int)real);
updateNormal(); @Native
return getTime(); public double valueOf() {
} if (normal == null) return Double.NaN;
@Native else return normal.getTimeInMillis();
public double setUTCSeconds(Context ctx, Object val) throws InterruptedException { }
var real = Values.toNumber(ctx, val);
if (Double.isNaN(real)) invalidate(); public DateLib(long timestamp) {
else utc.set(Calendar.SECOND, (int)real); normal = Calendar.getInstance();
updateNormal(); utc = Calendar.getInstance();
return getTime(); normal.setTimeInMillis(timestamp);
} utc.setTimeZone(TimeZone.getTimeZone("UTC"));
@Native utc.setTimeInMillis(timestamp);
public double setUTCMilliseconds(Context ctx, Object val) throws InterruptedException { }
var real = Values.toNumber(ctx, val);
if (Double.isNaN(real)) invalidate(); @Native
else utc.set(Calendar.MILLISECOND, (int)real); public DateLib() {
updateNormal(); this(new java.util.Date().getTime());
return getTime(); }
} }
@Native
public double getTime() {
if (utc == null) return Double.NaN;
return utc.getTimeInMillis();
}
@Native
public double getTimezoneOffset() {
if (normal == null) return Double.NaN;
return normal.getTimeZone().getRawOffset() / 60000;
}
@Native
public double valueOf() {
if (normal == null) return Double.NaN;
else return normal.getTimeInMillis();
}
// I'm not dealing with locales rn
// @Native
// public String toTimeString() {
// if (normal == null || utc == null) return "Invalid date";
// var res = "";
// }
// @Native @Override
// public String toString() {
// if (normal == null || utc == null) return "Invalid date";
// else return DateFormat..format(normal.getTime());
// }
}

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -10,7 +10,7 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class ErrorPolyfill { public class ErrorLib {
private static String toString(Context ctx, Object cause, Object name, Object message, ArrayValue stack) throws InterruptedException { private static String toString(Context ctx, Object cause, Object name, Object message, ArrayValue stack) throws InterruptedException {
if (name == null) name = ""; if (name == null) name = "";
else name = Values.toString(ctx, name).trim(); else name = Values.toString(ctx, name).trim();

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -11,7 +11,7 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class FunctionPolyfill { public class FunctionLib {
@Native(thisArg = true) public static Object apply(Context ctx, FunctionValue func, Object thisArg, ArrayValue args) throws InterruptedException { @Native(thisArg = true) public static Object apply(Context ctx, FunctionValue func, Object thisArg, ArrayValue args) throws InterruptedException {
return func.call(ctx, thisArg, args.toArray()); return func.call(ctx, thisArg, args.toArray());
} }
@ -41,13 +41,13 @@ public class FunctionPolyfill {
} }
@Native public static FunctionValue async(FunctionValue func) { @Native public static FunctionValue async(FunctionValue func) {
return new AsyncFunctionPolyfill(func); return new AsyncFunctionLib(func);
} }
@Native public static FunctionValue asyncGenerator(FunctionValue func) { @Native public static FunctionValue asyncGenerator(FunctionValue func) {
return new AsyncGeneratorPolyfill(func); return new AsyncGeneratorLib(func);
} }
@Native public static FunctionValue generator(FunctionValue func) { @Native public static FunctionValue generator(FunctionValue func) {
return new GeneratorPolyfill(func); return new GeneratorLib(func);
} }
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) { @NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.frame.CodeFrame; import me.topchetoeu.jscript.engine.frame.CodeFrame;
@ -10,7 +10,7 @@ import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
public class GeneratorPolyfill extends FunctionValue { public class GeneratorLib extends FunctionValue {
public final FunctionValue factory; public final FunctionValue factory;
public static class Generator { public static class Generator {
@ -94,7 +94,7 @@ public class GeneratorPolyfill extends FunctionValue {
return handler; return handler;
} }
public GeneratorPolyfill(FunctionValue factory) { public GeneratorLib(FunctionValue factory) {
super(factory.name, factory.length); super(factory.name, factory.length);
this.factory = factory; this.factory = factory;
} }

View File

@ -1,127 +1,127 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.HashMap; import java.util.HashMap;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.DataKey; import me.topchetoeu.jscript.engine.DataKey;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
import me.topchetoeu.jscript.engine.scope.GlobalScope; import me.topchetoeu.jscript.engine.scope.GlobalScope;
import me.topchetoeu.jscript.engine.values.FunctionValue; import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeWrapperProvider; import me.topchetoeu.jscript.interop.NativeWrapperProvider;
public class Internals { public class Internals {
private static final DataKey<HashMap<Integer, Thread>> THREADS = new DataKey<>(); private static final DataKey<HashMap<Integer, Thread>> THREADS = new DataKey<>();
private static final DataKey<Integer> I = new DataKey<>(); private static final DataKey<Integer> I = new DataKey<>();
@Native public static FunctionValue bind(FunctionValue func, Object thisArg) throws InterruptedException { @Native public static FunctionValue bind(FunctionValue func, Object thisArg) throws InterruptedException {
return FunctionPolyfill.bind(func, thisArg); return FunctionLib.bind(func, thisArg);
} }
@Native public static void log(Context ctx, Object ...args) throws InterruptedException { @Native public static void log(Context ctx, Object ...args) throws InterruptedException {
for (var arg : args) { for (var arg : args) {
Values.printValue(ctx, arg); Values.printValue(ctx, arg);
} }
System.out.println(); System.out.println();
} }
@Native public static int setTimeout(Context ctx, FunctionValue func, int delay, Object ...args) { @Native public static int setTimeout(Context ctx, FunctionValue func, int delay, Object ...args) {
var thread = new Thread(() -> { var thread = new Thread(() -> {
var ms = (long)delay; var ms = (long)delay;
var ns = (int)((delay - ms) * 10000000); var ns = (int)((delay - ms) * 10000000);
try { try {
Thread.sleep(ms, ns); Thread.sleep(ms, ns);
} }
catch (InterruptedException e) { return; } catch (InterruptedException e) { return; }
ctx.message.engine.pushMsg(false, ctx.message, func, null, args); ctx.message.engine.pushMsg(false, ctx.message, func, null, args);
}); });
thread.start(); thread.start();
int i = ctx.env.data.increase(I, 1, 0); int i = ctx.env.data.increase(I, 1, 0);
var threads = ctx.env.data.add(THREADS, new HashMap<>()); var threads = ctx.env.data.add(THREADS, new HashMap<>());
threads.put(++i, thread); threads.put(++i, thread);
return i; return i;
} }
@Native public static int setInterval(Context ctx, FunctionValue func, int delay, Object ...args) { @Native public static int setInterval(Context ctx, FunctionValue func, int delay, Object ...args) {
var thread = new Thread(() -> { var thread = new Thread(() -> {
var ms = (long)delay; var ms = (long)delay;
var ns = (int)((delay - ms) * 10000000); var ns = (int)((delay - ms) * 10000000);
while (true) { while (true) {
try { try {
Thread.sleep(ms, ns); Thread.sleep(ms, ns);
} }
catch (InterruptedException e) { return; } catch (InterruptedException e) { return; }
ctx.message.engine.pushMsg(false, ctx.message, func, null, args); ctx.message.engine.pushMsg(false, ctx.message, func, null, args);
} }
}); });
thread.start(); thread.start();
int i = ctx.env.data.increase(I, 1, 0); int i = ctx.env.data.increase(I, 1, 0);
var threads = ctx.env.data.add(THREADS, new HashMap<>()); var threads = ctx.env.data.add(THREADS, new HashMap<>());
threads.put(++i, thread); threads.put(++i, thread);
return i; return i;
} }
@Native public static void clearTimeout(Context ctx, int i) { @Native public static void clearTimeout(Context ctx, int i) {
var threads = ctx.env.data.add(THREADS, new HashMap<>()); var threads = ctx.env.data.add(THREADS, new HashMap<>());
var thread = threads.remove(i); var thread = threads.remove(i);
if (thread != null) thread.interrupt(); if (thread != null) thread.interrupt();
} }
@Native public static void clearInterval(Context ctx, int i) { @Native public static void clearInterval(Context ctx, int i) {
clearTimeout(ctx, i); clearTimeout(ctx, i);
} }
@Native public static double parseInt(Context ctx, String val) throws InterruptedException { @Native public static double parseInt(Context ctx, String val) throws InterruptedException {
return NumberPolyfill.parseInt(ctx, val); return NumberLib.parseInt(ctx, val);
} }
@Native public static double parseFloat(Context ctx, String val) throws InterruptedException { @Native public static double parseFloat(Context ctx, String val) throws InterruptedException {
return NumberPolyfill.parseFloat(ctx, val); return NumberLib.parseFloat(ctx, val);
} }
public void apply(Environment env) { public void apply(Environment env) {
var wp = env.wrappersProvider; var wp = env.wrappersProvider;
var glob = env.global = new GlobalScope(NativeWrapperProvider.makeNamespace(env, Internals.class)); var glob = env.global = new GlobalScope(NativeWrapperProvider.makeNamespace(env, Internals.class));
glob.define(null, "Object", false, wp.getConstr(ObjectPolyfill.class)); glob.define(null, "Object", false, wp.getConstr(ObjectLib.class));
glob.define(null, "Function", false, wp.getConstr(FunctionPolyfill.class)); glob.define(null, "Function", false, wp.getConstr(FunctionLib.class));
glob.define(null, "Array", false, wp.getConstr(ArrayPolyfill.class)); glob.define(null, "Array", false, wp.getConstr(ArrayLib.class));
glob.define(null, "Boolean", false, wp.getConstr(BooleanPolyfill.class)); glob.define(null, "Boolean", false, wp.getConstr(BooleanLib.class));
glob.define(null, "Number", false, wp.getConstr(NumberPolyfill.class)); glob.define(null, "Number", false, wp.getConstr(NumberLib.class));
glob.define(null, "String", false, wp.getConstr(StringPolyfill.class)); glob.define(null, "String", false, wp.getConstr(StringLib.class));
glob.define(null, "Symbol", false, wp.getConstr(SymbolPolyfill.class)); glob.define(null, "Symbol", false, wp.getConstr(SymbolLib.class));
glob.define(null, "Promise", false, wp.getConstr(PromisePolyfill.class)); glob.define(null, "Promise", false, wp.getConstr(PromiseLib.class));
glob.define(null, "RegExp", false, wp.getConstr(RegExpPolyfill.class)); glob.define(null, "RegExp", false, wp.getConstr(RegExpLib.class));
glob.define(null, "Map", false, wp.getConstr(MapPolyfill.class)); glob.define(null, "Map", false, wp.getConstr(MapLib.class));
glob.define(null, "Set", false, wp.getConstr(SetPolyfill.class)); glob.define(null, "Set", false, wp.getConstr(SetLib.class));
glob.define(null, "Error", false, wp.getConstr(ErrorPolyfill.class)); glob.define(null, "Error", false, wp.getConstr(ErrorLib.class));
glob.define(null, "SyntaxError", false, wp.getConstr(SyntaxErrorPolyfill.class)); glob.define(null, "SyntaxError", false, wp.getConstr(SyntaxErrorLib.class));
glob.define(null, "TypeError", false, wp.getConstr(TypeErrorPolyfill.class)); glob.define(null, "TypeError", false, wp.getConstr(TypeErrorLib.class));
glob.define(null, "RangeError", false, wp.getConstr(RangeErrorPolyfill.class)); glob.define(null, "RangeError", false, wp.getConstr(RangeErrorLib.class));
env.setProto("object", wp.getProto(ObjectPolyfill.class)); env.setProto("object", wp.getProto(ObjectLib.class));
env.setProto("function", wp.getProto(FunctionPolyfill.class)); env.setProto("function", wp.getProto(FunctionLib.class));
env.setProto("array", wp.getProto(ArrayPolyfill.class)); env.setProto("array", wp.getProto(ArrayLib.class));
env.setProto("bool", wp.getProto(BooleanPolyfill.class)); env.setProto("bool", wp.getProto(BooleanLib.class));
env.setProto("number", wp.getProto(NumberPolyfill.class)); env.setProto("number", wp.getProto(NumberLib.class));
env.setProto("string", wp.getProto(StringPolyfill.class)); env.setProto("string", wp.getProto(StringLib.class));
env.setProto("symbol", wp.getProto(SymbolPolyfill.class)); env.setProto("symbol", wp.getProto(SymbolLib.class));
env.setProto("error", wp.getProto(ErrorPolyfill.class)); env.setProto("error", wp.getProto(ErrorLib.class));
env.setProto("syntaxErr", wp.getProto(SyntaxErrorPolyfill.class)); env.setProto("syntaxErr", wp.getProto(SyntaxErrorLib.class));
env.setProto("typeErr", wp.getProto(TypeErrorPolyfill.class)); env.setProto("typeErr", wp.getProto(TypeErrorLib.class));
env.setProto("rangeErr", wp.getProto(RangeErrorPolyfill.class)); env.setProto("rangeErr", wp.getProto(RangeErrorLib.class));
wp.getProto(ObjectPolyfill.class).setPrototype(null, null); wp.getProto(ObjectLib.class).setPrototype(null, null);
System.out.println("Loaded polyfills!"); System.out.println("Loaded polyfills!");
} }
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.HashSet; import java.util.HashSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -14,12 +14,12 @@ import me.topchetoeu.jscript.json.JSONElement;
import me.topchetoeu.jscript.json.JSONList; import me.topchetoeu.jscript.json.JSONList;
import me.topchetoeu.jscript.json.JSONMap; import me.topchetoeu.jscript.json.JSONMap;
public class JSONPolyfill { public class JSONLib {
private static Object toJS(JSONElement val) { private static Object toJS(JSONElement val) {
if (val.isBoolean()) return val.bool(); if (val.isBoolean()) return val.bool();
if (val.isString()) return val.string(); if (val.isString()) return val.string();
if (val.isNumber()) return val.number(); if (val.isNumber()) return val.number();
if (val.isList()) return ArrayValue.of(null, val.list().stream().map(JSONPolyfill::toJS).collect(Collectors.toList())); if (val.isList()) return ArrayValue.of(null, val.list().stream().map(JSONLib::toJS).collect(Collectors.toList()));
if (val.isMap()) { if (val.isMap()) {
var res = new ObjectValue(); var res = new ObjectValue();
for (var el : val.map().entrySet()) { for (var el : val.map().entrySet()) {

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -12,7 +12,7 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter; import me.topchetoeu.jscript.interop.NativeGetter;
public class MapPolyfill { public class MapLib {
private LinkedHashMap<Object, Object> map = new LinkedHashMap<>(); private LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
@Native("@@Symbol.typeName") public final String name = "Map"; @Native("@@Symbol.typeName") public final String name = "Map";
@ -49,7 +49,7 @@ public class MapPolyfill {
@Native public Object get(Object key) { @Native public Object get(Object key) {
return map.get(key); return map.get(key);
} }
@Native public MapPolyfill set(Object key, Object val) { @Native public MapLib set(Object key, Object val) {
map.put(key, val); map.put(key, val);
return this; return this;
} }
@ -67,7 +67,7 @@ public class MapPolyfill {
for (var el : keys) func.call(ctx, thisArg, el, map.get(el), this); for (var el : keys) func.call(ctx, thisArg, el, map.get(el), this);
} }
@Native public MapPolyfill(Context ctx, Object iterable) throws InterruptedException { @Native public MapLib(Context ctx, Object iterable) throws InterruptedException {
for (var el : Values.toJavaIterable(ctx, iterable)) { for (var el : Values.toJavaIterable(ctx, iterable)) {
try { try {
set(Values.getMember(ctx, el, 0), Values.getMember(ctx, el, 1)); set(Values.getMember(ctx, el, 0), Values.getMember(ctx, el, 1));

View File

@ -0,0 +1,99 @@
package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.interop.Native;
public class MathLib {
@Native public static final double E = Math.E;
@Native public static final double PI = Math.PI;
@Native public static final double SQRT2 = Math.sqrt(2);
@Native public static final double SQRT1_2 = Math.sqrt(.5);
@Native public static final double LN2 = Math.log(2);
@Native public static final double LN10 = Math.log(10);
@Native public static final double LOG2E = Math.log(Math.E) / LN2;
@Native public static final double LOG10E = Math.log10(Math.E);
@Native public static double asin(double x) { return Math.asin(x); }
@Native public static double acos(double x) { return Math.acos(x); }
@Native public static double atan(double x) { return Math.atan(x); }
@Native public static double atan2(double y, double x) {
if (x == 0) {
if (y == 0) return Double.NaN;
return Math.signum(y) * Math.PI / 2;
}
else {
var val = Math.atan(y / x);
if (x > 0) return val;
else if (y < 0) return val - Math.PI;
else return val + Math.PI;
}
}
@Native public static double asinh(double x) { return Math.log(x + Math.sqrt(x * x + 1)); }
@Native public static double acosh(double x) { return Math.log(x + Math.sqrt(x * x - 1)); }
@Native public static double atanh(double x) {
if (x <= -1 || x >= 1) return Double.NaN;
return .5 * Math.log((1 + x) / (1 - x));
}
@Native public static double sin(double x) { return Math.sin(x); }
@Native public static double cos(double x) { return Math.cos(x); }
@Native public static double tan(double x) { return Math.tan(x); }
@Native public static double sinh(double x) { return Math.sinh(x); }
@Native public static double cosh(double x) { return Math.cosh(x); }
@Native public static double tanh(double x) { return Math.tanh(x); }
@Native public static double sqrt(double x) { return Math.sqrt(x); }
@Native public static double cbrt(double x) { return Math.cbrt(x); }
@Native public static double hypot(double ...vals) {
var res = 0.;
for (var el : vals) {
var val = el;
res += val * val;
}
return Math.sqrt(res);
}
@Native public static int imul(double a, double b) { return (int)a * (int)b; }
@Native public static double exp(double x) { return Math.exp(x); }
@Native public static double expm1(double x) { return Math.expm1(x); }
@Native public static double pow(double x, double y) { return Math.pow(x, y); }
@Native public static double log(double x) { return Math.log(x); }
@Native public static double log10(double x) { return Math.log10(x); }
@Native public static double log1p(double x) { return Math.log1p(x); }
@Native public static double log2(double x) { return Math.log(x) / LN2; }
@Native public static double ceil(double x) { return Math.ceil(x); }
@Native public static double floor(double x) { return Math.floor(x); }
@Native public static double round(double x) { return Math.round(x); }
@Native public static float fround(double x) { return (float)x; }
@Native public static double trunc(double x) { return Math.floor(Math.abs(x)) * Math.signum(x); }
@Native public static double abs(double x) { return Math.abs(x); }
@Native public static double max(double ...vals) {
var res = Double.NEGATIVE_INFINITY;
for (var el : vals) {
if (el > res) res = el;
}
return res;
}
@Native public static double min(double ...vals) {
var res = Double.POSITIVE_INFINITY;
for (var el : vals) {
if (el < res) res = el;
}
return res;
}
@Native public static double sign(double x) { return Math.signum(x); }
@Native public static double random() { return Math.random(); }
@Native public static int clz32(double x) { return Integer.numberOfLeadingZeros((int)x); }
}

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -9,7 +9,7 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class NumberPolyfill { public class NumberLib {
@Native public static final double EPSILON = java.lang.Math.ulp(1.0); @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 MAX_SAFE_INTEGER = 9007199254740991.;
@Native public static final double MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER; @Native public static final double MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER;
@ -38,18 +38,18 @@ public class NumberPolyfill {
@NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) throws InterruptedException { @NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) throws InterruptedException {
val = Values.toNumber(ctx, val); val = Values.toNumber(ctx, val);
if (thisArg instanceof ObjectValue) return new NumberPolyfill((double)val); if (thisArg instanceof ObjectValue) return new NumberLib((double)val);
else return val; else return val;
} }
@Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException { @Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException {
return Values.toString(ctx, Values.toNumber(ctx, thisArg)); return Values.toString(ctx, Values.toNumber(ctx, thisArg));
} }
@Native(thisArg = true) public static double valueOf(Context ctx, Object thisArg) throws InterruptedException { @Native(thisArg = true) public static double valueOf(Context ctx, Object thisArg) throws InterruptedException {
if (thisArg instanceof NumberPolyfill) return ((NumberPolyfill)thisArg).value; if (thisArg instanceof NumberLib) return ((NumberLib)thisArg).value;
else return Values.toNumber(ctx, thisArg); else return Values.toNumber(ctx, thisArg);
} }
public NumberPolyfill(double val) { public NumberLib(double val) {
this.value = val; this.value = val;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -13,7 +13,7 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class ObjectPolyfill { public class ObjectLib {
@Native public static ObjectValue assign(Context ctx, ObjectValue dst, Object... src) throws InterruptedException { @Native public static ObjectValue assign(Context ctx, ObjectValue dst, Object... src) throws InterruptedException {
for (var obj : src) { for (var obj : src) {
for (var key : Values.getMembers(ctx, obj, true, true)) { for (var key : Values.getMembers(ctx, obj, true, true)) {
@ -201,14 +201,14 @@ public class ObjectPolyfill {
return "[object " + name + "]"; return "[object " + name + "]";
} }
@Native(thisArg = true) public static boolean hasOwnProperty(Context ctx, Object thisArg, Object key) throws InterruptedException { @Native(thisArg = true) public static boolean hasOwnProperty(Context ctx, Object thisArg, Object key) throws InterruptedException {
return ObjectPolyfill.hasOwn(ctx, thisArg, Values.convert(ctx, key, String.class)); return ObjectLib.hasOwn(ctx, thisArg, Values.convert(ctx, key, String.class));
} }
@NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object arg) throws InterruptedException { @NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object arg) throws InterruptedException {
if (arg == null || arg == Values.NULL) return new ObjectValue(); if (arg == null || arg == Values.NULL) return new ObjectValue();
else if (arg instanceof Boolean) return BooleanPolyfill.constructor(ctx, thisArg, arg); else if (arg instanceof Boolean) return BooleanLib.constructor(ctx, thisArg, arg);
else if (arg instanceof Number) return NumberPolyfill.constructor(ctx, thisArg, arg); else if (arg instanceof Number) return NumberLib.constructor(ctx, thisArg, arg);
else if (arg instanceof String) return StringPolyfill.constructor(ctx, thisArg, arg); else if (arg instanceof String) return StringLib.constructor(ctx, thisArg, arg);
// else if (arg instanceof Symbol) return SymbolPolyfill.constructor(ctx, thisArg, arg); // else if (arg instanceof Symbol) return SymbolPolyfill.constructor(ctx, thisArg, arg);
else return arg; else return arg;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -18,7 +18,7 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class PromisePolyfill { public class PromiseLib {
private static class Handle { private static class Handle {
public final Context ctx; public final Context ctx;
public final FunctionValue fulfilled; public final FunctionValue fulfilled;
@ -32,24 +32,24 @@ public class PromisePolyfill {
} }
@Native("resolve") @Native("resolve")
public static PromisePolyfill ofResolved(Context ctx, Object val) throws InterruptedException { public static PromiseLib ofResolved(Context ctx, Object val) throws InterruptedException {
var res = new PromisePolyfill(); var res = new PromiseLib();
res.fulfill(ctx, val); res.fulfill(ctx, val);
return res; return res;
} }
@Native("reject") @Native("reject")
public static PromisePolyfill ofRejected(Context ctx, Object val) throws InterruptedException { public static PromiseLib ofRejected(Context ctx, Object val) throws InterruptedException {
var res = new PromisePolyfill(); var res = new PromiseLib();
res.reject(ctx, val); res.reject(ctx, val);
return res; return res;
} }
@Native public static PromisePolyfill any(Context ctx, Object _promises) throws InterruptedException { @Native public static PromiseLib any(Context ctx, Object _promises) throws InterruptedException {
if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array."); if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array.");
var promises = Values.array(_promises); var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue()); if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var n = new int[] { promises.size() }; var n = new int[] { promises.size() };
var res = new PromisePolyfill(); var res = new PromiseLib();
var errors = new ArrayValue(); var errors = new ArrayValue();
@ -69,11 +69,11 @@ public class PromisePolyfill {
return res; return res;
} }
@Native public static PromisePolyfill race(Context ctx, Object _promises) throws InterruptedException { @Native public static PromiseLib race(Context ctx, Object _promises) throws InterruptedException {
if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array."); if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array.");
var promises = Values.array(_promises); var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue()); if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var res = new PromisePolyfill(); var res = new PromiseLib();
for (var i = 0; i < promises.size(); i++) { for (var i = 0; i < promises.size(); i++) {
var val = promises.get(i); var val = promises.get(i);
@ -85,12 +85,12 @@ public class PromisePolyfill {
return res; return res;
} }
@Native public static PromisePolyfill all(Context ctx, Object _promises) throws InterruptedException { @Native public static PromiseLib all(Context ctx, Object _promises) throws InterruptedException {
if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array."); if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array.");
var promises = Values.array(_promises); var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue()); if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var n = new int[] { promises.size() }; var n = new int[] { promises.size() };
var res = new PromisePolyfill(); var res = new PromiseLib();
var result = new ArrayValue(); var result = new ArrayValue();
@ -112,12 +112,12 @@ public class PromisePolyfill {
return res; return res;
} }
@Native public static PromisePolyfill allSettled(Context ctx, Object _promises) throws InterruptedException { @Native public static PromiseLib allSettled(Context ctx, Object _promises) throws InterruptedException {
if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array."); if (!Values.isArray(_promises)) throw EngineException.ofType("Expected argument for any to be an array.");
var promises = Values.array(_promises); var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue()); if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var n = new int[] { promises.size() }; var n = new int[] { promises.size() };
var res = new PromisePolyfill(); var res = new PromiseLib();
var result = new ArrayValue(); var result = new ArrayValue();
@ -159,14 +159,14 @@ public class PromisePolyfill {
var onFulfill = _onFulfill instanceof FunctionValue ? ((FunctionValue)_onFulfill) : null; var onFulfill = _onFulfill instanceof FunctionValue ? ((FunctionValue)_onFulfill) : null;
var onReject = _onReject instanceof FunctionValue ? ((FunctionValue)_onReject) : null; var onReject = _onReject instanceof FunctionValue ? ((FunctionValue)_onReject) : null;
var res = new PromisePolyfill(); var res = new PromiseLib();
var fulfill = onFulfill == null ? new NativeFunction((_ctx, _thisArg, _args) -> _args.length > 0 ? _args[0] : null) : (FunctionValue)onFulfill; var fulfill = onFulfill == null ? new NativeFunction((_ctx, _thisArg, _args) -> _args.length > 0 ? _args[0] : null) : (FunctionValue)onFulfill;
var reject = onReject == null ? new NativeFunction((_ctx, _thisArg, _args) -> { var reject = onReject == null ? new NativeFunction((_ctx, _thisArg, _args) -> {
throw new EngineException(_args.length > 0 ? _args[0] : null); throw new EngineException(_args.length > 0 ? _args[0] : null);
}) : (FunctionValue)onReject; }) : (FunctionValue)onReject;
if (thisArg instanceof NativeWrapper && ((NativeWrapper)thisArg).wrapped instanceof PromisePolyfill) { if (thisArg instanceof NativeWrapper && ((NativeWrapper)thisArg).wrapped instanceof PromiseLib) {
thisArg = ((NativeWrapper)thisArg).wrapped; thisArg = ((NativeWrapper)thisArg).wrapped;
} }
@ -183,7 +183,7 @@ public class PromisePolyfill {
return null; return null;
}); });
if (thisArg instanceof PromisePolyfill) ((PromisePolyfill)thisArg).handle(ctx, fulfillHandle, rejectHandle); if (thisArg instanceof PromiseLib) ((PromiseLib)thisArg).handle(ctx, fulfillHandle, rejectHandle);
else { else {
Object next; Object next;
try { try {
@ -239,7 +239,7 @@ public class PromisePolyfill {
private void resolve(Context ctx, Object val, int state) throws InterruptedException { private void resolve(Context ctx, Object val, int state) throws InterruptedException {
if (this.state != STATE_PENDING) return; if (this.state != STATE_PENDING) return;
if (val instanceof PromisePolyfill) ((PromisePolyfill)val).handle(ctx, if (val instanceof PromiseLib) ((PromiseLib)val).handle(ctx,
new NativeFunction(null, (e, th, a) -> { this.resolve(ctx, a[0], state); return null; }), new NativeFunction(null, (e, th, a) -> { this.resolve(ctx, a[0], state); return null; }),
new NativeFunction(null, (e, th, a) -> { this.resolve(ctx, a[0], STATE_REJECTED); return null; }) new NativeFunction(null, (e, th, a) -> { this.resolve(ctx, a[0], STATE_REJECTED); return null; })
); );
@ -314,7 +314,7 @@ public class PromisePolyfill {
/** /**
* NOT THREAD SAFE - must be called from the engine executor thread * NOT THREAD SAFE - must be called from the engine executor thread
*/ */
@Native public PromisePolyfill(Context ctx, FunctionValue func) throws InterruptedException { @Native public PromiseLib(Context ctx, FunctionValue func) throws InterruptedException {
if (!(func instanceof FunctionValue)) throw EngineException.ofType("A function must be passed to the promise constructor."); if (!(func instanceof FunctionValue)) throw EngineException.ofType("A function must be passed to the promise constructor.");
try { try {
func.call( func.call(
@ -334,11 +334,11 @@ public class PromisePolyfill {
} }
} }
private PromisePolyfill(int state, Object val) { private PromiseLib(int state, Object val) {
this.state = state; this.state = state;
this.val = val; this.val = val;
} }
public PromisePolyfill() { public PromiseLib() {
this(STATE_PENDING, null); this(STATE_PENDING, null);
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -7,9 +7,9 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class RangeErrorPolyfill extends ErrorPolyfill { public class RangeErrorLib extends ErrorLib {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException { @NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message); var target = ErrorLib.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "RangeError"); target.defineProperty(ctx, "name", "RangeError");
return target; return target;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
@ -12,16 +12,16 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter; import me.topchetoeu.jscript.interop.NativeGetter;
public class RegExpPolyfill { public class RegExpLib {
// I used Regex to analyze Regex // I used Regex to analyze Regex
private static final Pattern NAMED_PATTERN = Pattern.compile("\\(\\?<([^=!].*?)>", Pattern.DOTALL); private static final Pattern NAMED_PATTERN = Pattern.compile("\\(\\?<([^=!].*?)>", Pattern.DOTALL);
private static final Pattern ESCAPE_PATTERN = Pattern.compile("[/\\-\\\\^$*+?.()|\\[\\]{}]"); private static final Pattern ESCAPE_PATTERN = Pattern.compile("[/\\-\\\\^$*+?.()|\\[\\]{}]");
private static String cleanupPattern(Context ctx, Object val) throws InterruptedException { private static String cleanupPattern(Context ctx, Object val) throws InterruptedException {
if (val == null) return "(?:)"; if (val == null) return "(?:)";
if (val instanceof RegExpPolyfill) return ((RegExpPolyfill)val).source; if (val instanceof RegExpLib) return ((RegExpLib)val).source;
if (val instanceof NativeWrapper && ((NativeWrapper)val).wrapped instanceof RegExpPolyfill) { if (val instanceof NativeWrapper && ((NativeWrapper)val).wrapped instanceof RegExpLib) {
return ((RegExpPolyfill)((NativeWrapper)val).wrapped).source; return ((RegExpLib)((NativeWrapper)val).wrapped).source;
} }
var res = Values.toString(ctx, val); var res = Values.toString(ctx, val);
if (res.equals("")) return "(?:)"; if (res.equals("")) return "(?:)";
@ -46,11 +46,11 @@ public class RegExpPolyfill {
} }
@Native @Native
public static RegExpPolyfill escape(Context ctx, Object raw, Object flags) throws InterruptedException { public static RegExpLib escape(Context ctx, Object raw, Object flags) throws InterruptedException {
return escape(Values.toString(ctx, raw), cleanupFlags(ctx, flags)); return escape(Values.toString(ctx, raw), cleanupFlags(ctx, flags));
} }
public static RegExpPolyfill escape(String raw, String flags) { public static RegExpLib escape(String raw, String flags) {
return new RegExpPolyfill(ESCAPE_PATTERN.matcher(raw).replaceAll("\\\\$0"), flags); return new RegExpLib(ESCAPE_PATTERN.matcher(raw).replaceAll("\\\\$0"), flags);
} }
private Pattern pattern; private Pattern pattern;
@ -153,7 +153,7 @@ public class RegExpPolyfill {
} }
@Native("@@Symvol.matchAll") public Object matchAll(Context ctx, String target) throws InterruptedException { @Native("@@Symvol.matchAll") public Object matchAll(Context ctx, String target) throws InterruptedException {
var pattern = new RegExpPolyfill(this.source, this.flags() + "g"); var pattern = new RegExpLib(this.source, this.flags() + "g");
return Values.fromJavaIterator(ctx, new Iterator<Object>() { return Values.fromJavaIterator(ctx, new Iterator<Object>() {
private Object val = null; private Object val = null;
@ -175,7 +175,7 @@ public class RegExpPolyfill {
} }
@Native("@@Symvol.split") public ArrayValue split(Context ctx, String target, Object limit, boolean sensible) throws InterruptedException { @Native("@@Symvol.split") public ArrayValue split(Context ctx, String target, Object limit, boolean sensible) throws InterruptedException {
var pattern = new RegExpPolyfill(this.source, this.flags() + "g"); var pattern = new RegExpLib(this.source, this.flags() + "g");
Object match; Object match;
int lastEnd = 0; int lastEnd = 0;
var res = new ArrayValue(); var res = new ArrayValue();
@ -260,10 +260,10 @@ public class RegExpPolyfill {
// else return -1; // else return -1;
// } // }
// }, // },
@Native public RegExpPolyfill(Context ctx, Object pattern, Object flags) throws InterruptedException { @Native public RegExpLib(Context ctx, Object pattern, Object flags) throws InterruptedException {
this(cleanupPattern(ctx, pattern), cleanupFlags(ctx, flags)); this(cleanupPattern(ctx, pattern), cleanupFlags(ctx, flags));
} }
public RegExpPolyfill(String pattern, String flags) { public RegExpLib(String pattern, String flags) {
if (pattern == null || pattern.equals("")) pattern = "(?:)"; if (pattern == null || pattern.equals("")) pattern = "(?:)";
if (flags == null || flags.equals("")) flags = ""; if (flags == null || flags.equals("")) flags = "";
@ -292,6 +292,6 @@ public class RegExpPolyfill {
namedGroups = groups.toArray(String[]::new); namedGroups = groups.toArray(String[]::new);
} }
public RegExpPolyfill(String pattern) { this(pattern, null); } public RegExpLib(String pattern) { this(pattern, null); }
public RegExpPolyfill() { this(null, null); } public RegExpLib() { this(null, null); }
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -12,7 +12,7 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter; import me.topchetoeu.jscript.interop.NativeGetter;
public class SetPolyfill { public class SetLib {
private LinkedHashSet<Object> set = new LinkedHashSet<>(); private LinkedHashSet<Object> set = new LinkedHashSet<>();
@Native("@@Symbol.typeName") public final String name = "Set"; @Native("@@Symbol.typeName") public final String name = "Set";
@ -57,7 +57,7 @@ public class SetPolyfill {
for (var el : keys) func.call(ctx, thisArg, el, el, this); for (var el : keys) func.call(ctx, thisArg, el, el, this);
} }
@Native public SetPolyfill(Context ctx, Object iterable) throws InterruptedException { @Native public SetLib(Context ctx, Object iterable) throws InterruptedException {
for (var el : Values.toJavaIterable(ctx, iterable)) add(el); for (var el : Values.toJavaIterable(ctx, iterable)) add(el);
} }
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -16,11 +16,11 @@ import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
// TODO: implement index wrapping properly // TODO: implement index wrapping properly
public class StringPolyfill { public class StringLib {
public final String value; public final String value;
private static String passThis(Context ctx, String funcName, Object val) throws InterruptedException { private static String passThis(Context ctx, String funcName, Object val) throws InterruptedException {
if (val instanceof StringPolyfill) return ((StringPolyfill)val).value; if (val instanceof StringLib) return ((StringLib)val).value;
else if (val instanceof String) return (String)val; else if (val instanceof String) return (String)val;
else throw EngineException.ofType(String.format("'%s' may only be called upon object and primitve strings.", funcName)); else throw EngineException.ofType(String.format("'%s' may only be called upon object and primitve strings.", funcName));
} }
@ -234,7 +234,7 @@ public class StringPolyfill {
@NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) throws InterruptedException { @NativeConstructor(thisArg = true) public static Object constructor(Context ctx, Object thisArg, Object val) throws InterruptedException {
val = Values.toString(ctx, val); val = Values.toString(ctx, val);
if (thisArg instanceof ObjectValue) return new StringPolyfill((String)val); if (thisArg instanceof ObjectValue) return new StringLib((String)val);
else return val; else return val;
} }
@Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException { @Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException {
@ -250,7 +250,7 @@ public class StringPolyfill {
return new String(arr); return new String(arr);
} }
public StringPolyfill(String val) { public StringLib(String val) {
this.value = val; this.value = val;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -15,7 +15,7 @@ import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeGetter; import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class SymbolPolyfill { public class SymbolLib {
private static final Map<String, Symbol> symbols = new HashMap<>(); private static final Map<String, Symbol> symbols = new HashMap<>();
@NativeGetter public static Symbol typeName(Context ctx) { return ctx.env.symbol("Symbol.typeName"); } @NativeGetter public static Symbol typeName(Context ctx) { return ctx.env.symbol("Symbol.typeName"); }
@ -30,7 +30,7 @@ public class SymbolPolyfill {
public final Symbol value; public final Symbol value;
private static Symbol passThis(Context ctx, String funcName, Object val) throws InterruptedException { private static Symbol passThis(Context ctx, String funcName, Object val) throws InterruptedException {
if (val instanceof SymbolPolyfill) return ((SymbolPolyfill)val).value; if (val instanceof SymbolLib) return ((SymbolLib)val).value;
else if (val instanceof Symbol) return (Symbol)val; 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)); else throw EngineException.ofType(String.format("'%s' may only be called upon object and primitve symbols.", funcName));
} }
@ -59,7 +59,7 @@ public class SymbolPolyfill {
return sym.value; return sym.value;
} }
public SymbolPolyfill(Symbol val) { public SymbolLib(Symbol val) {
this.value = val; this.value = val;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -7,9 +7,9 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class SyntaxErrorPolyfill extends ErrorPolyfill { public class SyntaxErrorLib extends ErrorLib {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException { @NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message); var target = ErrorLib.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "SyntaxError"); target.defineProperty(ctx, "name", "SyntaxError");
return target; return target;
} }

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills; package me.topchetoeu.jscript.lib;
import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.Environment;
@ -7,9 +7,9 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor; import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeInit; import me.topchetoeu.jscript.interop.NativeInit;
public class TypeErrorPolyfill extends ErrorPolyfill { public class TypeErrorLib extends ErrorLib {
@NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException { @NativeConstructor(thisArg = true) public static ObjectValue constructor(Context ctx, Object thisArg, Object message) throws InterruptedException {
var target = ErrorPolyfill.constructor(ctx, thisArg, message); var target = ErrorLib.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "TypeError"); target.defineProperty(ctx, "name", "TypeError");
return target; return target;
} }

View File

@ -1,211 +0,0 @@
package me.topchetoeu.jscript.polyfills;
import me.topchetoeu.jscript.engine.Message;
import me.topchetoeu.jscript.interop.Native;
public class Math {
@Native
public static final double E = java.lang.Math.E;
@Native
public static final double PI = java.lang.Math.PI;
@Native
public static final double SQRT2 = java.lang.Math.sqrt(2);
@Native
public static final double SQRT1_2 = java.lang.Math.sqrt(.5);
@Native
public static final double LN2 = java.lang.Math.log(2);
@Native
public static final double LN10 = java.lang.Math.log(10);
@Native
public static final double LOG2E = java.lang.Math.log(java.lang.Math.E) / LN2;
@Native
public static final double LOG10E = java.lang.Math.log10(java.lang.Math.E);
@Native
public static double asin(Message ctx, double x) throws InterruptedException {
return java.lang.Math.asin(x);
}
@Native
public static double acos(Message ctx, double x) throws InterruptedException {
return java.lang.Math.acos(x);
}
@Native
public static double atan(Message ctx, double x) throws InterruptedException {
return java.lang.Math.atan(x);
}
@Native
public static double atan2(Message ctx, double y, double x) throws InterruptedException {
double _y = y;
double _x = x;
if (_x == 0) {
if (_y == 0) return Double.NaN;
return java.lang.Math.signum(_y) * java.lang.Math.PI / 2;
}
else {
var val = java.lang.Math.atan(_y / _x);
if (_x > 0) return val;
else if (_y < 0) return val - java.lang.Math.PI;
else return val + java.lang.Math.PI;
}
}
@Native
public static double asinh(Message ctx, double x) throws InterruptedException {
double _x = x;
return java.lang.Math.log(_x + java.lang.Math.sqrt(_x * _x + 1));
}
@Native
public static double acosh(Message ctx, double x) throws InterruptedException {
double _x = x;
return java.lang.Math.log(_x + java.lang.Math.sqrt(_x * _x - 1));
}
@Native
public static double atanh(Message ctx, double x) throws InterruptedException {
double _x = x;
if (_x <= -1 || _x >= 1) return Double.NaN;
return .5 * java.lang.Math.log((1 + _x) / (1 - _x));
}
@Native
public static double sin(Message ctx, double x) throws InterruptedException {
return java.lang.Math.sin(x);
}
@Native
public static double cos(Message ctx, double x) throws InterruptedException {
return java.lang.Math.cos(x);
}
@Native
public static double tan(Message ctx, double x) throws InterruptedException {
return java.lang.Math.tan(x);
}
@Native
public static double sinh(Message ctx, double x) throws InterruptedException {
return java.lang.Math.sinh(x);
}
@Native
public static double cosh(Message ctx, double x) throws InterruptedException {
return java.lang.Math.cosh(x);
}
@Native
public static double tanh(Message ctx, double x) throws InterruptedException {
return java.lang.Math.tanh(x);
}
@Native
public static double sqrt(Message ctx, double x) throws InterruptedException {
return java.lang.Math.sqrt(x);
}
@Native
public static double cbrt(Message ctx, double x) throws InterruptedException {
return java.lang.Math.cbrt(x);
}
@Native
public static double hypot(Message ctx, double ...vals) throws InterruptedException {
var res = 0.;
for (var el : vals) {
var val = el;
res += val * val;
}
return java.lang.Math.sqrt(res);
}
@Native
public static int imul(Message ctx, double a, double b) throws InterruptedException {
return (int)a * (int)b;
}
@Native
public static double exp(Message ctx, double x) throws InterruptedException {
return java.lang.Math.exp(x);
}
@Native
public static double expm1(Message ctx, double x) throws InterruptedException {
return java.lang.Math.expm1(x);
}
@Native
public static double pow(Message ctx, double x, double y) throws InterruptedException {
return java.lang.Math.pow(x, y);
}
@Native
public static double log(Message ctx, double x) throws InterruptedException {
return java.lang.Math.log(x);
}
@Native
public static double log10(Message ctx, double x) throws InterruptedException {
return java.lang.Math.log10(x);
}
@Native
public static double log1p(Message ctx, double x) throws InterruptedException {
return java.lang.Math.log1p(x);
}
@Native
public static double log2(Message ctx, double x) throws InterruptedException {
return java.lang.Math.log(x) / LN2;
}
@Native
public static double ceil(Message ctx, double x) throws InterruptedException {
return java.lang.Math.ceil(x);
}
@Native
public static double floor(Message ctx, double x) throws InterruptedException {
return java.lang.Math.floor(x);
}
@Native
public static double round(Message ctx, double x) throws InterruptedException {
return java.lang.Math.round(x);
}
@Native
public static float fround(Message ctx, double x) throws InterruptedException {
return (float)x;
}
@Native
public static double trunc(Message ctx, double x) throws InterruptedException {
var _x = x;
return java.lang.Math.floor(java.lang.Math.abs(_x)) * java.lang.Math.signum(_x);
}
@Native
public static double abs(Message ctx, double x) throws InterruptedException {
return java.lang.Math.abs(x);
}
@Native
public static double max(Message ctx, double ...vals) throws InterruptedException {
var res = Double.NEGATIVE_INFINITY;
for (var el : vals) {
var val = el;
if (val > res) res = val;
}
return res;
}
@Native
public static double min(Message ctx, double ...vals) throws InterruptedException {
var res = Double.POSITIVE_INFINITY;
for (var el : vals) {
var val = el;
if (val < res) res = val;
}
return res;
}
@Native
public static double sign(Message ctx, double x) throws InterruptedException {
return java.lang.Math.signum(x);
}
@Native
public static double random() {
return java.lang.Math.random();
}
@Native
public static int clz32(Message ctx, double x) throws InterruptedException {
return Integer.numberOfLeadingZeros((int)x);
}
}