about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorFabrice Bellard <fabrice@bellard.org>2024-01-02 16:08:08 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-11 18:38:17 +0100
commit7aa1f7014caa781231eb0d78584b663d081a550a (patch)
treefd8c85840adcdfd70f8eb066e0bb01115f607c4c /lib
parentaeb9b9e5dddf3102773aa900865fe11a91cd65a6 (diff)
downloadchawan-7aa1f7014caa781231eb0d78584b663d081a550a.tar.gz
added Error cause
Diffstat (limited to 'lib')
-rw-r--r--lib/quickjs/quickjs-atom.h1
-rw-r--r--lib/quickjs/quickjs.c26
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/quickjs/quickjs-atom.h b/lib/quickjs/quickjs-atom.h
index e931c31f..f62a7c40 100644
--- a/lib/quickjs/quickjs-atom.h
+++ b/lib/quickjs/quickjs-atom.h
@@ -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")
diff --git a/lib/quickjs/quickjs.c b/lib/quickjs/quickjs.c
index 6f888b32..0a1ea0b0 100644
--- a/lib/quickjs/quickjs.c
+++ b/lib/quickjs/quickjs.c
@@ -38320,7 +38320,8 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
                                     int argc, JSValueConst *argv, int magic)
 {
     JSValue obj, msg, proto;
-    JSValueConst message;
+    JSValueConst message, options;
+    int arg_index;
 
     if (JS_IsUndefined(new_target))
         new_target = JS_GetActiveFunction(ctx);
@@ -38346,12 +38347,9 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
     JS_FreeValue(ctx, proto);
     if (JS_IsException(obj))
         return obj;
-    if (magic == JS_AGGREGATE_ERROR) {
-        message = argv[1];
-    } else {
-        message = argv[0];
-    }
+    arg_index = (magic == JS_AGGREGATE_ERROR);
 
+    message = argv[arg_index++];
     if (!JS_IsUndefined(message)) {
         msg = JS_ToString(ctx, message);
         if (unlikely(JS_IsException(msg)))
@@ -38360,6 +38358,22 @@ static JSValue js_error_constructor(JSContext *ctx, JSValueConst new_target,
                                JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
     }
 
+    if (arg_index < argc) {
+        options = argv[arg_index];
+        if (JS_IsObject(options)) {
+            int present = JS_HasProperty(ctx, options, JS_ATOM_cause);
+            if (present < 0)
+                goto exception;
+            if (present) {
+                JSValue cause = JS_GetProperty(ctx, options, JS_ATOM_cause);
+                if (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))