shithub: libmujs

Download patch

ref: ebbc191fe18bf1d0c8e6a588a92880af38b0ca71
parent: 3f5f3034d2bf159e4e03ce08835d43c37591ed10
author: Tor Andersson <[email protected]>
date: Tue May 23 06:54:17 EDT 2017

Add fast integer special case in jsV_numbertostring.

Also fix js_itoa to support signed integers.

--- a/jsi.h
+++ b/jsi.h
@@ -11,6 +11,7 @@
 #include <setjmp.h>
 #include <math.h>
 #include <float.h>
+#include <limits.h>
 
 /* Microsoft Visual C */
 #ifdef _MSC_VER
--- a/jsvalue.c
+++ b/jsvalue.c
@@ -128,10 +128,17 @@
 	}
 }
 
-const char *js_itoa(char *out, int a)
+const char *js_itoa(char *out, int v)
 {
 	char buf[32], *s = out;
+	unsigned int a;
 	int i = 0;
+	if (v < 0) {
+		a = -v;
+		*s++ = '-';
+	} else {
+		a = v;
+	}
 	while (a) {
 		buf[i++] = (a % 10) + '0';
 		a /= 10;
@@ -222,9 +229,18 @@
 	char digits[32], *p = buf, *s = digits;
 	int exp, ndigits, point;
 
+	if (f == 0) return "0";
 	if (isnan(f)) return "NaN";
 	if (isinf(f)) return f < 0 ? "-Infinity" : "Infinity";
-	if (f == 0) return "0";
+
+	/* Fast case for integers. This only works assuming all integers can be
+	 * exactly represented by a float. This is true for 32-bit integers and
+	 * 64-bit floats. */
+	if (f >= INT_MIN && f <= INT_MAX) {
+		int i = (int)f;
+		if ((double)i == f)
+			return js_itoa(buf, i);
+	}
 
 	ndigits = js_grisu2(f, digits, &exp);
 	point = ndigits + exp;