浏览代码

Added Error cause

shiroyk 1 年之前
父节点
当前提交
a38ab1794d
共有 2 个文件被更改,包括 26 次插入2 次删除
  1. 26 1
      builtin_error.go
  2. 0 1
      tc39_test.go

+ 26 - 1
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
 }
 

+ 0 - 1
tc39_test.go

@@ -226,7 +226,6 @@ var (
 		"__setter__",
 		"ShadowRealm",
 		"SharedArrayBuffer",
-		"error-cause",
 		"decorators",
 		"regexp-v-flag",
 	}