From a38ab1794d8fd9702ccf3b40f11804b6c38c8fe2 Mon Sep 17 00:00:00 2001 From: shiroyk Date: Sun, 4 Aug 2024 13:48:55 +0800 Subject: [PATCH] Added Error cause --- builtin_error.go | 27 ++++++++++++++++++++++++++- tc39_test.go | 1 - 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/builtin_error.go b/builtin_error.go index b07bf6a7..a7eae7da 100644 --- a/builtin_error.go +++ b/builtin_error.go @@ -122,7 +122,19 @@ func (r *Runtime) newErrorObject(proto *Object, class string) *errorObject { func (r *Runtime) builtin_Error(args []Value, proto *Object) *Object { obj := r.newErrorObject(proto, classError) if len(args) > 0 && args[0] != _undefined { - obj._putProp("message", args[0], true, false, true) + obj._putProp("message", args[0].ToString(), true, false, true) + } + if len(args) > 1 && args[1] != _undefined { + if options, ok := args[1].(*Object); ok { + if options.hasProperty(asciiString("cause")) { + obj.defineOwnPropertyStr("cause", PropertyDescriptor{ + Writable: FLAG_TRUE, + Enumerable: FLAG_FALSE, + Configurable: FLAG_TRUE, + Value: options.Get("cause"), + }, true) + } + } } return obj.val } @@ -138,6 +150,19 @@ func (r *Runtime) builtin_AggregateError(args []Value, proto *Object) *Object { } obj._putProp("errors", r.newArrayValues(errors), true, false, true) + if len(args) > 2 && args[2] != _undefined { + if options, ok := args[2].(*Object); ok { + if options.hasProperty(asciiString("cause")) { + obj.defineOwnPropertyStr("cause", PropertyDescriptor{ + Writable: FLAG_TRUE, + Enumerable: FLAG_FALSE, + Configurable: FLAG_TRUE, + Value: options.Get("cause"), + }, true) + } + } + } + return obj.val } diff --git a/tc39_test.go b/tc39_test.go index ea9d2234..bea79580 100644 --- a/tc39_test.go +++ b/tc39_test.go @@ -226,7 +226,6 @@ var ( "__setter__", "ShadowRealm", "SharedArrayBuffer", - "error-cause", "decorators", "regexp-v-flag", }