shithub: libmujs

Download patch

ref: b22dc395c99510adc9e7f15906accd8897be289c
parent: 46f726843cdc443b93cba6facecf6909b8ec78cb
author: Tor Andersson <tor@ccxvii.net>
date: Thu Jan 23 21:13:20 EST 2014

Use ES5 specification for implementation of Error.prototype.toString().

--- a/jserror.c
+++ b/jserror.c
@@ -7,11 +7,33 @@
 
 static int Ep_toString(js_State *J, int argc)
 {
+	const char *name = "Error";
+	const char *message = "";
+
+	if (!js_isobject(J, -1))
+		js_typeerror(J, "not an object");
+
 	js_getproperty(J, 0, "name");
-	js_pushliteral(J, ": ");
-	js_concat(J);
+	if (!js_isundefined(J, -1))
+		name = js_tostring(J, -1);
+	js_pop(J, 1);
+
 	js_getproperty(J, 0, "message");
-	js_concat(J);
+	if (!js_isundefined(J, -1))
+		message = js_tostring(J, -1);
+	js_pop(J, 1);
+
+	if (!strcmp(name, ""))
+		js_pushliteral(J, message);
+	else if (!strcmp(message, ""))
+		js_pushliteral(J, name);
+	else {
+		js_pushliteral(J, name);
+		js_pushliteral(J, ": ");
+		js_concat(J);
+		js_pushliteral(J, message);
+		js_concat(J);
+	}
 	return 1;
 }
 
--- a/main.c
+++ b/main.c
@@ -24,6 +24,7 @@
 			js_dostring(J, line, 1);
 			fputs(PS1, stdout);
 		}
+		putchar('\n');
 		js_gc(J, 1);
 	}