[njs] Making custom top-level objects enumerable.

Dmitry Volyntsev xeioex at nginx.com
Fri Nov 1 13:58:42 UTC 2019


details:   https://hg.nginx.org/njs/rev/9054169baaaf
branches:  
changeset: 1218:9054169baaaf
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Nov 01 16:02:21 2019 +0300
description:
Making custom top-level objects enumerable.

diffstat:

 src/njs_builtin.c        |  4 +++-
 src/test/njs_unit_test.c |  7 ++++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diffs (52 lines):

diff -r 3e3b00388d69 -r 9054169baaaf src/njs_builtin.c
--- a/src/njs_builtin.c	Fri Nov 01 15:48:21 2019 +0300
+++ b/src/njs_builtin.c	Fri Nov 01 16:02:21 2019 +0300
@@ -889,7 +889,7 @@ njs_top_level_object(njs_vm_t *vm, njs_o
     /* GC */
 
     prop->value = *retval;
-    prop->enumerable = 0;
+    prop->enumerable = self->enumerable;
 
     lhq.value = prop;
     njs_string_get(&self->name, &lhq.key);
@@ -1097,6 +1097,7 @@ static const njs_object_prop_t  njs_glob
         .value = njs_prop_handler2(njs_top_level_object, NJS_OBJECT_NJS,
                                    NJS_NJS_HASH),
         .writable = 1,
+        .enumerable = 1,
         .configurable = 1,
     },
 
@@ -1106,6 +1107,7 @@ static const njs_object_prop_t  njs_glob
         .value = njs_prop_handler2(njs_top_level_object, NJS_OBJECT_PROCESS,
                                    NJS_PROCESS_HASH),
         .writable = 1,
+        .enumerable = 1,
         .configurable = 1,
     },
 
diff -r 3e3b00388d69 -r 9054169baaaf src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Nov 01 15:48:21 2019 +0300
+++ b/src/test/njs_unit_test.c	Fri Nov 01 16:02:21 2019 +0300
@@ -9249,6 +9249,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Object.getOwnPropertyNames(this).includes('NaN')"),
       njs_str("true") },
 
+    { njs_str("Object.keys(this)"),
+      njs_str("njs,process") },
+
     { njs_str("this.a = 1; this.a"),
       njs_str("1") },
 
@@ -13259,7 +13262,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var global = this;"
               "function isMutableObject(v) {"
               "    var d = Object.getOwnPropertyDescriptor(global, v);"
-              "    return d.writable && !d.enumerable && d.configurable;"
+              "    /* Custom top-level objects are enumerable. */"
+              "    var enumerable = (v in {'njs':1, 'process':1}) ^ !d.enumerable;"
+              "    return d.writable && enumerable && d.configurable;"
               "};"
               "['njs', 'process', 'Math', 'JSON'].every((v)=>isMutableObject(v))"),
       njs_str("true") },


More information about the nginx-devel mailing list