From 3efdcfa2ea79a428c955dd8530aff95c9a6c839c Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Wed, 24 Sep 2014 21:40:06 +0400 Subject: [PATCH] Implementing String built-in object. --- .../ecma-builtin-global-object.c | 9 +- .../ecma-builtin-math-object.c | 1 + .../ecma-builtin-string-object.c | 365 ++++++++++++++++++ src/libecmabuiltins/ecma-builtins-internal.h | 17 + src/libecmabuiltins/ecma-builtins.c | 35 +- tests/jerry/test_new_string.js | 16 + 6 files changed, 437 insertions(+), 6 deletions(-) create mode 100644 src/libecmabuiltins/ecma-builtin-string-object.c create mode 100644 tests/jerry/test_new_string.js diff --git a/src/libecmabuiltins/ecma-builtin-global-object.c b/src/libecmabuiltins/ecma-builtin-global-object.c index 7449b1886..6ed7a975b 100644 --- a/src/libecmabuiltins/ecma-builtin-global-object.c +++ b/src/libecmabuiltins/ecma-builtin-global-object.c @@ -488,10 +488,17 @@ ecma_builtin_global_try_to_instantiate_property (ecma_object_t *obj_p, /**< obje break; } + case ECMA_MAGIC_STRING_STRING_UL: + { + ecma_object_t *string_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_STRING); + + value = ecma_make_object_value (string_obj_p); + + break; + } case ECMA_MAGIC_STRING_FUNCTION_UL: case ECMA_MAGIC_STRING_ARRAY_UL: - case ECMA_MAGIC_STRING_STRING_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-math-object.c b/src/libecmabuiltins/ecma-builtin-math-object.c index 556361dce..ba1a8d40f 100644 --- a/src/libecmabuiltins/ecma-builtin-math-object.c +++ b/src/libecmabuiltins/ecma-builtin-math-object.c @@ -1334,6 +1334,7 @@ ecma_builtin_math_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, / #undef ROUTINE_ARG_LIST_1 #undef ROUTINE_ARG_LIST_2 #undef ROUTINE_ARG_LIST_3 +#undef ROUTINE_ARG_LIST_NON_FIXED #undef ROUTINE_ARG default: diff --git a/src/libecmabuiltins/ecma-builtin-string-object.c b/src/libecmabuiltins/ecma-builtin-string-object.c new file mode 100644 index 000000000..ff20fa186 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-string-object.c @@ -0,0 +1,365 @@ +/* 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-string-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 object ECMA Object object built-in + * @{ + */ + +/** + * List of the String object built-in object value properties in format 'macro (name, value)'. + */ +#define ECMA_BUILTIN_STRING_OBJECT_OBJECT_VALUES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_PROTOTYPE, ecma_builtin_get (ECMA_BUILTIN_ID_STRING_PROTOTYPE)) + +/** + * List of the String 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_STRING_OBJECT_ROUTINES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_FROM_CHAR_CODE_UL, \ + ecma_builtin_string_object_from_char_code, \ + NON_FIXED, \ + 1) + +/** + * List of the String object's built-in property names + */ +static const ecma_magic_string_id_t ecma_builtin_string_property_names[] = +{ +#define VALUE_PROP_LIST(name, value) name, +#define ROUTINE_PROP_LIST(name, c_function_name, args_number, length) name, + ECMA_BUILTIN_STRING_OBJECT_OBJECT_VALUES_PROPERTY_LIST (VALUE_PROP_LIST) + ECMA_BUILTIN_STRING_OBJECT_ROUTINES_PROPERTY_LIST (ROUTINE_PROP_LIST) +#undef VALUE_PROP_LIST +#undef ROUTINE_PROP_LIST +}; + +/** + * Number of the String object's built-in properties + */ +const ecma_length_t ecma_builtin_string_property_number = (sizeof (ecma_builtin_string_property_names) / + sizeof (ecma_magic_string_id_t)); + +/** + * The String object's 'fromCharCode' routine + * + * See also: + * ECMA-262 v5, 15.5.3.2 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_string_object_from_char_code (ecma_value_t args[], /**< arguments list */ + ecma_length_t args_number) /**< number of arguments */ +{ + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + size_t zt_str_buffer_size = sizeof (ecma_char_t) * (args_number + 1u); + + ecma_char_t *ret_zt_str_p = mem_heap_alloc_block (zt_str_buffer_size, + MEM_HEAP_ALLOC_SHORT_TERM); + ret_zt_str_p [args_number] = ECMA_CHAR_NULL; + + for (ecma_length_t arg_index = 0; + arg_index < args_number; + arg_index++) + { + ECMA_TRY_CATCH (arg_num_value, + ecma_op_to_number (args[arg_index]), + ret_value); + + JERRY_ASSERT (arg_num_value.u.value.value_type == ECMA_TYPE_NUMBER); + ecma_number_t *arg_num_p = ECMA_GET_POINTER (arg_num_value.u.value.value); + + uint32_t uint32_char_code = ecma_number_to_uint32 (*arg_num_p); + uint16_t uint16_char_code = (uint16_t) uint32_char_code; + +#if defined (CONFIG_ECMA_CHAR_ASCII) + if ((uint16_char_code >> JERRY_BITSINBYTE) != 0) + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); + } + else + { + ret_zt_str_p [arg_index] = (ecma_char_t) uint16_char_code; + } +#elif defined (CONFIG_ECMA_CHAR_UTF16) + ret_zt_str_p [arg_index] = (ecma_char_t) uint16_char_code; +#else /* !CONFIG_ECMA_CHAR_ASCII && !CONFIG_ECMA_CHAR_UTF16 */ +# error "!CONFIG_ECMA_CHAR_ASCII && !CONFIG_ECMA_CHAR_UTF16" +#endif /* !CONFIG_ECMA_CHAR_ASCII && !CONFIG_ECMA_CHAR_UTF16 */ + + ECMA_FINALIZE (arg_num_value); + + if (ecma_is_completion_value_throw (ret_value)) + { + mem_heap_free_block (ret_zt_str_p); + + return ret_value; + } + + JERRY_ASSERT (ecma_is_completion_value_empty (ret_value)); + } + + ecma_string_t *ret_str_p = ecma_new_ecma_string (ret_zt_str_p); + + mem_heap_free_block (ret_zt_str_p); + + return ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p)); +} /* ecma_builtin_string_object_from_char_code */ + +/** + * If the property's name is one of built-in properties of the String 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_string_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_STRING)); + 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_string_property_names, + ecma_builtin_string_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_STRING_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_STRING, id); + + value = ecma_make_object_value (func_obj_p); + + break; + } +#define CASE_VALUE_PROP_LIST(name, value) case name: + ECMA_BUILTIN_STRING_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_STRING_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_string_try_to_instantiate_property */ + +/** + * Dispatcher of the String object's built-in routines + * + * @return completion-value + * Returned value must be freed with ecma_free_completion_value. + */ +ecma_completion_value_t +ecma_builtin_string_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, /**< String object's + built-in routine's name */ + ecma_value_t arguments_list [], /**< list of arguments passed to routine */ + ecma_length_t arguments_number) /**< length of arguments' list */ +{ + 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: \ + { \ + return c_function_name (ROUTINE_ARG_LIST_ ## args_number); \ + } + 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: + { + JERRY_UNREACHABLE (); + } + } +} /* ecma_builtin_string_dispatch_routine */ + +/** + * Get number of routine's parameters + * + * @return number of parameters + */ +ecma_length_t +ecma_builtin_string_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_STRING_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST + + default: + { + JERRY_UNREACHABLE (); + } + } +} /* ecma_builtin_string_get_routine_parameters_number */ + +/** + * Handle calling [[Call]] of built-in String object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_string_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); + + ecma_completion_value_t ret_value; + + if (arguments_list_len == 0) + { + ecma_string_t *str_p = ecma_new_ecma_string_from_magic_string_id (ECMA_MAGIC_STRING__EMPTY); + ecma_value_t str_value = ecma_make_string_value (str_p); + + ret_value = ecma_make_normal_completion_value (str_value); + } + else + { + ret_value = ecma_op_to_string (arguments_list_p [0]); + } + + return ret_value; +} /* ecma_builtin_string_dispatch_call */ + +/** + * Handle calling [[Construct]] of built-in String object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_string_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_string_object (arguments_list_p, arguments_list_len); +} /* ecma_builtin_string_dispatch_construct */ + +/** + * @} + * @} + * @} + */ diff --git a/src/libecmabuiltins/ecma-builtins-internal.h b/src/libecmabuiltins/ecma-builtins-internal.h index bca7f1f44..905a602a3 100644 --- a/src/libecmabuiltins/ecma-builtins-internal.h +++ b/src/libecmabuiltins/ecma-builtins-internal.h @@ -99,6 +99,23 @@ ecma_builtin_math_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, extern ecma_property_t* ecma_builtin_math_try_to_instantiate_property (ecma_object_t *obj_p, ecma_string_t *prop_name_p); +/* ecma-builtin-string-object.c */ +extern const ecma_length_t ecma_builtin_string_property_number; +extern ecma_length_t +ecma_builtin_string_get_routine_parameters_number (ecma_magic_string_id_t routine_id); +extern ecma_completion_value_t +ecma_builtin_string_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, + ecma_value_t arguments_list [], + ecma_length_t arguments_number); +extern ecma_property_t* +ecma_builtin_string_try_to_instantiate_property (ecma_object_t *obj_p, ecma_string_t *prop_name_p); +extern ecma_completion_value_t +ecma_builtin_string_dispatch_call (ecma_value_t *arguments_list_p, + ecma_length_t arguments_list_len); +extern ecma_completion_value_t +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); diff --git a/src/libecmabuiltins/ecma-builtins.c b/src/libecmabuiltins/ecma-builtins.c index f3a99d7f6..d14fa3e89 100644 --- a/src/libecmabuiltins/ecma-builtins.c +++ b/src/libecmabuiltins/ecma-builtins.c @@ -138,6 +138,11 @@ ecma_init_builtins (void) ECMA_OBJECT_CLASS_OBJECT, ecma_builtin_object_property_number); + ecma_builtin_objects [ECMA_BUILTIN_ID_STRING] = ecma_builtin_init_object (ECMA_BUILTIN_ID_STRING, + ECMA_OBJECT_TYPE_FUNCTION, + ECMA_OBJECT_CLASS_STRING, + ecma_builtin_string_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, @@ -205,12 +210,16 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object * return ecma_builtin_math_try_to_instantiate_property (object_p, string_p); } + case ECMA_BUILTIN_ID_STRING: + { + return ecma_builtin_string_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: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: case ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE: @@ -345,12 +354,16 @@ ecma_builtin_dispatch_call (ecma_object_t *obj_p, /**< built-in object */ return ecma_builtin_object_dispatch_call (arguments_list_p, arguments_list_len); } + case ECMA_BUILTIN_ID_STRING: + { + return ecma_builtin_string_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: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: case ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE: @@ -416,12 +429,16 @@ ecma_builtin_dispatch_construct (ecma_object_t *obj_p, /**< built-in object */ return ecma_builtin_object_dispatch_construct (arguments_list_p, arguments_list_len); } + case ECMA_BUILTIN_ID_STRING: + { + return ecma_builtin_string_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: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: case ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE: @@ -480,6 +497,10 @@ ecma_builtin_get_routine_parameters_number (ecma_builtin_id_t builtin_id, /**< i { return ecma_builtin_object_get_routine_parameters_number (routine_id); } + case ECMA_BUILTIN_ID_STRING: + { + return ecma_builtin_string_get_routine_parameters_number (routine_id); + } case ECMA_BUILTIN_ID_MATH: { return ecma_builtin_math_get_routine_parameters_number (routine_id); @@ -489,7 +510,6 @@ ecma_builtin_get_routine_parameters_number (ecma_builtin_id_t builtin_id, /**< i case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: - case ECMA_BUILTIN_ID_STRING: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: case ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE: @@ -547,6 +567,12 @@ ecma_builtin_dispatch_routine (ecma_builtin_id_t builtin_object_id, /**< built-i arguments_list, arguments_number); } + case ECMA_BUILTIN_ID_STRING: + { + return ecma_builtin_string_dispatch_routine (builtin_routine_id, + arguments_list, + arguments_number); + } case ECMA_BUILTIN_ID_MATH: { return ecma_builtin_math_dispatch_routine (builtin_routine_id, @@ -558,7 +584,6 @@ ecma_builtin_dispatch_routine (ecma_builtin_id_t builtin_object_id, /**< built-i case ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE: case ECMA_BUILTIN_ID_ARRAY: case ECMA_BUILTIN_ID_ARRAY_PROTOTYPE: - case ECMA_BUILTIN_ID_STRING: case ECMA_BUILTIN_ID_STRING_PROTOTYPE: case ECMA_BUILTIN_ID_BOOLEAN: case ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE: diff --git a/tests/jerry/test_new_string.js b/tests/jerry/test_new_string.js new file mode 100644 index 000000000..0ec88b47a --- /dev/null +++ b/tests/jerry/test_new_string.js @@ -0,0 +1,16 @@ +// 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. + +var a = new String ('abcd'); +var b = String.fromCharCode (97, 98, 99, 100);