From 3173919b49218fd41826119f3ebc4fae95ec3ce2 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 6 Jan 2024 17:48:35 +0200 Subject: [PATCH] fix: implement proper parseInt logic --- src/me/topchetoeu/jscript/lib/NumberLib.java | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/me/topchetoeu/jscript/lib/NumberLib.java b/src/me/topchetoeu/jscript/lib/NumberLib.java index d312e82..355feec 100644 --- a/src/me/topchetoeu/jscript/lib/NumberLib.java +++ b/src/me/topchetoeu/jscript/lib/NumberLib.java @@ -54,7 +54,28 @@ public class NumberLib { } @Expose(target = ExposeTarget.STATIC) public static double __parseInt(Arguments args) { - return args.getLong(0); + var radix = args.getInt(1, 10); + + if (radix < 2 || radix > 36) return Double.NaN; + else { + long res = 0; + + for (var c : args.getString(0).toCharArray()) { + var digit = 0; + + if (c >= '0' && c <= '9') digit = c - '0'; + else if (c >= 'a' && c <= 'z') digit = c - 'a' + 10; + else if (c >= 'A' && c <= 'Z') digit = c - 'A' + 10; + else break; + + if (digit > radix) break; + + res *= radix; + res += digit; + } + + return res; + } } @ExposeConstructor public static Object __constructor(Arguments args) {