Skip to content

Commit

Permalink
Implement Error causes (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Nov 21, 2023
1 parent d8ea7df commit 7aabea9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions quickjs-atom.h
Expand Up @@ -82,6 +82,7 @@ DEF(length, "length")
DEF(fileName, "fileName")
DEF(lineNumber, "lineNumber")
DEF(message, "message")
DEF(cause, "cause")
DEF(errors, "errors")
DEF(stack, "stack")
DEF(name, "name")
Expand Down
19 changes: 18 additions & 1 deletion quickjs.c
Expand Up @@ -35282,8 +35282,10 @@ static JSValue iterator_to_array(JSContext *ctx, JSValueConst items)
static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
int argc, JSValueConst *argv, int magic)
{
JSValue obj, msg, proto;
JSValue obj, msg, proto, cause;
JSValueConst message;
int opts;
BOOL present;

if (JS_IsUndefined(new_target))
new_target = JS_GetActiveFunction(ctx);
Expand Down Expand Up @@ -35311,8 +35313,10 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
return obj;
if (magic == JS_AGGREGATE_ERROR) {
message = argv[1];
opts = 2;
} else {
message = argv[0];
opts = 1;
}

if (!JS_IsUndefined(message)) {
Expand All @@ -35323,6 +35327,19 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
}

if (argc > opts && JS_VALUE_GET_TAG(argv[opts]) == JS_TAG_OBJECT) {
present = JS_HasProperty(ctx, argv[opts], JS_ATOM_cause);
if (unlikely(present < 0))
goto exception;
if (present) {
cause = JS_GetProperty(ctx, argv[opts], JS_ATOM_cause);
if (unlikely(JS_IsException(cause)))
goto exception;
JS_DefinePropertyValue(ctx, obj, JS_ATOM_cause, cause,
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
}
}

if (magic == JS_AGGREGATE_ERROR) {
JSValue error_list = iterator_to_array(ctx, argv[0]);
if (JS_IsException(error_list))
Expand Down
2 changes: 1 addition & 1 deletion test262.conf
Expand Up @@ -102,7 +102,7 @@ default-parameters
destructuring-assignment
destructuring-binding
dynamic-import
error-cause=skip
error-cause
exponentiation
export-star-as-namespace-from-module
FinalizationGroup=skip
Expand Down

0 comments on commit 7aabea9

Please sign in to comment.