]> git.kaiwu.me - njs.git/commitdiff
Making custom top-level objects enumerable.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 1 Nov 2019 13:02:21 +0000 (16:02 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 1 Nov 2019 13:02:21 +0000 (16:02 +0300)
src/njs_builtin.c
src/test/njs_unit_test.c

index d08c42014dfa3ed6c8e2137037912b7ee5d14e08..10b08f0fc050e7ee88fdf580b7ff4fc4e356afb1 100644 (file)
@@ -889,7 +889,7 @@ njs_top_level_object(njs_vm_t *vm, njs_object_prop_t *self,
     /* 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_global_this_object_properties[] =
         .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_global_this_object_properties[] =
         .value = njs_prop_handler2(njs_top_level_object, NJS_OBJECT_PROCESS,
                                    NJS_PROCESS_HASH),
         .writable = 1,
+        .enumerable = 1,
         .configurable = 1,
     },
 
index a82e07123d0db6c5da1e106fb93e77937b8dd4b2..649bdaa475a61a618f23919370e16c52218a9705 100644 (file)
@@ -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") },