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.exceptions.EngineException;
import me.topchetoeu.jscript.exceptions.SyntaxException;
import me.topchetoeu.jscript.polyfills.Internals;
import me.topchetoeu.jscript.lib.Internals;
public class Main {
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.Stack;
@ -16,7 +16,7 @@ import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit;
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 {
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.frame.CodeFrame;
@ -8,11 +8,11 @@ import me.topchetoeu.jscript.engine.values.FunctionValue;
import me.topchetoeu.jscript.engine.values.NativeFunction;
import me.topchetoeu.jscript.exceptions.EngineException;
public class AsyncFunctionPolyfill extends FunctionValue {
public class AsyncFunctionLib extends FunctionValue {
public final FunctionValue factory;
public static class AsyncHelper {
public PromisePolyfill promise = new PromisePolyfill();
public PromiseLib promise = new PromiseLib();
public CodeFrame frame;
private boolean awaiting = false;
@ -40,7 +40,7 @@ public class AsyncFunctionPolyfill extends FunctionValue {
ctx.message.popFrame(frame);
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;
}
public AsyncFunctionPolyfill(FunctionValue factory) {
public AsyncFunctionLib(FunctionValue factory) {
super(factory.name, factory.length);
this.factory = factory;
}

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills;
package me.topchetoeu.jscript.lib;
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.interop.Native;
public class AsyncGeneratorPolyfill extends FunctionValue {
public class AsyncGeneratorLib extends FunctionValue {
public final FunctionValue factory;
public static class AsyncGenerator {
@Native("@@Symbol.typeName") public final String name = "AsyncGenerator";
private int state = 0;
private boolean done = false;
private PromisePolyfill currPromise;
private PromiseLib currPromise;
public CodeFrame frame;
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);
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) {
var obj = new ObjectValue();
@ -85,21 +85,21 @@ public class AsyncGeneratorPolyfill extends FunctionValue {
}
@Native
public PromisePolyfill next(Context ctx, Object ...args) throws InterruptedException {
this.currPromise = new PromisePolyfill();
public PromiseLib next(Context ctx, Object ...args) throws InterruptedException {
this.currPromise = new PromiseLib();
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);
return this.currPromise;
}
@Native("throw")
public PromisePolyfill _throw(Context ctx, Object error) throws InterruptedException {
this.currPromise = new PromisePolyfill();
public PromiseLib _throw(Context ctx, Object error) throws InterruptedException {
this.currPromise = new PromiseLib();
next(ctx, Runners.NO_RETURN, Runners.NO_RETURN, error);
return this.currPromise;
}
@Native("return")
public PromisePolyfill _return(Context ctx, Object value) throws InterruptedException {
this.currPromise = new PromisePolyfill();
public PromiseLib _return(Context ctx, Object value) throws InterruptedException {
this.currPromise = new PromiseLib();
next(ctx, Runners.NO_RETURN, value, Runners.NO_RETURN);
return this.currPromise;
}
@ -127,7 +127,7 @@ public class AsyncGeneratorPolyfill extends FunctionValue {
return handler;
}
public AsyncGeneratorPolyfill(FunctionValue factory) {
public AsyncGeneratorLib(FunctionValue factory) {
super(factory.name, factory.length);
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.Environment;
@ -9,9 +9,9 @@ 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);
public static final BooleanPolyfill FALSE = new BooleanPolyfill(false);
public class BooleanLib {
public static final BooleanLib TRUE = new BooleanLib(true);
public static final BooleanLib FALSE = new BooleanLib(false);
public final boolean value;
@ -27,7 +27,7 @@ public class BooleanPolyfill {
return Values.toBoolean(thisArg);
}
public BooleanPolyfill(boolean val) {
public BooleanLib(boolean val) {
this.value = val;
}
@NativeInit(InitType.PROTOTYPE) public static void init(Environment env, ObjectValue target) {

View File

@ -1,29 +1,15 @@
package me.topchetoeu.jscript.polyfills;
package me.topchetoeu.jscript.lib;
import java.util.Calendar;
import java.util.TimeZone;
import me.topchetoeu.jscript.engine.Context;
import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native;
public class Date {
public class DateLib {
private Calendar normal;
private Calendar utc;
public Date(long timestamp) {
normal = Calendar.getInstance();
utc = Calendar.getInstance();
normal.setTimeInMillis(timestamp);
utc.setTimeZone(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(timestamp);
}
@Native
public Date() {
this(new java.util.Date().getTime());
}
private void updateUTC() {
if (utc == null || normal == null) return;
utc.setTimeInMillis(normal.getTimeInMillis());
@ -38,7 +24,7 @@ public class Date {
@Native
public static double now() {
return new Date().getTime();
return new DateLib().getTime();
}
@Native
@ -47,8 +33,7 @@ public class Date {
return normal.get(Calendar.YEAR) - 1900;
}
@Native
public double setYear(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setYear(Context ctx, double real) throws InterruptedException {
if (real >= 0 && real <= 99) real = real + 1900;
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.YEAR, (int)real);
@ -139,64 +124,56 @@ public class Date {
}
@Native
public double setFullYear(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setFullYear(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.YEAR, (int)real);
updateUTC();
return getTime();
}
@Native
public double setMonth(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setMonth(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.MONTH, (int)real);
updateUTC();
return getTime();
}
@Native
public double setDate(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setDate(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.DAY_OF_MONTH, (int)real);
updateUTC();
return getTime();
}
@Native
public double setDay(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setDay(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.DAY_OF_WEEK, (int)real);
updateUTC();
return getTime();
}
@Native
public double setHours(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setHours(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.HOUR_OF_DAY, (int)real);
updateUTC();
return getTime();
}
@Native
public double setMinutes(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setMinutes(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.MINUTE, (int)real);
updateUTC();
return getTime();
}
@Native
public double setSeconds(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setSeconds(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.SECOND, (int)real);
updateUTC();
return getTime();
}
@Native
public double setMilliseconds(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setMilliseconds(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else normal.set(Calendar.MILLISECOND, (int)real);
updateUTC();
@ -204,64 +181,56 @@ public class Date {
}
@Native
public double setUTCFullYear(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCFullYear(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.YEAR, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCMonth(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCMonth(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.MONTH, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCDate(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCDate(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.DAY_OF_MONTH, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCDay(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCDay(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.DAY_OF_WEEK, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCHours(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCHours(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.HOUR_OF_DAY, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCMinutes(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCMinutes(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.MINUTE, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCSeconds(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCSeconds(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.SECOND, (int)real);
updateNormal();
return getTime();
}
@Native
public double setUTCMilliseconds(Context ctx, Object val) throws InterruptedException {
var real = Values.toNumber(ctx, val);
public double setUTCMilliseconds(Context ctx, double real) throws InterruptedException {
if (Double.isNaN(real)) invalidate();
else utc.set(Calendar.MILLISECOND, (int)real);
updateNormal();
@ -285,18 +254,16 @@ public class Date {
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());
// }
public DateLib(long timestamp) {
normal = Calendar.getInstance();
utc = Calendar.getInstance();
normal.setTimeInMillis(timestamp);
utc.setTimeZone(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(timestamp);
}
@Native
public DateLib() {
this(new java.util.Date().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.Environment;
@ -10,7 +10,7 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
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 {
if (name == null) name = "";
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.Environment;
@ -11,7 +11,7 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
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 {
return func.call(ctx, thisArg, args.toArray());
}
@ -41,13 +41,13 @@ public class FunctionPolyfill {
}
@Native public static FunctionValue async(FunctionValue func) {
return new AsyncFunctionPolyfill(func);
return new AsyncFunctionLib(func);
}
@Native public static FunctionValue asyncGenerator(FunctionValue func) {
return new AsyncGeneratorPolyfill(func);
return new AsyncGeneratorLib(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) {

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.frame.CodeFrame;
@ -10,7 +10,7 @@ import me.topchetoeu.jscript.engine.values.ObjectValue;
import me.topchetoeu.jscript.exceptions.EngineException;
import me.topchetoeu.jscript.interop.Native;
public class GeneratorPolyfill extends FunctionValue {
public class GeneratorLib extends FunctionValue {
public final FunctionValue factory;
public static class Generator {
@ -94,7 +94,7 @@ public class GeneratorPolyfill extends FunctionValue {
return handler;
}
public GeneratorPolyfill(FunctionValue factory) {
public GeneratorLib(FunctionValue factory) {
super(factory.name, factory.length);
this.factory = factory;
}

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills;
package me.topchetoeu.jscript.lib;
import java.util.HashMap;
@ -16,7 +16,7 @@ public class Internals {
private static final DataKey<Integer> I = new DataKey<>();
@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 {
for (var arg : args) {
@ -77,50 +77,50 @@ public class Internals {
}
@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 {
return NumberPolyfill.parseFloat(ctx, val);
return NumberLib.parseFloat(ctx, val);
}
public void apply(Environment env) {
var wp = env.wrappersProvider;
var glob = env.global = new GlobalScope(NativeWrapperProvider.makeNamespace(env, Internals.class));
glob.define(null, "Object", false, wp.getConstr(ObjectPolyfill.class));
glob.define(null, "Function", false, wp.getConstr(FunctionPolyfill.class));
glob.define(null, "Array", false, wp.getConstr(ArrayPolyfill.class));
glob.define(null, "Object", false, wp.getConstr(ObjectLib.class));
glob.define(null, "Function", false, wp.getConstr(FunctionLib.class));
glob.define(null, "Array", false, wp.getConstr(ArrayLib.class));
glob.define(null, "Boolean", false, wp.getConstr(BooleanPolyfill.class));
glob.define(null, "Number", false, wp.getConstr(NumberPolyfill.class));
glob.define(null, "String", false, wp.getConstr(StringPolyfill.class));
glob.define(null, "Symbol", false, wp.getConstr(SymbolPolyfill.class));
glob.define(null, "Boolean", false, wp.getConstr(BooleanLib.class));
glob.define(null, "Number", false, wp.getConstr(NumberLib.class));
glob.define(null, "String", false, wp.getConstr(StringLib.class));
glob.define(null, "Symbol", false, wp.getConstr(SymbolLib.class));
glob.define(null, "Promise", false, wp.getConstr(PromisePolyfill.class));
glob.define(null, "RegExp", false, wp.getConstr(RegExpPolyfill.class));
glob.define(null, "Map", false, wp.getConstr(MapPolyfill.class));
glob.define(null, "Set", false, wp.getConstr(SetPolyfill.class));
glob.define(null, "Promise", false, wp.getConstr(PromiseLib.class));
glob.define(null, "RegExp", false, wp.getConstr(RegExpLib.class));
glob.define(null, "Map", false, wp.getConstr(MapLib.class));
glob.define(null, "Set", false, wp.getConstr(SetLib.class));
glob.define(null, "Error", false, wp.getConstr(ErrorPolyfill.class));
glob.define(null, "SyntaxError", false, wp.getConstr(SyntaxErrorPolyfill.class));
glob.define(null, "TypeError", false, wp.getConstr(TypeErrorPolyfill.class));
glob.define(null, "RangeError", false, wp.getConstr(RangeErrorPolyfill.class));
glob.define(null, "Error", false, wp.getConstr(ErrorLib.class));
glob.define(null, "SyntaxError", false, wp.getConstr(SyntaxErrorLib.class));
glob.define(null, "TypeError", false, wp.getConstr(TypeErrorLib.class));
glob.define(null, "RangeError", false, wp.getConstr(RangeErrorLib.class));
env.setProto("object", wp.getProto(ObjectPolyfill.class));
env.setProto("function", wp.getProto(FunctionPolyfill.class));
env.setProto("array", wp.getProto(ArrayPolyfill.class));
env.setProto("object", wp.getProto(ObjectLib.class));
env.setProto("function", wp.getProto(FunctionLib.class));
env.setProto("array", wp.getProto(ArrayLib.class));
env.setProto("bool", wp.getProto(BooleanPolyfill.class));
env.setProto("number", wp.getProto(NumberPolyfill.class));
env.setProto("string", wp.getProto(StringPolyfill.class));
env.setProto("symbol", wp.getProto(SymbolPolyfill.class));
env.setProto("bool", wp.getProto(BooleanLib.class));
env.setProto("number", wp.getProto(NumberLib.class));
env.setProto("string", wp.getProto(StringLib.class));
env.setProto("symbol", wp.getProto(SymbolLib.class));
env.setProto("error", wp.getProto(ErrorPolyfill.class));
env.setProto("syntaxErr", wp.getProto(SyntaxErrorPolyfill.class));
env.setProto("typeErr", wp.getProto(TypeErrorPolyfill.class));
env.setProto("rangeErr", wp.getProto(RangeErrorPolyfill.class));
env.setProto("error", wp.getProto(ErrorLib.class));
env.setProto("syntaxErr", wp.getProto(SyntaxErrorLib.class));
env.setProto("typeErr", wp.getProto(TypeErrorLib.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!");
}

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills;
package me.topchetoeu.jscript.lib;
import java.util.HashSet;
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.JSONMap;
public class JSONPolyfill {
public class JSONLib {
private static Object toJS(JSONElement val) {
if (val.isBoolean()) return val.bool();
if (val.isString()) return val.string();
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()) {
var res = new ObjectValue();
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.LinkedHashMap;
@ -12,7 +12,7 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter;
public class MapPolyfill {
public class MapLib {
private LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
@Native("@@Symbol.typeName") public final String name = "Map";
@ -49,7 +49,7 @@ public class MapPolyfill {
@Native public Object get(Object 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);
return this;
}
@ -67,7 +67,7 @@ public class MapPolyfill {
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)) {
try {
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.Environment;
@ -9,7 +9,7 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
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 MAX_SAFE_INTEGER = 9007199254740991.;
@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 {
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;
}
@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;
if (thisArg instanceof NumberLib) return ((NumberLib)thisArg).value;
else return Values.toNumber(ctx, thisArg);
}
public NumberPolyfill(double val) {
public NumberLib(double 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.Environment;
@ -13,7 +13,7 @@ import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeConstructor;
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 {
for (var obj : src) {
for (var key : Values.getMembers(ctx, obj, true, true)) {
@ -201,14 +201,14 @@ public class ObjectPolyfill {
return "[object " + name + "]";
}
@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 {
if (arg == null || arg == Values.NULL) return new ObjectValue();
else if (arg instanceof Boolean) return BooleanPolyfill.constructor(ctx, thisArg, arg);
else if (arg instanceof Number) return NumberPolyfill.constructor(ctx, thisArg, arg);
else if (arg instanceof String) return StringPolyfill.constructor(ctx, thisArg, arg);
else if (arg instanceof Boolean) return BooleanLib.constructor(ctx, thisArg, arg);
else if (arg instanceof Number) return NumberLib.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 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.List;
@ -18,7 +18,7 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeInit;
public class PromisePolyfill {
public class PromiseLib {
private static class Handle {
public final Context ctx;
public final FunctionValue fulfilled;
@ -32,24 +32,24 @@ public class PromisePolyfill {
}
@Native("resolve")
public static PromisePolyfill ofResolved(Context ctx, Object val) throws InterruptedException {
var res = new PromisePolyfill();
public static PromiseLib ofResolved(Context ctx, Object val) throws InterruptedException {
var res = new PromiseLib();
res.fulfill(ctx, val);
return res;
}
@Native("reject")
public static PromisePolyfill ofRejected(Context ctx, Object val) throws InterruptedException {
var res = new PromisePolyfill();
public static PromiseLib ofRejected(Context ctx, Object val) throws InterruptedException {
var res = new PromiseLib();
res.reject(ctx, val);
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.");
var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var n = new int[] { promises.size() };
var res = new PromisePolyfill();
var res = new PromiseLib();
var errors = new ArrayValue();
@ -69,11 +69,11 @@ public class PromisePolyfill {
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.");
var promises = Values.array(_promises);
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++) {
var val = promises.get(i);
@ -85,12 +85,12 @@ public class PromisePolyfill {
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.");
var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var n = new int[] { promises.size() };
var res = new PromisePolyfill();
var res = new PromiseLib();
var result = new ArrayValue();
@ -112,12 +112,12 @@ public class PromisePolyfill {
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.");
var promises = Values.array(_promises);
if (promises.size() == 0) return ofResolved(ctx, new ArrayValue());
var n = new int[] { promises.size() };
var res = new PromisePolyfill();
var res = new PromiseLib();
var result = new ArrayValue();
@ -159,14 +159,14 @@ public class PromisePolyfill {
var onFulfill = _onFulfill instanceof FunctionValue ? ((FunctionValue)_onFulfill) : 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 reject = onReject == null ? new NativeFunction((_ctx, _thisArg, _args) -> {
throw new EngineException(_args.length > 0 ? _args[0] : null);
}) : (FunctionValue)onReject;
if (thisArg instanceof NativeWrapper && ((NativeWrapper)thisArg).wrapped instanceof PromisePolyfill) {
if (thisArg instanceof NativeWrapper && ((NativeWrapper)thisArg).wrapped instanceof PromiseLib) {
thisArg = ((NativeWrapper)thisArg).wrapped;
}
@ -183,7 +183,7 @@ public class PromisePolyfill {
return null;
});
if (thisArg instanceof PromisePolyfill) ((PromisePolyfill)thisArg).handle(ctx, fulfillHandle, rejectHandle);
if (thisArg instanceof PromiseLib) ((PromiseLib)thisArg).handle(ctx, fulfillHandle, rejectHandle);
else {
Object next;
try {
@ -239,7 +239,7 @@ public class PromisePolyfill {
private void resolve(Context ctx, Object val, int state) throws InterruptedException {
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_REJECTED); return null; })
);
@ -314,7 +314,7 @@ public class PromisePolyfill {
/**
* 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.");
try {
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.val = val;
}
public PromisePolyfill() {
public PromiseLib() {
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.Environment;
@ -7,9 +7,9 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor;
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 {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
var target = ErrorLib.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "RangeError");
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.Iterator;
@ -12,16 +12,16 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter;
public class RegExpPolyfill {
public class RegExpLib {
// I used Regex to analyze Regex
private static final Pattern NAMED_PATTERN = Pattern.compile("\\(\\?<([^=!].*?)>", Pattern.DOTALL);
private static final Pattern ESCAPE_PATTERN = Pattern.compile("[/\\-\\\\^$*+?.()|\\[\\]{}]");
private static String cleanupPattern(Context ctx, Object val) throws InterruptedException {
if (val == null) return "(?:)";
if (val instanceof RegExpPolyfill) return ((RegExpPolyfill)val).source;
if (val instanceof NativeWrapper && ((NativeWrapper)val).wrapped instanceof RegExpPolyfill) {
return ((RegExpPolyfill)((NativeWrapper)val).wrapped).source;
if (val instanceof RegExpLib) return ((RegExpLib)val).source;
if (val instanceof NativeWrapper && ((NativeWrapper)val).wrapped instanceof RegExpLib) {
return ((RegExpLib)((NativeWrapper)val).wrapped).source;
}
var res = Values.toString(ctx, val);
if (res.equals("")) return "(?:)";
@ -46,11 +46,11 @@ public class RegExpPolyfill {
}
@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));
}
public static RegExpPolyfill escape(String raw, String flags) {
return new RegExpPolyfill(ESCAPE_PATTERN.matcher(raw).replaceAll("\\\\$0"), flags);
public static RegExpLib escape(String raw, String flags) {
return new RegExpLib(ESCAPE_PATTERN.matcher(raw).replaceAll("\\\\$0"), flags);
}
private Pattern pattern;
@ -153,7 +153,7 @@ public class RegExpPolyfill {
}
@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>() {
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 {
var pattern = new RegExpPolyfill(this.source, this.flags() + "g");
var pattern = new RegExpLib(this.source, this.flags() + "g");
Object match;
int lastEnd = 0;
var res = new ArrayValue();
@ -260,10 +260,10 @@ public class RegExpPolyfill {
// 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));
}
public RegExpPolyfill(String pattern, String flags) {
public RegExpLib(String pattern, String flags) {
if (pattern == null || pattern.equals("")) pattern = "(?:)";
if (flags == null || flags.equals("")) flags = "";
@ -292,6 +292,6 @@ public class RegExpPolyfill {
namedGroups = groups.toArray(String[]::new);
}
public RegExpPolyfill(String pattern) { this(pattern, null); }
public RegExpPolyfill() { this(null, null); }
public RegExpLib(String pattern) { this(pattern, 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.LinkedHashSet;
@ -12,7 +12,7 @@ import me.topchetoeu.jscript.engine.values.Values;
import me.topchetoeu.jscript.interop.Native;
import me.topchetoeu.jscript.interop.NativeGetter;
public class SetPolyfill {
public class SetLib {
private LinkedHashSet<Object> set = new LinkedHashSet<>();
@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);
}
@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);
}
}

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.polyfills;
package me.topchetoeu.jscript.lib;
import java.util.regex.Pattern;
@ -16,11 +16,11 @@ import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit;
// TODO: implement index wrapping properly
public class StringPolyfill {
public class StringLib {
public final String value;
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 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 {
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;
}
@Native(thisArg = true) public static String toString(Context ctx, Object thisArg) throws InterruptedException {
@ -250,7 +250,7 @@ public class StringPolyfill {
return new String(arr);
}
public StringPolyfill(String val) {
public StringLib(String 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.Map;
@ -15,7 +15,7 @@ import me.topchetoeu.jscript.interop.NativeConstructor;
import me.topchetoeu.jscript.interop.NativeGetter;
import me.topchetoeu.jscript.interop.NativeInit;
public class SymbolPolyfill {
public class SymbolLib {
private static final Map<String, Symbol> symbols = new HashMap<>();
@NativeGetter public static Symbol typeName(Context ctx) { return ctx.env.symbol("Symbol.typeName"); }
@ -30,7 +30,7 @@ public class SymbolPolyfill {
public final Symbol value;
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 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;
}
public SymbolPolyfill(Symbol val) {
public SymbolLib(Symbol 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.Environment;
@ -7,9 +7,9 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor;
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 {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
var target = ErrorLib.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "SyntaxError");
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.Environment;
@ -7,9 +7,9 @@ import me.topchetoeu.jscript.interop.InitType;
import me.topchetoeu.jscript.interop.NativeConstructor;
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 {
var target = ErrorPolyfill.constructor(ctx, thisArg, message);
var target = ErrorLib.constructor(ctx, thisArg, message);
target.defineProperty(ctx, "name", "TypeError");
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);
}
}