[njs] Respecting the enumerable attribute while iterating by for in.

Dmitry Volyntsev xeioex at nginx.com
Mon Aug 27 13:55:47 UTC 2018


details:   http://hg.nginx.org/njs/rev/76cee25a8228
branches:  
changeset: 587:76cee25a8228
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Aug 27 16:37:35 2018 +0300
description:
Respecting the enumerable attribute while iterating by for in.

diffstat:

 njs/njs_vm.c             |  18 ++++++++++++------
 njs/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 24 insertions(+), 6 deletions(-)

diffs (50 lines):

diff -r c572768e287b -r 76cee25a8228 njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Aug 27 16:23:08 2018 +0300
+++ b/njs/njs_vm.c	Mon Aug 27 16:37:35 2018 +0300
@@ -1053,12 +1053,18 @@ njs_vmcode_property_next(njs_vm_t *vm, n
             next->index = -1;
         }
 
-        prop = nxt_lvlhsh_each(&object->data.u.object->hash, &next->lhe);
-
-        if (prop != NULL) {
-            *retval = prop->name;
-
-            return code->offset;
+        for ( ;; ) {
+            prop = nxt_lvlhsh_each(&object->data.u.object->hash, &next->lhe);
+
+            if (prop == NULL) {
+                break;
+            }
+
+            if (prop->enumerable) {
+                *retval = prop->name;
+
+                return code->offset;
+            }
         }
 
         nxt_mem_cache_free(vm->mem_cache_pool, next);
diff -r c572768e287b -r 76cee25a8228 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Aug 27 16:23:08 2018 +0300
+++ b/njs/test/njs_unit_test.c	Mon Aug 27 16:37:35 2018 +0300
@@ -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"),


More information about the nginx-devel mailing list