]> git.kaiwu.me - njs.git/commitdiff
Improved njs_object_own_enumerate().
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 14 Jun 2019 18:19:52 +0000 (21:19 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 14 Jun 2019 18:19:52 +0000 (21:19 +0300)
Enumerating enumerable shared_hash entries even if all is false.

njs/njs_builtin.c
njs/njs_json.c
njs/njs_object.c
njs/test/njs_unit_test.c

index 14e03d1ce838657fa1bc46bee6d0a4f4024ec422..27587b0173fb70b6060fffcb0cd1ac622a32aa56 100644 (file)
@@ -1081,6 +1081,8 @@ static const njs_object_prop_t  njs_njs_object_properties[] =
         .type = NJS_PROPERTY,
         .name = njs_string("version"),
         .value = njs_string(NJS_VERSION),
+        .configurable = 1,
+        .enumerable = 1,
     },
 
     {
index be7569dc1b57be1e1a1f6b3e2369c6a08455debf..f39add7cb92f4f0b452a247d584801867c8c7f86 100644 (file)
@@ -2354,6 +2354,7 @@ njs_vm_value_dump(njs_vm_t *vm, nxt_str_t *retval, const njs_value_t *value,
     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_t *retval, const njs_value_t *value,
                 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;
index b3d6df090885d7bccd96266042e01052bc3fec07..b8e68e47d0455885ca5c5496705f127e3c078f26 100644 (file)
@@ -667,29 +667,27 @@ njs_object_own_enumerate_object_length(const njs_object_t *object,
         }
     }
 
-    if (nxt_slow_path(all)) {
-        nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
-        hash = &object->shared_hash;
+    nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
+    hash = &object->shared_hash;
 
-        for ( ;; ) {
-            prop = nxt_lvlhsh_each(hash, &lhe);
+    for ( ;; ) {
+        prop = nxt_lvlhsh_each(hash, &lhe);
 
-            if (prop == NULL) {
-                break;
-            }
+        if (prop == NULL) {
+            break;
+        }
 
-            lhq.key_hash = lhe.key_hash;
-            njs_string_get(&prop->name, &lhq.key);
+        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);
+        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 (ret != NXT_OK) {
+            ext_prop = njs_object_exist_in_proto(parent, object, &lhq);
 
-                if (ext_prop == NULL) {
-                    length++;
-                }
+            if (ext_prop == NULL && (prop->enumerable || all)) {
+                length++;
             }
         }
     }
@@ -949,29 +947,27 @@ njs_object_own_enumerate_object(njs_vm_t *vm, const njs_object_t *object,
             }
         }
 
-        if (nxt_slow_path(all)) {
-            nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
-            hash = &object->shared_hash;
+        nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
+        hash = &object->shared_hash;
 
-            for ( ;; ) {
-                prop = nxt_lvlhsh_each(hash, &lhe);
+        for ( ;; ) {
+            prop = nxt_lvlhsh_each(hash, &lhe);
 
-                if (prop == NULL) {
-                    break;
-                }
+            if (prop == NULL) {
+                break;
+            }
 
-                lhq.key_hash = lhe.key_hash;
-                njs_string_get(&prop->name, &lhq.key);
+            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);
+            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 (ret != NXT_OK) {
+                ext_prop = njs_object_exist_in_proto(parent, object, &lhq);
 
-                    if (ext_prop == NULL) {
-                        njs_string_copy(item++, &prop->name);
-                    }
+                if (ext_prop == NULL && (prop->enumerable || all)) {
+                    njs_string_copy(item++, &prop->name);
                 }
             }
         }
index d971214487dcf0f328471aaa0dd7121c55855de0..d410793262cfa78d042e75d915f3207ebeff22a6 100644 (file)
@@ -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(