From 9b178f5ab0618e9f2b9edf42800f43b6e66a8767 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Wed, 15 Oct 2014 19:02:41 +0400 Subject: [PATCH] Implementing 'length' property of String instances. --- src/libecmaoperations/ecma-string-object.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libecmaoperations/ecma-string-object.c b/src/libecmaoperations/ecma-string-object.c index 6d21ae56b..b52e45627 100644 --- a/src/libecmaoperations/ecma-string-object.c +++ b/src/libecmaoperations/ecma-string-object.c @@ -48,9 +48,12 @@ ecma_op_create_string_object (ecma_value_t *arguments_list_p, /**< list of argum ecma_string_t* prim_prop_str_value_p; + ecma_number_t length_value; + if (arguments_list_len == 0) { prim_prop_str_value_p = ecma_new_ecma_string_from_magic_string_id (ECMA_MAGIC_STRING__EMPTY); + length_value = ECMA_NUMBER_ZERO; } else { @@ -66,6 +69,11 @@ ecma_op_create_string_object (ecma_value_t *arguments_list_p, /**< list of argum 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); + + int32_t string_len = ecma_string_get_length (prim_prop_str_value_p); + JERRY_ASSERT (string_len >= 0); + + length_value = ecma_uint32_to_number ((uint32_t) string_len); } } @@ -82,6 +90,18 @@ ecma_op_create_string_object (ecma_value_t *arguments_list_p, /**< list of argum ECMA_INTERNAL_PROPERTY_PRIMITIVE_STRING_VALUE); ECMA_SET_POINTER (prim_value_prop_p->u.internal_property.value, prim_prop_str_value_p); + // 15.5.5.1 + ecma_string_t *length_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); + ecma_property_t *length_prop_p = ecma_create_named_data_property (obj_p, + length_magic_string_p, + false, + false, + false); + ecma_number_t *length_prop_value_p = ecma_alloc_number (); + *length_prop_value_p = length_value; + length_prop_p->u.named_data_property.value = ecma_make_number_value (length_prop_value_p); + ecma_deref_ecma_string (length_magic_string_p); + return ecma_make_normal_completion_value (ecma_make_object_value (obj_p)); } /* ecma_op_create_string_object */