From: Dmitry Volyntsev Date: Mon, 27 Aug 2018 13:37:35 +0000 (+0300) Subject: Respecting the enumerable attribute while iterating by for in. X-Git-Tag: 0.2.4~20 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=8114521665020a0e31a37653ea4c79342d9e7ab0;p=njs.git Respecting the enumerable attribute while iterating by for in. --- diff --git a/njs/njs_vm.c b/njs/njs_vm.c index a63378a6..60ccacb3 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -1053,12 +1053,18 @@ njs_vmcode_property_next(njs_vm_t *vm, njs_value_t *object, njs_value_t *value) next->index = -1; } - prop = nxt_lvlhsh_each(&object->data.u.object->hash, &next->lhe); + for ( ;; ) { + prop = nxt_lvlhsh_each(&object->data.u.object->hash, &next->lhe); + + if (prop == NULL) { + break; + } - if (prop != NULL) { - *retval = prop->name; + if (prop->enumerable) { + *retval = prop->name; - return code->offset; + return code->offset; + } } nxt_mem_cache_free(vm->mem_cache_pool, next); diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 3e2a65bf..51d85632 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -2070,6 +2070,18 @@ static njs_unit_test_t njs_test[] = { nxt_string("for (null in undefined);"), nxt_string("ReferenceError: Invalid left-hand side \"null\" in for-in statement in 1") }, + { nxt_string("var s = ''; for (var p in [1,2]) {s += p}; s"), + nxt_string("01") }, + + { nxt_string("var s = ''; for (var p in {a:1, b:2}) {s += p}; s"), + nxt_string("ab") }, + + { nxt_string("var s = '';" + "var o = Object.defineProperty({}, 'x', {value:1});" + "Object.defineProperty(o, 'y', {value:2, enumerable:true});" + "for (var p in o) {s += p}; s"), + nxt_string("y") }, + /* switch. */ { nxt_string("switch"),