[njs] Fixed Object.prototype.valueOf() according to the specification.

Dmitry Volyntsev xeioex at nginx.com
Sat Nov 23 11:29:18 UTC 2019


details:   https://hg.nginx.org/njs/rev/180b28e542b6
branches:  
changeset: 1252:180b28e542b6
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Sat Nov 23 00:09:26 2019 +0300
description:
Fixed Object.prototype.valueOf() according to the specification.

This closes #256 issue on Github.

diffstat:

 src/njs_object.c         |   6 +++++-
 src/test/njs_unit_test.c |  11 +++++++++++
 2 files changed, 16 insertions(+), 1 deletions(-)

diffs (37 lines):

diff -r 133c31ef36e5 -r 180b28e542b6 src/njs_object.c
--- a/src/njs_object.c	Fri Nov 22 19:03:23 2019 +0300
+++ b/src/njs_object.c	Sat Nov 23 00:09:26 2019 +0300
@@ -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;
 }
diff -r 133c31ef36e5 -r 180b28e542b6 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Nov 22 19:03:23 2019 +0300
+++ b/src/test/njs_unit_test.c	Sat Nov 23 00:09:26 2019 +0300
@@ -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") },
 


More information about the nginx-devel mailing list