Here is the patch for the first one (update 2):
```diff
Added Object.getOwnPropertyNames().
diff -r 5dae5875b22e -r 95c25197c73a njs/njs_object.c
--- a/njs/njs_object.c Tue Mar 26 16:58:43 2019 +0300
+++ b/njs/njs_object.c Tue Mar 26 18:54:50 2019 +0300
@@ -954,6 +954,7 @@
njs_object_enumerate(njs_vm_t *vm, const njs_value_t *value,
njs_object_enum_t kind)
{
exotic_length = 0;
array = NULL;
length = 0;
items_length = 0;
switch (value->type) {
exotic_length = enum_all;
break;
@@ -992,6 +1003,8 @@
length = njs_string_prop(&string_prop, string);
items_length += length;
default:
@@ -1013,15 +1026,32 @@
break;
}
if (prop->type != NJS_WHITEOUT && (prop->enumerable || enum_all)) {
properties++;
}
}
if (nxt_slow_path(enum_all)) {
}
+
items_length += properties;
}
items = njs_array_alloc(vm, items_length, NJS_ARRAY_SPARE);
items = njs_array_alloc(vm, items_length + exotic_length, NJS_ARRAY_SPARE);
if (nxt_slow_path(items == NULL)) {
return NULL;
}
@@ -1031,6 +1061,7 @@
if (array != NULL) {
switch (kind) {
case NJS_ENUM_PROP_NAMES:
case NJS_ENUM_KEYS:
for (i = 0; i < length; i++) {
if (njs_is_valid(&array->start[i])) {
@@ -1077,6 +1108,7 @@
} else if (length != 0) {
switch (kind) {
case NJS_ENUM_PROP_NAMES:
case NJS_ENUM_KEYS:
for (i = 0; i < length; i++) {
njs_uint32_to_string(item++, i);
@@ -1179,11 +1211,16 @@
}
}
if (nxt_slow_path(exotic_length != 0)) {
}
+
if (nxt_fast_path(properties != 0)) {
nxt_lvlhsh_each_init(&lhe, &njs_object_hash_proto);
switch (kind) {
case NJS_ENUM_PROP_NAMES:
case NJS_ENUM_KEYS:
for ( ;; ) {
prop = nxt_lvlhsh_each(hash, &lhe);
@@ -1192,7 +1229,22 @@
break;
}
if (prop->type != NJS_WHITEOUT && prop->enumerable) {
}
+
njs_string_copy(item++, &prop->name);
}
}
@@ -1721,6 +1773,35 @@
static njs_ret_t
+njs_object_get_own_property_names(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
return NXT_OK;
+}
+
+
+static njs_ret_t
njs_object_get_prototype_of(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
njs_index_t unused)
{
@@ -2152,6 +2233,14 @@
NJS_STRING_ARG),
},
/* Object.getOwnPropertyNames(). */
},
+
/* Object.getPrototypeOf(). */
{
.type = NJS_METHOD,
diff -r 5dae5875b22e -r 95c25197c73a njs/njs_object.h
--- a/njs/njs_object.h Tue Mar 26 16:58:43 2019 +0300
+++ b/njs/njs_object.h Tue Mar 26 18:54:50 2019 +0300
@@ -18,7 +18,8 @@
typedef enum {
NJS_ENUM_KEYS,
NJS_ENUM_VALUES,
NJS_ENUM_BOTH,
} njs_object_enum_t;
diff -r 5dae5875b22e -r 95c25197c73a njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Tue Mar 26 16:58:43 2019 +0300
+++ b/njs/test/njs_unit_test.c Tue Mar 26 18:54:50 2019 +0300
@@ -8673,6 +8673,41 @@
{ nxt_string("var o = {}; o[void 0] = 'a'; Object.getOwnPropertyDescriptor(o, undefined).value"),
nxt_string("a") },
{ nxt_string("Object.getOwnPropertyNames()"),
nxt_string("length") },
+#endif
+
{ nxt_string("Object.defineProperty(Object.freeze({}), 'b', {})"),
nxt_string("TypeError: object is not extensible") },
```
the patch updated to support functions. not sure it is correct.
@drsm
Looks good. I plan to commit the patch with minor improvements: https://gist.github.com/xeioex/2d200ac7594e9ae2c834dbce5420d00f
@drsm
Object.getOwnPropertyDescriptors()
BTW, I think we need a separate ticket for it.
@xeioex
Looks good. I plan to commit the patch with minor improvements: https://gist.github.com/xeioex/2d200ac7594e9ae2c834dbce5420d00f
+ NJS_ENUM_OWN_KEYS = 0,
no ).
NJS_ENUM_KEYS|VALUES|BOTH - enum all enumerable own properties (it doesn't scan the prototype chain)
NJS_ENUM_PROP_NAMES or maybe NJS_ENUM_PROPERTIES enum all own properties including non-enumerable.
and maybe NJS_ENUM_BOTH has to be renamed to NJS_ENUM_ENTRIES,
as there will be NJS_ENUM_DESCRIPTORS before NJS_ENUM_KEYS for Object.getOwnPropertyDescriptors()
@drsm
Thanks for catching.
NJS_ENUM_OWN_KEYS -> NJS_ENUM_ALL_KEYS?
NJS_ENUM_DESCRIPTORS -> NJS_ENUM_ALL_DESCRIPTORS?
I am trying to make it easier to understand.
So, by default, only enumerable properties are enumerated. All properties are enumerated if _ALL_ is present in the flag name.
@drsm
Alternatively, we can use an additional argument for the function:
njs_object_enumerate(vm, value, NJS_ENUM_KEYS, 1 /* all */);
@xeioex
Alternatively, we can use an additional argument for the function:
njs_object_enumerate(vm, value, NJS_ENUM_KEYS, 1 /* all */);
i think no, this will cover the KEYS case, but not the DESCRIPTORS, and there is no API where non-enumerable VALUES|ENTRIES are used.
@drsm
Take a look: https://gist.github.com/xeioex/f67d80cbcfcb267104e74a0da42b59d5
and there is no API where non-enumerable VALUES|ENTRIES are used.
I think it is more or less OK. Because many internal functions, in fact, work only for special combinations of arguments.
Take a look: https://gist.github.com/xeioex/f67d80cbcfcb267104e74a0da42b59d5
LGTM