diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index d36966f19..ac00e9e06 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -2922,8 +2922,21 @@ jerry_get_object_keys (const jerry_value_t obj_val) /**< object value */ return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p))); } - return ecma_builtin_helper_object_get_properties (ecma_get_object_from_value (obj_val), - ECMA_LIST_ENUMERABLE); + ecma_collection_t *prop_names = ecma_op_object_get_enumerable_property_names (ecma_get_object_from_value (obj_val), + ECMA_ENUMERABLE_PROPERTY_KEYS); + +#if ENABLED (JERRY_BUILTIN_PROXY) + if (JERRY_UNLIKELY (prop_names == NULL)) + { + return ECMA_VALUE_ERROR; + } +#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ + + ecma_value_t result_array = ecma_op_create_array_object (prop_names->buffer_p, prop_names->item_count, false); + + ecma_collection_free (prop_names); + + return result_array; } /* jerry_get_object_keys */ /** @@ -3219,7 +3232,7 @@ jerry_foreach_object_property (const jerry_value_t obj_val, /**< object value */ } ecma_object_t *object_p = ecma_get_object_from_value (obj_val); - ecma_collection_t *names_p = ecma_op_object_get_property_names (object_p, ECMA_LIST_ENUMERABLE_PROTOTYPE); + ecma_collection_t *names_p = ecma_op_object_enumerate (object_p); #if ENABLED (JERRY_BUILTIN_PROXY) if (names_p == NULL) diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h index 8209214f2..b5b639b46 100644 --- a/jerry-core/ecma/base/ecma-globals.h +++ b/jerry-core/ecma/base/ecma-globals.h @@ -1431,7 +1431,7 @@ typedef struct * Compute the total allocated size of the collection based on it's capacity */ #define ECMA_COLLECTION_ALLOCATED_SIZE(capacity) \ - (uint32_t) (sizeof (ecma_collection_t) + (capacity * sizeof (ecma_value_t))) + (uint32_t) (capacity * sizeof (ecma_value_t)) /** * Initial allocated size of an ecma-collection @@ -2066,6 +2066,17 @@ typedef struct } ecma_revocable_proxy_object_t; #endif /* ENABLED (JERRY_BUILTIN_PROXY) */ +/** + * Struct for counting the different types properties in objects + */ +typedef struct +{ + uint32_t array_index_named_props; /**< number of array index named properties */ + uint32_t string_named_props; /**< number of string named properties */ + uint32_t symbol_named_props; /**< number of symbol named properties */ + uint32_t lazy_string_named_props; /**< number of lazy instantiated properties */ +} ecma_property_counter_t; + /** * @} * @} diff --git a/jerry-core/ecma/base/ecma-helpers-collection.c b/jerry-core/ecma/base/ecma-helpers-collection.c index 0b4ba4865..536c6ddf3 100644 --- a/jerry-core/ecma/base/ecma-helpers-collection.c +++ b/jerry-core/ecma/base/ecma-helpers-collection.c @@ -14,6 +14,7 @@ */ #include "ecma-alloc.h" +#include "ecma-conversion.h" #include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" @@ -187,6 +188,61 @@ ecma_collection_append (ecma_collection_t *collection_p, /**< value collection * collection_p->item_count += count; } /* ecma_collection_append */ +/** + * Helper function to check if a given collection have duplicated properties or not + * + * @return true - if there are duplicated properties in the collection + * false - otherwise + */ +bool +ecma_collection_check_duplicated_entries (ecma_collection_t *collection_p) /**< prop name collection */ +{ + ecma_value_t *buffer_p = collection_p->buffer_p; + + for (uint32_t i = 0; i < collection_p->item_count - 1; i++) + { + ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[i]); + + for (uint32_t j = i + 1; j < collection_p->item_count; j++) + { + if (ecma_compare_ecma_strings (current_name_p, ecma_get_prop_name_from_value (buffer_p[j]))) + { + return true; + } + } + } + + return false; +} /* ecma_collection_check_duplicated_entries */ + +/** + * Check the string value existance in the collection. + * + * Used by: + * - ecma_builtin_json_stringify step 4.b.ii.5 + * - ecma_op_object_enumerate + * + * @return true, if the string is already in the collection. + */ +bool +ecma_collection_has_string_value (ecma_collection_t *collection_p, /**< collection */ + ecma_string_t *string_p) /**< string */ +{ + ecma_value_t *buffer_p = collection_p->buffer_p; + + for (uint32_t i = 0; i < collection_p->item_count; i++) + { + ecma_string_t *current_p = ecma_get_string_from_value (buffer_p[i]); + + if (ecma_compare_ecma_strings (current_p, string_p)) + { + return true; + } + } + + return false; +} /* ecma_collection_has_string_value */ + /** * @} * @} diff --git a/jerry-core/ecma/base/ecma-helpers.h b/jerry-core/ecma/base/ecma-helpers.h index 6b3e28a94..3f46b4705 100644 --- a/jerry-core/ecma/base/ecma-helpers.h +++ b/jerry-core/ecma/base/ecma-helpers.h @@ -439,6 +439,8 @@ void ecma_collection_destroy (ecma_collection_t *collection_p); void ecma_collection_free (ecma_collection_t *collection_p); void ecma_collection_free_if_not_object (ecma_collection_t *collection_p); void ecma_collection_free_objects (ecma_collection_t *collection_p); +bool ecma_collection_check_duplicated_entries (ecma_collection_t *collection_p); +bool ecma_collection_has_string_value (ecma_collection_t *collection_p, ecma_string_t *string_p); /* ecma-helpers.c */ ecma_object_t *ecma_create_object (ecma_object_t *prototype_object_p, size_t ext_object_size, ecma_object_type_t type); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c index 958637f17..85622d149 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c @@ -1079,38 +1079,38 @@ ecma_builtin_array_prototype_object_sort (ecma_value_t this_arg, /**< this argum return ecma_raise_type_error (ECMA_ERR_MSG ("Compare function is not callable.")); } - ecma_collection_t *array_index_props_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_ARRAY_INDICES); + ecma_collection_t *array_index_props_p = ecma_new_collection (); -#if ENABLED (JERRY_BUILTIN_PROXY) - if (array_index_props_p == NULL) + for (uint32_t i = 0; i < len; i++) { - return ECMA_VALUE_ERROR; - } -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ + ecma_string_t *prop_name_p = ecma_new_ecma_string_from_uint32 (i); - uint32_t defined_prop_count = 0; + ecma_property_descriptor_t prop_desc; + ecma_value_t get_desc = ecma_op_object_get_own_property_descriptor (obj_p, prop_name_p, &prop_desc); - ecma_value_t *buffer_p = array_index_props_p->buffer_p; - - /* Count properties with name that is array index less than len */ - for (uint32_t i = 0; i < array_index_props_p->item_count; i++) - { - ecma_string_t *property_name_p = ecma_get_string_from_value (buffer_p[i]); - - uint32_t index = ecma_string_get_array_index (property_name_p); - JERRY_ASSERT (index != ECMA_STRING_NOT_ARRAY_INDEX); - - if (index < len) + if (ECMA_IS_VALUE_ERROR (get_desc)) { - defined_prop_count++; + ecma_collection_free (array_index_props_p); + ecma_deref_ecma_string (prop_name_p); + return get_desc; + } + + if (ecma_is_value_true (get_desc)) + { + ecma_ref_ecma_string (prop_name_p); + ecma_collection_push_back (array_index_props_p, ecma_make_string_value (prop_name_p)); + ecma_free_property_descriptor (&prop_desc); + continue; } } + uint32_t defined_prop_count = array_index_props_p->item_count; + ecma_value_t ret_value = ECMA_VALUE_ERROR; uint32_t copied_num = 0; JMEM_DEFINE_LOCAL_ARRAY (values_buffer, defined_prop_count, ecma_value_t); - buffer_p = array_index_props_p->buffer_p; + ecma_value_t *buffer_p = array_index_props_p->buffer_p; /* Copy unsorted array into a native c array. */ for (uint32_t i = 0; i < array_index_props_p->item_count; i++) @@ -1184,8 +1184,7 @@ clean_up: JERRY_ASSERT (ecma_is_value_empty (ret_value)); /* Undefined properties should be in the back of the array. */ - - buffer_p = array_index_props_p->buffer_p; + ecma_value_t *buffer_p = array_index_props_p->buffer_p; for (uint32_t i = 0; i < array_index_props_p->item_count; i++) { diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-json.c b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-json.c index e89cfe9dc..404ad9adf 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-json.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-json.c @@ -54,33 +54,6 @@ ecma_json_has_object_in_stack (ecma_json_occurence_stack_item_t *stack_p, /**< s return false; } /* ecma_json_has_object_in_stack */ -/** - * Check the string value existance in the collection. - * - * Used by: - * - ecma_builtin_json_stringify step 4.b.ii.5 - * - * @return true, if the string is already in the collection. - */ -bool -ecma_has_string_value_in_collection (ecma_collection_t *collection_p, /**< collection */ - ecma_string_t *string_p) /**< string */ -{ - ecma_value_t *buffer_p = collection_p->buffer_p; - - for (uint32_t i = 0; i < collection_p->item_count; i++) - { - ecma_string_t *current_p = ecma_get_string_from_value (buffer_p[i]); - - if (ecma_compare_ecma_strings (current_p, string_p)) - { - return true; - } - } - - return false; -} /* ecma_has_string_value_in_collection */ - #endif /* ENABLED (JERRY_BUILTIN_JSON) */ /** diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c index 48e83b1e2..f7f8d5c70 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c @@ -226,42 +226,6 @@ ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /**< th return ret_string_p; } /* ecma_builtin_helper_get_to_locale_string_at_index */ -/** - * The Object's 'getOwnPropertyNames' routine. - * - * See also: - * ECMA-262 v5, 15.2.3.4 steps 2-5 - * - * @return ecma value - Array of property names. - * Returned value must be freed with ecma_free_value. - */ -ecma_value_t -ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /**< object */ - uint32_t opts) /**< any combination of ecma_list_properties_options_t */ -{ - JERRY_ASSERT (obj_p != NULL); - - ecma_collection_t *props_p = ecma_op_object_get_property_names (obj_p, opts); - -#if ENABLED (JERRY_BUILTIN_PROXY) - if (props_p == NULL) - { - return ECMA_VALUE_ERROR; - } -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ - - if (props_p->item_count == 0) - { - ecma_collection_destroy (props_p); - return ecma_op_create_array_object (NULL, 0, false); - } - - ecma_value_t new_array = ecma_op_create_array_object (props_p->buffer_p, props_p->item_count, false); - ecma_collection_free (props_p); - - return new_array; -} /* ecma_builtin_helper_object_get_properties */ - /** * Helper function to normalizing an array index * diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h index afaa35280..226780518 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h @@ -46,8 +46,6 @@ ecma_builtin_helper_object_to_string (const ecma_value_t this_arg); ecma_string_t * ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, uint32_t index); ecma_value_t -ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, uint32_t opts); -ecma_value_t ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, uint32_t *length_p, ecma_value_t value); uint32_t ecma_builtin_helper_array_index_normalize (ecma_value_t arg, uint32_t length, uint32_t *number_p); @@ -216,7 +214,6 @@ ecma_value_t ecma_builtin_json_parse_buffer (const lit_utf8_byte_t * str_start_p lit_utf8_size_t string_size); ecma_value_t ecma_builtin_json_stringify_no_opts (const ecma_value_t value); bool ecma_json_has_object_in_stack (ecma_json_occurence_stack_item_t *stack_p, ecma_object_t *object_p); -bool ecma_has_string_value_in_collection (ecma_collection_t *collection_p, ecma_string_t *string_p); ecma_value_t ecma_builtin_helper_json_create_non_formatted_json (lit_utf8_byte_t left_bracket, lit_utf8_byte_t right_bracket, diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c index 1c931bb35..ff7dee890 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c @@ -694,7 +694,8 @@ ecma_builtin_json_internalize_property (ecma_object_t *reviver_p, /**< reviver f /* 3.d */ else { - ecma_collection_t *props_p = ecma_op_object_get_property_names (object_p, ECMA_LIST_ENUMERABLE); + ecma_collection_t *props_p = ecma_op_object_get_enumerable_property_names (object_p, + ECMA_ENUMERABLE_PROPERTY_KEYS); JERRY_ASSERT (props_p != NULL); @@ -983,7 +984,7 @@ ecma_builtin_json_serialize_object (ecma_json_stringify_context_t *context_p, /* /* 6. */ else { - property_keys_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_ENUMERABLE); + property_keys_p = ecma_op_object_get_enumerable_property_names (obj_p, ECMA_ENUMERABLE_PROPERTY_KEYS); #if ENABLED (JERRY_BUILTIN_PROXY) if (property_keys_p == NULL) @@ -1573,7 +1574,7 @@ ecma_builtin_json_stringify (ecma_value_t this_arg, /**< 'this' argument */ JERRY_ASSERT (ecma_is_value_string (item)); ecma_string_t *string_p = ecma_get_string_from_value (item); - if (!ecma_has_string_value_in_collection (context.property_list_p, string_p)) + if (!ecma_collection_has_string_value (context.property_list_p, string_p)) { ecma_collection_push_back (context.property_list_p, item); } diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-object.c b/jerry-core/ecma/builtin-objects/ecma-builtin-object.c index 8d9a145fe..44290623c 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-object.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-object.c @@ -55,13 +55,15 @@ enum ECMA_OBJECT_ROUTINE_ASSIGN, ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_DESCRIPTOR, ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_DESCRIPTORS, - ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_NAMES, - ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_SYMBOLS, ECMA_OBJECT_ROUTINE_GET_PROTOTYPE_OF, ECMA_OBJECT_ROUTINE_KEYS, ECMA_OBJECT_ROUTINE_VALUES, ECMA_OBJECT_ROUTINE_ENTRIES, + /* These should be in this order. */ + ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_NAMES, + ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_SYMBOLS, + /* These should be in this order. */ ECMA_OBJECT_ROUTINE_FREEZE, ECMA_OBJECT_ROUTINE_PREVENT_EXTENSIONS, @@ -289,40 +291,6 @@ ecma_builtin_object_object_set_proto (ecma_value_t arg1, /**< routine's first ar } /* ecma_builtin_object_object_set_proto */ #endif /* ENABLED (JERRY_ESNEXT) */ -/** - * The Object object's 'getOwnPropertyNames' routine - * - * See also: - * ECMA-262 v5, 15.2.3.4 - * - * @return ecma value - * Returned value must be freed with ecma_free_value. - */ -static ecma_value_t -ecma_builtin_object_object_get_own_property_names (ecma_object_t *obj_p) /**< routine's argument */ -{ - return ecma_builtin_helper_object_get_properties (obj_p, ECMA_LIST_NO_OPTS); -} /* ecma_builtin_object_object_get_own_property_names */ - -#if ENABLED (JERRY_ESNEXT) - -/** - * The Object object's 'getOwnPropertySymbols' routine - * - * See also: - * ECMA-262 v6, 19.1.2.7 - * - * @return ecma value - * Returned value must be freed with ecma_free_value. - */ -static ecma_value_t -ecma_builtin_object_object_get_own_property_symbols (ecma_object_t *obj_p) /**< routine's argument */ -{ - return ecma_builtin_helper_object_get_properties (obj_p, ECMA_LIST_SYMBOLS_ONLY); -} /* ecma_builtin_object_object_get_own_property_symbols */ - -#endif /* ENABLED (JERRY_ESNEXT) */ - /** * SetIntegrityLevel operation * @@ -349,20 +317,13 @@ ecma_builtin_object_set_integrity_level (ecma_object_t *obj_p, /**< object */ } } else +#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ { -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ ecma_op_ordinary_object_prevent_extensions (obj_p); -#if ENABLED (JERRY_BUILTIN_PROXY) } -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ /* 6. */ - uint32_t opts = ECMA_LIST_CONVERT_FAST_ARRAYS; -#if ENABLED (JERRY_ESNEXT) - opts |= ECMA_LIST_SYMBOLS; -#endif /* ENABLED (JERRY_ESNEXT) */ - - ecma_collection_t *props_p = ecma_op_object_get_property_names (obj_p, opts); + ecma_collection_t *props_p = ecma_op_object_own_property_keys (obj_p); #if ENABLED (JERRY_BUILTIN_PROXY) if (props_p == NULL) @@ -606,12 +567,10 @@ ecma_builtin_object_test_integrity_level (ecma_object_t *obj_p, /**< routine's a is_extensible = ecma_is_value_true (status); } else +#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ { -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ is_extensible = ecma_op_ordinary_object_is_extensible (obj_p); -#if ENABLED (JERRY_BUILTIN_PROXY) } -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ if (is_extensible) { @@ -622,7 +581,7 @@ ecma_builtin_object_test_integrity_level (ecma_object_t *obj_p, /**< routine's a ecma_value_t ret_value = ECMA_VALUE_TRUE; /* 2. */ - ecma_collection_t *props_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_NO_OPTS); + ecma_collection_t *props_p = ecma_op_object_own_property_keys (obj_p); #if ENABLED (JERRY_BUILTIN_PROXY) if (props_p == NULL) @@ -780,7 +739,7 @@ static ecma_value_t ecma_builtin_object_object_get_own_property_descriptors (ecma_object_t *obj_p) /**< routine's first argument */ { /* 2 */ - ecma_collection_t *prop_names_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_SYMBOLS); + ecma_collection_t *prop_names_p = ecma_op_object_own_property_keys (obj_p); #if ENABLED (JERRY_BUILTIN_PROXY) if (prop_names_p == NULL) @@ -841,6 +800,7 @@ ecma_builtin_object_object_get_own_property_descriptors (ecma_object_t *obj_p) / * * See also: * ECMA-262 v5, 15.2.3.7 + * ECMA-262 v11, 19.1.2.3.1 * * @return ecma value * Returned value must be freed with ecma_free_value. @@ -858,14 +818,9 @@ ecma_builtin_object_object_define_properties (ecma_object_t *obj_p, /**< routine } ecma_object_t *props_p = ecma_get_object_from_value (props); + /* 3. */ - uint32_t options = ECMA_LIST_CONVERT_FAST_ARRAYS | ECMA_LIST_ENUMERABLE; - -#if ENABLED (JERRY_ESNEXT) - options |= ECMA_LIST_SYMBOLS; -#endif /* ENABLED (JERRY_ESNEXT) */ - - ecma_collection_t *prop_names_p = ecma_op_object_get_property_names (props_p, options); + ecma_collection_t *prop_names_p = ecma_op_object_own_property_keys (props_p); ecma_value_t ret_value = ECMA_VALUE_ERROR; #if ENABLED (JERRY_BUILTIN_PROXY) @@ -881,40 +836,67 @@ ecma_builtin_object_object_define_properties (ecma_object_t *obj_p, /**< routine /* 4. */ JMEM_DEFINE_LOCAL_ARRAY (property_descriptors, prop_names_p->item_count, ecma_property_descriptor_t); uint32_t property_descriptor_number = 0; + ecma_collection_t *enum_prop_names = ecma_new_collection (); + /* 5. */ for (uint32_t i = 0; i < prop_names_p->item_count; i++) { - /* 5.a */ - ecma_value_t desc_obj = ecma_op_object_get (props_p, ecma_get_prop_name_from_value (buffer_p[i])); + ecma_string_t *prop_name_p = ecma_get_prop_name_from_value (buffer_p[i]); - if (ECMA_IS_VALUE_ERROR (desc_obj)) + ecma_property_descriptor_t prop_desc; + ecma_value_t get_desc = ecma_op_object_get_own_property_descriptor (props_p, + prop_name_p, + &prop_desc); + +#if ENABLED (JERRY_ESNEXT) + if (ECMA_IS_VALUE_ERROR (get_desc)) { goto cleanup; } +#endif /* ENABLED (JERRY_ESNEXT) */ - /* 5.b */ - ecma_value_t conv_result = ecma_op_to_property_descriptor (desc_obj, - &property_descriptors[property_descriptor_number]); - - property_descriptors[property_descriptor_number].flags |= ECMA_PROP_IS_THROW; - - ecma_free_value (desc_obj); - - if (ECMA_IS_VALUE_ERROR (conv_result)) + if (ecma_is_value_true (get_desc)) { - goto cleanup; + if (prop_desc.flags & ECMA_PROP_IS_ENUMERABLE) + { + ecma_value_t desc_obj = ecma_op_object_get (props_p, prop_name_p); + + if (ECMA_IS_VALUE_ERROR (desc_obj)) + { + ecma_free_property_descriptor (&prop_desc); + goto cleanup; + } + + ecma_value_t conv_result = ecma_op_to_property_descriptor (desc_obj, + &property_descriptors[property_descriptor_number]); + + property_descriptors[property_descriptor_number].flags |= ECMA_PROP_IS_THROW; + + ecma_free_value (desc_obj); + + if (ECMA_IS_VALUE_ERROR (conv_result)) + { + ecma_free_property_descriptor (&prop_desc); + goto cleanup; + } + + property_descriptor_number++; + ecma_free_value (conv_result); + ecma_ref_ecma_string (prop_name_p); + ecma_collection_push_back (enum_prop_names, buffer_p[i]); + } + + ecma_free_property_descriptor (&prop_desc); } - - property_descriptor_number++; - - ecma_free_value (conv_result); } /* 6. */ - for (uint32_t i = 0; i < prop_names_p->item_count; i++) + for (uint32_t i = 0; i < enum_prop_names->item_count; i++) { + ecma_string_t *prop_name_p = ecma_get_prop_name_from_value (enum_prop_names->buffer_p[i]); + ecma_value_t define_own_prop_ret = ecma_op_object_define_own_property (obj_p, - ecma_get_prop_name_from_value (buffer_p[i]), + prop_name_p, &property_descriptors[i]); if (ECMA_IS_VALUE_ERROR (define_own_prop_ret)) { @@ -936,6 +918,8 @@ cleanup: ecma_free_property_descriptor (&property_descriptors[index]); } + ecma_collection_free (enum_prop_names); + JMEM_FINALIZE_LOCAL_ARRAY (property_descriptors); ecma_collection_free (prop_names_p); @@ -1071,8 +1055,8 @@ ecma_builtin_object_object_assign (ecma_object_t *target_p, /**< target object * ecma_object_t *from_obj_p = ecma_get_object_from_value (from_value); /* 5.b.iii */ - ecma_collection_t *props_p = ecma_op_object_get_property_names (from_obj_p, ECMA_LIST_CONVERT_FAST_ARRAYS - | ECMA_LIST_SYMBOLS); + ecma_collection_t *props_p = ecma_op_object_own_property_keys (from_obj_p); + #if ENABLED (JERRY_BUILTIN_PROXY) if (props_p == NULL) { @@ -1166,6 +1150,71 @@ ecma_builtin_object_object_is (ecma_value_t arg1, /**< routine's first argument #endif /* ENABLED (JERRY_ESNEXT) */ +/** + * GetOwnPropertyKeys abstract method + * + * See also: + * ECMA-262 v11, 19.1.2.11.1 + * + * @return ecma value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_op_object_get_own_property_keys (ecma_value_t this_arg, /**< this argument */ + uint16_t type) /**< routine type */ +{ +#if ENABLED (JERRY_ESNEXT) + /* 1. */ + ecma_value_t object = ecma_op_to_object (this_arg); + + if (ECMA_IS_VALUE_ERROR (object)) + { + return object; + } + + ecma_object_t *obj_p = ecma_get_object_from_value (object); + + /* 2. */ + ecma_collection_t *props_p = ecma_op_object_own_property_keys (obj_p); + + if (props_p == NULL) + { + ecma_deref_object (obj_p); + return ECMA_VALUE_ERROR; + } + + /* 3. */ + ecma_collection_t *name_list = ecma_new_collection (); + + /* 4. */ + for (uint32_t i = 0; i < props_p->item_count; i++) + { + ecma_value_t prop_name = props_p->buffer_p[i]; + ecma_string_t *name_p = ecma_get_prop_name_from_value (prop_name); + + if ((ecma_prop_name_is_symbol (name_p) && type == ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_SYMBOLS) + || (ecma_is_value_string (prop_name) && type == ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_NAMES)) + { + ecma_ref_ecma_string (name_p); + ecma_collection_push_back (name_list, prop_name); + } + } + + ecma_value_t result_array = ecma_op_create_array_object (name_list->buffer_p, name_list->item_count, false); + + ecma_deref_object (obj_p); + ecma_collection_free (name_list); +#else /* !ENABLED (JERRY_ESNEXT) */ + JERRY_UNUSED (type); + ecma_object_t *obj_p = ecma_get_object_from_value (this_arg); + ecma_collection_t *props_p = ecma_op_object_own_property_keys (obj_p); + ecma_value_t result_array = ecma_op_create_array_object (props_p->buffer_p, props_p->item_count, false); +#endif /* ENABLED (JERRY_ESNEXT) */ + + ecma_collection_free (props_p); + return result_array; +} /* ecma_op_object_get_own_property_keys */ + /** * Dispatcher of the built-in's routines * @@ -1269,22 +1318,12 @@ ecma_builtin_object_dispatch_routine (uint16_t builtin_routine_id, /**< built-in result = ecma_builtin_object_object_get_prototype_of (obj_p); break; } - case ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_NAMES: - { - result = ecma_builtin_object_object_get_own_property_names (obj_p); - break; - } #if ENABLED (JERRY_ESNEXT) case ECMA_OBJECT_ROUTINE_ASSIGN: { result = ecma_builtin_object_object_assign (obj_p, arguments_list_p + 1, arguments_number - 1); break; } - case ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_SYMBOLS: - { - result = ecma_builtin_object_object_get_own_property_symbols (obj_p); - break; - } case ECMA_OBJECT_ROUTINE_ENTRIES: case ECMA_OBJECT_ROUTINE_VALUES: #endif /* ENABLED (JERRY_ESNEXT) */ @@ -1329,6 +1368,10 @@ ecma_builtin_object_dispatch_routine (uint16_t builtin_routine_id, /**< built-in #endif /* ENABLED (JERRY_ESNEXT) */ return result; } + else if (builtin_routine_id <= ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_SYMBOLS) + { + return ecma_op_object_get_own_property_keys (arg1, builtin_routine_id); + } else if (builtin_routine_id <= ECMA_OBJECT_ROUTINE_SEAL) { #if ENABLED (JERRY_ESNEXT) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-reflect.c b/jerry-core/ecma/builtin-objects/ecma-builtin-reflect.c index 6714d9f0a..88ea0e6c2 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-reflect.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-reflect.c @@ -165,8 +165,21 @@ ecma_builtin_reflect_dispatch_routine (uint16_t builtin_routine_id, /**< built-i ecma_object_t *target_p = ecma_get_object_from_value (arguments_list[0]); - /* 2. 3. */ - return ecma_builtin_helper_object_get_properties (target_p, ECMA_LIST_SYMBOLS); + /* 2. */ + ecma_collection_t *prop_names = ecma_op_object_own_property_keys (target_p); + +#if ENABLED (JERRY_BUILTIN_PROXY) + if (prop_names == NULL) + { + return ECMA_VALUE_ERROR; + } +#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ + + /* 3. */ + ecma_value_t new_array = ecma_op_create_array_object (prop_names->buffer_p, prop_names->item_count, false); + ecma_collection_free (prop_names); + + return new_array; } if (builtin_routine_id == ECMA_REFLECT_OBJECT_CONSTRUCT) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtins.c b/jerry-core/ecma/builtin-objects/ecma-builtins.c index 0ca9130f6..a41303073 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtins.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtins.c @@ -1041,43 +1041,32 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object * */ void ecma_builtin_routine_list_lazy_property_names (ecma_object_t *object_p, /**< a built-in object */ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped 'non-enumerable' - * collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { JERRY_ASSERT (ecma_get_object_is_builtin (object_p)); JERRY_ASSERT (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_FUNCTION); JERRY_ASSERT (ecma_builtin_function_is_routine (object_p)); - const bool separate_enumerable = (opts & ECMA_LIST_ENUMERABLE) != 0; - const bool is_array_indices_only = (opts & ECMA_LIST_ARRAY_INDICES) != 0; - - ecma_collection_t *for_enumerable_p = main_collection_p; - JERRY_UNUSED (for_enumerable_p); - - ecma_collection_t *for_non_enumerable_p = separate_enumerable ? non_enum_collection_p : main_collection_p; - - if (!is_array_indices_only) - { #if ENABLED (JERRY_ESNEXT) - ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) object_p; - if (!(ext_func_p->u.built_in.u.builtin_routine.bitset & ECMA_BUILTIN_ROUTINE_LENGTH_INITIALIZED)) - { - /* Unintialized 'length' property is non-enumerable (ECMA-262 v6, 19.2.4.1) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); - } - if (!(ext_func_p->u.built_in.u.builtin_routine.bitset & ECMA_BUILTIN_ROUTINE_NAME_INITIALIZED)) - { - /* Unintialized 'name' property is non-enumerable (ECMA-262 v6, 19.2.4.2) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_NAME)); - } -#else /* !ENABLED (JERRY_ESNEXT) */ - /* 'length' property is non-enumerable (ECMA-262 v5, 15) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); -#endif /* ENABLED (JERRY_ESNEXT) */ + ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) object_p; + if (!(ext_func_p->u.built_in.u.builtin_routine.bitset & ECMA_BUILTIN_ROUTINE_LENGTH_INITIALIZED)) + { + /* Unintialized 'length' property is non-enumerable (ECMA-262 v6, 19.2.4.1) */ + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; } + if (!(ext_func_p->u.built_in.u.builtin_routine.bitset & ECMA_BUILTIN_ROUTINE_NAME_INITIALIZED)) + { + /* Unintialized 'name' property is non-enumerable (ECMA-262 v6, 19.2.4.2) */ + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_NAME)); + prop_counter_p->string_named_props++; + } +#else /* !ENABLED (JERRY_ESNEXT) */ + /* 'length' property is non-enumerable (ECMA-262 v5, 15) */ + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; +#endif /* ENABLED (JERRY_ESNEXT) */ } /* ecma_builtin_routine_list_lazy_property_names */ /** @@ -1088,19 +1077,13 @@ ecma_builtin_routine_list_lazy_property_names (ecma_object_t *object_p, /**< a b */ void ecma_builtin_list_lazy_property_names (ecma_object_t *object_p, /**< a built-in object */ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped 'non-enumerable' - * collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { JERRY_ASSERT (ecma_get_object_is_builtin (object_p)); JERRY_ASSERT (ecma_get_object_type (object_p) != ECMA_OBJECT_TYPE_FUNCTION || !ecma_builtin_function_is_routine (object_p)); - const bool separate_enumerable = (opts & ECMA_LIST_ENUMERABLE) != 0; - const bool is_array_indices_only = (opts & ECMA_LIST_ARRAY_INDICES) != 0; - ecma_built_in_props_t *built_in_props_p; ecma_object_type_t object_type = ecma_get_object_type (object_p); @@ -1123,9 +1106,6 @@ ecma_builtin_list_lazy_property_names (ecma_object_t *object_p, /**< a built-in uint32_t index = 0; uint32_t *bitset_p = built_in_props_p->u.instantiated_bitset; - ecma_collection_t *for_non_enumerable_p = (separate_enumerable ? non_enum_collection_p - : main_collection_p); - while (curr_property_p->magic_string_id != LIT_MAGIC_STRING__COUNT) { if (index == 32) @@ -1145,28 +1125,13 @@ ecma_builtin_list_lazy_property_names (ecma_object_t *object_p, /**< a built-in ecma_string_t *name_p = ecma_get_magic_string ((lit_magic_string_id_t) curr_property_p->magic_string_id); - if (is_array_indices_only && ecma_string_get_array_index (name_p) == ECMA_STRING_NOT_ARRAY_INDEX) - { - curr_property_p++; - continue; - } - uint32_t bit_for_index = (uint32_t) 1u << index; if (!(*bitset_p & bit_for_index) || ecma_op_ordinary_object_has_own_property (object_p, name_p)) { ecma_value_t name = ecma_make_magic_string_value ((lit_magic_string_id_t) curr_property_p->magic_string_id); - -#if ENABLED (JERRY_ESNEXT) - if (curr_property_p->attributes & ECMA_PROPERTY_FLAG_ENUMERABLE) - { - ecma_collection_push_back (main_collection_p, name); - } - else -#endif /* ENABLED (JERRY_ESNEXT) */ - { - ecma_collection_push_back (for_non_enumerable_p, name); - } + ecma_collection_push_back (prop_names_p, name); + prop_counter_p->string_named_props++; } curr_property_p++; diff --git a/jerry-core/ecma/builtin-objects/ecma-builtins.h b/jerry-core/ecma/builtin-objects/ecma-builtins.h index 35f3f6cd5..e8955f173 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtins.h +++ b/jerry-core/ecma/builtin-objects/ecma-builtins.h @@ -93,14 +93,12 @@ ecma_property_t * ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, ecma_string_t *string_p); void ecma_builtin_routine_list_lazy_property_names (ecma_object_t *object_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); void ecma_builtin_list_lazy_property_names (ecma_object_t *object_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); bool ecma_builtin_is (ecma_object_t *obj_p, ecma_builtin_id_t builtin_id); ecma_object_t * diff --git a/jerry-core/ecma/operations/ecma-array-object.c b/jerry-core/ecma/operations/ecma-array-object.c index cfdef1417..3e70e1d48 100644 --- a/jerry-core/ecma/operations/ecma-array-object.c +++ b/jerry-core/ecma/operations/ecma-array-object.c @@ -511,61 +511,35 @@ ecma_fast_array_set_length (ecma_object_t *object_p, /**< fast access mode array * @return collection of strings - property names */ ecma_collection_t * -ecma_fast_array_get_property_names (ecma_object_t *object_p, /**< fast access mode array object */ - uint32_t opts) /**< any combination of ecma_list_properties_options_t values */ +ecma_fast_array_object_own_property_keys (ecma_object_t *object_p) /**< fast access mode array object */ { JERRY_ASSERT (ecma_op_object_is_fast_array (object_p)); ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) object_p; ecma_collection_t *ret_p = ecma_new_collection (); - -#if ENABLED (JERRY_ESNEXT) - if (opts & ECMA_LIST_SYMBOLS_ONLY) - { - return ret_p; - } -#endif /* ENABLED (JERRY_ESNEXT) */ - uint32_t length = ext_obj_p->u.array.length; - bool append_length = (!(opts & ECMA_LIST_ENUMERABLE) && !(opts & ECMA_LIST_ARRAY_INDICES)); - - if (length == 0) + if (length != 0) { - if (append_length) + ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, object_p->u1.property_list_cp); + + for (uint32_t i = 0; i < length; i++) { - ecma_collection_push_back (ret_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + if (ecma_is_value_array_hole (values_p[i])) + { + continue; + } + + ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i); + + ecma_collection_push_back (ret_p, ecma_make_string_value (index_str_p)); } - - return ret_p; } - ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, object_p->u1.property_list_cp); - - for (uint32_t i = 0; i < length; i++) - { - if (ecma_is_value_array_hole (values_p[i])) - { - continue; - } - - ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i); - - ecma_collection_push_back (ret_p, ecma_make_string_value (index_str_p)); - } - - if (append_length) - { - ecma_collection_push_back (ret_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); - } - - if (opts & ECMA_LIST_CONVERT_FAST_ARRAYS) - { - ecma_fast_array_convert_to_normal (object_p); - } + ecma_collection_push_back (ret_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); return ret_p; -} /* ecma_fast_array_get_property_names */ +} /* ecma_fast_array_object_own_property_keys */ /** * Array object creation operation. @@ -1240,30 +1214,6 @@ ecma_array_object_to_string (ecma_value_t this_arg) /**< this argument */ return ret_value; } /* ecma_array_object_to_string */ -/** - * List names of a String object's lazy instantiated properties - * - * @return string values collection - */ -void -ecma_op_array_list_lazy_property_names (ecma_object_t *obj_p, /**< a String object */ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped - * 'non-enumerable' - * collection */ -{ - JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_ARRAY); - - ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p; - - if ((opts & ECMA_LIST_ARRAY_INDICES) == 0) - { - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); - } -} /* ecma_op_array_list_lazy_property_names */ - /** * @} * @} diff --git a/jerry-core/ecma/operations/ecma-array-object.h b/jerry-core/ecma/operations/ecma-array-object.h index 982639a84..a0c2e9562 100644 --- a/jerry-core/ecma/operations/ecma-array-object.h +++ b/jerry-core/ecma/operations/ecma-array-object.h @@ -90,7 +90,7 @@ uint32_t ecma_delete_fast_array_properties (ecma_object_t *object_p, uint32_t new_length); ecma_collection_t * -ecma_fast_array_get_property_names (ecma_object_t *object_p, uint32_t opts); +ecma_fast_array_object_own_property_keys (ecma_object_t *object_p); void ecma_fast_array_convert_to_normal (ecma_object_t *object_p); @@ -121,12 +121,6 @@ uint32_t ecma_array_get_length (ecma_object_t *array_p); ecma_value_t ecma_array_object_to_string (ecma_value_t this_arg); -void -ecma_op_array_list_lazy_property_names (ecma_object_t *obj_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); - /** * @} * @} diff --git a/jerry-core/ecma/operations/ecma-function-object.c b/jerry-core/ecma/operations/ecma-function-object.c index f0ea54c77..bdbaf31c1 100644 --- a/jerry-core/ecma/operations/ecma-function-object.c +++ b/jerry-core/ecma/operations/ecma-function-object.c @@ -1747,27 +1747,21 @@ ecma_op_bound_function_try_to_lazy_instantiate_property (ecma_object_t *object_p */ void ecma_op_function_list_lazy_property_names (ecma_object_t *object_p, /**< functionobject */ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped - * 'non-enumerable' - * collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { - JERRY_UNUSED (main_collection_p); - - ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p; - #if ENABLED (JERRY_ESNEXT) ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) object_p; if (!ECMA_GET_FIRST_BIT_FROM_POINTER_TAG (ext_func_p->u.function.scope_cp)) { /* Unintialized 'length' property is non-enumerable (ECMA-262 v6, 19.2.4.1) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; } #else /* !ENABLED (JERRY_ESNEXT) */ /* 'length' property is non-enumerable (ECMA-262 v5, 13.2.5) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; #endif /* ENABLED (JERRY_ESNEXT) */ const ecma_compiled_code_t *bytecode_data_p; @@ -1781,7 +1775,8 @@ ecma_op_function_list_lazy_property_names (ecma_object_t *object_p, /**< functio #endif /* ENABLED (JERRY_ESNEXT) */ /* 'prototype' property is non-enumerable (ECMA-262 v5, 13.2.18) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_PROTOTYPE)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_PROTOTYPE)); + prop_counter_p->string_named_props++; #if ENABLED (JERRY_ESNEXT) bool append_caller_and_arguments = !(bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE); @@ -1792,10 +1787,12 @@ ecma_op_function_list_lazy_property_names (ecma_object_t *object_p, /**< functio if (append_caller_and_arguments) { /* 'caller' property is non-enumerable (ECMA-262 v5, 13.2.5) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_CALLER)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_CALLER)); /* 'arguments' property is non-enumerable (ECMA-262 v5, 13.2.5) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_ARGUMENTS)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_ARGUMENTS)); + + prop_counter_p->string_named_props += 2; } } /* ecma_op_function_list_lazy_property_names */ @@ -1808,16 +1805,9 @@ ecma_op_function_list_lazy_property_names (ecma_object_t *object_p, /**< functio */ void ecma_op_external_function_list_lazy_property_names (ecma_object_t *object_p, /**< function object */ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped - * collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { - JERRY_UNUSED (main_collection_p); - - ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p; - #if !ENABLED (JERRY_ESNEXT) JERRY_UNUSED (object_p); #else /* ENABLED (JERRY_ESNEXT) */ @@ -1825,7 +1815,8 @@ ecma_op_external_function_list_lazy_property_names (ecma_object_t *object_p, /** #endif /* !ENABLED (JERRY_ESNEXT) */ { /* 'prototype' property is non-enumerable (ECMA-262 v5, 13.2.18) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_PROTOTYPE)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_PROTOTYPE)); + prop_counter_p->string_named_props++; } } /* ecma_op_external_function_list_lazy_property_names */ @@ -1838,35 +1829,31 @@ ecma_op_external_function_list_lazy_property_names (ecma_object_t *object_p, /** */ void ecma_op_bound_function_list_lazy_property_names (ecma_object_t *object_p, /**< bound function object*/ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped - * 'non-enumerable' - * collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { - JERRY_UNUSED (main_collection_p); - - ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p; - #if ENABLED (JERRY_ESNEXT) /* Unintialized 'length' property is non-enumerable (ECMA-262 v6, 19.2.4.1) */ ecma_bound_function_t *bound_func_p = (ecma_bound_function_t *) object_p; if (!ECMA_GET_FIRST_BIT_FROM_POINTER_TAG (bound_func_p->header.u.bound_function.target_function)) { - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; } #else /* !ENABLED (JERRY_ESNEXT) */ JERRY_UNUSED (object_p); /* 'length' property is non-enumerable (ECMA-262 v5, 13.2.5) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; #endif /* ENABLED (JERRY_ESNEXT) */ /* 'caller' property is non-enumerable (ECMA-262 v5, 13.2.5) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_CALLER)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_CALLER)); /* 'arguments' property is non-enumerable (ECMA-262 v5, 13.2.5) */ - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_ARGUMENTS)); + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_ARGUMENTS)); + + prop_counter_p->string_named_props += 2; } /* ecma_op_bound_function_list_lazy_property_names */ /** diff --git a/jerry-core/ecma/operations/ecma-function-object.h b/jerry-core/ecma/operations/ecma-function-object.h index 5ba199d0d..6aa6c5bdc 100644 --- a/jerry-core/ecma/operations/ecma-function-object.h +++ b/jerry-core/ecma/operations/ecma-function-object.h @@ -89,21 +89,18 @@ ecma_op_bound_function_try_to_lazy_instantiate_property (ecma_object_t *object_p void ecma_op_function_list_lazy_property_names (ecma_object_t *object_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); void ecma_op_external_function_list_lazy_property_names (ecma_object_t *object_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); void ecma_op_bound_function_list_lazy_property_names (ecma_object_t *object_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); /** * @} diff --git a/jerry-core/ecma/operations/ecma-objects.c b/jerry-core/ecma/operations/ecma-objects.c index 14add5e2b..8279fd1bf 100644 --- a/jerry-core/ecma/operations/ecma-objects.c +++ b/jerry-core/ecma/operations/ecma-objects.c @@ -1997,7 +1997,7 @@ ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p, /**< routine ecma_enumerable_property_names_options_t option) /**< listing option */ { /* 2. */ - ecma_collection_t *prop_names_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_NO_OPTS); + ecma_collection_t *prop_names_p = ecma_op_object_own_property_keys (obj_p); #if ENABLED (JERRY_BUILTIN_PROXY) if (JERRY_UNLIKELY (prop_names_p == NULL)) @@ -2014,79 +2014,69 @@ ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p, /**< routine for (uint32_t i = 0; i < prop_names_p->item_count; i++) { /* 4.a */ - JERRY_ASSERT (ecma_is_value_string (names_buffer_p[i])); - - ecma_string_t *key_p = ecma_get_string_from_value (names_buffer_p[i]); - - /* 4.a.i */ - ecma_property_descriptor_t prop_desc; - ecma_value_t status; - -#if ENABLED (JERRY_BUILTIN_PROXY) - if (ECMA_OBJECT_IS_PROXY (obj_p)) + if (ecma_is_value_string (names_buffer_p[i])) { - status = ecma_proxy_object_get_own_property_descriptor (obj_p, key_p, &prop_desc); - } - else -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ - { - status = ecma_op_object_get_own_property_descriptor (obj_p, key_p, &prop_desc); - } + ecma_string_t *key_p = ecma_get_string_from_value (names_buffer_p[i]); - if (ECMA_IS_VALUE_ERROR (status)) - { - ecma_collection_free (prop_names_p); - ecma_collection_free (properties_p); + /* 4.a.i */ + ecma_property_descriptor_t prop_desc; + ecma_value_t status = ecma_op_object_get_own_property_descriptor (obj_p, key_p, &prop_desc); - return NULL; - } - - /* 4.a.ii */ - if ((prop_desc.flags & ECMA_PROP_IS_ENUMERABLE) != 0) - { - /* 4.a.ii.1 */ - if (option == ECMA_ENUMERABLE_PROPERTY_KEYS) + if (ECMA_IS_VALUE_ERROR (status)) { - ecma_collection_push_back (properties_p, ecma_copy_value (names_buffer_p[i])); + ecma_collection_free (prop_names_p); + ecma_collection_free (properties_p); + + return NULL; } - else + + /* 4.a.ii */ + if ((prop_desc.flags & ECMA_PROP_IS_ENUMERABLE) != 0) { - /* 4.a.ii.2.a */ - ecma_value_t value = ecma_op_object_get (obj_p, key_p); - - if (ECMA_IS_VALUE_ERROR (value)) + /* 4.a.ii.1 */ + if (option == ECMA_ENUMERABLE_PROPERTY_KEYS) { - ecma_collection_free (prop_names_p); - ecma_collection_free (properties_p); - - return NULL; - } - - /* 4.a.ii.2.b */ - if (option == ECMA_ENUMERABLE_PROPERTY_VALUES) - { - ecma_collection_push_back (properties_p, value); + ecma_collection_push_back (properties_p, ecma_copy_value (names_buffer_p[i])); } else { - /* 4.a.ii.2.c.i */ - JERRY_ASSERT (option == ECMA_ENUMERABLE_PROPERTY_ENTRIES); + /* 4.a.ii.2.a */ + ecma_value_t value = ecma_op_object_get (obj_p, key_p); - /* 4.a.ii.2.c.ii */ - ecma_object_t *entry_p = ecma_op_new_fast_array_object (2); - ecma_fast_array_set_property (entry_p, 0, names_buffer_p[i]); - ecma_fast_array_set_property (entry_p, 1, value); - ecma_free_value (value); + if (ECMA_IS_VALUE_ERROR (value)) + { + ecma_collection_free (prop_names_p); + ecma_collection_free (properties_p); - /* 4.a.ii.2.c.iii */ - ecma_collection_push_back (properties_p, ecma_make_object_value (entry_p)); + return NULL; + } + + /* 4.a.ii.2.b */ + if (option == ECMA_ENUMERABLE_PROPERTY_VALUES) + { + ecma_collection_push_back (properties_p, value); + } + else + { + /* 4.a.ii.2.c.i */ + JERRY_ASSERT (option == ECMA_ENUMERABLE_PROPERTY_ENTRIES); + + /* 4.a.ii.2.c.ii */ + ecma_object_t *entry_p = ecma_op_new_fast_array_object (2); + ecma_fast_array_set_property (entry_p, 0, names_buffer_p[i]); + ecma_fast_array_set_property (entry_p, 1, value); + ecma_free_value (value); + + /* 4.a.ii.2.c.iii */ + ecma_collection_push_back (properties_p, ecma_make_object_value (entry_p)); + } } } - } - if (ecma_is_value_true (status)) - { - ecma_free_property_descriptor (&prop_desc); + if (ecma_is_value_true (status)) + { + ecma_free_property_descriptor (&prop_desc); + } } } @@ -2096,7 +2086,207 @@ ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p, /**< routine } /* ecma_op_object_get_enumerable_property_names */ /** - * Get collection of property names + * Helper method to check if a given property is already in the collection or not + * + * @return true - if the property is already in the collection + * false - otherwise + */ +static bool +ecma_object_prop_name_is_duplicated (ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_string_t *name_p) /**< property name */ +{ + for (uint64_t i = 0; i < prop_names_p->item_count; i++) + { + if (ecma_compare_ecma_strings (ecma_get_prop_name_from_value (prop_names_p->buffer_p[i]), name_p)) + { + return true; + } + } + + return false; +} /* ecma_object_prop_name_is_duplicated */ + +/** + * Helper method for getting lazy instantiated properties for [[OwnPropertyKeys]] + */ +static void +ecma_object_list_lazy_property_names (ecma_object_t *obj_p, /**< object */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ +{ + const ecma_object_type_t type = ecma_get_object_type (obj_p); + const bool obj_is_builtin = ecma_get_object_is_builtin (obj_p); + + if (obj_is_builtin) + { + if (type == ECMA_OBJECT_TYPE_FUNCTION && ecma_builtin_function_is_routine (obj_p)) + { + ecma_builtin_routine_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + } + else + { + ecma_builtin_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + } + } + else + { + switch (type) + { + case ECMA_OBJECT_TYPE_PSEUDO_ARRAY: + { +#if ENABLED (JERRY_BUILTIN_TYPEDARRAY) + if (ecma_object_is_typedarray (obj_p)) + { + ecma_op_typedarray_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + } +#endif /* ENABLED (JERRY_BUILTIN_TYPEDARRAY) */ + break; + } + case ECMA_OBJECT_TYPE_FUNCTION: + { + ecma_op_function_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + break; + } + case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION: + { + ecma_op_external_function_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + break; + } + case ECMA_OBJECT_TYPE_BOUND_FUNCTION: + { + ecma_op_bound_function_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + break; + } + case ECMA_OBJECT_TYPE_CLASS: + { + ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p; + + if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL) + { + ecma_op_string_list_lazy_property_names (obj_p, prop_names_p, prop_counter_p); + } + + break; + } + case ECMA_OBJECT_TYPE_ARRAY: + { + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; + break; + } + default: + { + JERRY_ASSERT (type == ECMA_OBJECT_TYPE_GENERAL); + break; + } + } + } +} /* ecma_object_list_lazy_property_names */ + +/** + * Helper method for sorting the given property names based on [[OwnPropertyKeys]] + */ +static void +ecma_object_sort_property_names (ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter) /**< prop counter */ +{ + uint32_t lazy_prop_name_count = (uint32_t) (prop_counter->lazy_string_named_props); + uint32_t string_name_pos = prop_counter->string_named_props; + +#if ENABLED (JERRY_ESNEXT) + uint32_t symbol_name_pos = prop_counter->symbol_named_props; +#endif /* ENABLED (JERRY_ESNEXT) */ + + uint32_t all_prop_count = (prop_counter->array_index_named_props) + (string_name_pos); +#if ENABLED (JERRY_ESNEXT) + all_prop_count += symbol_name_pos; +#endif /* ENABLED (JERRY_ESNEXT) */ + + ecma_value_t *names_p = jmem_heap_alloc_block (all_prop_count * sizeof (ecma_value_t)); + + ecma_value_t *string_names_p = names_p + prop_counter->array_index_named_props; +#if ENABLED (JERRY_ESNEXT) + ecma_value_t *symbol_names_p = string_names_p + string_name_pos; +#endif /* ENABLED (JERRY_ESNEXT) */ + + uint32_t array_index_name_pos = 0; + uint32_t lazy_string_name_pos = 0; + + for (uint32_t i = 0; i < prop_names_p->item_count; i++) + { + ecma_value_t prop_name = prop_names_p->buffer_p[i]; + ecma_string_t *name_p = ecma_get_prop_name_from_value (prop_name); + uint32_t index = ecma_string_get_array_index (name_p); + + /* sort array index named properties in ascending order */ + if (index != ECMA_STRING_NOT_ARRAY_INDEX) + { + JERRY_ASSERT (array_index_name_pos < prop_counter->array_index_named_props); + + uint32_t insert_pos = 0; + while (insert_pos < array_index_name_pos + && index > ecma_string_get_array_index (ecma_get_string_from_value (names_p[insert_pos]))) + { + insert_pos++; + } + + if (insert_pos == array_index_name_pos) + { + names_p[array_index_name_pos++] = prop_name; + } + else + { + JERRY_ASSERT (insert_pos < array_index_name_pos); + JERRY_ASSERT (index <= ecma_string_get_array_index (ecma_get_string_from_value (names_p[insert_pos]))); + + uint32_t move_pos = array_index_name_pos++; + + while (move_pos > insert_pos) + { + names_p[move_pos] = names_p[move_pos - 1u]; + + move_pos--; + } + + names_p[insert_pos] = prop_name; + } + } +#if ENABLED (JERRY_ESNEXT) + /* sort symbol named properites in creation order */ + else if (ecma_prop_name_is_symbol (name_p)) + { + JERRY_ASSERT (symbol_name_pos > 0); + JERRY_ASSERT (symbol_name_pos <= prop_counter->symbol_named_props); + + symbol_names_p[--symbol_name_pos] = prop_name; + } +#endif /* ENABLED (JERRY_ESNEXT) */ + /* sort string named properties in creation order */ + else + { + JERRY_ASSERT (string_name_pos > 0); + JERRY_ASSERT (string_name_pos <= prop_counter->string_named_props); + + if (i < lazy_prop_name_count) + { + string_names_p[lazy_string_name_pos++] = prop_name; + } + else + { + string_names_p[--string_name_pos] = prop_name; + } + } + } + + /* Free the unsorted buffer and copy the sorted one in its place */ + jmem_heap_free_block (prop_names_p->buffer_p, ECMA_COLLECTION_ALLOCATED_SIZE (prop_names_p->capacity)); + prop_names_p->buffer_p = names_p; + prop_names_p->item_count = all_prop_count; + prop_names_p->capacity = all_prop_count; +} /* ecma_object_sort_property_names */ + +/** + * Object's [[OwnPropertyKeys]] internal method * * Order of names in the collection: * - integer indices in ascending order @@ -2111,606 +2301,211 @@ ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p, /**< routine * collection of property names - otherwise */ ecma_collection_t * -ecma_op_object_get_property_names (ecma_object_t *obj_p, /**< object */ - uint32_t opts) /**< any combination of ecma_list_properties_options_t values */ +ecma_op_object_own_property_keys (ecma_object_t *obj_p) /**< object */ { - JERRY_ASSERT (obj_p != NULL - && !ecma_is_lexical_environment (obj_p)); - -#if ENABLED (JERRY_BUILTIN_PROXY) +#if ENABLED (JERRY_ESNEXT) if (ECMA_OBJECT_IS_PROXY (obj_p)) { - /* Integrated a part of ECMA 262 v6 7.3.21 EnumerableOwnNames operation. */ - ecma_collection_t *proxy_keys = ecma_proxy_object_own_property_keys (obj_p); - if (JERRY_UNLIKELY (proxy_keys == NULL)) - { - return proxy_keys; - } - ecma_collection_t *return_keys = ecma_new_collection (); - - /* Move valid elements to the output collection */ - for (uint32_t i = 0; i < proxy_keys->item_count; i++) - { - ecma_value_t entry = proxy_keys->buffer_p[i]; - ecma_string_t *prop_name_p = ecma_get_prop_name_from_value (entry); - bool prop_is_symbol = ecma_prop_name_is_symbol (prop_name_p); - - if (prop_is_symbol && ((opts & (ECMA_LIST_SYMBOLS | ECMA_LIST_SYMBOLS_ONLY)) != 0)) - { - ecma_collection_push_back (return_keys, entry); - } - else if (!prop_is_symbol && (opts & ECMA_LIST_SYMBOLS_ONLY) == 0) - { - if ((opts & ECMA_LIST_ENUMERABLE) == 0) - { - ecma_collection_push_back (return_keys, entry); - } - else - { - ecma_property_descriptor_t prop_desc; - ecma_value_t status = ecma_proxy_object_get_own_property_descriptor (obj_p, prop_name_p, &prop_desc); - - if (ECMA_IS_VALUE_ERROR (status)) - { - ecma_collection_destroy (proxy_keys); - - return NULL; - } - - if ((prop_desc.flags & ECMA_PROP_IS_ENUMERABLE) == 0) - { - ecma_deref_ecma_string (prop_name_p); - } - else - { - ecma_collection_push_back (return_keys, entry); - } - - if (ecma_is_value_true (status)) - { - ecma_free_property_descriptor (&prop_desc); - } - } - } - else - { - ecma_free_value (entry); - } - } - - ecma_collection_destroy (proxy_keys); - return return_keys; + return ecma_proxy_object_own_property_keys (obj_p); } -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ +#endif /* ENABLED (JERRY_ESNEXT) */ if (ecma_op_object_is_fast_array (obj_p)) { - return ecma_fast_array_get_property_names (obj_p, opts); + return ecma_fast_array_object_own_property_keys (obj_p); } - ecma_collection_t *ret_p = ecma_new_collection (); - ecma_collection_t *skipped_non_enumerable_p = ecma_new_collection (); + ecma_collection_t *prop_names_p = ecma_new_collection (); + ecma_property_counter_t prop_counter = {0, 0, 0, 0}; - const bool is_enumerable_only = (opts & ECMA_LIST_ENUMERABLE) != 0; - const bool is_array_indices_only = (opts & ECMA_LIST_ARRAY_INDICES) != 0; - const bool is_with_prototype_chain = (opts & ECMA_LIST_PROTOTYPE) != 0; -#if ENABLED (JERRY_ESNEXT) - const bool is_symbols = (opts & ECMA_LIST_SYMBOLS) != 0; - const bool is_symbols_only = (opts & ECMA_LIST_SYMBOLS_ONLY) != 0; -#endif /* ENABLED (JERRY_ESNEXT) */ + ecma_object_list_lazy_property_names (obj_p, prop_names_p, &prop_counter); - const size_t bitmap_row_size = sizeof (uint32_t) * JERRY_BITSINBYTE; - const size_t names_hashes_bitmap_size = ECMA_OBJECT_HASH_BITMAP_SIZE / bitmap_row_size; - JERRY_VLA (uint32_t, names_hashes_bitmap, names_hashes_bitmap_size); + prop_counter.lazy_string_named_props = prop_names_p->item_count; - memset (names_hashes_bitmap, 0, names_hashes_bitmap_size * sizeof (names_hashes_bitmap[0])); + jmem_cpointer_t prop_iter_cp = obj_p->u1.property_list_cp; + +#if ENABLED (JERRY_PROPRETY_HASHMAP) + if (prop_iter_cp != JMEM_CP_NULL) + { + ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp); + + if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP) + { + prop_iter_cp = prop_iter_p->next_property_cp; + } + } +#endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */ + + while (prop_iter_cp != JMEM_CP_NULL) + { + ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp); + JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p)); + + for (int i = 0; i < ECMA_PROPERTY_PAIR_ITEM_COUNT; i++) + { + ecma_property_t *property_p = prop_iter_p->types + i; + + if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA + || ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR) + { + ecma_property_pair_t *prop_pair_p = (ecma_property_pair_t *) prop_iter_p; + + if (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC + && prop_pair_p->names_cp[i] >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT + && prop_pair_p->names_cp[i] < LIT_MAGIC_STRING__COUNT) + { + /* Internal properties are never enumerated. */ + continue; + } + + ecma_string_t *name_p = ecma_string_from_property_name (*property_p, + prop_pair_p->names_cp[i]); + + if (!ecma_object_prop_name_is_duplicated (prop_names_p, name_p)) + { + if (ecma_string_get_array_index (name_p) != ECMA_STRING_NOT_ARRAY_INDEX) + { + prop_counter.array_index_named_props++; + } + #if ENABLED (JERRY_ESNEXT) + else if (ecma_prop_name_is_symbol (name_p)) + { + prop_counter.symbol_named_props++; + } + #endif /* ENABLED (JERRY_ESNEXT) */ + else + { + prop_counter.string_named_props++; + } + + ecma_collection_push_back (prop_names_p, ecma_make_prop_name_value (name_p)); + } + } + } + + prop_iter_cp = prop_iter_p->next_property_cp; + } + + if (prop_names_p->item_count != 0) + { + ecma_object_sort_property_names (prop_names_p, &prop_counter); + } + + return prop_names_p; +} /* ecma_op_object_own_property_keys */ + +/** + * EnumerateObjectProperties abstract method + * + * See also: + * ECMA-262 v11, 13.7.5.15 + * + * @return NULL - if the Proxy.[[OwnPropertyKeys]] operation raises error + * collection of enumerable property names - otherwise + */ +ecma_collection_t * +ecma_op_object_enumerate (ecma_object_t *obj_p) /**< object */ +{ + ecma_collection_t *visited_names_p = ecma_new_collection (); + ecma_collection_t *return_names_p = ecma_new_collection (); + + jmem_cpointer_t obj_cp; + ECMA_SET_NON_NULL_POINTER (obj_cp, obj_p); while (true) { - const ecma_object_type_t type = ecma_get_object_type (obj_p); - const bool obj_is_builtin = ecma_get_object_is_builtin (obj_p); - uint32_t string_named_properties_count = 0; - uint32_t array_index_named_properties_count = 0; -#if ENABLED (JERRY_ESNEXT) - uint32_t symbol_named_properties_count = 0; -#endif /* ENABLED (JERRY_ESNEXT) */ - ecma_collection_t *prop_names_p = ecma_new_collection (); + ecma_collection_t *keys = ecma_op_object_own_property_keys (obj_p); #if ENABLED (JERRY_ESNEXT) - if (JERRY_LIKELY (!is_symbols_only)) + if (JERRY_UNLIKELY (keys == NULL)) { -#endif /* ENABLED (JERRY_ESNEXT) */ - - if (obj_is_builtin) - { - if (type == ECMA_OBJECT_TYPE_FUNCTION && ecma_builtin_function_is_routine (obj_p)) - { - ecma_builtin_routine_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - } - else - { - ecma_builtin_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - } - } - else - { - switch (type) - { - case ECMA_OBJECT_TYPE_PSEUDO_ARRAY: - { -#if ENABLED (JERRY_BUILTIN_TYPEDARRAY) - if (ecma_object_is_typedarray (obj_p)) - { - ecma_op_typedarray_list_lazy_property_names (obj_p, prop_names_p); - } -#endif /* ENABLED (JERRY_BUILTIN_TYPEDARRAY) */ - break; - } - case ECMA_OBJECT_TYPE_FUNCTION: - { - if (!is_array_indices_only) - { - ecma_op_function_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - } - break; - } - case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION: - { - if (!is_array_indices_only) - { - ecma_op_external_function_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - } - break; - } - case ECMA_OBJECT_TYPE_BOUND_FUNCTION: - { - if (!is_array_indices_only) - { - ecma_op_bound_function_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - } - break; - } - case ECMA_OBJECT_TYPE_CLASS: - { - ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p; - - if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL) - { - ecma_op_string_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - } - - break; - } - case ECMA_OBJECT_TYPE_ARRAY: - { - ecma_op_array_list_lazy_property_names (obj_p, - opts, - prop_names_p, - skipped_non_enumerable_p); - break; - } - default: - { - JERRY_ASSERT (type == ECMA_OBJECT_TYPE_GENERAL); - - break; - } - } - } -#if ENABLED (JERRY_ESNEXT) + ecma_collection_free (return_names_p); + ecma_collection_free (visited_names_p); + return keys; } #endif /* ENABLED (JERRY_ESNEXT) */ - ecma_value_t *buffer_p = prop_names_p->buffer_p; - uint32_t lazy_prop_name_count = prop_names_p->item_count; - - const size_t own_names_hashes_bitmap_size = ECMA_OBJECT_HASH_BITMAP_SIZE / bitmap_row_size; - JERRY_VLA (uint32_t, own_names_hashes_bitmap, own_names_hashes_bitmap_size); - memset (own_names_hashes_bitmap, 0, own_names_hashes_bitmap_size * sizeof (own_names_hashes_bitmap[0])); - - for (uint32_t i = 0; i < prop_names_p->item_count; i++) + for (uint32_t i = 0; i < keys->item_count; i++) { - ecma_string_t *name_p = ecma_get_string_from_value (buffer_p[i]); - - if (ecma_string_get_array_index (name_p) != ECMA_STRING_NOT_ARRAY_INDEX) - { - array_index_named_properties_count++; - } -#if ENABLED (JERRY_ESNEXT) - else if (ecma_prop_name_is_symbol (name_p)) - { - symbol_named_properties_count++; - } -#endif /* ENABLED (JERRY_ESNEXT) */ - else - { - string_named_properties_count++; - } + ecma_value_t prop_name = keys->buffer_p[i]; + ecma_string_t *name_p = ecma_get_prop_name_from_value (prop_name); #if ENABLED (JERRY_ESNEXT) - /* Symbols are never lazy listed */ - JERRY_ASSERT (!ecma_prop_name_is_symbol (name_p)); + if (ecma_prop_name_is_symbol (name_p)) + { + continue; + } #endif /* ENABLED (JERRY_ESNEXT) */ - uint8_t hash = (uint8_t) ecma_string_hash (name_p); - uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size); - uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size); + ecma_property_descriptor_t prop_desc; + ecma_value_t get_desc = ecma_op_object_get_own_property_descriptor (obj_p, name_p, &prop_desc); - if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) == 0) + if (ECMA_IS_VALUE_ERROR (get_desc)) { - own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column); + ecma_collection_free (keys); + ecma_collection_free (return_names_p); + ecma_collection_free (visited_names_p); + return NULL; } - } - jmem_cpointer_t prop_iter_cp = obj_p->u1.property_list_cp; - - if (ecma_op_object_is_fast_array (obj_p) && prop_iter_cp != JMEM_CP_NULL) - { - ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p; - - uint32_t length = ext_obj_p->u.array.length; - array_index_named_properties_count = length - ecma_fast_array_get_hole_count (obj_p); - - ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, prop_iter_cp); - - for (uint32_t i = 0; i < length; i++) + if (ecma_is_value_true (get_desc)) { - if (ecma_is_value_array_hole (values_p[i])) + bool is_enumerable = (prop_desc.flags & ECMA_PROP_IS_ENUMERABLE) != 0; + ecma_free_property_descriptor (&prop_desc); + + if (ecma_collection_has_string_value (visited_names_p, name_p) + || ecma_collection_has_string_value (return_names_p, name_p)) { continue; } - ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i); + ecma_ref_ecma_string (name_p); - uint8_t hash = (uint8_t) ecma_string_hash (index_str_p); - uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size); - uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size); - - bool is_add = true; - - if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0) + if (is_enumerable) { - buffer_p = prop_names_p->buffer_p; - - for (uint32_t j = 0; j < prop_names_p->item_count; j++) - { - ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]); - - if (ecma_compare_ecma_strings (index_str_p, current_name_p)) - { - is_add = false; - break; - } - } + ecma_collection_push_back (return_names_p, prop_name); } - - if (is_add) + else { - own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column); - - ecma_collection_push_back (prop_names_p, ecma_make_string_value (index_str_p)); + ecma_collection_push_back (visited_names_p, prop_name); } } } + + ecma_collection_free (keys); + +#if ENABLED (JERRY_BUILTIN_PROXY) + if (ECMA_OBJECT_IS_PROXY (obj_p)) + { + ecma_value_t parent = ecma_proxy_object_get_prototype_of (obj_p); + + if (ECMA_IS_VALUE_ERROR (parent)) + { + ecma_collection_free (return_names_p); + ecma_collection_free (visited_names_p); + return NULL; + } + + obj_cp = ecma_proxy_object_prototype_to_cp (parent); + } else +#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ { -#if ENABLED (JERRY_PROPRETY_HASHMAP) - if (prop_iter_cp != JMEM_CP_NULL) - { - ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp); - - if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP) - { - prop_iter_cp = prop_iter_p->next_property_cp; - } - } - #endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */ - - while (prop_iter_cp != JMEM_CP_NULL) - { - ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp); - JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p)); - - for (int i = 0; i < ECMA_PROPERTY_PAIR_ITEM_COUNT; i++) - { - ecma_property_t *property_p = prop_iter_p->types + i; - - if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA - || ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR) - { - ecma_property_pair_t *prop_pair_p = (ecma_property_pair_t *) prop_iter_p; - - if (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC - && prop_pair_p->names_cp[i] >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT - && prop_pair_p->names_cp[i] < LIT_MAGIC_STRING__COUNT) - { - /* Internal properties are never enumerated. */ - continue; - } - - ecma_string_t *name_p = ecma_string_from_property_name (*property_p, - prop_pair_p->names_cp[i]); - - if (!(is_enumerable_only && !ecma_is_property_enumerable (*property_p))) - { - #if ENABLED (JERRY_ESNEXT) - /* We skip the current property in the following cases: - 1. We don't want to list symbols (is_symbols and is_symbols_only are false) - and the current property is a symbol. - 2. We only want to list symbols (is_symbols_only is true) and the current - property is NOT a symbol. */ - bool is_symbol = ecma_prop_name_is_symbol (name_p); - if ((!(is_symbols || is_symbols_only) && is_symbol) || (is_symbols_only && !is_symbol)) - { - ecma_deref_ecma_string (name_p); - continue; - } - #endif /* ENABLED (JERRY_ESNEXT) */ - - uint8_t hash = (uint8_t) ecma_string_hash (name_p); - uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size); - uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size); - - bool is_add = true; - - if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0) - { - buffer_p = prop_names_p->buffer_p; - - for (uint32_t j = 0; j < prop_names_p->item_count; j++) - { - ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]); - - if (ecma_compare_ecma_strings (name_p, current_name_p)) - { - is_add = false; - break; - } - } - } - - if (is_add) - { - if (ecma_string_get_array_index (name_p) != ECMA_STRING_NOT_ARRAY_INDEX) - { - /* The name is a valid array index. */ - array_index_named_properties_count++; - } - else if (!is_array_indices_only) - { - #if ENABLED (JERRY_ESNEXT) - if (ecma_prop_name_is_symbol (name_p)) - { - symbol_named_properties_count++; - } - else - { - #endif /* ENABLED (JERRY_ESNEXT) */ - string_named_properties_count++; - #if ENABLED (JERRY_ESNEXT) - } - #endif /* ENABLED (JERRY_ESNEXT) */ - } - else - { - ecma_deref_ecma_string (name_p); - continue; - } - - own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column); - - ecma_collection_push_back (prop_names_p, ecma_make_prop_name_value (name_p)); - } - else - { - ecma_deref_ecma_string (name_p); - } - } - else - { - JERRY_ASSERT (is_enumerable_only && !ecma_is_property_enumerable (*property_p)); - - ecma_collection_push_back (skipped_non_enumerable_p, ecma_make_prop_name_value (name_p)); - } - } - } - - prop_iter_cp = prop_iter_p->next_property_cp; - } + obj_cp = ecma_op_ordinary_object_get_prototype_of (obj_p); } - uint32_t all_properties_count = array_index_named_properties_count + string_named_properties_count; - -#if ENABLED (JERRY_ESNEXT) - all_properties_count += symbol_named_properties_count; -#endif /* ENABLED (JERRY_ESNEXT) */ - - /* Second pass: collecting property names into an array. */ - JMEM_DEFINE_LOCAL_ARRAY (names_p, all_properties_count, ecma_string_t *); - - ecma_string_t **string_names_p = names_p + array_index_named_properties_count; -#if ENABLED (JERRY_ESNEXT) - ecma_string_t **symbol_names_p = string_names_p + string_named_properties_count; -#endif /* ENABLED (JERRY_ESNEXT) */ - - uint32_t array_index_name_pos = 0; - uint32_t string_name_pos = string_named_properties_count; - uint32_t lazy_string_name_pos = 0; -#if ENABLED (JERRY_ESNEXT) - uint32_t symbol_name_pos = symbol_named_properties_count; -#endif /* ENABLED (JERRY_ESNEXT) */ - - buffer_p = prop_names_p->buffer_p; - - for (uint32_t i = 0; i < prop_names_p->item_count; i++) - { - ecma_string_t *name_p = ecma_get_prop_name_from_value (buffer_p[i]); - ecma_ref_ecma_string (name_p); - - uint32_t index = ecma_string_get_array_index (name_p); - - if (index != ECMA_STRING_NOT_ARRAY_INDEX) - { - JERRY_ASSERT (array_index_name_pos < array_index_named_properties_count); - - uint32_t insertion_pos = 0; - while (insertion_pos < array_index_name_pos - && index > ecma_string_get_array_index (names_p[insertion_pos])) - { - insertion_pos++; - } - - if (insertion_pos == array_index_name_pos) - { - names_p[array_index_name_pos++] = name_p; - } - else - { - JERRY_ASSERT (insertion_pos < array_index_name_pos); - JERRY_ASSERT (index <= ecma_string_get_array_index (names_p[insertion_pos])); - - uint32_t move_pos = array_index_name_pos++; - - while (move_pos > insertion_pos) - { - names_p[move_pos] = names_p[move_pos - 1u]; - - move_pos--; - } - - names_p[insertion_pos] = name_p; - } - } -#if ENABLED (JERRY_ESNEXT) - else if (ecma_prop_name_is_symbol (name_p)) - { - // Put in the symbols in reverse order. - JERRY_ASSERT (symbol_name_pos > 0); - JERRY_ASSERT (symbol_name_pos <= symbol_named_properties_count); - - symbol_names_p[--symbol_name_pos] = name_p; - } -#endif /* ENABLED (JERRY_ESNEXT) */ - else - { - // Put in the strings in reverse order. - JERRY_ASSERT (string_name_pos > 0); - JERRY_ASSERT (string_name_pos <= string_named_properties_count); - - if (i < lazy_prop_name_count) - { - string_names_p[lazy_string_name_pos++] = name_p; - } - else - { - string_names_p[--string_name_pos] = name_p; - } - } - } - - JERRY_ASSERT (array_index_name_pos == array_index_named_properties_count); - JERRY_ASSERT (string_name_pos - lazy_string_name_pos == 0); -#if ENABLED (JERRY_ESNEXT) - JERRY_ASSERT (symbol_name_pos == 0); -#endif /* ENABLED (JERRY_ESNEXT) */ - - ecma_collection_free (prop_names_p); - - /* Third pass: - * embedding own property names of current object of prototype chain to aggregate property names collection */ - for (uint32_t i = 0; i < all_properties_count; i++) - { - bool is_append = true; - - ecma_string_t *name_p = names_p[i]; - - uint8_t hash = (uint8_t) ecma_string_hash (name_p); - uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size); - uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size); - - if ((names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) == 0) - { - /* This hash has not been used before (for non-skipped). */ - names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column); - } - else - { - /* Name with same hash has already occured. */ - buffer_p = ret_p->buffer_p; - - for (uint32_t j = 0; j < ret_p->item_count; j++) - { - ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]); - - if (ecma_compare_ecma_strings (name_p, current_name_p)) - { - is_append = false; - break; - } - } - } - - if (is_append) - { - buffer_p = skipped_non_enumerable_p->buffer_p; - - for (uint32_t j = 0; j < skipped_non_enumerable_p->item_count; j++) - { - ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]); - - if (ecma_compare_ecma_strings (name_p, current_name_p)) - { - is_append = false; - break; - } - } - } - - if (is_append) - { - JERRY_ASSERT ((names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0); - - ecma_collection_push_back (ret_p, ecma_make_prop_name_value (name_p)); - } - else - { - ecma_deref_ecma_string (name_p); - } - - } - - JMEM_FINALIZE_LOCAL_ARRAY (names_p); - - if (!is_with_prototype_chain || obj_p->u2.prototype_cp == JMEM_CP_NULL) + if (obj_cp == JMEM_CP_NULL) { break; } - obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, obj_p->u2.prototype_cp); + obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, obj_cp); } - ecma_collection_free (skipped_non_enumerable_p); + ecma_collection_free (visited_names_p); - return ret_p; -} /* ecma_op_object_get_property_names */ + return return_names_p; +} /* ecma_op_object_enumerate */ /** * The function is used in the assert of ecma_object_get_class_name diff --git a/jerry-core/ecma/operations/ecma-objects.h b/jerry-core/ecma/operations/ecma-objects.h index f31513d8e..d4185b527 100644 --- a/jerry-core/ecma/operations/ecma-objects.h +++ b/jerry-core/ecma/operations/ecma-objects.h @@ -67,9 +67,10 @@ ecma_value_t ecma_op_object_get_own_property_descriptor (ecma_object_t *object_p ecma_property_descriptor_t *prop_desc_p); ecma_value_t ecma_op_object_has_instance (ecma_object_t *obj_p, ecma_value_t value); ecma_value_t ecma_op_object_is_prototype_of (ecma_object_t *base_p, ecma_object_t *target_p); -ecma_collection_t * ecma_op_object_get_property_names (ecma_object_t *obj_p, uint32_t opts); ecma_collection_t * ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p, ecma_enumerable_property_names_options_t option); +ecma_collection_t *ecma_op_object_own_property_keys (ecma_object_t *obj_p); +ecma_collection_t *ecma_op_object_enumerate (ecma_object_t *obj_p); lit_magic_string_id_t ecma_object_get_class_name (ecma_object_t *obj_p); bool ecma_object_class_is (ecma_object_t *object_p, uint32_t class_id); diff --git a/jerry-core/ecma/operations/ecma-proxy-object.c b/jerry-core/ecma/operations/ecma-proxy-object.c index 89a3e1923..93ce2b52f 100644 --- a/jerry-core/ecma/operations/ecma-proxy-object.c +++ b/jerry-core/ecma/operations/ecma-proxy-object.c @@ -1396,25 +1396,6 @@ ecma_proxy_object_delete_property (ecma_object_t *obj_p, /**< proxy object */ return ret_value; } /* ecma_proxy_object_delete_property */ -/** - * The Proxy object [[Enumerate]] internal routine - * - * See also: - * ECMAScript v6, 9.5.11 - * - * Note: Returned value must be freed with ecma_free_value. - * - * @return ECMA_VALUE_ERROR - if the operation fails - * ecma-object - otherwise - */ -ecma_value_t -ecma_proxy_object_enumerate (ecma_object_t *obj_p) /**< proxy object */ -{ - JERRY_ASSERT (ECMA_OBJECT_IS_PROXY (obj_p)); - JERRY_UNUSED (obj_p); - return ecma_raise_type_error (ECMA_ERR_MSG ("UNIMPLEMENTED: Proxy.[[Enumerate]]")); -} /* ecma_proxy_object_enumerate */ - /** * Helper method for the Proxy object [[OwnPropertyKeys]] operation * @@ -1532,7 +1513,7 @@ ecma_proxy_check_invariants_for_own_prop_keys (ecma_collection_t *trap_result, * The Proxy object [[OwnPropertyKeys]] internal routine * * See also: - * ECMAScript v6, 9.5.12 + * ECMAScript v11, 9.5.11 * * Note: If the returned collection is not NULL, it must be freed with * ecma_collection_free if it is no longer needed @@ -1553,7 +1534,6 @@ ecma_proxy_object_own_property_keys (ecma_object_t *obj_p) /**< proxy object */ /* 2-5. */ ecma_value_t trap = ecma_validate_proxy_object (handler, LIT_MAGIC_STRING_OWN_KEYS_UL); - /* 6. */ if (ECMA_IS_VALUE_ERROR (trap)) { return NULL; @@ -1562,15 +1542,15 @@ ecma_proxy_object_own_property_keys (ecma_object_t *obj_p) /**< proxy object */ ecma_value_t target = proxy_obj_p->target; ecma_object_t *target_obj_p = ecma_get_object_from_value (target); - /* 7. */ + /* 6. */ if (ecma_is_value_undefined (trap)) { - return ecma_op_object_get_property_names (target_obj_p, ECMA_LIST_SYMBOLS); + return ecma_op_object_own_property_keys (target_obj_p); } ecma_object_t *func_obj_p = ecma_get_object_from_value (trap); - /* 8. */ + /* 7. */ ecma_value_t trap_result_array = ecma_op_function_call (func_obj_p, handler, &target, 1); ecma_deref_object (func_obj_p); @@ -1580,46 +1560,51 @@ ecma_proxy_object_own_property_keys (ecma_object_t *obj_p) /**< proxy object */ return NULL; } - /* 9. */ + /* 8. */ ecma_collection_t *trap_result = ecma_op_create_list_from_array_like (trap_result_array, true); ecma_free_value (trap_result_array); - /* 10. */ if (trap_result == NULL) { return trap_result; } - /* 11. */ + /* 9. */ + if (ecma_collection_check_duplicated_entries (trap_result)) + { + ecma_collection_free (trap_result); + ecma_raise_type_error (ECMA_ERR_MSG ("Trap returned duplicate entries")); + return NULL; + } + + /* 10. */ ecma_value_t extensible_target = ecma_builtin_object_object_is_extensible (target_obj_p); - /* 12. */ if (ECMA_IS_VALUE_ERROR (extensible_target)) { ecma_collection_free (trap_result); return NULL; } - /* 13. */ - ecma_collection_t *target_keys = ecma_op_object_get_property_names (target_obj_p, ECMA_LIST_SYMBOLS); + /* 11. */ + ecma_collection_t *target_keys = ecma_op_object_own_property_keys (target_obj_p); - /* 14. */ if (target_keys == NULL) { ecma_collection_free (trap_result); return target_keys; } - /* 16. */ + /* 14. */ ecma_collection_t *target_configurable_keys = ecma_new_collection (); - /* 17. */ + /* 15. */ ecma_collection_t *target_non_configurable_keys = ecma_new_collection (); ecma_collection_t *ret_value = NULL; - /* 18. */ + /* 16. */ for (uint32_t i = 0; i < target_keys->item_count; i++) { ecma_property_descriptor_t target_desc; @@ -1654,12 +1639,12 @@ ecma_proxy_object_own_property_keys (ecma_object_t *obj_p) /**< proxy object */ } } - /* 19. */ + /* 17. */ if (ecma_is_value_true (extensible_target) && target_non_configurable_keys->item_count == 0) { ret_value = trap_result; } - /* 20-25. */ + /* 18-22. */ else if (ecma_proxy_check_invariants_for_own_prop_keys (trap_result, target_non_configurable_keys, target_configurable_keys, @@ -1678,6 +1663,7 @@ free_target_collections: ecma_collection_free (target_configurable_keys); ecma_collection_free (target_non_configurable_keys); + /* 23. */ return ret_value; } /* ecma_proxy_object_own_property_keys */ diff --git a/jerry-core/ecma/operations/ecma-proxy-object.h b/jerry-core/ecma/operations/ecma-proxy-object.h index 594dbf929..e22075bf2 100644 --- a/jerry-core/ecma/operations/ecma-proxy-object.h +++ b/jerry-core/ecma/operations/ecma-proxy-object.h @@ -92,9 +92,6 @@ ecma_value_t ecma_proxy_object_delete_property (ecma_object_t *obj_p, ecma_string_t *prop_name_p); -ecma_value_t -ecma_proxy_object_enumerate (ecma_object_t *obj_p); - ecma_collection_t * ecma_proxy_object_own_property_keys (ecma_object_t *obj_p); diff --git a/jerry-core/ecma/operations/ecma-string-object.c b/jerry-core/ecma/operations/ecma-string-object.c index ec37de91d..141435a22 100644 --- a/jerry-core/ecma/operations/ecma-string-object.c +++ b/jerry-core/ecma/operations/ecma-string-object.c @@ -84,19 +84,11 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of */ void ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String object */ - uint32_t opts, /**< listing options using flags - * from ecma_list_properties_options_t */ - ecma_collection_t *main_collection_p, /**< 'main' collection */ - ecma_collection_t *non_enum_collection_p) /**< skipped - * 'non-enumerable' - * collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_CLASS); - ecma_collection_t *for_enumerable_p = main_collection_p; - - ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p; - ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p; JERRY_ASSERT (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL); @@ -109,13 +101,13 @@ ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String obj ecma_string_t *name_p = ecma_new_ecma_string_from_uint32 (i); /* the properties are enumerable (ECMA-262 v5, 15.5.5.2.9) */ - ecma_collection_push_back (for_enumerable_p, ecma_make_string_value (name_p)); + ecma_collection_push_back (prop_names_p, ecma_make_string_value (name_p)); } - if ((opts & ECMA_LIST_ARRAY_INDICES) == 0) - { - ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); - } + prop_counter_p->array_index_named_props += length; + + ecma_collection_push_back (prop_names_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); + prop_counter_p->string_named_props++; } /* ecma_op_string_list_lazy_property_names */ /** diff --git a/jerry-core/ecma/operations/ecma-string-object.h b/jerry-core/ecma/operations/ecma-string-object.h index 1be44c454..4ce1d4432 100644 --- a/jerry-core/ecma/operations/ecma-string-object.h +++ b/jerry-core/ecma/operations/ecma-string-object.h @@ -30,9 +30,8 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, uint32_t arg void ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, - uint32_t opts, - ecma_collection_t *main_collection_p, - ecma_collection_t *non_enum_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); /** * @} diff --git a/jerry-core/ecma/operations/ecma-typedarray-object.c b/jerry-core/ecma/operations/ecma-typedarray-object.c index a69998e46..d46ef14bb 100644 --- a/jerry-core/ecma/operations/ecma-typedarray-object.c +++ b/jerry-core/ecma/operations/ecma-typedarray-object.c @@ -1300,7 +1300,8 @@ ecma_is_typedarray (ecma_value_t value) /**< the target need to be checked */ */ void ecma_op_typedarray_list_lazy_property_names (ecma_object_t *obj_p, /**< a TypedArray object */ - ecma_collection_t *main_collection_p) /**< 'main' collection */ + ecma_collection_t *prop_names_p, /**< prop name collection */ + ecma_property_counter_t *prop_counter_p) /**< prop counter */ { JERRY_ASSERT (ecma_object_is_typedarray (obj_p)); @@ -1309,9 +1310,10 @@ ecma_op_typedarray_list_lazy_property_names (ecma_object_t *obj_p, /**< a TypedA for (uint32_t i = 0; i < array_length; i++) { ecma_string_t *name_p = ecma_new_ecma_string_from_uint32 (i); - - ecma_collection_push_back (main_collection_p, ecma_make_string_value (name_p)); + ecma_collection_push_back (prop_names_p, ecma_make_string_value (name_p)); } + + prop_counter_p->array_index_named_props += array_length; } /* ecma_op_typedarray_list_lazy_property_names */ /** diff --git a/jerry-core/ecma/operations/ecma-typedarray-object.h b/jerry-core/ecma/operations/ecma-typedarray-object.h index b3eae8f4c..fa12544f6 100644 --- a/jerry-core/ecma/operations/ecma-typedarray-object.h +++ b/jerry-core/ecma/operations/ecma-typedarray-object.h @@ -64,7 +64,8 @@ ecma_typedarray_iterators_helper (ecma_value_t this_arg, ecma_iterator_kind_t ki bool ecma_object_is_typedarray (ecma_object_t *obj_p); bool ecma_is_typedarray (ecma_value_t target); void ecma_op_typedarray_list_lazy_property_names (ecma_object_t *obj_p, - ecma_collection_t *main_collection_p); + ecma_collection_t *prop_names_p, + ecma_property_counter_t *prop_counter_p); bool ecma_op_typedarray_define_index_prop (ecma_object_t *obj_p, uint32_t index, const ecma_property_descriptor_t *property_desc_p); diff --git a/jerry-core/vm/opcodes.c b/jerry-core/vm/opcodes.c index f2a3a9609..c6e3b1bf1 100644 --- a/jerry-core/vm/opcodes.c +++ b/jerry-core/vm/opcodes.c @@ -345,10 +345,7 @@ opfunc_for_in (ecma_value_t left_value, /**< left value */ /* ecma_op_to_object will only raise error on null/undefined values but those are handled above. */ JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (obj_expr_value)); ecma_object_t *obj_p = ecma_get_object_from_value (obj_expr_value); -#if ENABLED (JERRY_BUILTIN_PROXY) - JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (obj_p)); -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ - ecma_collection_t *prop_names_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_ENUMERABLE_PROTOTYPE); + ecma_collection_t *prop_names_p = ecma_op_object_enumerate (obj_p); if (prop_names_p->item_count != 0) { diff --git a/jerry-core/vm/vm.c b/jerry-core/vm/vm.c index 707c02c01..68f0b8c46 100644 --- a/jerry-core/vm/vm.c +++ b/jerry-core/vm/vm.c @@ -1773,7 +1773,8 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ } ecma_object_t *object_p = ecma_get_object_from_value (result); - ecma_collection_t *names_p = ecma_op_object_get_property_names (object_p, ECMA_LIST_ENUMERABLE); + ecma_collection_t *names_p = ecma_op_object_get_enumerable_property_names (object_p, + ECMA_ENUMERABLE_PROPERTY_KEYS); #if ENABLED (JERRY_BUILTIN_PROXY) if (names_p == NULL) @@ -3712,21 +3713,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p); -#if ENABLED (JERRY_BUILTIN_PROXY) - if (ecma_is_value_object (value) - && ECMA_OBJECT_IS_PROXY (ecma_get_object_from_value (value))) - { - /* Note: - For proxy objects we should create a new object which implements the iterable protocol, - and iterates through the enumerated collection below. - - This inkoves that the VM context type should be adjusted and checked in all FOR-IN related - instruction. - - For other objects we should keep the current implementation due to performance reasons.*/ - result = ecma_raise_type_error (ECMA_ERR_MSG ("UNIMPLEMENTED: Proxy support in for-in.")); - ecma_free_value (value); - goto error; - } -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ - ecma_value_t expr_obj_value = ECMA_VALUE_UNDEFINED; ecma_collection_t *prop_names_p = opfunc_for_in (value, &expr_obj_value); ecma_free_value (value); @@ -3784,9 +3770,6 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */ ecma_value_t *buffer_p = collection_p->buffer_p; ecma_object_t *object_p = ecma_get_object_from_value (stack_top_p[-4]); uint32_t index = stack_top_p[-3]; -#if ENABLED (JERRY_BUILTIN_PROXY) - JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p)); -#endif /* ENABLED (JERRY_BUILTIN_PROXY) */ while (index < collection_p->item_count) { diff --git a/tests/jerry/es.next/array-prototype-sort.js b/tests/jerry/es.next/array-prototype-sort.js new file mode 100644 index 000000000..1f00d7cae --- /dev/null +++ b/tests/jerry/es.next/array-prototype-sort.js @@ -0,0 +1,24 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var proxy = new Proxy({length: 5}, { + getOwnPropertyDescriptor() { throw 42.5; } +}) + +try { + Array.prototype.sort.call(proxy); + assert(false); +} catch (e) { + assert(e === 42.5); +} diff --git a/tests/jerry/es.next/function-properties.js b/tests/jerry/es.next/function-properties.js index a80b93c7b..df9352b61 100644 --- a/tests/jerry/es.next/function-properties.js +++ b/tests/jerry/es.next/function-properties.js @@ -39,7 +39,7 @@ assert(getProperties(func) == "dummy"); var bound_func = (function() {}).bind(null); Object.setPrototypeOf(bound_func, prototype_obj); -assert(getProperties(bound_func) == "dummy prototype"); +assert(getProperties(bound_func) == "dummy caller arguments prototype"); // 'print' is an external function Object.setPrototypeOf(print, prototype_obj); diff --git a/tests/jerry/es.next/object-define-properties.js b/tests/jerry/es.next/object-define-properties.js index 9c3caad00..7db9a66a1 100644 --- a/tests/jerry/es.next/object-define-properties.js +++ b/tests/jerry/es.next/object-define-properties.js @@ -124,7 +124,6 @@ assert (object[symbol] === "a symbol"); assert (object.bar === 42); assert (object[symbol] === undefined); -// This code should throw TypeError var object = {}; var props = { [symbol]: { @@ -142,9 +141,9 @@ var props = { }, }; -try { - Object.defineProperties(object, props); - assert (false); -} catch (e) { - assert (e instanceof TypeError); -} +Object.defineProperties(object, props); +var bar_desc = Object.getOwnPropertyDescriptor(object, 'bar'); +assert(bar_desc.value === 2); +assert(bar_desc.writable === true); +assert(object.prop1 === undefined); +assert(object[symbol] === undefined); diff --git a/tests/jerry/es.next/proxy_own_keys.js b/tests/jerry/es.next/proxy_own_keys.js index 00daae4cc..e32c8c345 100644 --- a/tests/jerry/es.next/proxy_own_keys.js +++ b/tests/jerry/es.next/proxy_own_keys.js @@ -31,14 +31,6 @@ var handler = { ownKeys (target) { var proxy = new Proxy(target, handler); -try { - // Array.prototype.sort - Array.prototype.sort.call(proxy); - assert(false); -} catch (e) { - assert(e === 42); -} - try { // 19.1.2.14.4 Object.keys(proxy); @@ -63,6 +55,14 @@ try { assert(e === 42); } +var handler = { ownKeys (target) { + return ["a"]; +}}; + +var proxy = new Proxy(target, handler); +var sort_result = Array.prototype.sort.call(proxy); +assert(Object.keys(sort_result).length === 0); + // test basic functionality var symA = Symbol("smA"); var symB = Symbol("smB"); @@ -149,16 +149,18 @@ try { } // test with duplicated keys -var target = { prop1: "prop1", prop2: "prop2"}; -var handler = { +var p = new Proxy({}, { ownKeys: function(target) { - return ["a", "a", "a"]; - } -}; + return ["foo", "foo"]; + } +}); -var proxy = new Proxy(target, handler); - -assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["a","a","a"]'); +try { + Object.keys(p); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} // test with lots of keys var keyslist = []; @@ -240,4 +242,4 @@ var handler = { var proxy = new Proxy(object, handler); assert(Object.keys(proxy).length === 1); -assert(Object.keys(proxy)[0] === "b"); +assert(Object.keys(proxy)[0] === "b"); \ No newline at end of file diff --git a/tests/jerry/es.next/regression-test-issue-3868.js b/tests/jerry/es.next/regression-test-issue-3868.js index 82a25273a..2d0dd5552 100644 --- a/tests/jerry/es.next/regression-test-issue-3868.js +++ b/tests/jerry/es.next/regression-test-issue-3868.js @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -var a = new Proxy({}, {}); +var a = new Proxy({ a : 4, b :4}, {}); +var reached = false; -try { - for (var $ in a); - assert(false); -} catch (e) { - assert(e instanceof TypeError); +for (var $ in a) +{ + reached = true; } + +assert (reached === true); diff --git a/tests/jerry/object-define-properties.js b/tests/jerry/object-define-properties.js index 69c119791..0579e44bf 100644 --- a/tests/jerry/object-define-properties.js +++ b/tests/jerry/object-define-properties.js @@ -174,7 +174,6 @@ assert (obj.a === "b"); assert (obj.bar === 42); assert (obj.a === undefined); -// This code should throw TypeError var obj = {}; var props = { prop1: { @@ -196,9 +195,15 @@ var props = { } }; -try { - Object.defineProperties(obj, props); - assert (false); -} catch (e) { - assert (e instanceof TypeError); -} +Object.defineProperties(obj, props); +var bar_desc = Object.getOwnPropertyDescriptor(obj, 'bar'); +assert(bar_desc.value === 2); +assert(bar_desc.writable === true); +assert(obj.prop2 === undefined); + +var prop1_desc = Object.getOwnPropertyDescriptor(obj, 'prop1'); +var prop3_desc = Object.getOwnPropertyDescriptor(obj, 'prop3'); +assert(prop1_desc.value === 1); +assert(prop1_desc.writable === true); +assert(prop3_desc.value === 4); +assert(prop3_desc.writable === true); diff --git a/tests/test262-es6-excludelist.xml b/tests/test262-es6-excludelist.xml index 2f4f27ba3..3f7e0f203 100644 --- a/tests/test262-es6-excludelist.xml +++ b/tests/test262-es6-excludelist.xml @@ -64,10 +64,15 @@ + For-in supports proxy + For-in supports proxy + For-in supports proxy + For-in supports proxy + For-in supports proxy + For-in supports proxy - - + For-in supports proxy