-
Notifications
You must be signed in to change notification settings - Fork 680
/
Copy pathecma-builtin-reflect.c
348 lines (291 loc) · 10.3 KB
/
ecma-builtin-reflect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* 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-array-object.h"
#include "ecma-builtins.h"
#include "ecma-builtin-function-prototype.h"
#include "ecma-iterator-object.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtin-object.h"
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-proxy-object.h"
#include "jcontext.h"
#if JERRY_BUILTIN_REFLECT
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
/**
* This object has a custom dispatch function.
*/
#define BUILTIN_CUSTOM_DISPATCH
/**
* List of built-in routine identifiers.
*/
enum
{
ECMA_REFLECT_OBJECT_ROUTINE_START = 0,
ECMA_REFLECT_OBJECT_GET, /* ECMA-262 v6, 26.1.6 */
ECMA_REFLECT_OBJECT_SET, /* ECMA-262 v6, 26.1.13 */
ECMA_REFLECT_OBJECT_HAS, /* ECMA-262 v6, 26.1.9 */
ECMA_REFLECT_OBJECT_DELETE_PROPERTY, /* ECMA-262 v6, 26.1.4 */
ECMA_REFLECT_OBJECT_CONSTRUCT, /* ECMA-262, 26.1.2 */
ECMA_REFLECT_OBJECT_OWN_KEYS, /* ECMA-262 v6, 26.1.11 */
ECMA_REFLECT_OBJECT_GET_PROTOTYPE_OF, /* ECMA-262 v6, 26.1.8 */
ECMA_REFLECT_OBJECT_SET_PROTOTYPE_OF, /* ECMA-262 v6, 26.1.14 */
ECMA_REFLECT_OBJECT_APPLY, /* ECMA-262 v6, 26.1.1 */
ECMA_REFLECT_OBJECT_DEFINE_PROPERTY, /* ECMA-262 v6, 26.1.3 */
ECMA_REFLECT_OBJECT_GET_OWN_PROPERTY_DESCRIPTOR, /* ECMA-262 v6, 26.1.7 */
ECMA_REFLECT_OBJECT_IS_EXTENSIBLE, /* ECMA-262 v6, 26.1.10 */
ECMA_REFLECT_OBJECT_PREVENT_EXTENSIONS, /* ECMA-262 v6, 26.1.12 */
};
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-reflect.inc.h"
#define BUILTIN_UNDERSCORED_ID reflect
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup object ECMA Reflect object built-in
* @{
*/
/**
* Dispatcher for the built-in's routines.
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_reflect_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine identifier */
ecma_value_t this_arg, /**< 'this' argument value */
const ecma_value_t arguments_list[], /**< list of arguments
* passed to routine */
uint32_t arguments_number) /**< length of arguments' list */
{
JERRY_UNUSED (this_arg);
if (builtin_routine_id < ECMA_REFLECT_OBJECT_CONSTRUCT)
{
/* 1. */
if (arguments_number == 0 || !ecma_is_value_object (arguments_list[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_argument_is_not_an_object));
}
/* 2. */
ecma_string_t *name_str_p = ecma_op_to_property_key (arguments_list[1]);
/* 3. */
if (name_str_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_value_t ret_value;
ecma_object_t *target_p = ecma_get_object_from_value (arguments_list[0]);
switch (builtin_routine_id)
{
case ECMA_REFLECT_OBJECT_GET:
{
ecma_value_t receiver = arguments_list[0];
/* 4. */
if (arguments_number > 2)
{
receiver = arguments_list[2];
}
ret_value = ecma_op_object_get_with_receiver (target_p, name_str_p, receiver);
break;
}
case ECMA_REFLECT_OBJECT_HAS:
{
ret_value = ecma_op_object_has_property (target_p, name_str_p);
break;
}
case ECMA_REFLECT_OBJECT_DELETE_PROPERTY:
{
ret_value = ecma_op_object_delete (target_p, name_str_p, false);
break;
}
default:
{
JERRY_ASSERT (builtin_routine_id == ECMA_REFLECT_OBJECT_SET);
ecma_value_t receiver = arguments_list[0];
if (arguments_number > 3)
{
receiver = arguments_list[3];
}
ret_value = ecma_op_object_put_with_receiver (target_p, name_str_p, arguments_list[2], receiver, false);
break;
}
}
ecma_deref_ecma_string (name_str_p);
return ret_value;
}
if (builtin_routine_id == ECMA_REFLECT_OBJECT_OWN_KEYS)
{
/* 1. */
if (arguments_number == 0 || !ecma_is_value_object (arguments_list[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_argument_is_not_an_object));
}
ecma_object_t *target_p = ecma_get_object_from_value (arguments_list[0]);
/* 2. */
ecma_collection_t *prop_names = ecma_op_object_own_property_keys (target_p, JERRY_PROPERTY_FILTER_ALL);
#if JERRY_BUILTIN_PROXY
if (prop_names == NULL)
{
return ECMA_VALUE_ERROR;
}
#endif /* JERRY_BUILTIN_PROXY */
/* 3. */
return ecma_op_new_array_object_from_collection (prop_names, false);
}
if (builtin_routine_id == ECMA_REFLECT_OBJECT_CONSTRUCT)
{
/* 1. */
if (arguments_number < 1 || !ecma_is_constructor (arguments_list[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_target_is_not_a_constructor));
}
ecma_object_t *target_p = ecma_get_object_from_value (arguments_list[0]);
/* 2. */
ecma_object_t *new_target_p = target_p;
if (arguments_number > 2)
{
/* 3. */
if (!ecma_is_constructor (arguments_list[2]))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_target_is_not_a_constructor));
}
new_target_p = ecma_get_object_from_value (arguments_list[2]);
}
/* 4. */
if (arguments_number < 2)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Reflect.construct expects an object as second argument"));
}
ecma_collection_t *coll_p = ecma_op_create_list_from_array_like (arguments_list[1], ECMA_FROM_ARRAY_LIKE_ANY);
if (coll_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_value_t ret_value = ecma_op_function_construct (target_p,
new_target_p,
coll_p->buffer_p,
coll_p->item_count);
ecma_collection_free (coll_p);
return ret_value;
}
if (!ecma_is_value_object (arguments_list[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_argument_is_not_an_object));
}
switch (builtin_routine_id)
{
case ECMA_REFLECT_OBJECT_GET_PROTOTYPE_OF:
{
return ecma_builtin_object_object_get_prototype_of (ecma_get_object_from_value (arguments_list[0]));
}
case ECMA_REFLECT_OBJECT_SET_PROTOTYPE_OF:
{
if (!ecma_is_value_object (arguments_list[1]) && !ecma_is_value_null (arguments_list[1]))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Prototype is neither object nor null"));
}
ecma_object_t *obj_p = ecma_get_object_from_value (arguments_list[0]);
ecma_value_t status;
#if JERRY_BUILTIN_PROXY
if (ECMA_OBJECT_IS_PROXY (obj_p))
{
status = ecma_proxy_object_set_prototype_of (obj_p, arguments_list[1]);
}
else
#endif /* JERRY_BUILTIN_PROXY */
{
status = ecma_op_ordinary_object_set_prototype_of (obj_p, arguments_list[1]);
}
return status;
}
case ECMA_REFLECT_OBJECT_APPLY:
{
if (!ecma_op_is_callable (arguments_list[0]))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a function"));
}
ecma_object_t *func_obj_p = ecma_get_object_from_value (arguments_list[0]);
return ecma_builtin_function_prototype_object_apply (func_obj_p, arguments_list[1], arguments_list[2]);
}
case ECMA_REFLECT_OBJECT_DEFINE_PROPERTY:
{
ecma_object_t *obj_p = ecma_get_object_from_value (arguments_list[0]);
ecma_string_t *name_str_p = ecma_op_to_property_key (arguments_list[1]);
if (name_str_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_property_descriptor_t prop_desc;
ecma_value_t conv_result = ecma_op_to_property_descriptor (arguments_list[2], &prop_desc);
if (ECMA_IS_VALUE_ERROR (conv_result))
{
ecma_deref_ecma_string (name_str_p);
return conv_result;
}
ecma_value_t result = ecma_op_object_define_own_property (obj_p,
name_str_p,
&prop_desc);
ecma_deref_ecma_string (name_str_p);
ecma_free_property_descriptor (&prop_desc);
if (ECMA_IS_VALUE_ERROR (result))
{
return result;
}
bool boolean_result = ecma_op_to_boolean (result);
return ecma_make_boolean_value (boolean_result);
}
case ECMA_REFLECT_OBJECT_GET_OWN_PROPERTY_DESCRIPTOR:
{
ecma_object_t *obj_p = ecma_get_object_from_value (arguments_list[0]);
ecma_string_t *name_str_p = ecma_op_to_property_key (arguments_list[1]);
if (name_str_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_value_t ret_val = ecma_builtin_object_object_get_own_property_descriptor (obj_p, name_str_p);
ecma_deref_ecma_string (name_str_p);
return ret_val;
}
case ECMA_REFLECT_OBJECT_IS_EXTENSIBLE:
{
ecma_object_t *obj_p = ecma_get_object_from_value (arguments_list[0]);
return ecma_builtin_object_object_is_extensible (obj_p);
}
default:
{
JERRY_ASSERT (builtin_routine_id == ECMA_REFLECT_OBJECT_PREVENT_EXTENSIONS);
ecma_object_t *obj_p = ecma_get_object_from_value (arguments_list[0]);
#if JERRY_BUILTIN_PROXY
if (ECMA_OBJECT_IS_PROXY (obj_p))
{
return ecma_proxy_object_prevent_extensions (obj_p);
}
#endif /* !JERRY_BUILTIN_PROXY */
ecma_op_ordinary_object_prevent_extensions (obj_p);
return ECMA_VALUE_TRUE;
}
}
} /* ecma_builtin_reflect_dispatch_routine */
/**
* @}
* @}
* @}
*/
#endif /* JERRY_BUILTIN_REFLECT */