• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-builtins.h"
17 #include "ecma-array-object.h"
18 #include "ecma-gc.h"
19 #include "lit-char-helpers.h"
20 
21 #if ENABLED (JERRY_ES2015)
22 
23 #define ECMA_BUILTINS_INTERNAL
24 #include "ecma-builtins-internal.h"
25 
26 /**
27  * This object has a custom dispatch function.
28  */
29 #define BUILTIN_CUSTOM_DISPATCH
30 
31 /**
32  * List of built-in routine identifiers.
33  */
34 enum
35 {
36   ECMA_INTRINSIC_ROUTINE_START = ECMA_BUILTIN_ID__COUNT - 1,
37   ECMA_INTRINSIC_PARSE_FLOAT,
38   ECMA_INTRINSIC_PARSE_INT,
39   ECMA_INTRINSIC_ARRAY_PROTOTYPE_VALUES
40 };
41 
42 #define BUILTIN_INC_HEADER_NAME "ecma-builtin-intrinsic.inc.h"
43 #define BUILTIN_UNDERSCORED_ID intrinsic
44 #include "ecma-builtin-internal-routines-template.inc.h"
45 
46 /** \addtogroup ecma ECMA
47  * @{
48  *
49  * \addtogroup ecmabuiltins
50  * @{
51  *
52  * \addtogroup intrinsic ECMA Intrinsic object built-in
53  * @{
54  */
55 
56 /**
57  * The %ArrayProto_values% intrinsic routine
58  *
59  * See also:
60  *          ECMA-262 v5, 15.4.4.4
61  *
62  * @return ecma value
63  *         Returned value must be freed with ecma_free_value.
64  */
65 static ecma_value_t
ecma_builtin_intrinsic_array_prototype_values(ecma_value_t this_value)66 ecma_builtin_intrinsic_array_prototype_values (ecma_value_t this_value) /**< this argument */
67 {
68   ecma_value_t this_obj = ecma_op_to_object (this_value);
69 
70   if (ECMA_IS_VALUE_ERROR (this_obj))
71   {
72     return this_obj;
73   }
74 
75   ecma_object_t *this_obj_p = ecma_get_object_from_value (this_obj);
76 
77   ecma_value_t ret_value = ecma_op_create_array_iterator (this_obj_p, ECMA_ITERATOR_VALUES);
78 
79   ecma_deref_object (this_obj_p);
80 
81   return ret_value;
82 } /* ecma_builtin_intrinsic_array_prototype_values */
83 
84 /**
85  * Dispatcher of the built-in's routines
86  *
87  * @return ecma value
88  *         Returned value must be freed with ecma_free_value.
89  */
90 ecma_value_t
ecma_builtin_intrinsic_dispatch_routine(uint16_t builtin_routine_id,ecma_value_t this_arg,const ecma_value_t arguments_list_p[],ecma_length_t arguments_number)91 ecma_builtin_intrinsic_dispatch_routine (uint16_t builtin_routine_id, /**< built-in wide routine identifier */
92                                          ecma_value_t this_arg, /**< 'this' argument value */
93                                          const ecma_value_t arguments_list_p[], /**< list of arguments
94                                                                                  *   passed to routine */
95                                          ecma_length_t arguments_number) /**< length of arguments' list */
96 {
97   JERRY_UNUSED (arguments_number);
98 
99   ecma_value_t routine_arg_1 = arguments_list_p[0];
100   ecma_value_t routine_arg_2 = arguments_list_p[1];
101 
102   if (builtin_routine_id == ECMA_INTRINSIC_ARRAY_PROTOTYPE_VALUES)
103   {
104     return ecma_builtin_intrinsic_array_prototype_values (this_arg);
105   }
106   ecma_value_t ret_value = ECMA_VALUE_EMPTY;
107   if (builtin_routine_id <= ECMA_INTRINSIC_PARSE_INT)
108   {
109     ecma_string_t *str_p = ecma_op_to_string (routine_arg_1);
110 
111     if (JERRY_UNLIKELY (str_p == NULL))
112     {
113       return ECMA_VALUE_ERROR;
114     }
115 
116     ECMA_STRING_TO_UTF8_STRING (str_p, string_buff, string_buff_size);
117 
118     if (builtin_routine_id == ECMA_INTRINSIC_PARSE_INT)
119     {
120       ret_value = ecma_number_parse_int (string_buff,
121                                          string_buff_size,
122                                          routine_arg_2);
123     }
124     else
125     {
126       JERRY_ASSERT (builtin_routine_id == ECMA_INTRINSIC_PARSE_FLOAT);
127       ret_value = ecma_number_parse_float (string_buff,
128                                            string_buff_size);
129     }
130     ECMA_FINALIZE_UTF8_STRING (string_buff, string_buff_size);
131     ecma_deref_ecma_string (str_p);
132   }
133 
134   return ret_value;
135 } /* ecma_builtin_intrinsic_dispatch_routine */
136 
137 /**
138  * @}
139  * @}
140  * @}
141  */
142 
143 #endif /* ENABLED (JERRY_ES2015) */
144