[njs] Fixed string object creation using Object() constructor.

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 25 12:19:45 UTC 2019


details:   https://hg.nginx.org/njs/rev/e104c8c583c2
branches:  
changeset: 925:e104c8c583c2
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Apr 25 15:19:36 2019 +0300
description:
Fixed string object creation using Object() constructor.

diffstat:

 njs/njs_object.c         |  9 ++++++++-
 njs/njs_string.c         |  2 --
 njs/test/njs_unit_test.c |  3 +++
 3 files changed, 11 insertions(+), 3 deletions(-)

diffs (44 lines):

diff -r cbb1817c5a39 -r e104c8c583c2 njs/njs_object.c
--- a/njs/njs_object.c	Wed Apr 24 19:04:23 2019 +0300
+++ b/njs/njs_object.c	Thu Apr 25 15:19:36 2019 +0300
@@ -107,7 +107,14 @@ njs_object_value_alloc(njs_vm_t *vm, con
 
     if (nxt_fast_path(ov != NULL)) {
         nxt_lvlhsh_init(&ov->object.hash);
-        nxt_lvlhsh_init(&ov->object.shared_hash);
+
+        if (type == NJS_STRING) {
+            ov->object.shared_hash = vm->shared->string_instance_hash;
+
+        } else {
+            nxt_lvlhsh_init(&ov->object.shared_hash);
+        }
+
         ov->object.type = njs_object_value_type(type);
         ov->object.shared = 0;
         ov->object.extensible = 1;
diff -r cbb1817c5a39 -r e104c8c583c2 njs/njs_string.c
--- a/njs/njs_string.c	Wed Apr 24 19:04:23 2019 +0300
+++ b/njs/njs_string.c	Thu Apr 25 15:19:36 2019 +0300
@@ -555,8 +555,6 @@ njs_string_constructor(njs_vm_t *vm, njs
             return NXT_ERROR;
         }
 
-        object->shared_hash = vm->shared->string_instance_hash;
-
         vm->retval.data.u.object = object;
         vm->retval.type = NJS_OBJECT_STRING;
         vm->retval.data.truth = 1;
diff -r cbb1817c5a39 -r e104c8c583c2 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Apr 24 19:04:23 2019 +0300
+++ b/njs/test/njs_unit_test.c	Thu Apr 25 15:19:36 2019 +0300
@@ -8437,6 +8437,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("new String(123)"),
       nxt_string("123") },
 
+    { nxt_string("Object('123').length"),
+      nxt_string("3") },
+
     { nxt_string("new String(123).length"),
       nxt_string("3") },
 


More information about the nginx-devel mailing list