From: Dmitry Volyntsev Date: Fri, 14 Jun 2019 18:19:52 +0000 (+0300) Subject: Improved njs_object_own_enumerate(). X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=acddac9e92c0a951ab393af735f7fa43f795676e;p=njs.git Improved njs_object_own_enumerate(). Enumerating enumerable shared_hash entries even if all is false. --- diff --git a/njs/njs_builtin.c b/njs/njs_builtin.c index 14e03d1c..27587b01 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -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, }, { diff --git a/njs/njs_json.c b/njs/njs_json.c index be7569dc..f39add7c 100644 --- a/njs/njs_json.c +++ b/njs/njs_json.c @@ -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; diff --git a/njs/njs_object.c b/njs/njs_object.c index b3d6df09..b8e68e47 100644 --- a/njs/njs_object.c +++ b/njs/njs_object.c @@ -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); } } } diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index d9712144..d4107932 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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(