From a17ec737b766cbc85b35bdfe7b3551d8af4aaa19 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Fri, 6 Oct 2023 11:57:26 +0300 Subject: [PATCH] refactor: rename polyfills to libs --- src/me/topchetoeu/jscript/Main.java | 2 +- .../ArrayPolyfill.java => lib/ArrayLib.java} | 4 +- .../AsyncFunctionLib.java} | 10 +- .../AsyncGeneratorLib.java} | 22 +- .../BooleanLib.java} | 10 +- .../{polyfills/Date.java => lib/DateLib.java} | 571 +++++++++--------- .../ErrorPolyfill.java => lib/ErrorLib.java} | 4 +- .../FunctionLib.java} | 10 +- .../GeneratorLib.java} | 6 +- .../jscript/{polyfills => lib}/Internals.java | 254 ++++---- .../JSONPolyfill.java => lib/JSONLib.java} | 6 +- .../MapPolyfill.java => lib/MapLib.java} | 8 +- src/me/topchetoeu/jscript/lib/MathLib.java | 99 +++ .../NumberLib.java} | 10 +- .../ObjectLib.java} | 12 +- .../PromiseLib.java} | 42 +- .../RangeErrorLib.java} | 6 +- .../RegExpLib.java} | 28 +- .../SetPolyfill.java => lib/SetLib.java} | 6 +- .../StringLib.java} | 10 +- .../SymbolLib.java} | 8 +- .../SyntaxErrorLib.java} | 6 +- .../TypeErrorLib.java} | 6 +- src/me/topchetoeu/jscript/polyfills/Math.java | 211 ------- 24 files changed, 603 insertions(+), 748 deletions(-) rename src/me/topchetoeu/jscript/{polyfills/ArrayPolyfill.java => lib/ArrayLib.java} (99%) rename src/me/topchetoeu/jscript/{polyfills/AsyncFunctionPolyfill.java => lib/AsyncFunctionLib.java} (88%) rename src/me/topchetoeu/jscript/{polyfills/AsyncGeneratorPolyfill.java => lib/AsyncGeneratorLib.java} (85%) rename src/me/topchetoeu/jscript/{polyfills/BooleanPolyfill.java => lib/BooleanLib.java} (82%) rename src/me/topchetoeu/jscript/{polyfills/Date.java => lib/DateLib.java} (70%) rename src/me/topchetoeu/jscript/{polyfills/ErrorPolyfill.java => lib/ErrorLib.java} (97%) rename src/me/topchetoeu/jscript/{polyfills/FunctionPolyfill.java => lib/FunctionLib.java} (91%) rename src/me/topchetoeu/jscript/{polyfills/GeneratorPolyfill.java => lib/GeneratorLib.java} (96%) rename src/me/topchetoeu/jscript/{polyfills => lib}/Internals.java (63%) rename src/me/topchetoeu/jscript/{polyfills/JSONPolyfill.java => lib/JSONLib.java} (96%) rename src/me/topchetoeu/jscript/{polyfills/MapPolyfill.java => lib/MapLib.java} (91%) create mode 100644 src/me/topchetoeu/jscript/lib/MathLib.java rename src/me/topchetoeu/jscript/{polyfills/NumberPolyfill.java => lib/NumberLib.java} (90%) rename src/me/topchetoeu/jscript/{polyfills/ObjectPolyfill.java => lib/ObjectLib.java} (95%) rename src/me/topchetoeu/jscript/{polyfills/PromisePolyfill.java => lib/PromiseLib.java} (90%) rename src/me/topchetoeu/jscript/{polyfills/RangeErrorPolyfill.java => lib/RangeErrorLib.java} (82%) rename src/me/topchetoeu/jscript/{polyfills/RegExpPolyfill.java => lib/RegExpLib.java} (91%) rename src/me/topchetoeu/jscript/{polyfills/SetPolyfill.java => lib/SetLib.java} (92%) rename src/me/topchetoeu/jscript/{polyfills/StringPolyfill.java => lib/StringLib.java} (97%) rename src/me/topchetoeu/jscript/{polyfills/SymbolPolyfill.java => lib/SymbolLib.java} (94%) rename src/me/topchetoeu/jscript/{polyfills/SyntaxErrorPolyfill.java => lib/SyntaxErrorLib.java} (82%) rename src/me/topchetoeu/jscript/{polyfills/TypeErrorPolyfill.java => lib/TypeErrorLib.java} (82%) delete mode 100644 src/me/topchetoeu/jscript/polyfills/Math.java diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index b72caea..314f9c6 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -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; diff --git a/src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java b/src/me/topchetoeu/jscript/lib/ArrayLib.java similarity index 99% rename from src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java rename to src/me/topchetoeu/jscript/lib/ArrayLib.java index 7f9edde..fc5fcf1 100644 --- a/src/me/topchetoeu/jscript/polyfills/ArrayPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/ArrayLib.java @@ -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(); } diff --git a/src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java b/src/me/topchetoeu/jscript/lib/AsyncFunctionLib.java similarity index 88% rename from src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java rename to src/me/topchetoeu/jscript/lib/AsyncFunctionLib.java index b568cf6..f14078c 100644 --- a/src/me/topchetoeu/jscript/polyfills/AsyncFunctionPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/AsyncFunctionLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/AsyncGeneratorPolyfill.java b/src/me/topchetoeu/jscript/lib/AsyncGeneratorLib.java similarity index 85% rename from src/me/topchetoeu/jscript/polyfills/AsyncGeneratorPolyfill.java rename to src/me/topchetoeu/jscript/lib/AsyncGeneratorLib.java index 18950e3..81fcd23 100644 --- a/src/me/topchetoeu/jscript/polyfills/AsyncGeneratorPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/AsyncGeneratorLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java b/src/me/topchetoeu/jscript/lib/BooleanLib.java similarity index 82% rename from src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java rename to src/me/topchetoeu/jscript/lib/BooleanLib.java index 026dad2..7866668 100644 --- a/src/me/topchetoeu/jscript/polyfills/BooleanPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/BooleanLib.java @@ -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) { diff --git a/src/me/topchetoeu/jscript/polyfills/Date.java b/src/me/topchetoeu/jscript/lib/DateLib.java similarity index 70% rename from src/me/topchetoeu/jscript/polyfills/Date.java rename to src/me/topchetoeu/jscript/lib/DateLib.java index 8ce78a4..c456717 100644 --- a/src/me/topchetoeu/jscript/polyfills/Date.java +++ b/src/me/topchetoeu/jscript/lib/DateLib.java @@ -1,302 +1,269 @@ -package me.topchetoeu.jscript.polyfills; - -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 { - 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()); - } - private void updateNormal() { - if (utc == null || normal == null) return; - normal.setTimeInMillis(utc.getTimeInMillis()); - } - private void invalidate() { - normal = utc = null; - } - - @Native - public static double now() { - return new Date().getTime(); - } - - @Native - public double getYear() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.YEAR) - 1900; - } - @Native - public double setYear(Context ctx, Object val) throws InterruptedException { - var real = Values.toNumber(ctx, val); - if (real >= 0 && real <= 99) real = real + 1900; - if (Double.isNaN(real)) invalidate(); - else normal.set(Calendar.YEAR, (int)real); - updateUTC(); - return getTime(); - } - - @Native - public double getFullYear() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.YEAR); - } - @Native - public double getMonth() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.MONTH); - } - @Native - public double getDate() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.DAY_OF_MONTH); - } - @Native - public double getDay() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.DAY_OF_WEEK); - } - @Native - public double getHours() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.HOUR_OF_DAY); - } - @Native - public double getMinutes() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.MINUTE); - } - @Native - public double getSeconds() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.SECOND); - } - @Native - public double getMilliseconds() { - if (normal == null) return Double.NaN; - return normal.get(Calendar.MILLISECOND); - } - - @Native - public double getUTCFullYear() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.YEAR); - } - @Native - public double getUTCMonth() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.MONTH); - } - @Native - public double getUTCDate() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.DAY_OF_MONTH); - } - @Native - public double getUTCDay() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.DAY_OF_WEEK); - } - @Native - public double getUTCHours() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.HOUR_OF_DAY); - } - @Native - public double getUTCMinutes() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.MINUTE); - } - @Native - public double getUTCSeconds() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.SECOND); - } - @Native - public double getUTCMilliseconds() { - if (utc == null) return Double.NaN; - return utc.get(Calendar.MILLISECOND); - } - - @Native - public double setFullYear(Context ctx, Object val) throws InterruptedException { - var real = Values.toNumber(ctx, val); - 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); - 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); - 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); - 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); - 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); - 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); - 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); - if (Double.isNaN(real)) invalidate(); - else normal.set(Calendar.MILLISECOND, (int)real); - updateUTC(); - return getTime(); - } - - @Native - public double setUTCFullYear(Context ctx, Object val) throws InterruptedException { - var real = Values.toNumber(ctx, val); - 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); - 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); - 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); - 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); - 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); - 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); - 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); - if (Double.isNaN(real)) invalidate(); - else utc.set(Calendar.MILLISECOND, (int)real); - updateNormal(); - 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()); - // } -} +package me.topchetoeu.jscript.lib; + +import java.util.Calendar; +import java.util.TimeZone; + +import me.topchetoeu.jscript.engine.Context; +import me.topchetoeu.jscript.interop.Native; + +public class DateLib { + private Calendar normal; + private Calendar utc; + + private void updateUTC() { + if (utc == null || normal == null) return; + utc.setTimeInMillis(normal.getTimeInMillis()); + } + private void updateNormal() { + if (utc == null || normal == null) return; + normal.setTimeInMillis(utc.getTimeInMillis()); + } + private void invalidate() { + normal = utc = null; + } + + @Native + public static double now() { + return new DateLib().getTime(); + } + + @Native + public double getYear() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.YEAR) - 1900; + } + @Native + 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); + updateUTC(); + return getTime(); + } + + @Native + public double getFullYear() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.YEAR); + } + @Native + public double getMonth() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.MONTH); + } + @Native + public double getDate() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.DAY_OF_MONTH); + } + @Native + public double getDay() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.DAY_OF_WEEK); + } + @Native + public double getHours() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.HOUR_OF_DAY); + } + @Native + public double getMinutes() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.MINUTE); + } + @Native + public double getSeconds() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.SECOND); + } + @Native + public double getMilliseconds() { + if (normal == null) return Double.NaN; + return normal.get(Calendar.MILLISECOND); + } + + @Native + public double getUTCFullYear() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.YEAR); + } + @Native + public double getUTCMonth() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.MONTH); + } + @Native + public double getUTCDate() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.DAY_OF_MONTH); + } + @Native + public double getUTCDay() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.DAY_OF_WEEK); + } + @Native + public double getUTCHours() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.HOUR_OF_DAY); + } + @Native + public double getUTCMinutes() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.MINUTE); + } + @Native + public double getUTCSeconds() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.SECOND); + } + @Native + public double getUTCMilliseconds() { + if (utc == null) return Double.NaN; + return utc.get(Calendar.MILLISECOND); + } + + @Native + 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, 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, 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, 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, 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, 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, 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, double real) throws InterruptedException { + if (Double.isNaN(real)) invalidate(); + else normal.set(Calendar.MILLISECOND, (int)real); + updateUTC(); + return getTime(); + } + + @Native + 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, 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, 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, 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, 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, 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, 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, double real) throws InterruptedException { + if (Double.isNaN(real)) invalidate(); + else utc.set(Calendar.MILLISECOND, (int)real); + updateNormal(); + 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(); + } + + 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()); + } +} diff --git a/src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java b/src/me/topchetoeu/jscript/lib/ErrorLib.java similarity index 97% rename from src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java rename to src/me/topchetoeu/jscript/lib/ErrorLib.java index a466de9..ffa5f73 100644 --- a/src/me/topchetoeu/jscript/polyfills/ErrorPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/ErrorLib.java @@ -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(); diff --git a/src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java b/src/me/topchetoeu/jscript/lib/FunctionLib.java similarity index 91% rename from src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java rename to src/me/topchetoeu/jscript/lib/FunctionLib.java index 6c5eb6a..2c05aa7 100644 --- a/src/me/topchetoeu/jscript/polyfills/FunctionPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/FunctionLib.java @@ -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) { diff --git a/src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java b/src/me/topchetoeu/jscript/lib/GeneratorLib.java similarity index 96% rename from src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java rename to src/me/topchetoeu/jscript/lib/GeneratorLib.java index db176ed..109491e 100644 --- a/src/me/topchetoeu/jscript/polyfills/GeneratorPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/GeneratorLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/Internals.java b/src/me/topchetoeu/jscript/lib/Internals.java similarity index 63% rename from src/me/topchetoeu/jscript/polyfills/Internals.java rename to src/me/topchetoeu/jscript/lib/Internals.java index fd808ad..432ff2e 100644 --- a/src/me/topchetoeu/jscript/polyfills/Internals.java +++ b/src/me/topchetoeu/jscript/lib/Internals.java @@ -1,127 +1,127 @@ -package me.topchetoeu.jscript.polyfills; - -import java.util.HashMap; - -import me.topchetoeu.jscript.engine.Context; -import me.topchetoeu.jscript.engine.DataKey; -import me.topchetoeu.jscript.engine.Environment; -import me.topchetoeu.jscript.engine.scope.GlobalScope; -import me.topchetoeu.jscript.engine.values.FunctionValue; -import me.topchetoeu.jscript.engine.values.Values; -import me.topchetoeu.jscript.interop.Native; -import me.topchetoeu.jscript.interop.NativeWrapperProvider; - -public class Internals { - private static final DataKey> THREADS = new DataKey<>(); - private static final DataKey I = new DataKey<>(); - - @Native public static FunctionValue bind(FunctionValue func, Object thisArg) throws InterruptedException { - return FunctionPolyfill.bind(func, thisArg); - } - @Native public static void log(Context ctx, Object ...args) throws InterruptedException { - for (var arg : args) { - Values.printValue(ctx, arg); - } - System.out.println(); - } - - @Native public static int setTimeout(Context ctx, FunctionValue func, int delay, Object ...args) { - var thread = new Thread(() -> { - var ms = (long)delay; - var ns = (int)((delay - ms) * 10000000); - - try { - Thread.sleep(ms, ns); - } - catch (InterruptedException e) { return; } - - ctx.message.engine.pushMsg(false, ctx.message, func, null, args); - }); - thread.start(); - - int i = ctx.env.data.increase(I, 1, 0); - var threads = ctx.env.data.add(THREADS, new HashMap<>()); - threads.put(++i, thread); - return i; - } - @Native public static int setInterval(Context ctx, FunctionValue func, int delay, Object ...args) { - var thread = new Thread(() -> { - var ms = (long)delay; - var ns = (int)((delay - ms) * 10000000); - - while (true) { - try { - Thread.sleep(ms, ns); - } - catch (InterruptedException e) { return; } - - ctx.message.engine.pushMsg(false, ctx.message, func, null, args); - } - }); - thread.start(); - - int i = ctx.env.data.increase(I, 1, 0); - var threads = ctx.env.data.add(THREADS, new HashMap<>()); - threads.put(++i, thread); - return i; - } - - @Native public static void clearTimeout(Context ctx, int i) { - var threads = ctx.env.data.add(THREADS, new HashMap<>()); - - var thread = threads.remove(i); - if (thread != null) thread.interrupt(); - } - @Native public static void clearInterval(Context ctx, int i) { - clearTimeout(ctx, i); - } - - @Native public static double parseInt(Context ctx, String val) throws InterruptedException { - return NumberPolyfill.parseInt(ctx, val); - } - @Native public static double parseFloat(Context ctx, String val) throws InterruptedException { - return NumberPolyfill.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, "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, "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, "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)); - - env.setProto("object", wp.getProto(ObjectPolyfill.class)); - env.setProto("function", wp.getProto(FunctionPolyfill.class)); - env.setProto("array", wp.getProto(ArrayPolyfill.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("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)); - - wp.getProto(ObjectPolyfill.class).setPrototype(null, null); - - System.out.println("Loaded polyfills!"); - } -} +package me.topchetoeu.jscript.lib; + +import java.util.HashMap; + +import me.topchetoeu.jscript.engine.Context; +import me.topchetoeu.jscript.engine.DataKey; +import me.topchetoeu.jscript.engine.Environment; +import me.topchetoeu.jscript.engine.scope.GlobalScope; +import me.topchetoeu.jscript.engine.values.FunctionValue; +import me.topchetoeu.jscript.engine.values.Values; +import me.topchetoeu.jscript.interop.Native; +import me.topchetoeu.jscript.interop.NativeWrapperProvider; + +public class Internals { + private static final DataKey> THREADS = new DataKey<>(); + private static final DataKey I = new DataKey<>(); + + @Native public static FunctionValue bind(FunctionValue func, Object thisArg) throws InterruptedException { + return FunctionLib.bind(func, thisArg); + } + @Native public static void log(Context ctx, Object ...args) throws InterruptedException { + for (var arg : args) { + Values.printValue(ctx, arg); + } + System.out.println(); + } + + @Native public static int setTimeout(Context ctx, FunctionValue func, int delay, Object ...args) { + var thread = new Thread(() -> { + var ms = (long)delay; + var ns = (int)((delay - ms) * 10000000); + + try { + Thread.sleep(ms, ns); + } + catch (InterruptedException e) { return; } + + ctx.message.engine.pushMsg(false, ctx.message, func, null, args); + }); + thread.start(); + + int i = ctx.env.data.increase(I, 1, 0); + var threads = ctx.env.data.add(THREADS, new HashMap<>()); + threads.put(++i, thread); + return i; + } + @Native public static int setInterval(Context ctx, FunctionValue func, int delay, Object ...args) { + var thread = new Thread(() -> { + var ms = (long)delay; + var ns = (int)((delay - ms) * 10000000); + + while (true) { + try { + Thread.sleep(ms, ns); + } + catch (InterruptedException e) { return; } + + ctx.message.engine.pushMsg(false, ctx.message, func, null, args); + } + }); + thread.start(); + + int i = ctx.env.data.increase(I, 1, 0); + var threads = ctx.env.data.add(THREADS, new HashMap<>()); + threads.put(++i, thread); + return i; + } + + @Native public static void clearTimeout(Context ctx, int i) { + var threads = ctx.env.data.add(THREADS, new HashMap<>()); + + var thread = threads.remove(i); + if (thread != null) thread.interrupt(); + } + @Native public static void clearInterval(Context ctx, int i) { + clearTimeout(ctx, i); + } + + @Native public static double parseInt(Context ctx, String val) throws InterruptedException { + return NumberLib.parseInt(ctx, val); + } + @Native public static double parseFloat(Context ctx, String val) throws InterruptedException { + 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(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(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(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(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(ObjectLib.class)); + env.setProto("function", wp.getProto(FunctionLib.class)); + env.setProto("array", wp.getProto(ArrayLib.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(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(ObjectLib.class).setPrototype(null, null); + + System.out.println("Loaded polyfills!"); + } +} diff --git a/src/me/topchetoeu/jscript/polyfills/JSONPolyfill.java b/src/me/topchetoeu/jscript/lib/JSONLib.java similarity index 96% rename from src/me/topchetoeu/jscript/polyfills/JSONPolyfill.java rename to src/me/topchetoeu/jscript/lib/JSONLib.java index 9d70719..d36a4ea 100644 --- a/src/me/topchetoeu/jscript/polyfills/JSONPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/JSONLib.java @@ -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()) { diff --git a/src/me/topchetoeu/jscript/polyfills/MapPolyfill.java b/src/me/topchetoeu/jscript/lib/MapLib.java similarity index 91% rename from src/me/topchetoeu/jscript/polyfills/MapPolyfill.java rename to src/me/topchetoeu/jscript/lib/MapLib.java index 1791c81..6fc6dab 100644 --- a/src/me/topchetoeu/jscript/polyfills/MapPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/MapLib.java @@ -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 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)); diff --git a/src/me/topchetoeu/jscript/lib/MathLib.java b/src/me/topchetoeu/jscript/lib/MathLib.java new file mode 100644 index 0000000..0dbf1cb --- /dev/null +++ b/src/me/topchetoeu/jscript/lib/MathLib.java @@ -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); } +} diff --git a/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java b/src/me/topchetoeu/jscript/lib/NumberLib.java similarity index 90% rename from src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java rename to src/me/topchetoeu/jscript/lib/NumberLib.java index a9e30c6..28dac9c 100644 --- a/src/me/topchetoeu/jscript/polyfills/NumberPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/NumberLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java b/src/me/topchetoeu/jscript/lib/ObjectLib.java similarity index 95% rename from src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java rename to src/me/topchetoeu/jscript/lib/ObjectLib.java index 1dca001..35f9c6b 100644 --- a/src/me/topchetoeu/jscript/polyfills/ObjectPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/ObjectLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java b/src/me/topchetoeu/jscript/lib/PromiseLib.java similarity index 90% rename from src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java rename to src/me/topchetoeu/jscript/lib/PromiseLib.java index 0235667..bd393e8 100644 --- a/src/me/topchetoeu/jscript/polyfills/PromisePolyfill.java +++ b/src/me/topchetoeu/jscript/lib/PromiseLib.java @@ -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); } diff --git a/src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java b/src/me/topchetoeu/jscript/lib/RangeErrorLib.java similarity index 82% rename from src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java rename to src/me/topchetoeu/jscript/lib/RangeErrorLib.java index 99bfa82..b0ce327 100644 --- a/src/me/topchetoeu/jscript/polyfills/RangeErrorPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/RangeErrorLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java b/src/me/topchetoeu/jscript/lib/RegExpLib.java similarity index 91% rename from src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java rename to src/me/topchetoeu/jscript/lib/RegExpLib.java index 1fb885c..3e54df0 100644 --- a/src/me/topchetoeu/jscript/polyfills/RegExpPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/RegExpLib.java @@ -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() { 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); } } diff --git a/src/me/topchetoeu/jscript/polyfills/SetPolyfill.java b/src/me/topchetoeu/jscript/lib/SetLib.java similarity index 92% rename from src/me/topchetoeu/jscript/polyfills/SetPolyfill.java rename to src/me/topchetoeu/jscript/lib/SetLib.java index c6bc7f3..069c79f 100644 --- a/src/me/topchetoeu/jscript/polyfills/SetPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/SetLib.java @@ -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 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); } } diff --git a/src/me/topchetoeu/jscript/polyfills/StringPolyfill.java b/src/me/topchetoeu/jscript/lib/StringLib.java similarity index 97% rename from src/me/topchetoeu/jscript/polyfills/StringPolyfill.java rename to src/me/topchetoeu/jscript/lib/StringLib.java index de56291..9621487 100644 --- a/src/me/topchetoeu/jscript/polyfills/StringPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/StringLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java b/src/me/topchetoeu/jscript/lib/SymbolLib.java similarity index 94% rename from src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java rename to src/me/topchetoeu/jscript/lib/SymbolLib.java index 6dfb33e..8edb69f 100644 --- a/src/me/topchetoeu/jscript/polyfills/SymbolPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/SymbolLib.java @@ -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 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; } diff --git a/src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java b/src/me/topchetoeu/jscript/lib/SyntaxErrorLib.java similarity index 82% rename from src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java rename to src/me/topchetoeu/jscript/lib/SyntaxErrorLib.java index 27c6ed1..70d61fa 100644 --- a/src/me/topchetoeu/jscript/polyfills/SyntaxErrorPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/SyntaxErrorLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java b/src/me/topchetoeu/jscript/lib/TypeErrorLib.java similarity index 82% rename from src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java rename to src/me/topchetoeu/jscript/lib/TypeErrorLib.java index 5814f69..9df269c 100644 --- a/src/me/topchetoeu/jscript/polyfills/TypeErrorPolyfill.java +++ b/src/me/topchetoeu/jscript/lib/TypeErrorLib.java @@ -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; } diff --git a/src/me/topchetoeu/jscript/polyfills/Math.java b/src/me/topchetoeu/jscript/polyfills/Math.java deleted file mode 100644 index 3be504a..0000000 --- a/src/me/topchetoeu/jscript/polyfills/Math.java +++ /dev/null @@ -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); - } -}