Skip to content

Commit 2605297

Browse files
committed
DOMException backtrace
The standard doesn't specify where to put "stack". This follows node instead of browsers because it's more straightforward to implement it this way.
1 parent 2977be7 commit 2605297

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

quickjs.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6720,7 +6720,7 @@ static bool can_add_backtrace(JSValueConst obj)
67206720
if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
67216721
return false;
67226722
p = JS_VALUE_GET_OBJ(obj);
6723-
if (p->class_id != JS_CLASS_ERROR)
6723+
if (p->class_id != JS_CLASS_ERROR && p->class_id != JS_CLASS_DOM_EXCEPTION)
67246724
return false;
67256725
if (find_own_property1(p, JS_ATOM_stack))
67266726
return false;
@@ -57684,7 +57684,8 @@ static void js_domexception_mark(JSRuntime *rt, JSValueConst val,
5768457684
}
5768557685

5768657686
static JSValue js_domexception_constructor0(JSContext *ctx, JSValueConst new_target,
57687-
int argc, JSValueConst *argv)
57687+
int argc, JSValueConst *argv,
57688+
bool add_backtrace)
5768857689
{
5768957690
JSDOMExceptionData *s;
5769057691
JSValue obj, message, name;
@@ -57711,6 +57712,9 @@ static JSValue js_domexception_constructor0(JSContext *ctx, JSValueConst new_tar
5771157712
s->message = message;
5771257713
s->code = -1;
5771357714
JS_SetOpaqueInternal(obj, s);
57715+
if (add_backtrace)
57716+
build_backtrace(ctx, obj, JS_UNDEFINED, NULL, 0, 0,
57717+
JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL);
5771457718
return obj;
5771557719
fail3:
5771657720
JS_FreeValue(ctx, name);
@@ -57726,7 +57730,7 @@ static JSValue js_domexception_constructor(JSContext *ctx, JSValueConst new_targ
5772657730
{
5772757731
if (JS_IsUndefined(new_target))
5772857732
return JS_ThrowTypeError(ctx, "constructor requires 'new'");
57729-
return js_domexception_constructor0(ctx, new_target, argc, argv);
57733+
return js_domexception_constructor0(ctx, new_target, argc, argv, true);
5773057734
}
5773157735

5773257736
static JSValue js_domexception_get_name(JSContext *ctx, JSValueConst this_val)
@@ -57803,9 +57807,10 @@ JSValue JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowDOMException(JSContext *ctx, const c
5780357807
}
5780457808
argv[0] = js_message;
5780557809
argv[1] = js_name;
57806-
obj = js_domexception_constructor0(ctx, JS_UNDEFINED, 2, argv);
57810+
obj = js_domexception_constructor0(ctx, JS_UNDEFINED, 2, argv, false);
5780757811
JS_FreeValue(ctx, js_message);
5780857812
JS_FreeValue(ctx, js_name);
57813+
build_backtrace(ctx, obj, JS_UNDEFINED, NULL, 0, 0, 0);
5780957814
JS_Throw(ctx, obj);
5781057815
end:
5781157816
va_end(ap);

tests/test_domexception.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ function test_properties() {
2424
assertThrows(TypeError, () => ex.message = "");
2525
assertThrows(TypeError, () => ex.name = "test");
2626
assert(ex.__proto__, DOMException.prototype);
27-
assert(Object.getOwnPropertyNames(ex).length, 0);
27+
/* Note: browsers set "stack" on the prototype, not the object.
28+
* This follows node. */
29+
assert(Object.getOwnPropertyNames(ex), ["stack"]);
2830
}
2931

3032
test_code();

0 commit comments

Comments
 (0)