1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecma-alloc.h"
17 #include "ecma-builtins.h"
18 #include "ecma-exceptions.h"
19 #include "ecma-gc.h"
20 #include "ecma-globals.h"
21 #include "ecma-helpers.h"
22 #include "ecma-objects.h"
23 #include "ecma-objects-general.h"
24 #include "ecma-string-object.h"
25
26 /** \addtogroup ecma ECMA
27 * @{
28 *
29 * \addtogroup ecmastringobject ECMA String object related routines
30 * @{
31 */
32
33 /**
34 * String object creation operation.
35 *
36 * See also: ECMA-262 v5, 15.5.2.1
37 *
38 * @return ecma value
39 * Returned value must be freed with ecma_free_value
40 */
41 ecma_value_t
ecma_op_create_string_object(const ecma_value_t * arguments_list_p,ecma_length_t arguments_list_len)42 ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of arguments that
43 are passed to String constructor */
44 ecma_length_t arguments_list_len) /**< length of the arguments' list */
45 {
46 JERRY_ASSERT (arguments_list_len == 0
47 || arguments_list_p != NULL);
48
49 ecma_value_t prim_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
50
51 if (arguments_list_len > 0)
52 {
53 ecma_string_t *str_p = ecma_op_to_string (arguments_list_p[0]);
54
55 if (JERRY_UNLIKELY (str_p == NULL))
56 {
57 return ECMA_VALUE_ERROR;
58 }
59
60 prim_value = ecma_make_string_value (str_p);
61 }
62
63 #if ENABLED (JERRY_BUILTIN_STRING)
64 ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_STRING_PROTOTYPE);
65 #else /* !ENABLED (JERRY_BUILTIN_STRING) */
66 ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
67 #endif /* ENABLED (JERRY_BUILTIN_STRING) */
68
69 ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
70 sizeof (ecma_extended_object_t),
71 ECMA_OBJECT_TYPE_CLASS);
72
73 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
74 ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_STRING_UL;
75 ext_object_p->u.class_prop.u.value = prim_value;
76
77 return ecma_make_object_value (object_p);
78 } /* ecma_op_create_string_object */
79
80 /**
81 * List names of a String object's lazy instantiated properties
82 *
83 * @return string values collection
84 */
85 void
ecma_op_string_list_lazy_property_names(ecma_object_t * obj_p,uint32_t opts,ecma_collection_t * main_collection_p,ecma_collection_t * non_enum_collection_p)86 ecma_op_string_list_lazy_property_names (ecma_object_t *obj_p, /**< a String object */
87 uint32_t opts, /**< listing options using flags
88 * from ecma_list_properties_options_t */
89 ecma_collection_t *main_collection_p, /**< 'main' collection */
90 ecma_collection_t *non_enum_collection_p) /**< skipped
91 * 'non-enumerable'
92 * collection */
93 {
94 JERRY_ASSERT (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_CLASS);
95
96 ecma_collection_t *for_enumerable_p = main_collection_p;
97
98 ecma_collection_t *for_non_enumerable_p = (opts & ECMA_LIST_ENUMERABLE) ? non_enum_collection_p : main_collection_p;
99
100 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
101 JERRY_ASSERT (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL);
102
103 ecma_string_t *prim_value_str_p = ecma_get_string_from_value (ext_object_p->u.class_prop.u.value);
104
105 ecma_length_t length = ecma_string_get_length (prim_value_str_p);
106
107 for (ecma_length_t i = 0; i < length; i++)
108 {
109 ecma_string_t *name_p = ecma_new_ecma_string_from_uint32 (i);
110
111 /* the properties are enumerable (ECMA-262 v5, 15.5.5.2.9) */
112 ecma_collection_push_back (for_enumerable_p, ecma_make_string_value (name_p));
113 }
114
115 if ((opts & ECMA_LIST_ARRAY_INDICES) == 0)
116 {
117 ecma_collection_push_back (for_non_enumerable_p, ecma_make_magic_string_value (LIT_MAGIC_STRING_LENGTH));
118 }
119 } /* ecma_op_string_list_lazy_property_names */
120
121 /**
122 * @}
123 * @}
124 */
125