Skip to content

Commit

Permalink
Fixed Object.prototype.hasOwnProperty() for non-object properties.
Browse files Browse the repository at this point in the history
This fixes #9 issue on Github.
  • Loading branch information
xeioex committed Oct 19, 2018
1 parent 15099bc commit 8c954b5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
53 changes: 23 additions & 30 deletions njs/njs_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
nxt_int_t ret;
const njs_value_t *value, *property;
njs_property_query_t pq;

retval = &njs_value_false;
value = &args[0];
value = njs_arg(args, nargs, 0);

if (njs_is_object(value)) {
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;
}

prop = njs_arg(args, nargs, 1);
property = njs_arg(args, nargs, 1);

if (njs_is_array(value)) {
array = value->data.u.array;
index = njs_string_to_index(prop);
njs_property_query_init(&pq, NJS_PROPERTY_QUERY_GET, 1);

if (index < array->length && njs_is_valid(&array->start[index])) {
retval = &njs_value_true;
goto done;
}
}
ret = njs_property_query(vm, &pq, (njs_value_t *) value, property);

njs_string_get(prop, &lhq.key);
lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
lhq.proto = &njs_object_hash_proto;
switch (ret) {
case NXT_OK:
vm->retval = njs_value_true;
return NXT_OK;

ret = nxt_lvlhsh_find(&value->data.u.object->hash, &lhq);
case NXT_DECLINED:
vm->retval = njs_value_false;
return NXT_OK;

if (ret == NXT_OK) {
retval = &njs_value_true;
}
case NJS_TRAP:
case NXT_ERROR:
default:
return ret;
}

done:

vm->retval = *retval;

return NXT_OK;
}


Expand Down
24 changes: 24 additions & 0 deletions njs/test/njs_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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") },

Expand Down Expand Up @@ -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") },

Expand Down

0 comments on commit 8c954b5

Please sign in to comment.