Bug 703459: Handle undefined argument to Error constructor.

This commit is contained in:
Tor Andersson
2021-02-03 17:18:28 +01:00
parent d11c71072e
commit 2eb4fd2272
3 changed files with 10 additions and 8 deletions

View File

@@ -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);

View File

@@ -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:

7
main.c
View File

@@ -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"
;