1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include "ecma-alloc.h" 17425bb815Sopenharmony_ci#include "ecma-builtins.h" 18425bb815Sopenharmony_ci#include "ecma-exceptions.h" 19425bb815Sopenharmony_ci#include "ecma-gc.h" 20425bb815Sopenharmony_ci#include "ecma-globals.h" 21425bb815Sopenharmony_ci#include "ecma-helpers.h" 22425bb815Sopenharmony_ci#include "ecma-objects.h" 23425bb815Sopenharmony_ci#include "ecma-objects-general.h" 24425bb815Sopenharmony_ci#include "ecma-string-object.h" 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_ci/** \addtogroup ecma ECMA 27425bb815Sopenharmony_ci * @{ 28425bb815Sopenharmony_ci * 29425bb815Sopenharmony_ci * \addtogroup ecmastringobject ECMA String object related routines 30425bb815Sopenharmony_ci * @{ 31425bb815Sopenharmony_ci */ 32425bb815Sopenharmony_ci 33425bb815Sopenharmony_ci/** 34425bb815Sopenharmony_ci * String object creation operation. 35425bb815Sopenharmony_ci * 36425bb815Sopenharmony_ci * See also: ECMA-262 v5, 15.5.2.1 37425bb815Sopenharmony_ci * 38425bb815Sopenharmony_ci * @return ecma value 39425bb815Sopenharmony_ci * Returned value must be freed with ecma_free_value 40425bb815Sopenharmony_ci */ 41425bb815Sopenharmony_ciecma_value_t 42425bb815Sopenharmony_ciecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of arguments that 43425bb815Sopenharmony_ci are passed to String constructor */ 44425bb815Sopenharmony_ci ecma_length_t arguments_list_len) /**< length of the arguments' list */ 45425bb815Sopenharmony_ci{ 46425bb815Sopenharmony_ci JERRY_ASSERT (arguments_list_len == 0 47425bb815Sopenharmony_ci || arguments_list_p != NULL); 48425bb815Sopenharmony_ci 49425bb815Sopenharmony_ci ecma_value_t prim_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci if (arguments_list_len > 0) 52425bb815Sopenharmony_ci { 53425bb815Sopenharmony_ci ecma_string_t *str_p = ecma_op_to_string (arguments_list_p[0]); 54425bb815Sopenharmony_ci 55425bb815Sopenharmony_ci if (JERRY_UNLIKELY (str_p == NULL)) 56425bb815Sopenharmony_ci { 57425bb815Sopenharmony_ci return ECMA_VALUE_ERROR; 58425bb815Sopenharmony_ci } 59425bb815Sopenharmony_ci 60425bb815Sopenharmony_ci prim_value = ecma_make_string_value (str_p); 61425bb815Sopenharmony_ci } 62425bb815Sopenharmony_ci 63425bb815Sopenharmony_ci#if ENABLED (JERRY_BUILTIN_STRING) 64425bb815Sopenharmony_ci ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_STRING_PROTOTYPE); 65425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_BUILTIN_STRING) */ 66425bb815Sopenharmony_ci ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE); 67425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_BUILTIN_STRING) */ 68425bb815Sopenharmony_ci 69425bb815Sopenharmony_ci ecma_object_t *object_p = ecma_create_object (prototype_obj_p, 70425bb815Sopenharmony_ci sizeof (ecma_extended_object_t), 71425bb815Sopenharmony_ci ECMA_OBJECT_TYPE_CLASS); 72425bb815Sopenharmony_ci 73425bb815Sopenharmony_ci ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p; 74425bb815Sopenharmony_ci ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_STRING_UL; 75425bb815Sopenharmony_ci ext_object_p->u.class_prop.u.value = prim_value; 76425bb815Sopenharmony_ci 77425bb815Sopenharmony_ci return ecma_make_object_value (object_p); 78425bb815Sopenharmony_ci} /* ecma_op_create_string_object */ 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_ci/** 81425bb815Sopenharmony_ci * List names of a String object's lazy instantiated properties 82425bb815Sopenharmony_ci * 83425bb815Sopenharmony_ci * @return string values collection 84425bb815Sopenharmony_ci */ 85425bb815Sopenharmony_civoid 86425bb815Sopenharmony_ciecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String object */ 87425bb815Sopenharmony_ci uint32_t opts, /**< listing options using flags 88425bb815Sopenharmony_ci * from ecma_list_properties_options_t */ 89425bb815Sopenharmony_ci ecma_collection_t *main_collection_p, /**< 'main' collection */ 90425bb815Sopenharmony_ci ecma_collection_t *non_enum_collection_p) /**< skipped 91425bb815Sopenharmony_ci * 'non-enumerable' 92425bb815Sopenharmony_ci * collection */ 93425bb815Sopenharmony_ci{ 94425bb815Sopenharmony_ci JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_CLASS); 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci ecma_collection_t *for_enumerable_p = main_collection_p; 97425bb815Sopenharmony_ci 98425bb815Sopenharmony_ci ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p; 99425bb815Sopenharmony_ci 100425bb815Sopenharmony_ci ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p; 101425bb815Sopenharmony_ci JERRY_ASSERT (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL); 102425bb815Sopenharmony_ci 103425bb815Sopenharmony_ci ecma_string_t *prim_value_str_p = ecma_get_string_from_value (ext_object_p->u.class_prop.u.value); 104425bb815Sopenharmony_ci 105425bb815Sopenharmony_ci ecma_length_t length = ecma_string_get_length (prim_value_str_p); 106425bb815Sopenharmony_ci 107425bb815Sopenharmony_ci for (ecma_length_t i = 0; i < length; i++) 108425bb815Sopenharmony_ci { 109425bb815Sopenharmony_ci ecma_string_t *name_p = ecma_new_ecma_string_from_uint32 (i); 110425bb815Sopenharmony_ci 111425bb815Sopenharmony_ci /* the properties are enumerable (ECMA-262 v5, 15.5.5.2.9) */ 112425bb815Sopenharmony_ci ecma_collection_push_back (for_enumerable_p, ecma_make_string_value (name_p)); 113425bb815Sopenharmony_ci } 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_ci if ((opts & ECMA_LIST_ARRAY_INDICES) == 0) 116425bb815Sopenharmony_ci { 117425bb815Sopenharmony_ci ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH)); 118425bb815Sopenharmony_ci } 119425bb815Sopenharmony_ci} /* ecma_op_string_list_lazy_property_names */ 120425bb815Sopenharmony_ci 121425bb815Sopenharmony_ci/** 122425bb815Sopenharmony_ci * @} 123425bb815Sopenharmony_ci * @} 124425bb815Sopenharmony_ci */ 125