[njs] Added Object.prototype.propertyIsEnumerable().

Dmitry Volyntsev xeioex at nginx.com
Mon Mar 11 15:31:48 UTC 2019


details:   https://hg.nginx.org/njs/rev/49f4eb5afc9b
branches:  
changeset: 826:49f4eb5afc9b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Mar 11 18:31:40 2019 +0300
description:
Added Object.prototype.propertyIsEnumerable().

diffstat:

 njs/njs_object.c         |  52 ++++++++++++++++++++++++++++++++++++++++++++++++
 njs/test/njs_unit_test.c |  24 ++++++++++++++++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)

diffs (117 lines):

diff -r a835619baf84 -r 49f4eb5afc9b njs/njs_object.c
--- a/njs/njs_object.c	Mon Mar 11 11:15:20 2019 +0300
+++ b/njs/njs_object.c	Mon Mar 11 18:31:40 2019 +0300
@@ -2458,6 +2458,51 @@ njs_object_prototype_has_own_property(nj
 
 
 static njs_ret_t
+njs_object_prototype_prop_is_enumerable(njs_vm_t *vm, njs_value_t *args,
+    nxt_uint_t nargs, njs_index_t unused)
+{
+    nxt_int_t             ret;
+    const njs_value_t     *value, *property, *retval;
+    njs_object_prop_t     *prop;
+    njs_property_query_t  pq;
+
+    value = njs_arg(args, nargs, 0);
+
+    if (njs_is_null_or_void(value)) {
+        njs_type_error(vm, "cannot convert %s argument to object",
+                       njs_type_string(value->type));
+        return NXT_ERROR;
+    }
+
+    property = njs_arg(args, nargs, 1);
+
+    njs_property_query_init(&pq, NJS_PROPERTY_QUERY_GET, 1);
+
+    ret = njs_property_query(vm, &pq, (njs_value_t *) value, property);
+
+    switch (ret) {
+    case NXT_OK:
+        prop = pq.lhq.value;
+        retval = prop->enumerable ? &njs_value_true : &njs_value_false;
+        break;
+
+    case NXT_DECLINED:
+        retval = &njs_value_false;
+        break;
+
+    case NJS_TRAP:
+    case NXT_ERROR:
+    default:
+        return ret;
+    }
+
+    vm->retval = *retval;
+
+    return NXT_OK;
+}
+
+
+static njs_ret_t
 njs_object_prototype_is_prototype_of(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused)
 {
@@ -2524,6 +2569,13 @@ static const njs_object_prop_t  njs_obje
 
     {
         .type = NJS_METHOD,
+        .name = njs_long_string("propertyIsEnumerable"),
+        .value = njs_native_function(njs_object_prototype_prop_is_enumerable, 0,
+                                     NJS_OBJECT_ARG, NJS_STRING_ARG),
+    },
+
+    {
+        .type = NJS_METHOD,
         .name = njs_string("isPrototypeOf"),
         .value = njs_native_function(njs_object_prototype_is_prototype_of, 0,
                                      NJS_OBJECT_ARG, NJS_OBJECT_ARG),
diff -r a835619baf84 -r 49f4eb5afc9b njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Mar 11 11:15:20 2019 +0300
+++ b/njs/test/njs_unit_test.c	Mon Mar 11 18:31:40 2019 +0300
@@ -8464,6 +8464,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("[1,2].hasOwnProperty('len')"),
       nxt_string("false") },
 
+    { nxt_string("[1,2].hasOwnProperty('0')"),
+      nxt_string("true") },
+
+    { nxt_string("[1,2].hasOwnProperty('2')"),
+      nxt_string("false") },
+
     { nxt_string("[].hasOwnProperty('length')"),
       nxt_string("true") },
 
@@ -8485,6 +8491,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'s'.hasOwnProperty('b')"),
       nxt_string("false") },
 
+    { nxt_string("'s'.hasOwnProperty('0')"),
+      nxt_string("true") },
+
+    { nxt_string("'s'.hasOwnProperty('1')"),
+      nxt_string("false") },
+
     { nxt_string("var p = { a:5 }; var o = Object.create(p);"
                  "Object.getPrototypeOf(o) === p"),
       nxt_string("true") },
@@ -8607,6 +8619,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("Object.getOwnPropertyDescriptor(1, '0')"),
       nxt_string("undefined") },
 
+    { nxt_string("'αβγδ'.propertyIsEnumerable('0')"),
+      nxt_string("true") },
+
+    { nxt_string("({a:1}).propertyIsEnumerable({toString:function () {return 'a';}})"),
+      nxt_string("true") },
+
+    { nxt_string("'αβγδ'.propertyIsEnumerable('a')"),
+      nxt_string("false") },
+
+    { nxt_string("'αβγδ'.propertyIsEnumerable('length')"),
+      nxt_string("false") },
+
     { nxt_string("var min = Object.getOwnPropertyDescriptor(Math, 'min').value;"
                  "[min(1,2), min(2,1), min(-1,1)]"),
       nxt_string("1,1,-1") },


More information about the nginx-devel mailing list