]> git.kaiwu.me - njs.git/commitdiff
Respecting the enumerable attribute while iterating by for in.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 27 Aug 2018 13:37:35 +0000 (16:37 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 27 Aug 2018 13:37:35 +0000 (16:37 +0300)
njs/njs_vm.c
njs/test/njs_unit_test.c

index a63378a62076b31fc3f163a2341346efd228bd82..60ccacb3a5bb12843916d92fc9e37100a0d86dff 100644 (file)
@@ -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);
index 3e2a65bf72bc99be2b099973ba83f8b5e57e1b25..51d85632cb3d7331d903eb5b732d5ebd627701ca 100644 (file)
@@ -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"),