Skip to content

Commit 8b08b3d

Browse files
Added Object.hasOwn().
1 parent d40f4a5 commit 8b08b3d

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/njs_atom_defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ NJS_DEF_STRING(getMonth, "getMonth", 0, 0)
286286
NJS_DEF_STRING(global, "global", 0, 0)
287287
NJS_DEF_STRING(globalThis, "globalThis", 0, 0)
288288
NJS_DEF_STRING(groups, "groups", 0, 0)
289+
NJS_DEF_STRING(hasOwn, "hasOwn", 0, 0)
289290
NJS_DEF_STRING(hasOwnProperty, "hasOwnProperty", 0, 0)
290291
NJS_DEF_STRING(hasInstance, "hasInstance", 0, 0)
291292
NJS_DEF_STRING(hypot, "hypot", 0, 0)

src/njs_object.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ static njs_int_t njs_object_define_properties(njs_vm_t *vm, njs_value_t *args,
3131
njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
3232
static njs_int_t njs_object_set_prototype(njs_vm_t *vm, njs_object_t *object,
3333
const njs_value_t *value);
34+
static njs_int_t njs_object_prototype_has_own_property(njs_vm_t *vm,
35+
njs_value_t *args, njs_uint_t nargs, njs_index_t magic,
36+
njs_value_t *retval);
3437

3538

3639
njs_object_t *
@@ -2274,8 +2277,10 @@ static const njs_object_prop_init_t njs_object_constructor_properties[] =
22742277
NJS_DECLARE_PROP_NATIVE(STRING_assign, njs_object_assign, 2, 0),
22752278

22762279
NJS_DECLARE_PROP_NATIVE(STRING_is, njs_object_is, 2, 0),
2277-
};
22782280

2281+
NJS_DECLARE_PROP_NATIVE(STRING_hasOwn,
2282+
njs_object_prototype_has_own_property, 2, 1),
2283+
};
22792284

22802285
const njs_object_init_t njs_object_constructor_init = {
22812286
njs_object_constructor_properties,
@@ -2590,27 +2595,47 @@ njs_object_to_string(njs_vm_t *vm, njs_value_t *this, njs_value_t *retval)
25902595

25912596
static njs_int_t
25922597
njs_object_prototype_has_own_property(njs_vm_t *vm, njs_value_t *args,
2593-
njs_uint_t nargs, njs_index_t unused, njs_value_t *retval)
2598+
njs_uint_t nargs, njs_index_t magic, njs_value_t *retval)
25942599
{
25952600
njs_int_t ret;
25962601
njs_value_t *value, *property, lvalue;
25972602
njs_property_query_t pq;
25982603

2599-
property = njs_lvalue_arg(&lvalue, args, nargs, 1);
26002604

2601-
if (njs_slow_path(!njs_is_key(property))) {
2602-
ret = njs_value_to_key(vm, property, property);
2603-
if (njs_slow_path(ret != NJS_OK)) {
2605+
value = njs_argument(args, magic);
2606+
property = njs_lvalue_arg(&lvalue, args, nargs, 1 + magic);
2607+
2608+
if (magic == 0) {
2609+
/* hasOwnProperty. */
2610+
2611+
if (njs_slow_path(!njs_is_key(property))) {
2612+
ret = njs_value_to_key(vm, property, property);
2613+
if (njs_slow_path(ret != NJS_OK)) {
2614+
return NJS_ERROR;
2615+
}
2616+
}
2617+
2618+
if (njs_is_null_or_undefined(value)) {
2619+
njs_type_error(vm, "cannot convert %s argument to object",
2620+
njs_type_string(value->type));
26042621
return NJS_ERROR;
26052622
}
2606-
}
26072623

2608-
value = njs_argument(args, 0);
2624+
} else {
2625+
/* hasOwn. */
26092626

2610-
if (njs_is_null_or_undefined(value)) {
2611-
njs_type_error(vm, "cannot convert %s argument to object",
2612-
njs_type_string(value->type));
2613-
return NJS_ERROR;
2627+
if (njs_is_null_or_undefined(value)) {
2628+
njs_type_error(vm, "cannot convert %s argument to object",
2629+
njs_type_string(value->type));
2630+
return NJS_ERROR;
2631+
}
2632+
2633+
if (njs_slow_path(!njs_is_key(property))) {
2634+
ret = njs_value_to_key(vm, property, property);
2635+
if (njs_slow_path(ret != NJS_OK)) {
2636+
return NJS_ERROR;
2637+
}
2638+
}
26142639
}
26152640

26162641
njs_property_query_init(&pq, NJS_PROPERTY_QUERY_GET, 1);

0 commit comments

Comments
 (0)