[njs] Fixed Object.prototype.hasOwnProperty() for non-object properties.

Dmitry Volyntsev xeioex at nginx.com
Fri Oct 19 18:30:53 UTC 2018


details:   http://hg.nginx.org/njs/rev/7e76c67af046
branches:  
changeset: 625:7e76c67af046
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Oct 19 20:55:23 2018 +0300
description:
Fixed Object.prototype.hasOwnProperty() for non-object properties.

This fixes #9 issue on Github.

diffstat:

 njs/njs_object.c         |  67 +++++++++++++++++++++--------------------------
 njs/test/njs_unit_test.c |  24 +++++++++++++++++
 2 files changed, 54 insertions(+), 37 deletions(-)

diffs (120 lines):

diff -r da92ae8bcae3 -r 7e76c67af046 njs/njs_object.c
--- a/njs/njs_object.c	Fri Oct 19 20:54:59 2018 +0300
+++ b/njs/njs_object.c	Fri Oct 19 20:55:23 2018 +0300
@@ -2079,45 +2079,38 @@ static njs_ret_t
 njs_object_prototype_has_own_property(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused)
 {
-    uint32_t            index;
-    nxt_int_t           ret;
-    njs_array_t         *array;
-    const njs_value_t   *value, *prop, *retval;
-    nxt_lvlhsh_query_t  lhq;
-
-    retval = &njs_value_false;
-    value = &args[0];
-
-    if (njs_is_object(value)) {
-
-        prop = njs_arg(args, nargs, 1);
-
-        if (njs_is_array(value)) {
-            array = value->data.u.array;
-            index = njs_string_to_index(prop);
-
-            if (index < array->length && njs_is_valid(&array->start[index])) {
-                retval = &njs_value_true;
-                goto done;
-            }
-        }
-
-        njs_string_get(prop, &lhq.key);
-        lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
-        lhq.proto = &njs_object_hash_proto;
-
-        ret = nxt_lvlhsh_find(&value->data.u.object->hash, &lhq);
-
-        if (ret == NXT_OK) {
-            retval = &njs_value_true;
-        }
+    nxt_int_t             ret;
+    const njs_value_t     *value, *property;
+    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;
     }
 
-done:
-
-    vm->retval = *retval;
-
-    return NXT_OK;
+    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:
+        vm->retval = njs_value_true;
+        return NXT_OK;
+
+    case NXT_DECLINED:
+        vm->retval = njs_value_false;
+        return NXT_OK;
+
+    case NJS_TRAP:
+    case NXT_ERROR:
+    default:
+        return ret;
+    }
 }
 
 
diff -r da92ae8bcae3 -r 7e76c67af046 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri Oct 19 20:54:59 2018 +0300
+++ b/njs/test/njs_unit_test.c	Fri Oct 19 20:55:23 2018 +0300
@@ -3671,6 +3671,15 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'abc'.length"),
       nxt_string("3") },
 
+    { nxt_string("''.hasOwnProperty('length')"),
+      nxt_string("true") },
+
+    { nxt_string("'abc'.hasOwnProperty('length')"),
+      nxt_string("true") },
+
+    { nxt_string("(new String('abc')).hasOwnProperty('length')"),
+      nxt_string("true") },
+
     { nxt_string("'abc'.toUTF8().length"),
       nxt_string("3") },
 
@@ -7103,6 +7112,21 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("[,].hasOwnProperty()"),
       nxt_string("false") },
 
+    { nxt_string("[1,2].hasOwnProperty('len')"),
+      nxt_string("false") },
+
+    { nxt_string("[].hasOwnProperty('length')"),
+      nxt_string("true") },
+
+    { nxt_string("[1,2].hasOwnProperty('length')"),
+      nxt_string("true") },
+
+    { nxt_string("(new Array()).hasOwnProperty('length')"),
+      nxt_string("true") },
+
+    { nxt_string("(new Array(10)).hasOwnProperty('length')"),
+      nxt_string("true") },
+
     { nxt_string("Object.valueOf.hasOwnProperty()"),
       nxt_string("false") },
 


More information about the nginx-devel mailing list