Skip to content

Commit fea10bb

Browse files
authored
Add name to the function property names (#4760)
Furthermore prototype should be the first property. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 1523ca3 commit fea10bb

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

jerry-core/ecma/operations/ecma-function-object.c

+26-10
Original file line numberDiff line numberDiff line change
@@ -1981,35 +1981,51 @@ ecma_op_function_list_lazy_property_names (ecma_object_t *object_p, /**< functio
19811981
ecma_collection_t *prop_names_p, /**< prop name collection */
19821982
ecma_property_counter_t *prop_counter_p) /**< prop counter */
19831983
{
1984+
const ecma_compiled_code_t *bytecode_data_p;
1985+
bytecode_data_p = ecma_op_function_get_compiled_code ((ecma_extended_object_t *) object_p);
1986+
1987+
#if JERRY_ESNEXT
1988+
bool append_prototype = CBC_FUNCTION_HAS_PROTOTYPE (bytecode_data_p->status_flags);
1989+
#else /* !JERRY_ESNEXT */
1990+
bool append_prototype = true;
1991+
#endif /* JERRY_ESNEXT */
1992+
1993+
if (append_prototype)
1994+
{
1995+
/* 'prototype' property is non-enumerable (ECMA-262 v5, 13.2.18) */
1996+
ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_PROTOTYPE));
1997+
prop_counter_p->string_named_props++;
1998+
}
1999+
19842000
#if JERRY_ESNEXT
19852001
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) object_p;
2002+
19862003
if (!ECMA_GET_FIRST_BIT_FROM_POINTER_TAG (ext_func_p->u.function.scope_cp))
19872004
{
19882005
/* Unintialized 'length' property is non-enumerable (ECMA-262 v6, 19.2.4.1) */
19892006
ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH));
19902007
prop_counter_p->string_named_props++;
19912008
}
2009+
2010+
if (CBC_FUNCTION_GET_TYPE (bytecode_data_p->status_flags) != CBC_FUNCTION_CONSTRUCTOR
2011+
&& !ECMA_GET_SECOND_BIT_FROM_POINTER_TAG (ext_func_p->u.function.scope_cp))
2012+
{
2013+
/* Unintialized 'name' property is non-enumerable (ECMA-262 v6, 19.2.4.2) */
2014+
ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_NAME));
2015+
prop_counter_p->string_named_props++;
2016+
}
19922017
#else /* !JERRY_ESNEXT */
19932018
/* 'length' property is non-enumerable (ECMA-262 v5, 13.2.5) */
19942019
ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH));
19952020
prop_counter_p->string_named_props++;
19962021
#endif /* JERRY_ESNEXT */
19972022

1998-
const ecma_compiled_code_t *bytecode_data_p;
1999-
bytecode_data_p = ecma_op_function_get_compiled_code ((ecma_extended_object_t *) object_p);
2000-
20012023
#if JERRY_ESNEXT
2002-
if (!CBC_FUNCTION_HAS_PROTOTYPE (bytecode_data_p->status_flags))
2024+
if (!append_prototype)
20032025
{
20042026
return;
20052027
}
2006-
#endif /* JERRY_ESNEXT */
20072028

2008-
/* 'prototype' property is non-enumerable (ECMA-262 v5, 13.2.18) */
2009-
ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_PROTOTYPE));
2010-
prop_counter_p->string_named_props++;
2011-
2012-
#if JERRY_ESNEXT
20132029
bool append_caller_and_arguments = !(bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE);
20142030
#else /* !JERRY_ESNEXT */
20152031
bool append_caller_and_arguments = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE);

tests/jerry/es.next/function-properties.js

+6
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ assert(getProperties(bound_func) == "dummy caller arguments prototype");
4444
// 'print' is an external function
4545
Object.setPrototypeOf(print, prototype_obj);
4646
assert(getProperties(print) == "dummy length caller arguments");
47+
48+
function f1() {}
49+
assert(Reflect.ownKeys(f1).toString() === "prototype,length,name,caller,arguments")
50+
51+
async function f2() {}
52+
assert(Reflect.ownKeys(f2).toString() === "length,name")

tests/jerry/es.next/regression-test-issue-3267.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Object.preventExtensions(hasProp);
1717
assert (Object.isSealed(hasProp) === false);
1818

1919
var keys = Object.getOwnPropertyNames(hasProp);
20-
assert (keys.length === 1);
21-
assert (keys[0] === "length");
22-
20+
assert (keys.length === 2);
21+
/* Note: the order is currently wrong. */
22+
assert (keys[0] === "name");
23+
assert (keys[1] === "length");

0 commit comments

Comments
 (0)