From c2ca158d19b946698a23177d034f91c88d80da70 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Thu, 25 Sep 2014 14:58:45 +0400 Subject: [PATCH] Implementing Array built-in. --- .../ecma-builtin-array-object.c | 315 ++++++++++++++++++ .../ecma-builtin-global-object.c | 9 +- .../ecma-builtin-string-object.c | 2 - src/libecmabuiltins/ecma-builtins-internal.h | 35 +- src/libecmabuiltins/ecma-builtins.c | 38 ++- src/libecmaoperations/ecma-array-object.c | 132 +++----- tests/jerry/array.js | 52 ++- 7 files changed, 473 insertions(+), 110 deletions(-) create mode 100644 src/libecmabuiltins/ecma-builtin-array-object.c diff --git a/src/libecmabuiltins/ecma-builtin-array-object.c b/src/libecmabuiltins/ecma-builtin-array-object.c new file mode 100644 index 000000000..e9752d3bc --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-array-object.c @@ -0,0 +1,315 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include "ecma-alloc.h" +#include "ecma-builtins.h" +#include "ecma-conversion.h" +#include "ecma-exceptions.h" +#include "ecma-gc.h" +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-objects.h" +#include "ecma-array-object.h" +#include "ecma-try-catch-macro.h" +#include "globals.h" + +#define ECMA_BUILTINS_INTERNAL +#include "ecma-builtins-internal.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmabuiltins + * @{ + * + * \addtogroup array ECMA Array object built-in + * @{ + */ + +/** + * List of the Array object built-in object value properties in format 'macro (name, value)'. + */ +#define ECMA_BUILTIN_ARRAY_OBJECT_OBJECT_VALUES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_PROTOTYPE, ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY_PROTOTYPE)) + +/** + * List of the Array object built-in routine properties in format + * 'macro (name, C function name, arguments number of the routine, length value of the routine)'. + */ +#define ECMA_BUILTIN_ARRAY_OBJECT_ROUTINES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_IS_ARRAY_UL, \ + ecma_builtin_array_object_is_array, \ + 1, \ + 1) + +/** + * List of the Array object's built-in property names + */ +static const ecma_magic_string_id_t ecma_builtin_array_property_names[] = +{ +#define VALUE_PROP_LIST(name, value) name, +#define ROUTINE_PROP_LIST(name, c_function_name, args_number, length) name, + ECMA_BUILTIN_ARRAY_OBJECT_OBJECT_VALUES_PROPERTY_LIST (VALUE_PROP_LIST) + ECMA_BUILTIN_ARRAY_OBJECT_ROUTINES_PROPERTY_LIST (ROUTINE_PROP_LIST) +#undef VALUE_PROP_LIST +#undef ROUTINE_PROP_LIST +}; + +/** + * Number of the Array object's built-in properties + */ +const ecma_length_t ecma_builtin_array_property_number = (sizeof (ecma_builtin_array_property_names) / + sizeof (ecma_magic_string_id_t)); + +/** + * The Array object's 'isArray' routine + * + * See also: + * ECMA-262 v5, 15.4.3.2 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_array_object_is_array (ecma_value_t arg) /**< first argument */ +{ + ecma_simple_value_t is_array = ECMA_SIMPLE_VALUE_FALSE; + + if (arg.value_type == ECMA_TYPE_OBJECT) + { + ecma_object_t *obj_p = ECMA_GET_POINTER (arg.value); + + ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_CLASS); + + if (class_prop_p->u.internal_property.value == ECMA_OBJECT_CLASS_ARRAY) + { + is_array = ECMA_SIMPLE_VALUE_TRUE; + } + } + + return ecma_make_simple_completion_value (is_array); +} /* ecma_builtin_array_object_is_array */ + +/** + * If the property's name is one of built-in properties of the Array object + * that is not instantiated yet, instantiate the property and + * return pointer to the instantiated property. + * + * @return pointer property, if one was instantiated, + * NULL - otherwise. + */ +ecma_property_t* +ecma_builtin_array_try_to_instantiate_property (ecma_object_t *obj_p, /**< object */ + ecma_string_t *prop_name_p) /**< property's name */ +{ + JERRY_ASSERT (ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ARRAY)); + JERRY_ASSERT (ecma_find_named_property (obj_p, prop_name_p) == NULL); + + ecma_magic_string_id_t id; + + if (!ecma_is_string_magic (prop_name_p, &id)) + { + return NULL; + } + + int32_t index = ecma_builtin_bin_search_for_magic_string_id_in_array (ecma_builtin_array_property_names, + ecma_builtin_array_property_number, + id); + + if (index == -1) + { + return NULL; + } + + JERRY_ASSERT (index >= 0 && (uint32_t) index < sizeof (uint64_t) * JERRY_BITSINBYTE); + + uint32_t bit; + ecma_internal_property_id_t mask_prop_id; + + if (index >= 32) + { + mask_prop_id = ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63; + bit = (uint32_t) 1u << (index - 32); + } + else + { + mask_prop_id = ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31; + bit = (uint32_t) 1u << index; + } + + ecma_property_t *mask_prop_p = ecma_get_internal_property (obj_p, mask_prop_id); + uint32_t bit_mask = mask_prop_p->u.internal_property.value; + + if (!(bit_mask & bit)) + { + return NULL; + } + + bit_mask &= ~bit; + + mask_prop_p->u.internal_property.value = bit_mask; + + ecma_value_t value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); + ecma_property_writable_value_t writable = ECMA_PROPERTY_WRITABLE; + ecma_property_enumerable_value_t enumerable = ECMA_PROPERTY_NOT_ENUMERABLE; + ecma_property_configurable_value_t configurable = ECMA_PROPERTY_CONFIGURABLE; + + switch (id) + { +#define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) case name: + ECMA_BUILTIN_ARRAY_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST + { + ecma_object_t *func_obj_p = ecma_builtin_make_function_object_for_routine (ECMA_BUILTIN_ID_ARRAY, id); + + value = ecma_make_object_value (func_obj_p); + + break; + } +#define CASE_VALUE_PROP_LIST(name, value) case name: + ECMA_BUILTIN_ARRAY_OBJECT_OBJECT_VALUES_PROPERTY_LIST (CASE_VALUE_PROP_LIST) +#undef CASE_VALUE_PROP_LIST + { + writable = ECMA_PROPERTY_NOT_WRITABLE; + enumerable = ECMA_PROPERTY_NOT_ENUMERABLE; + configurable = ECMA_PROPERTY_NOT_CONFIGURABLE; + + switch (id) + { +#define CASE_OBJECT_VALUE_PROP_LIST(name, value_obj) case name: { value = ecma_make_object_value (value_obj); break; } + ECMA_BUILTIN_ARRAY_OBJECT_OBJECT_VALUES_PROPERTY_LIST (CASE_OBJECT_VALUE_PROP_LIST) +#undef CASE_OBJECT_VALUE_PROP_LIST + default: + { + JERRY_UNREACHABLE (); + } + } + + break; + } + + default: + { + JERRY_UNREACHABLE (); + } + } + + ecma_property_t *prop_p = ecma_create_named_data_property (obj_p, + prop_name_p, + writable, + enumerable, + configurable); + + prop_p->u.named_data_property.value = ecma_copy_value (value, false); + ecma_gc_update_may_ref_younger_object_flag_by_value (obj_p, + prop_p->u.named_data_property.value); + + ecma_free_value (value, true); + + return prop_p; +} /* ecma_builtin_array_try_to_instantiate_property */ + +/** + * Dispatcher of the Array object's built-in routines + * + * @return completion-value + * Returned value must be freed with ecma_free_completion_value. + */ +ecma_completion_value_t +ecma_builtin_array_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, /**< Array object's + built-in routine's name */ + ecma_value_t this_arg_value __unused, /**< 'this' argument value */ + ecma_value_t arguments_list [], /**< list of arguments passed to routine */ + ecma_length_t arguments_number) /**< length of arguments' list */ +{ + const ecma_value_t value_undefined = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); + + switch (builtin_routine_id) + { +#define ROUTINE_ARG(n) (arguments_number >= n ? arguments_list[n - 1] : value_undefined) +#define ROUTINE_ARG_LIST_1 ROUTINE_ARG(1) +#define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) \ + case name: \ + { \ + return c_function_name (ROUTINE_ARG_LIST_ ## args_number); \ + } + ECMA_BUILTIN_ARRAY_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST +#undef ROUTINE_ARG_LIST_1 +#undef ROUTINE_ARG + + default: + { + JERRY_UNREACHABLE (); + } + } +} /* ecma_builtin_array_dispatch_routine */ + +/** + * Get number of routine's parameters + * + * @return number of parameters + */ +ecma_length_t +ecma_builtin_array_get_routine_parameters_number (ecma_magic_string_id_t builtin_routine_id) /**< built-in routine's + name */ +{ + switch (builtin_routine_id) + { +#define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) case name: return length; + ECMA_BUILTIN_ARRAY_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST + + default: + { + JERRY_UNREACHABLE (); + } + } +} /* ecma_builtin_array_get_routine_parameters_number */ + +/** + * Handle calling [[Call]] of built-in Array object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_array_dispatch_call (ecma_value_t *arguments_list_p, /**< arguments list */ + ecma_length_t arguments_list_len) /**< number of arguments */ +{ + JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); + + return ecma_builtin_array_dispatch_construct (arguments_list_p, arguments_list_len); +} /* ecma_builtin_array_dispatch_call */ + +/** + * Handle calling [[Construct]] of built-in Array object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_array_dispatch_construct (ecma_value_t *arguments_list_p, /**< arguments list */ + ecma_length_t arguments_list_len) /**< number of arguments */ +{ + JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL); + + return ecma_op_create_array_object (arguments_list_p, arguments_list_len, true); +} /* ecma_builtin_array_dispatch_construct */ + +/** + * @} + * @} + * @} + */ diff --git a/src/libecmabuiltins/ecma-builtin-global-object.c b/src/libecmabuiltins/ecma-builtin-global-object.c index 8ffe43df4..7eb535009 100644 --- a/src/libecmabuiltins/ecma-builtin-global-object.c +++ b/src/libecmabuiltins/ecma-builtin-global-object.c @@ -497,9 +497,16 @@ ecma_builtin_global_try_to_instantiate_property (ecma_object_t *obj_p, /**< obje break; } + case ECMA_MAGIC_STRING_ARRAY_UL: + { + ecma_object_t *array_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY); + + value = ecma_make_object_value (array_obj_p); + + break; + } case ECMA_MAGIC_STRING_FUNCTION_UL: - case ECMA_MAGIC_STRING_ARRAY_UL: case ECMA_MAGIC_STRING_BOOLEAN_UL: case ECMA_MAGIC_STRING_NUMBER_UL: case ECMA_MAGIC_STRING_DATE_UL: diff --git a/src/libecmabuiltins/ecma-builtin-string-object.c b/src/libecmabuiltins/ecma-builtin-string-object.c index 480aa3f33..279040ac2 100644 --- a/src/libecmabuiltins/ecma-builtin-string-object.c +++ b/src/libecmabuiltins/ecma-builtin-string-object.c @@ -276,7 +276,6 @@ ecma_builtin_string_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, { switch (builtin_routine_id) { -#define ROUTINE_ARG(n) (arguments_number >= n ? arguments_list[n - 1] : value_undefined) #define ROUTINE_ARG_LIST_NON_FIXED arguments_list, arguments_number #define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) \ case name: \ @@ -286,7 +285,6 @@ ecma_builtin_string_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, ECMA_BUILTIN_STRING_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) #undef CASE_ROUTINE_PROP_LIST #undef ROUTINE_ARG_LIST_NON_FIXED -#undef ROUTINE_ARG default: { diff --git a/src/libecmabuiltins/ecma-builtins-internal.h b/src/libecmabuiltins/ecma-builtins-internal.h index c63e7c5c0..2e2b28e1d 100644 --- a/src/libecmabuiltins/ecma-builtins-internal.h +++ b/src/libecmabuiltins/ecma-builtins-internal.h @@ -49,6 +49,10 @@ extern ecma_object_t* ecma_builtin_make_function_object_for_routine (ecma_builtin_id_t builtin_id, ecma_magic_string_id_t routine_id); +extern int32_t +ecma_builtin_bin_search_for_magic_string_id_in_array (const ecma_magic_string_id_t ids[], + ecma_length_t array_length, + ecma_magic_string_id_t key); /* ecma-builtin-global-object.c */ extern ecma_length_t @@ -121,21 +125,20 @@ ecma_builtin_string_dispatch_construct (ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len); /* ecma-builtin-array-object.c */ -extern void ecma_builtin_init_array_object (void); -extern void ecma_builtin_finalize_array_object (void); +extern const ecma_length_t ecma_builtin_array_property_number; +extern ecma_length_t +ecma_builtin_array_get_routine_parameters_number (ecma_magic_string_id_t routine_id); +extern ecma_completion_value_t +ecma_builtin_array_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, + ecma_value_t this_arg_value, + ecma_value_t arguments_list [], + ecma_length_t arguments_number); extern ecma_property_t* -ecma_builtin_array_try_to_instantiate_property (ecma_object_t *obj_p, - ecma_string_t *prop_name_p); - - -extern void ecma_builtin_init_array_prototype_object (void); -extern void ecma_builtin_finalize_array_prototype_object (void); -extern ecma_property_t* -ecma_builtin_array_prototype_try_to_instantiate_property (ecma_object_t *obj_p, - ecma_string_t *prop_name_p); - -extern int32_t -ecma_builtin_bin_search_for_magic_string_id_in_array (const ecma_magic_string_id_t ids[], - ecma_length_t array_length, - ecma_magic_string_id_t key); +ecma_builtin_array_try_to_instantiate_property (ecma_object_t *obj_p, ecma_string_t *prop_name_p); +extern ecma_completion_value_t +ecma_builtin_array_dispatch_call (ecma_value_t *arguments_list_p, + ecma_length_t arguments_list_len); +extern ecma_completion_value_t +ecma_builtin_array_dispatch_construct (ecma_value_t *arguments_list_p, + ecma_length_t arguments_list_len); #endif /* !ECMA_BUILTINS_INTERNAL_H */ diff --git a/src/libecmabuiltins/ecma-builtins.c b/src/libecmabuiltins/ecma-builtins.c index 5e43ce04c..dcd049458 100644 --- a/src/libecmabuiltins/ecma-builtins.c +++ b/src/libecmabuiltins/ecma-builtins.c @@ -144,6 +144,11 @@ ecma_init_builtins (void) ECMA_OBJECT_CLASS_STRING, ecma_builtin_string_property_number); + ecma_builtin_objects [ECMA_BUILTIN_ID_ARRAY] = ecma_builtin_init_object (ECMA_BUILTIN_ID_ARRAY, + ECMA_OBJECT_TYPE_FUNCTION, + ECMA_OBJECT_CLASS_ARRAY, + ecma_builtin_array_property_number); + ecma_builtin_objects [ECMA_BUILTIN_ID_MATH] = ecma_builtin_init_object (ECMA_BUILTIN_ID_MATH, ECMA_OBJECT_TYPE_GENERAL, ECMA_OBJECT_CLASS_MATH, @@ -216,10 +221,14 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object * return ecma_builtin_string_try_to_instantiate_property (object_p, string_p); } + case ECMA_BUILTIN_ID_ARRAY: + { + return ecma_builtin_array_try_to_instantiate_property (object_p, string_p); + } + case ECMA_BUILTIN_ID_OBJECT_PROTOTYPE: case ECMA_BUILTIN_ID_FUNCTION: case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: - case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: @@ -362,10 +371,14 @@ ecma_builtin_dispatch_call (ecma_object_t *obj_p, /**< built-in object */ return ecma_builtin_string_dispatch_call (arguments_list_p, arguments_list_len); } + case ECMA_BUILTIN_ID_ARRAY: + { + return ecma_builtin_array_dispatch_call (arguments_list_p, arguments_list_len); + } + case ECMA_BUILTIN_ID_OBJECT_PROTOTYPE: case ECMA_BUILTIN_ID_FUNCTION: case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: - case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: @@ -437,10 +450,14 @@ ecma_builtin_dispatch_construct (ecma_object_t *obj_p, /**< built-in object */ return ecma_builtin_string_dispatch_construct (arguments_list_p, arguments_list_len); } + case ECMA_BUILTIN_ID_ARRAY: + { + return ecma_builtin_array_dispatch_construct (arguments_list_p, arguments_list_len); + } + case ECMA_BUILTIN_ID_OBJECT_PROTOTYPE: case ECMA_BUILTIN_ID_FUNCTION: case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: - case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: @@ -508,10 +525,14 @@ ecma_builtin_get_routine_parameters_number (ecma_builtin_id_t builtin_id, /**< i { return ecma_builtin_math_get_routine_parameters_number (routine_id); } + case ECMA_BUILTIN_ID_ARRAY: + { + return ecma_builtin_array_get_routine_parameters_number (routine_id); + } + case ECMA_BUILTIN_ID_OBJECT_PROTOTYPE: case ECMA_BUILTIN_ID_FUNCTION: case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: - case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: @@ -587,10 +608,17 @@ ecma_builtin_dispatch_routine (ecma_builtin_id_t builtin_object_id, /**< built-i arguments_list, arguments_number); } + case ECMA_BUILTIN_ID_ARRAY: + { + return ecma_builtin_array_dispatch_routine (builtin_routine_id, + this_arg_value, + arguments_list, + arguments_number); + } + case ECMA_BUILTIN_ID_OBJECT_PROTOTYPE: case ECMA_BUILTIN_ID_FUNCTION: case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: - case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: diff --git a/src/libecmaoperations/ecma-array-object.c b/src/libecmaoperations/ecma-array-object.c index 2b5c46a96..e958c6cfe 100644 --- a/src/libecmaoperations/ecma-array-object.c +++ b/src/libecmaoperations/ecma-array-object.c @@ -150,76 +150,6 @@ ecma_op_create_array_object (ecma_value_t *arguments_list_p, /**< list of argume return ecma_make_normal_completion_value (ecma_make_object_value (obj_p)); } /* ecma_op_create_array_object */ -/** - * Reduce length of Array to specified value. - * - * Warning: - * the routine may change value of number, stored in passed property descriptor template. - * - * See also: - * ECMA-262 v5, 15.4.5.1, Block 3.l - * - * @return true - length was reduced successfully, - * false - reduce routine stopped at some length > new_length - * because element with index (length - 1) cannot be deleted. - */ -static bool -ecma_array_object_reduce_length (ecma_object_t *obj_p, /**< the array object */ - uint32_t new_length, /**< length to reduce to */ - uint32_t old_length, /**< current length of array */ - ecma_property_descriptor_t new_len_desc) /**< property descriptor template - for length property */ -{ - JERRY_ASSERT (new_length < old_length); - - while (new_length < old_length) - { - // i. - old_length--; - - // ii. - ecma_string_t *old_length_string_p = ecma_new_ecma_string_from_number (ecma_uint32_to_number (old_length)); - ecma_completion_value_t delete_succeeded = ecma_op_object_delete (obj_p, - old_length_string_p, - false); - ecma_deref_ecma_string (old_length_string_p); - - // iii. - if (ecma_is_completion_value_normal_false (delete_succeeded)) - { - JERRY_ASSERT (new_len_desc.value.value_type == ECMA_TYPE_NUMBER); - - ecma_number_t *new_len_num_p = ECMA_GET_POINTER (new_len_desc.value.value); - - // 1. - *new_len_num_p = ecma_uint32_to_number (old_length + 1); - - // 2. - JERRY_ASSERT (new_len_desc.is_writable_defined - && new_len_desc.writable == ECMA_PROPERTY_NOT_WRITABLE); - - // 3. - ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); - ecma_completion_value_t completion = ecma_op_general_object_define_own_property (obj_p, - magic_string_length_p, - new_len_desc, - false); - ecma_deref_ecma_string (magic_string_length_p); - - JERRY_ASSERT (ecma_is_completion_value_normal_true (completion) - || ecma_is_completion_value_normal_false (completion)); - - return false; - } - else - { - JERRY_ASSERT (ecma_is_completion_value_normal_true (delete_succeeded)); - } - } - - return true; -} /* ecma_array_object_reduce_length */ - /** * [[DefineOwnProperty]] ecma array object's operation * @@ -349,25 +279,65 @@ ecma_op_array_object_define_own_property (ecma_object_t *obj_p, /**< the array o /* Handling normal false and throw values */ if (!ecma_is_completion_value_normal_true (succeeded)) { + JERRY_ASSERT (ecma_is_completion_value_normal_false (succeeded) + || ecma_is_completion_value_throw (succeeded)); + // k. ret_value = succeeded; } else { // l - - // iii.2 - if (!new_writable) + JERRY_ASSERT (new_len_uint32 < old_len_uint32); + + bool reduce_succeeded = true; + + while (new_len_uint32 < old_len_uint32) { - new_len_property_desc.is_writable_defined = true; - new_len_property_desc.writable = ECMA_PROPERTY_NOT_WRITABLE; + // i + old_len_uint32--; + + // ii + ecma_string_t *old_length_string_p = ecma_new_ecma_string_from_uint32 (old_len_uint32); + ecma_completion_value_t delete_succeeded = ecma_op_object_delete (obj_p, + old_length_string_p, + false); + ecma_deref_ecma_string (old_length_string_p); + + // iii + if (ecma_is_completion_value_normal_false (delete_succeeded)) + { + JERRY_ASSERT (new_len_property_desc.value.value_type == ECMA_TYPE_NUMBER); + + ecma_number_t *new_len_num_p = ECMA_GET_POINTER (new_len_property_desc.value.value); + + // 1. + *new_len_num_p = ecma_uint32_to_number (old_len_uint32 + 1); + + // 2. + if (!new_writable) + { + new_len_property_desc.is_writable_defined = true; + new_len_property_desc.writable = ECMA_PROPERTY_NOT_WRITABLE; + } + + // 3. + ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); + ecma_completion_value_t completion = ecma_op_general_object_define_own_property (obj_p, + magic_string_length_p, + new_len_property_desc, + false); + ecma_deref_ecma_string (magic_string_length_p); + + JERRY_ASSERT (ecma_is_completion_value_normal_true (completion) + || ecma_is_completion_value_normal_false (completion)); + + reduce_succeeded = false; + + break; + } } - - bool reduce_succeeded = ecma_array_object_reduce_length (obj_p, - new_len_uint32, - old_len_uint32, - new_len_property_desc); - + if (!reduce_succeeded) { ret_value = ecma_reject (is_throw); diff --git a/tests/jerry/array.js b/tests/jerry/array.js index 2c81c84da..0b1ac4bd4 100644 --- a/tests/jerry/array.js +++ b/tests/jerry/array.js @@ -18,8 +18,50 @@ assert (cars[0] === "Saab"); assert (cars[1] === "Volvo"); assert (cars[2] === "BMW"); -// FIXME Uncomment when 'new Array' will be ready -// var cars1 = new Array("Saab", "Volvo", "BMW"); -// assert (cars[0] === cars1[0] === "Saab"); -// assert (cars[1] === cars1[1] === "Volvo"); -// assert (cars[2] === cars1[2] === "BMW"); \ No newline at end of file +var cars1 = new Array("Saab", "Volvo", "BMW"); +assert (cars[0] === cars1[0]); +assert (cars[1] === cars1[1]); +assert (cars[2] === cars1[2]); + +var a = new Array(); +assert (typeof (a) === "object"); +assert (Array.isArray (a)); +assert (Array.isArray ([1, 2, 3])); + +var b = new Array (30000); +assert(b.length === 30000); +assert (b[20000] === undefined); +b[20000] = 1; +assert (b[20000] === 1); +b[20000] = 10; +assert (b[20000] === 10); + +assert(b.length === 30000); +assert(b[10000] === undefined); +Object.defineProperty (b, '10000', {value : 25, writable : false}); +assert(b[10000] === 25); +b[10000] = 30; +assert(b[10000] === 25); + +assert(b.length === 30000); +assert(b[50000] === undefined); +assert(b.length === 30000); +b[50000] = 5; +assert(b.length === 50001); +assert(b[50000] === 5); +b[50000] = 10; +assert(b[50000] === 10); +Object.defineProperty (b, '50000', {writable : false}); +assert(b[50000] === 10); +b[50000] = 20; +assert(b[50000] === 10); + +Object.defineProperty (b, '50000', {writable : true}); +assert(b[50000] === 10); +b[50000] = 30; +assert(b[50000] === 30); + +b.length = 5; +assert(b[50000] === undefined); + +assert(([1, 2, 3]).length === 3);