]> git.kaiwu.me - njs.git/commitdiff
Fixed Error() constructor with no arguments.
authorhongzhidao <hongzhidao@gmail.com>
Sun, 4 Aug 2019 07:59:42 +0000 (03:59 -0400)
committerhongzhidao <hongzhidao@gmail.com>
Sun, 4 Aug 2019 07:59:42 +0000 (03:59 -0400)
src/njs_error.c
src/njs_value.h
src/test/njs_unit_test.c

index 97f8c0c7b8de6f858e474a0a82d2de46d8aace80..1729f3a1de6891be8bcd430198b2c0f78f958667 100644 (file)
@@ -133,14 +133,10 @@ njs_error_create(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_object_t       *error;
     const njs_value_t  *value;
 
-    if (nargs == 1) {
-        value = &njs_string_empty;
+    value = njs_arg(args, nargs, 1);
 
-    } else {
-        value = &args[1];
-    }
-
-    error = njs_error_alloc(vm, type, NULL, value);
+    error = njs_error_alloc(vm, type, NULL,
+                            njs_is_defined(value) ? value : NULL);
     if (njs_slow_path(error == NULL)) {
         return NJS_ERROR;
     }
index b59ef9e027ad6b95a976a45eb7ee6a2f779f3c85..fac48342439e31d9a7bbff73c9ec4dc6ab7d9d29 100644 (file)
@@ -425,6 +425,10 @@ typedef struct {
     ((value)->type == NJS_UNDEFINED)
 
 
+#define njs_is_defined(value)                                                 \
+    ((value)->type != NJS_UNDEFINED)
+
+
 #define njs_is_null_or_undefined(value)                                       \
     ((value)->type <= NJS_UNDEFINED)
 
index 61c6ad158a0e03ed3e4d4c95d1d3a3c0f0a3e7c3..a87ab398800998c2b269fb46f921e24c5720afee 100644 (file)
@@ -7865,6 +7865,11 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Error().__proto__.__proto__ == Object.prototype"),
       njs_str("true") },
 
+    { njs_str("Error.prototype.message = 'm';"
+              "Error.prototype.name = 'n';"
+              "new Error()"),
+      njs_str("n: m") },
+
     { njs_str("EvalError('e')"),
       njs_str("EvalError: e") },