shithub: libmujs

Download patch

ref: 2eb4fd22728afe62063a7b3ec77adf9c57ac6d09
parent: d11c71072e86425f98c24a45be8af9aacbe0ff08
author: Tor Andersson <tor.andersson@artifex.com>
date: Wed Feb 3 12:18:28 EST 2021

Bug 703459: Handle undefined argument to Error constructor.

--- a/jserror.c
+++ b/jserror.c
@@ -57,9 +57,8 @@
 
 static int jsB_ErrorX(js_State *J, js_Object *prototype)
 {
-	int top = js_gettop(J);
 	js_pushobject(J, jsV_newobject(J, JS_CERROR, prototype));
-	if (top > 1) {
+	if (js_isdefined(J, 1)) {
 		js_pushstring(J, js_tostring(J, 1));
 		js_defproperty(J, -2, "message", JS_DONTENUM);
 	}
@@ -109,7 +108,6 @@
 	js_pushobject(J, J->Error_prototype);
 	{
 			jsB_props(J, "name", "Error");
-			jsB_props(J, "message", "an error has occurred");
 			jsB_propf(J, "Error.prototype.toString", Ep_toString, 0);
 	}
 	js_newcconstructor(J, jsB_Error, jsB_Error, "Error", 1);
--- a/jsrepr.c
+++ b/jsrepr.c
@@ -220,9 +220,10 @@
 			js_puts(J, sb, js_tostring(J, -1));
 			js_pop(J, 1);
 			js_putc(J, sb, '(');
-			js_getproperty(J, -1, "message");
-			reprstr(J, sb, js_tostring(J, -1));
-			js_pop(J, 1);
+			if (js_hasproperty(J, -1, "message")) {
+				reprvalue(J, sb);
+				js_pop(J, 1);
+			}
 			js_puts(J, sb, "))");
 			break;
 		case JS_CMATH:
--- a/main.c
+++ b/main.c
@@ -220,10 +220,13 @@
 	"require.cache = Object.create(null);\n"
 ;
 
+
 static const char *stacktrace_js =
 	"Error.prototype.toString = function() {\n"
-	"if (this.stackTrace) return this.name + ': ' + this.message + this.stackTrace;\n"
-	"return this.name + ': ' + this.message;\n"
+	"var s = this.name;\n"
+	"if ('message' in this) s += ': ' + this.message;\n"
+	"if ('stackTrace' in this) s += this.stackTrace;\n"
+	"return s;\n"
 	"};\n"
 ;