]> git.kaiwu.me - njs.git/commitdiff
Fixed Object.prototype.valueOf() according to the specification.
authorArtem S. Povalyukhin <artem.povaluhin@gmail.com>
Fri, 22 Nov 2019 21:09:26 +0000 (00:09 +0300)
committerArtem S. Povalyukhin <artem.povaluhin@gmail.com>
Fri, 22 Nov 2019 21:09:26 +0000 (00:09 +0300)
This closes #256 issue on Github.

src/njs_object.c
src/test/njs_unit_test.c

index 3f1ac4001f0b480b2168b4c3ba1b5bd940d7de55..7ab79c21eb1c1ba6b9595b98709b89250222a864 100644 (file)
@@ -2242,7 +2242,11 @@ static njs_int_t
 njs_object_prototype_value_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
-    vm->retval = args[0];
+    vm->retval = *njs_argument(args, 0);
+
+    if (!njs_is_object(&vm->retval)) {
+        return njs_value_to_object(vm, &vm->retval);
+    }
 
     return NJS_OK;
 }
index 6a651a91f9194be8e9f2fff4e5af9c0251b22a24..657568428ad01446689df42712439cb6fd6cf875 100644 (file)
@@ -9531,6 +9531,17 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Object.prototype.valueOf.prototype"),
       njs_str("undefined") },
 
+    { njs_str("Object.prototype.valueOf.call()"),
+      njs_str("TypeError: cannot convert null or undefined to object") },
+
+    { njs_str("Object.prototype.valueOf.call(null)"),
+      njs_str("TypeError: cannot convert null or undefined to object") },
+
+    { njs_str("[false, NaN, Symbol(), '']"
+              ".map((x) => Object.prototype.valueOf.call(x))"
+              ".map((x) => Object.prototype.toString.call(x))"),
+      njs_str("[object Boolean],[object Number],[object Symbol],[object String]") },
+
     { njs_str("Object.constructor === Function"),
       njs_str("true") },