shithub: libmujs

Download patch

ref: 17019d29e5494d4b0ae148a3043a940be78e3215
parent: 78ed7e6a99f2ab22b725203541c77ed4626b5d49
author: Tor Andersson <tor.andersson@artifex.com>
date: Wed Apr 8 10:05:53 EDT 2015

Rewrite parseInt to follow spec.

Return NaN for invalid radixes. Return -0 for parseInt('-0').

--- a/jsbuiltin.c
+++ b/jsbuiltin.c
@@ -31,22 +31,34 @@
 static void jsB_parseInt(js_State *J)
 {
 	const char *s = js_tostring(J, 1);
-	double radix = js_isdefined(J, 2) ? js_tonumber(J, 2) : 10;
-	char *e;
+	int radix = js_isdefined(J, 2) ? js_toint32(J, 2) : 10;
+	double sign = 1;
 	double n;
+	char *e;
 
-	while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
-	if (radix == 0)
+	while (jsY_iswhite(*s) || jsY_isnewline(*s))
+		++s;
+	if (*s == '-') {
+		++s;
+		sign = -1;
+	} else if (*s == '+') {
+		++s;
+	}
+	if (radix == 0) {
 		radix = 10;
-	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
-		s += 2;
-		radix = 16;
+		if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
+			s += 2;
+			radix = 16;
+		}
+	} else if (radix < 2 || radix > 32) {
+		js_pushnumber(J, NAN);
+		return;
 	}
 	n = strtol(s, &e, radix);
 	if (s == e)
 		js_pushnumber(J, NAN);
 	else
-		js_pushnumber(J, n);
+		js_pushnumber(J, n * sign);
 }
 
 static void jsB_parseFloat(js_State *J)