Skip to content

Object.hasOwn #985

@drsm

Description

@drsm

Hi!

Is your feature request related to a problem? Please describe

Object.hasOwn

Describe the solution you'd like

diff --git a/src/njs_atom_defs.h b/src/njs_atom_defs.h
index 6328c34b..2711a202 100644
--- a/src/njs_atom_defs.h
+++ b/src/njs_atom_defs.h
@@ -287,6 +287,7 @@ NJS_DEF_STRING(global, "global", 0, 0)
 NJS_DEF_STRING(globalThis, "globalThis", 0, 0)
 NJS_DEF_STRING(groups, "groups", 0, 0)
 NJS_DEF_STRING(hasOwnProperty, "hasOwnProperty", 0, 0)
+NJS_DEF_STRING(hasOwn, "hasOwn", 0, 0)
 NJS_DEF_STRING(hasInstance, "hasInstance", 0, 0)
 NJS_DEF_STRING(hypot, "hypot", 0, 0)
 NJS_DEF_STRING(ignoreBOM, "ignoreBOM", 0, 0)
diff --git a/src/njs_object.c b/src/njs_object.c
index 26e8182b..f399d5c2 100644
--- a/src/njs_object.c
+++ b/src/njs_object.c
@@ -31,6 +31,9 @@ static njs_int_t njs_object_define_properties(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
 static njs_int_t njs_object_set_prototype(njs_vm_t *vm, njs_object_t *object,
     const njs_value_t *value);
+static njs_int_t njs_object_prototype_has_own_property(njs_vm_t *vm,
+    njs_value_t *args, njs_uint_t nargs, njs_index_t magic,
+    njs_value_t *retval);


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

     NJS_DECLARE_PROP_NATIVE(STRING_is, njs_object_is, 2, 0),
-};

+    NJS_DECLARE_PROP_NATIVE(STRING_hasOwn,
+                            njs_object_prototype_has_own_property, 2, 1),
+};

 const njs_object_init_t  njs_object_constructor_init = {
     njs_object_constructor_properties,
@@ -2590,13 +2595,13 @@ njs_object_to_string(njs_vm_t *vm, njs_value_t *this, njs_value_t *retval)

 static njs_int_t
 njs_object_prototype_has_own_property(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t unused, njs_value_t *retval)
+    njs_uint_t nargs, njs_index_t magic, njs_value_t *retval)
 {
     njs_int_t             ret;
     njs_value_t           *value, *property, lvalue;
     njs_property_query_t  pq;

-    value = njs_argument(args, 0);
+    value = njs_argument(args, magic);

     if (njs_is_null_or_undefined(value)) {
         njs_type_error(vm, "cannot convert %s argument to object",
@@ -2604,7 +2609,7 @@ njs_object_prototype_has_own_property(njs_vm_t *vm, njs_value_t *args,
         return NJS_ERROR;
     }

-    property = njs_lvalue_arg(&lvalue, args, nargs, 1);
+    property = njs_lvalue_arg(&lvalue, args, nargs, 1 + magic);

     if (njs_slow_path(!njs_is_key(property))) {
         ret = njs_value_to_key(vm, property, property);

Additional context

BTW, does not conform 20.1.3.2 Object.prototype.hasOwnProperty ( V )

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions