[njs] Improved njs_object_own_enumerate().

Dmitry Volyntsev xeioex at nginx.com
Fri Jun 14 18:20:50 UTC 2019


details:   https://hg.nginx.org/njs/rev/fb9621798309
branches:  
changeset: 1005:fb9621798309
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jun 14 21:19:52 2019 +0300
description:
Improved njs_object_own_enumerate().

Enumerating enumerable shared_hash entries even if all is false.

diffstat:

 njs/njs_builtin.c        |   2 +
 njs/njs_json.c           |  11 ++++-
 njs/njs_object.c         |  88 ++++++++++++++++++++++-------------------------
 njs/test/njs_unit_test.c |   3 +
 4 files changed, 55 insertions(+), 49 deletions(-)

diffs (160 lines):

diff -r 028277f5c551 -r fb9621798309 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Tue Jun 11 17:34:59 2019 +0300
+++ b/njs/njs_builtin.c	Fri Jun 14 21:19:52 2019 +0300
@@ -1081,6 +1081,8 @@ static const njs_object_prop_t  njs_njs_
         .type = NJS_PROPERTY,
         .name = njs_string("version"),
         .value = njs_string(NJS_VERSION),
+        .configurable = 1,
+        .enumerable = 1,
     },
 
     {
diff -r 028277f5c551 -r fb9621798309 njs/njs_json.c
--- a/njs/njs_json.c	Tue Jun 11 17:34:59 2019 +0300
+++ b/njs/njs_json.c	Fri Jun 14 21:19:52 2019 +0300
@@ -2354,6 +2354,7 @@ njs_vm_value_dump(njs_vm_t *vm, nxt_str_
     njs_ret_t             ret;
     nxt_str_t             str;
     njs_value_t           *key, *val, ext_val;
+    njs_object_t          *object;
     njs_json_state_t      *state;
     njs_object_prop_t     *prop;
     nxt_lvlhsh_query_t    lhq;
@@ -2446,11 +2447,15 @@ njs_vm_value_dump(njs_vm_t *vm, nxt_str_
                 val = &ext_val;
 
             } else {
+                object = state->value.data.u.object;
                 lhq.proto = &njs_object_hash_proto;
 
-                ret = nxt_lvlhsh_find(&state->value.data.u.object->hash, &lhq);
-                if (nxt_slow_path(ret == NXT_DECLINED)) {
-                    break;
+                ret = nxt_lvlhsh_find(&object->hash, &lhq);
+                if (ret == NXT_DECLINED) {
+                    ret = nxt_lvlhsh_find(&object->shared_hash, &lhq);
+                    if (nxt_slow_path(ret == NXT_DECLINED)) {
+                        break;
+                    }
                 }
 
                 prop = lhq.value;
diff -r 028277f5c551 -r fb9621798309 njs/njs_object.c
--- a/njs/njs_object.c	Tue Jun 11 17:34:59 2019 +0300
+++ b/njs/njs_object.c	Fri Jun 14 21:19:52 2019 +0300
@@ -667,29 +667,27 @@ njs_object_own_enumerate_object_length(c
         }
     }
 
-    if (nxt_slow_path(all)) {
-        nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
-        hash = &object->shared_hash;
-
-        for ( ;; ) {
-            prop = nxt_lvlhsh_each(hash, &lhe);
-
-            if (prop == NULL) {
-                break;
-            }
-
-            lhq.key_hash = lhe.key_hash;
-            njs_string_get(&prop->name, &lhq.key);
-
-            lhq.proto = &njs_object_hash_proto;
-            ret = nxt_lvlhsh_find(&object->hash, &lhq);
-
-            if (ret != NXT_OK) {
-                ext_prop = njs_object_exist_in_proto(parent, object, &lhq);
-
-                if (ext_prop == NULL) {
-                    length++;
-                }
+    nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
+    hash = &object->shared_hash;
+
+    for ( ;; ) {
+        prop = nxt_lvlhsh_each(hash, &lhe);
+
+        if (prop == NULL) {
+            break;
+        }
+
+        lhq.key_hash = lhe.key_hash;
+        njs_string_get(&prop->name, &lhq.key);
+
+        lhq.proto = &njs_object_hash_proto;
+        ret = nxt_lvlhsh_find(&object->hash, &lhq);
+
+        if (ret != NXT_OK) {
+            ext_prop = njs_object_exist_in_proto(parent, object, &lhq);
+
+            if (ext_prop == NULL && (prop->enumerable || all)) {
+                length++;
             }
         }
     }
@@ -949,29 +947,27 @@ njs_object_own_enumerate_object(njs_vm_t
             }
         }
 
-        if (nxt_slow_path(all)) {
-            nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
-            hash = &object->shared_hash;
-
-            for ( ;; ) {
-                prop = nxt_lvlhsh_each(hash, &lhe);
-
-                if (prop == NULL) {
-                    break;
-                }
-
-                lhq.key_hash = lhe.key_hash;
-                njs_string_get(&prop->name, &lhq.key);
-
-                lhq.proto = &njs_object_hash_proto;
-                ret = nxt_lvlhsh_find(&object->hash, &lhq);
-
-                if (ret != NXT_OK) {
-                    ext_prop = njs_object_exist_in_proto(parent, object, &lhq);
-
-                    if (ext_prop == NULL) {
-                        njs_string_copy(item++, &prop->name);
-                    }
+        nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
+        hash = &object->shared_hash;
+
+        for ( ;; ) {
+            prop = nxt_lvlhsh_each(hash, &lhe);
+
+            if (prop == NULL) {
+                break;
+            }
+
+            lhq.key_hash = lhe.key_hash;
+            njs_string_get(&prop->name, &lhq.key);
+
+            lhq.proto = &njs_object_hash_proto;
+            ret = nxt_lvlhsh_find(&object->hash, &lhq);
+
+            if (ret != NXT_OK) {
+                ext_prop = njs_object_exist_in_proto(parent, object, &lhq);
+
+                if (ext_prop == NULL && (prop->enumerable || all)) {
+                    njs_string_copy(item++, &prop->name);
                 }
             }
         }
diff -r 028277f5c551 -r fb9621798309 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Jun 11 17:34:59 2019 +0300
+++ b/njs/test/njs_unit_test.c	Fri Jun 14 21:19:52 2019 +0300
@@ -12278,6 +12278,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("njs.dump($r.header)"),
       nxt_string("{type:\"object\",props:[\"getter\",\"foreach\",\"next\"]}") },
 
+    { nxt_string("njs.dump(njs) == `{version:'${njs.version}'}`"),
+      nxt_string("true") },
+
     /* Built-in methods name. */
 
     { nxt_string(


More information about the nginx-devel mailing list