diff --git a/src/me/topchetoeu/jscript/lib/ArrayLib.java b/src/me/topchetoeu/jscript/lib/ArrayLib.java index 25756bc..b9b1065 100644 --- a/src/me/topchetoeu/jscript/lib/ArrayLib.java +++ b/src/me/topchetoeu/jscript/lib/ArrayLib.java @@ -6,6 +6,7 @@ import java.util.Stack; import me.topchetoeu.jscript.engine.Context; import me.topchetoeu.jscript.engine.values.ArrayValue; import me.topchetoeu.jscript.engine.values.FunctionValue; +import me.topchetoeu.jscript.engine.values.NativeFunction; import me.topchetoeu.jscript.engine.values.ObjectValue; import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.interop.Native; @@ -22,10 +23,10 @@ import me.topchetoeu.jscript.interop.NativeSetter; } @Native(thisArg = true) public static ObjectValue values(Context ctx, ArrayValue thisArg) { - return Values.fromJavaIterable(ctx, thisArg); + return Values.toJSIterator(ctx, thisArg); } @Native(thisArg = true) public static ObjectValue keys(Context ctx, ArrayValue thisArg) { - return Values.fromJavaIterable(ctx, () -> new Iterator() { + return Values.toJSIterator(ctx, () -> new Iterator() { private int i = 0; @Override @@ -40,7 +41,7 @@ import me.topchetoeu.jscript.interop.NativeSetter; }); } @Native(thisArg = true) public static ObjectValue entries(Context ctx, ArrayValue thisArg) { - return Values.fromJavaIterable(ctx, () -> new Iterator() { + return Values.toJSIterator(ctx, () -> new Iterator() { private int i = 0; @Override @@ -91,8 +92,11 @@ import me.topchetoeu.jscript.interop.NativeSetter; } @Native(thisArg = true) public static ArrayValue sort(Context ctx, ArrayValue arr, FunctionValue cmp) { + var defaultCmp = new NativeFunction("", (_ctx, thisArg, args) -> { + return Values.toString(ctx, args[0]).compareTo(Values.toString(ctx, args[1])); + }); arr.sort((a, b) -> { - var res = Values.toNumber(ctx, cmp.call(ctx, null, a, b)); + var res = Values.toNumber(ctx, (cmp == null ? defaultCmp : cmp).call(ctx, null, a, b)); if (res < 0) return -1; if (res > 0) return 1; return 0;