njs_ret_t
njs_method_private_copy(njs_vm_t *vm, njs_property_query_t *pq)
{
- njs_function_t *function;
- njs_object_prop_t *prop, *shared;
+ nxt_int_t ret;
+ njs_function_t *function;
+ njs_object_prop_t *prop, *shared, *name;
+ nxt_lvlhsh_query_t lhq;
+
+ static const njs_value_t name_string = njs_string("name");
prop = nxt_mp_align(vm->mem_pool, sizeof(njs_value_t),
sizeof(njs_object_prop_t));
function->object.shared_hash = vm->shared->arrow_instance_hash;
}
+ name = njs_object_prop_alloc(vm, &name_string, &prop->name, 0);
+ if (nxt_slow_path(name == NULL)) {
+ return NXT_ERROR;
+ }
+
+ name->configurable = 1;
+
+ lhq.key_hash = NJS_NAME_HASH;
+ lhq.key = nxt_string_value("name");
+ lhq.replace = 0;
+ lhq.value = name;
+ lhq.proto = &njs_object_hash_proto;
+ lhq.pool = vm->mem_pool;
+
+ ret = nxt_lvlhsh_insert(&function->object.hash, &lhq);
+ if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, "lvlhsh insert failed");
+ return NXT_ERROR;
+ }
+
pq->lhq.replace = 0;
pq->lhq.value = prop;
pq->lhq.pool = vm->mem_pool;
nxt_string("name,length,prototype,isArray,of") },
{ nxt_string("Object.getOwnPropertyNames(Array.isArray)"),
- nxt_string("length") },
+ nxt_string("name,length") },
{ nxt_string("Object.defineProperty(Object.freeze({}), 'b', {})"),
nxt_string("TypeError: object is not extensible") },
{ nxt_string("njs.dump($r.header)"),
nxt_string("{type:\"object\",props:[\"getter\",\"foreach\",\"next\"]}") },
+ /* Built-in methods name. */
+
+ { nxt_string(
+ "var fail;"
+ "function isMethodsHaveName(o) {"
+ " var except = ["
+ " 'prototype',"
+ " 'constructor',"
+ " ];"
+ " return Object.getOwnPropertyNames(o)"
+ " .filter(v => !except.includes(v)"
+ " && typeof o[v] == 'function')"
+ " .every(v => o[v].name == v"
+ " || !(fail = `${o.name}.${v}: ${o[v].name}`));"
+ "}"
+ "["
+ " Boolean, Boolean.prototype,"
+ " Number, Number.prototype,"
+ " String, String.prototype,"
+ " Object, Object.prototype,"
+ " Array, Array.prototype,"
+ " Function, Function.prototype,"
+ " RegExp, RegExp.prototype,"
+ " Date, Date.prototype,"
+ " Error, Error.prototype,"
+ " Math,"
+ " JSON,"
+ "].every(obj => isMethodsHaveName(obj)) || fail"),
+
+ nxt_string("true") },
+
/* require(). */
{ nxt_string("require('unknown_module')"),