Implementing String's constructor and [[GetOwnProperty]].

This commit is contained in:
Ruben Ayrapetyan
2014-09-24 21:25:16 +04:00
parent 10ee3c4fb1
commit c4ec42635b
8 changed files with 284 additions and 2 deletions
+3
View File
@@ -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 */
+7 -2
View File
@@ -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;
+32
View File
@@ -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;
}
+17
View File
@@ -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 */
+1
View File
@@ -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);
+7
View File
@@ -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();
+176
View File
@@ -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 */
/**
* @}
* @}
*/
@@ -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 */