From 2eb4fd22728afe62063a7b3ec77adf9c57ac6d09 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 3 Feb 2021 17:18:28 +0100 Subject: [PATCH] Bug 703459: Handle undefined argument to Error constructor. --- jserror.c | 4 +--- jsrepr.c | 7 ++++--- main.c | 7 +++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/jserror.c b/jserror.c index b3a71f0..c454157 100644 --- a/jserror.c +++ b/jserror.c @@ -57,9 +57,8 @@ static void Ep_toString(js_State *J) 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 @@ void jsB_initerror(js_State *J) 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); diff --git a/jsrepr.c b/jsrepr.c index 9074f2a..8d63222 100644 --- a/jsrepr.c +++ b/jsrepr.c @@ -220,9 +220,10 @@ static void reprvalue(js_State *J, js_Buffer **sb) 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: diff --git a/main.c b/main.c index 7535c81..bbc5f2f 100644 --- a/main.c +++ b/main.c @@ -220,10 +220,13 @@ static const char *require_js = "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" ;