diff --git a/src/libecmaobjects/ecma-gc.c b/src/libecmaobjects/ecma-gc.c index 9126d5080..3973efc9a 100644 --- a/src/libecmaobjects/ecma-gc.c +++ b/src/libecmaobjects/ecma-gc.c @@ -405,6 +405,9 @@ ecma_gc_mark (ecma_object_t *object_p, /**< start object */ } case ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS: /* a collection of strings */ + case ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE: /* compressed pointer to a ecma_string_t */ + case ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE: /* compressed pointer to a ecma_number_t */ + case ECMA_INTERNAL_PROPERTY_PRIMITIVE_BOOLEAN_VALUE: /* a simple boolean value */ case ECMA_INTERNAL_PROPERTY_PROVIDE_THIS: /* a boolean */ case ECMA_INTERNAL_PROPERTY_CLASS: /* an enum */ case ECMA_INTERNAL_PROPERTY_CODE: /* an integer */ diff --git a/src/libecmaobjects/ecma-globals.h b/src/libecmaobjects/ecma-globals.h index b81b55c51..87868913c 100644 --- a/src/libecmaobjects/ecma-globals.h +++ b/src/libecmaobjects/ecma-globals.h @@ -183,6 +183,9 @@ typedef enum ECMA_INTERNAL_PROPERTY_PARAMETERS_MAP, /**< [[ParametersMap]] */ ECMA_INTERNAL_PROPERTY_CODE, /**< [[Code]] */ ECMA_INTERNAL_PROPERTY_FORMAL_PARAMETERS, /**< [[FormalParameters]] */ + ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE, /**< [[Primitive value]] for String objects */ + ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE, /**< [[Primitive value]] for Number objects */ + ECMA_INTERNAL_PROPERTY_PRIMITIVE_BOOLEAN_VALUE, /**< [[Primitive value]] for Boolean objects */ /** provideThis property of lexical environment */ ECMA_INTERNAL_PROPERTY_PROVIDE_THIS, @@ -212,7 +215,7 @@ typedef enum /** * Bit-mask of non-instantiated built-in's properties (bits 32-63) */ - ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63, + ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63 } ecma_internal_property_id_t; /** @@ -298,7 +301,7 @@ typedef struct ecma_property_t struct __packed ecma_internal_property_t { /** Internal property's type */ - unsigned int type : 4; + unsigned int type : 5; /** Value (may be a compressed pointer) */ uint32_t value; @@ -740,6 +743,8 @@ typedef enum ECMA_MAGIC_STRING_SIN, /**< "sin" */ ECMA_MAGIC_STRING_SQRT, /**< "sqrt" */ ECMA_MAGIC_STRING_TAN, /**< "tan" */ + ECMA_MAGIC_STRING_FROM_CHAR_CODE_UL, /**< "fromCharCode" */ + ECMA_MAGIC_STRING__EMPTY, /**< "" */ ECMA_MAGIC_STRING__COUNT /**< number of magic strings */ } ecma_magic_string_id_t; diff --git a/src/libecmaobjects/ecma-helpers-string.c b/src/libecmaobjects/ecma-helpers-string.c index 14acbe046..2e15a8aa0 100644 --- a/src/libecmaobjects/ecma-helpers-string.c +++ b/src/libecmaobjects/ecma-helpers-string.c @@ -992,6 +992,36 @@ ecma_string_get_length (const ecma_string_t *string_p) /**< ecma-string */ } } /* ecma_string_get_length */ +/** + * Get character from specified position in the ecma-string. + * + * @return character value + */ +ecma_char_t +ecma_string_get_char_at_pos (const ecma_string_t *string_p, /**< ecma-string */ + uint32_t index) /**< index of character */ +{ + uint32_t length = (uint32_t) ecma_string_get_length (string_p); + JERRY_ASSERT (index < (uint32_t) length); + + size_t buffer_size = sizeof (ecma_char_t) * (length + 1); + ecma_char_t *zt_str_p = mem_heap_alloc_block (buffer_size, + MEM_HEAP_ALLOC_SHORT_TERM); + + if (zt_str_p == NULL) + { + jerry_exit (ERR_MEMORY); + } + + ecma_string_to_zt_string (string_p, zt_str_p, (ssize_t) buffer_size); + + ecma_char_t ch = zt_str_p [index]; + + mem_heap_free_block (zt_str_p); + + return ch; +} /* ecma_string_get_char_at_pos */ + /** * Compare zero-terminated string to zero-terminated string * @@ -1309,6 +1339,8 @@ ecma_get_magic_string_zt (ecma_magic_string_id_t id) /**< magic string id */ case ECMA_MAGIC_STRING_SIN: return (ecma_char_t*) "sin"; case ECMA_MAGIC_STRING_SQRT: return (ecma_char_t*) "sqrt"; case ECMA_MAGIC_STRING_TAN: return (ecma_char_t*) "tan"; + case ECMA_MAGIC_STRING_FROM_CHAR_CODE_UL: return (ecma_char_t*) "fromCharCode"; + case ECMA_MAGIC_STRING__EMPTY: return (ecma_char_t*) ""; case ECMA_MAGIC_STRING__COUNT: break; } diff --git a/src/libecmaobjects/ecma-helpers.c b/src/libecmaobjects/ecma-helpers.c index ee50061c6..66523f018 100644 --- a/src/libecmaobjects/ecma-helpers.c +++ b/src/libecmaobjects/ecma-helpers.c @@ -655,6 +655,23 @@ ecma_free_internal_property (ecma_property_t *property_p) /**< the property */ break; } + case ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE: /* compressed pointer to a ecma_string_t */ + { + ecma_string_t *str_p = ECMA_GET_POINTER (property_value); + ecma_deref_ecma_string (str_p); + + break; + } + + case ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE: /* pointer to a ecma_number_t */ + { + ecma_number_t *num_p = ECMA_GET_POINTER (property_value); + ecma_dealloc_number (num_p); + + break; + } + + case ECMA_INTERNAL_PROPERTY_PRIMITIVE_BOOLEAN_VALUE: /* a simple boolean value */ case ECMA_INTERNAL_PROPERTY_SCOPE: /* a lexical environment */ case ECMA_INTERNAL_PROPERTY_PARAMETERS_MAP: /* an object */ case ECMA_INTERNAL_PROPERTY_BINDING_OBJECT: /* an object */ diff --git a/src/libecmaobjects/ecma-helpers.h b/src/libecmaobjects/ecma-helpers.h index 0ba703681..8d97d390f 100644 --- a/src/libecmaobjects/ecma-helpers.h +++ b/src/libecmaobjects/ecma-helpers.h @@ -113,6 +113,7 @@ extern bool ecma_compare_ecma_strings (const ecma_string_t *string1_p, extern bool ecma_compare_ecma_strings_relational (const ecma_string_t *string1_p, const ecma_string_t *string2_p); extern int32_t ecma_string_get_length (const ecma_string_t *string_p); +extern ecma_char_t ecma_string_get_char_at_pos (const ecma_string_t *string_p, uint32_t index); extern bool ecma_compare_zt_strings (const ecma_char_t *string1_p, const ecma_char_t *string2_p); extern bool ecma_compare_zt_strings_relational (const ecma_char_t *string1_p, const ecma_char_t *string2_p); extern ssize_t ecma_copy_zt_string_to_buffer (const ecma_char_t *string_p, ecma_char_t *buffer_p, ssize_t buffer_size); diff --git a/src/libecmaoperations/ecma-objects.c b/src/libecmaoperations/ecma-objects.c index 99f8f86aa..3d4b21912 100644 --- a/src/libecmaoperations/ecma-objects.c +++ b/src/libecmaoperations/ecma-objects.c @@ -18,6 +18,7 @@ #include "ecma-globals.h" #include "ecma-array-object.h" #include "ecma-function-object.h" +#include "ecma-string-object.h" #include "ecma-objects-arguments.h" #include "ecma-objects-general.h" #include "ecma-objects.h" @@ -117,6 +118,12 @@ ecma_op_object_get_own_property (ecma_object_t *obj_p, /**< the object */ } case ECMA_OBJECT_TYPE_STRING: + { + prop_p = ecma_op_string_object_get_own_property (obj_p, property_name_p); + + break; + } + case ECMA_OBJECT_TYPE_HOST: { JERRY_UNIMPLEMENTED(); diff --git a/src/libecmaoperations/ecma-string-object.c b/src/libecmaoperations/ecma-string-object.c new file mode 100644 index 000000000..c135a90d6 --- /dev/null +++ b/src/libecmaoperations/ecma-string-object.c @@ -0,0 +1,176 @@ +/* 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-exceptions.h" +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-objects.h" +#include "ecma-objects-general.h" +#include "ecma-string-object.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmastringobject ECMA String object related routines + * @{ + */ + +/** + * String object creation operation. + * + * See also: ECMA-262 v5, 15.5.2.1 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_completion_value_t +ecma_op_create_string_object (ecma_value_t *arguments_list_p, /**< list of arguments that + are passed to String constructor */ + ecma_length_t arguments_list_len) /**< length of the arguments' list */ +{ + JERRY_ASSERT (arguments_list_len == 0 + || arguments_list_p != NULL); + + ecma_string_t* prim_prop_str_value_p; + + if (arguments_list_len == 0) + { + prim_prop_str_value_p = ecma_new_ecma_string_from_magic_string_id (ECMA_MAGIC_STRING__EMPTY); + } + else + { + ecma_completion_value_t to_str_arg_value = ecma_op_to_string (arguments_list_p [0]); + + if (ecma_is_completion_value_throw (to_str_arg_value)) + { + return to_str_arg_value; + } + else + { + JERRY_ASSERT (ecma_is_completion_value_normal (to_str_arg_value)); + + JERRY_ASSERT (to_str_arg_value.u.value.value_type == ECMA_TYPE_STRING); + prim_prop_str_value_p = ECMA_GET_POINTER (to_str_arg_value.u.value.value); + } + } + + FIXME (/* Set to built-in String prototype (15.5.4) */); + + ecma_object_t *obj_p = ecma_create_object (NULL, true, ECMA_OBJECT_TYPE_STRING); + + ecma_property_t *class_prop_p = ecma_create_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_CLASS); + class_prop_p->u.internal_property.value = ECMA_OBJECT_CLASS_STRING; + + ecma_property_t *prim_value_prop_p = ecma_create_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE); + ECMA_SET_POINTER (prim_value_prop_p->u.internal_property.value, prim_prop_str_value_p); + + return ecma_make_normal_completion_value (ecma_make_object_value (obj_p)); +} /* ecma_op_create_string_object */ + +/** + * [[GetOwnProperty]] ecma String object's operation + * + * See also: + * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8 + * ECMA-262 v5, 15.5.5.2 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value + */ +ecma_property_t* +ecma_op_string_object_get_own_property (ecma_object_t *obj_p, /**< the array object */ + ecma_string_t *property_name_p) /**< property name */ +{ + JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_STRING); + + // 1. + ecma_property_t *prop_p = ecma_op_general_object_get_own_property (obj_p, property_name_p); + + // 2. + if (prop_p != NULL) + { + return prop_p; + } + + // 3., 5. + uint32_t uint32_index; + ecma_string_t *new_prop_name_p; + + if (property_name_p->container == ECMA_STRING_CONTAINER_UINT32_IN_DESC) + { + uint32_index = property_name_p->u.uint32_number; + + ecma_ref_ecma_string (property_name_p); + new_prop_name_p = property_name_p; + } + else + { + ecma_number_t index = ecma_string_to_number (property_name_p); + uint32_index = ecma_number_to_uint32 (index); + + ecma_string_t *to_str_p = ecma_new_ecma_string_from_uint32 (uint32_index); + + bool are_equal = ecma_compare_ecma_strings (to_str_p, property_name_p); + + if (!are_equal) + { + ecma_deref_ecma_string (to_str_p); + + return NULL; + } + else + { + new_prop_name_p = to_str_p; + } + } + + // 4. + ecma_property_t* prim_value_prop_p = ecma_get_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE); + ecma_string_t *prim_value_str_p = ECMA_GET_POINTER (prim_value_prop_p->u.internal_property.value); + + // 6. + int32_t length = ecma_string_get_length (prim_value_str_p); + JERRY_ASSERT (length >= 0); + + // 7. + if (uint32_index >= (uint32_t) length) + { + return NULL; + } + + // 8. + ecma_char_t c = ecma_string_get_char_at_pos (prim_value_str_p, uint32_index); + + // 9. + ecma_property_t *new_prop_p = ecma_create_named_data_property (obj_p, + new_prop_name_p, + false, + true, + false); + ecma_char_t new_prop_zt_str_p [2] = { c, ECMA_CHAR_NULL }; + ecma_string_t *new_prop_str_value_p = ecma_new_ecma_string (new_prop_zt_str_p); + ecma_value_t new_prop_str_value = ecma_make_string_value (new_prop_str_value_p); + new_prop_p->u.named_data_property.value = new_prop_str_value; + + return new_prop_p; +} /* ecma_op_string_object_get_own_property */ + +/** + * @} + * @} + */ diff --git a/src/libecmaoperations/ecma-string-object.h b/src/libecmaoperations/ecma-string-object.h new file mode 100644 index 000000000..49322cb3f --- /dev/null +++ b/src/libecmaoperations/ecma-string-object.h @@ -0,0 +1,41 @@ +/* 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. + */ + +#ifndef ECMA_STRING_OBJECT_H +#define ECMA_STRING_OBJECT_H + +#include "ecma-globals.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmastringobject ECMA String object related routines + * @{ + */ + +extern ecma_completion_value_t +ecma_op_create_string_object (ecma_value_t *arguments_list_p, + ecma_length_t arguments_list_len); + +extern ecma_property_t* +ecma_op_string_object_get_own_property (ecma_object_t *obj_p, + ecma_string_t *property_name_p); + +/** + * @} + * @} + */ + +#endif /* !ECMA_STRING_OBJECT_H */