shithub: libmujs

Download patch

ref: b7afd400f6fe637f73175b514a42b978fe62b74e
parent: 6c37f967b4972c98e0bd55a804f3dab0795cb0b5
author: Tor Andersson <tor@ccxvii.net>
date: Fri Jan 31 11:14:51 EST 2014

Add js_pushlstring to avoid a lot of try/malloc/pushstring boilerplate.

--- a/js.h
+++ b/js.h
@@ -107,6 +107,7 @@
 void js_pushboolean(js_State *J, int v);
 void js_pushnumber(js_State *J, double v);
 void js_pushstring(js_State *J, const char *v);
+void js_pushlstring(js_State *J, const char *v, int n);
 void js_pushliteral(js_State *J, const char *v);
 
 void js_newobject(js_State *J);
--- a/jsarray.c
+++ b/jsarray.c
@@ -448,7 +448,7 @@
 	}
 	js_newcconstructor(J, jsB_new_Array, jsB_new_Array, 1);
 	{
-		/* ECMA-262-5 */
+		/* ES5 */
 		jsB_propf(J, "isArray", A_isArray, 1);
 	}
 	js_defglobal(J, "Array", JS_DONTENUM);
--- a/jsregexp.c
+++ b/jsregexp.c
@@ -11,8 +11,7 @@
 	int flags, opts;
 	regex_t *prog;
 	regmatch_t m[10];
-	char *s;
-	int i, n;
+	int i;
 
 	prog = js_toregexp(J, idx, &flags);
 
@@ -22,23 +21,10 @@
 
 	if (!regexec(prog, text, nelem(m), m, opts)) {
 		js_newarray(J);
-
-		s = malloc(strlen(text) + 1);
-		if (js_try(J)) {
-			free(s);
-			js_throw(J);
-		}
-
 		for (i = 0; i < nelem(m) && m[i].rm_so >= 0; ++i) {
-			n = m[i].rm_eo - m[i].rm_so;
-			memcpy(s, text + m[i].rm_so, n);
-			s[n] = 0;
-			js_pushstring(J, s);
+			js_pushlstring(J, text + m[i].rm_so, m[i].rm_eo - m[i].rm_so);
 			js_setindex(J, -2, i);
 		}
-
-		js_endtry(J);
-		free(s);
 		return 1;
 	}
 
--- a/jsrun.c
+++ b/jsrun.c
@@ -50,6 +50,27 @@
 	++TOP;
 }
 
+void js_pushlstring(js_State *J, const char *v, int n)
+{
+	char buf[256];
+	if (n + 1 < sizeof buf) {
+		memcpy(buf, v, n);
+		buf[n] = 0;
+		js_pushstring(J, buf);
+	} else {
+		char *s = malloc(n + 1);
+		memcpy(s, v, n);
+		s[n] = 0;
+		if (js_try(J)) {
+			free(s);
+			js_throw(J);
+		}
+		js_pushstring(J, s);
+		js_endtry(J);
+		free(s);
+	}
+}
+
 void js_pushliteral(js_State *J, const char *v)
 {
 	STACK[TOP].type = JS_TSTRING;
--- a/jsstring.c
+++ b/jsstring.c
@@ -48,7 +48,7 @@
 	return rune;
 }
 
-static inline const char *utfindex(const char *s, int i)
+static inline const char *utfidx(const char *s, int i)
 {
 	Rune rune = 0;
 	while (i-- > 0) {
@@ -167,27 +167,13 @@
 	return strcmp(a, b);
 }
 
-static char *substr(js_State *J, const char *src, int a, int b)
-{
-	int n = b - a;
-	const char *s = utfindex(src, a);
-	char *dst = malloc(UTFmax * n + 1), *d = dst;
-	while (n--) {
-		Rune rune;
-		s += chartorune(&rune, s);
-		d += runetochar(d, &rune);
-	}
-	*d = 0;
-	return dst;
-}
-
 static int Sp_slice(js_State *J, int argc)
 {
 	const char *str = js_tostring(J, 0);
+	const char *ss, *ee;
 	int len = utflen(str);
 	int s = js_tointeger(J, 1);
 	int e = argc > 1 ? js_tointeger(J, 2) : len;
-	char *out;
 
 	s = s < 0 ? s + len : s;
 	e = e < 0 ? e + len : e;
@@ -195,17 +181,15 @@
 	s = s < 0 ? 0 : s > len ? len : s;
 	e = e < 0 ? 0 : e > len ? len : e;
 
-	if (s < e)
-		out = substr(J, str, s, e);
-	else
-		out = substr(J, str, e, s);
-
-	if (js_try(J)) {
-		free(out);
-		js_throw(J);
+	if (s < e) {
+		ss = utfidx(str, s);
+		ee = utfidx(ss, e - s);
+	} else {
+		ss = utfidx(str, e);
+		ee = utfidx(ss, s - e);
 	}
-	js_pushstring(J, out);
-	js_endtry(J);
+
+	js_pushlstring(J, ss, ee - ss);
 	return 1;
 }
 
@@ -212,25 +196,23 @@
 static int Sp_substring(js_State *J, int argc)
 {
 	const char *str = js_tostring(J, 0);
+	const char *ss, *ee;
 	int len = utflen(str);
 	int s = js_tointeger(J, 1);
 	int e = argc > 1 ? js_tointeger(J, 2) : len;
-	char *out;
 
 	s = s < 0 ? 0 : s > len ? len : s;
 	e = e < 0 ? 0 : e > len ? len : e;
 
-	if (s < e)
-		out = substr(J, str, s, e);
-	else
-		out = substr(J, str, e, s);
-
-	if (js_try(J)) {
-		free(out);
-		js_throw(J);
+	if (s < e) {
+		ss = utfidx(str, s);
+		ee = utfidx(ss, e - s);
+	} else {
+		ss = utfidx(str, e);
+		ee = utfidx(ss, s - e);
 	}
-	js_pushstring(J, out);
-	js_endtry(J);
+
+	js_pushlstring(J, ss, ee - ss);
 	return 1;
 }