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-array-object.h"
17 #include "ecma-builtins.h"
18 #include "ecma-builtin-helpers.h"
19 #include "ecma-exceptions.h"
20 #include "ecma-gc.h"
21 #include "ecma-globals.h"
22 #include "ecma-helpers.h"
23 #include "ecma-function-object.h"
24 #include "ecma-lex-env.h"
25 #include "ecma-string-object.h"
26 #include "ecma-objects-arguments.h"
27 #include "ecma-objects-general.h"
28 #include "ecma-objects.h"
29 #include "ecma-proxy-object.h"
30 #include "jcontext.h"
31
32 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
33 #include "ecma-typedarray-object.h"
34 #include "ecma-arraybuffer-object.h"
35 #include "ecma-try-catch-macro.h"
36 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
37
38 /** \addtogroup ecma ECMA
39 * @{
40 *
41 * \addtogroup ecmaobjectsinternalops ECMA objects' operations
42 * @{
43 */
44
45 /**
46 * Hash bitmap size for ecma objects
47 */
48 #define ECMA_OBJECT_HASH_BITMAP_SIZE 256
49
50 /**
51 * Assert that specified object type value is valid
52 *
53 * @param type object's implementation-defined type
54 */
55 #ifndef JERRY_NDEBUG
56 #define JERRY_ASSERT_OBJECT_TYPE_IS_VALID(type) \
57 JERRY_ASSERT (type < ECMA_OBJECT_TYPE__MAX);
58 #else /* JERRY_NDEBUG */
59 #define JERRY_ASSERT_OBJECT_TYPE_IS_VALID(type)
60 #endif /* !JERRY_NDEBUG */
61
62 /**
63 * [[GetOwnProperty]] ecma object's operation
64 *
65 * See also:
66 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
67 *
68 * @return pointer to a property - if it exists,
69 * NULL (i.e. ecma-undefined) - otherwise.
70 */
71 ecma_property_t
ecma_op_object_get_own_property(ecma_object_t * object_p,ecma_string_t * property_name_p,ecma_property_ref_t * property_ref_p,uint32_t options)72 ecma_op_object_get_own_property (ecma_object_t *object_p, /**< the object */
73 ecma_string_t *property_name_p, /**< property name */
74 ecma_property_ref_t *property_ref_p, /**< property reference */
75 uint32_t options) /**< option bits */
76 {
77 JERRY_ASSERT (object_p != NULL
78 && !ecma_is_lexical_environment (object_p));
79 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
80 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
81 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
82 JERRY_ASSERT (property_name_p != NULL);
83 JERRY_ASSERT (options == ECMA_PROPERTY_GET_NO_OPTIONS
84 || options == ECMA_PROPERTY_GET_HAS_OWN_PROP
85 || property_ref_p != NULL);
86
87 ecma_object_type_t type = ecma_get_object_type (object_p);
88
89 switch (type)
90 {
91 case ECMA_OBJECT_TYPE_CLASS:
92 {
93 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
94
95 if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL)
96 {
97 if (ecma_string_is_length (property_name_p))
98 {
99 if (options & ECMA_PROPERTY_GET_VALUE)
100 {
101 ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
102 ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
103
104 ecma_length_t length = ecma_string_get_length (prim_value_str_p);
105 property_ref_p->virtual_value = ecma_make_uint32_value (length);
106 }
107
108 return ECMA_PROPERTY_TYPE_VIRTUAL;
109 }
110
111 uint32_t index = ecma_string_get_array_index (property_name_p);
112
113 if (index != ECMA_STRING_NOT_ARRAY_INDEX)
114 {
115 ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
116 ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
117
118 if (index < ecma_string_get_length (prim_value_str_p))
119 {
120 if (options & ECMA_PROPERTY_GET_VALUE)
121 {
122 ecma_char_t char_at_idx = ecma_string_get_char_at_pos (prim_value_str_p, index);
123 ecma_string_t *char_str_p = ecma_new_ecma_string_from_code_unit (char_at_idx);
124 property_ref_p->virtual_value = ecma_make_string_value (char_str_p);
125 }
126
127 return ECMA_PROPERTY_FLAG_ENUMERABLE | ECMA_PROPERTY_TYPE_VIRTUAL;
128 }
129 }
130 }
131 break;
132 }
133 case ECMA_OBJECT_TYPE_ARRAY:
134 {
135 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
136
137 if (ecma_string_is_length (property_name_p))
138 {
139 if (options & ECMA_PROPERTY_GET_VALUE)
140 {
141 property_ref_p->virtual_value = ecma_make_uint32_value (ext_object_p->u.array.length);
142 }
143
144 return ext_object_p->u.array.u.length_prop & (ECMA_PROPERTY_TYPE_VIRTUAL | ECMA_PROPERTY_FLAG_WRITABLE);
145 }
146
147 if (ecma_op_array_is_fast_array (ext_object_p))
148 {
149 uint32_t index = ecma_string_get_array_index (property_name_p);
150
151 if (index != ECMA_STRING_NOT_ARRAY_INDEX)
152 {
153 if (JERRY_LIKELY (index < ext_object_p->u.array.length))
154 {
155 ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, object_p->u1.property_list_cp);
156
157 if (ecma_is_value_array_hole (values_p[index]))
158 {
159 return ECMA_PROPERTY_TYPE_NOT_FOUND;
160 }
161
162 if (options & ECMA_PROPERTY_GET_VALUE)
163 {
164 property_ref_p->virtual_value = ecma_fast_copy_value (values_p[index]);
165 }
166
167 return (ecma_property_t) (ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE | ECMA_PROPERTY_TYPE_VIRTUAL);
168 }
169 }
170
171 return ECMA_PROPERTY_TYPE_NOT_FOUND;
172 }
173
174 break;
175 }
176 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
177 case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
178 {
179 /* ES2015 9.4.5.1 */
180 if (ecma_object_is_typedarray (object_p))
181 {
182 #if ENABLED (JERRY_ES2015)
183 if (ecma_prop_name_is_symbol (property_name_p))
184 {
185 break;
186 }
187 #endif /* ENABLED (JERRY_ES2015) */
188
189 uint32_t array_index = ecma_string_get_array_index (property_name_p);
190
191 if (array_index != ECMA_STRING_NOT_ARRAY_INDEX)
192 {
193 ecma_typedarray_info_t info = ecma_typedarray_get_info (object_p);
194 ecma_value_t value = ECMA_VALUE_UNDEFINED;
195
196 if (array_index < info.length)
197 {
198 ecma_length_t byte_pos = array_index << info.shift;
199 ecma_number_t num = ecma_get_typedarray_element (info.buffer_p + byte_pos, info.id);
200 value = ecma_make_number_value (num);
201 }
202
203 if (!ecma_is_value_undefined (value))
204 {
205 if (options & ECMA_PROPERTY_GET_VALUE)
206 {
207 property_ref_p->virtual_value = value;
208 }
209 else
210 {
211 ecma_fast_free_value (value);
212 }
213
214 return ECMA_PROPERTY_ENUMERABLE_WRITABLE | ECMA_PROPERTY_TYPE_VIRTUAL;
215 }
216
217 return ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP;
218 }
219
220 ecma_number_t num = ecma_string_to_number (property_name_p);
221 ecma_string_t *num_to_str = ecma_new_ecma_string_from_number (num);
222
223 if (ecma_compare_ecma_strings (property_name_p, num_to_str))
224 {
225 ecma_deref_ecma_string (num_to_str);
226
227 return ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP;
228 }
229
230 ecma_deref_ecma_string (num_to_str);
231 }
232
233 break;
234 }
235 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
236 default:
237 {
238 break;
239 }
240 }
241
242 ecma_property_t *property_p = ecma_find_named_property (object_p, property_name_p);
243
244 if (property_p == NULL)
245 {
246 if (ecma_get_object_is_builtin (object_p))
247 {
248 if (type == ECMA_OBJECT_TYPE_FUNCTION && ecma_builtin_function_is_routine (object_p))
249 {
250 property_p = ecma_builtin_routine_try_to_instantiate_property (object_p, property_name_p);
251 }
252 else
253 {
254 property_p = ecma_builtin_try_to_instantiate_property (object_p, property_name_p);
255 }
256 }
257 else if (type == ECMA_OBJECT_TYPE_FUNCTION)
258 {
259 #if !ENABLED (JERRY_ES2015)
260 if (ecma_string_is_length (property_name_p))
261 {
262 if (options & ECMA_PROPERTY_GET_VALUE)
263 {
264 /* Get length virtual property. */
265 ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) object_p;
266 const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
267
268 uint32_t len;
269 if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
270 {
271 cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_data_p;
272 len = args_p->argument_end;
273 }
274 else
275 {
276 cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_data_p;
277 len = args_p->argument_end;
278 }
279
280 property_ref_p->virtual_value = ecma_make_uint32_value (len);
281 }
282
283 return ECMA_PROPERTY_TYPE_VIRTUAL;
284 }
285 #endif /* !ENABLED (JERRY_ES2015) */
286
287 /* Get prototype physical property. */
288 property_p = ecma_op_function_try_to_lazy_instantiate_property (object_p, property_name_p);
289 }
290 else if (type == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION)
291 {
292 property_p = ecma_op_external_function_try_to_lazy_instantiate_property (object_p, property_name_p);
293 }
294 else if (type == ECMA_OBJECT_TYPE_BOUND_FUNCTION)
295 {
296 property_p = ecma_op_bound_function_try_to_lazy_instantiate_property (object_p, property_name_p);
297 }
298
299 if (property_p == NULL)
300 {
301 return ECMA_PROPERTY_TYPE_NOT_FOUND;
302 }
303 }
304 else if (type == ECMA_OBJECT_TYPE_PSEUDO_ARRAY
305 && (options & ECMA_PROPERTY_GET_HAS_OWN_PROP))
306 {
307 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
308
309 if (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS)
310 {
311 uint32_t index = ecma_string_get_array_index (property_name_p);
312
313 if (index != ECMA_STRING_NOT_ARRAY_INDEX
314 && index < ext_object_p->u.pseudo_array.u1.length)
315 {
316 ecma_value_t *arg_Literal_p = (ecma_value_t *) (ext_object_p + 1);
317
318 if (arg_Literal_p[index] != ECMA_VALUE_EMPTY)
319 {
320 ecma_string_t *arg_name_p = ecma_get_string_from_value (arg_Literal_p[index]);
321
322 ecma_object_t *lex_env_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
323 ext_object_p->u.pseudo_array.u2.lex_env_cp);
324
325 JERRY_ASSERT (lex_env_p != NULL
326 && ecma_is_lexical_environment (lex_env_p));
327
328 ecma_value_t binding_value = ecma_op_get_binding_value (lex_env_p, arg_name_p, true);
329
330 ecma_named_data_property_assign_value (object_p,
331 ECMA_PROPERTY_VALUE_PTR (property_p),
332 binding_value);
333 ecma_free_value (binding_value);
334 }
335 }
336 }
337 }
338
339 if (options & ECMA_PROPERTY_GET_EXT_REFERENCE)
340 {
341 ((ecma_extended_property_ref_t *) property_ref_p)->property_p = property_p;
342 }
343
344 if (property_ref_p != NULL)
345 {
346 property_ref_p->value_p = ECMA_PROPERTY_VALUE_PTR (property_p);
347 }
348
349 return *property_p;
350 } /* ecma_op_object_get_own_property */
351
352 /**
353 * [[GetProperty]] ecma object's operation
354 *
355 * See also:
356 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
357 *
358 * @return pointer to a property - if it exists,
359 * NULL (i.e. ecma-undefined) - otherwise.
360 */
361 static ecma_property_t
ecma_op_object_get_property(ecma_object_t * object_p,ecma_string_t * property_name_p,ecma_property_ref_t * property_ref_p,uint32_t options)362 ecma_op_object_get_property (ecma_object_t *object_p, /**< the object */
363 ecma_string_t *property_name_p, /**< property name */
364 ecma_property_ref_t *property_ref_p, /**< property reference */
365 uint32_t options) /**< option bits */
366 {
367 while (true)
368 {
369 ecma_property_t property = ecma_op_object_get_own_property (object_p,
370 property_name_p,
371 property_ref_p,
372 options);
373
374 if (property != ECMA_PROPERTY_TYPE_NOT_FOUND && property != ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP)
375 {
376 return property;
377 }
378
379 if (property == ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP)
380 {
381 break;
382 }
383
384 if (object_p->u2.prototype_cp == JMEM_CP_NULL)
385 {
386 break;
387 }
388
389 object_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, object_p->u2.prototype_cp);
390 }
391
392 return ECMA_PROPERTY_TYPE_NOT_FOUND;
393 } /* ecma_op_object_get_property */
394
395 /**
396 * Generic [[HasProperty]] operation
397 *
398 * See also:
399 * ECMAScript v6, 9.1.7.1
400 *
401 * @return ECMA_VALUE_ERROR - if the operation fails
402 * ECMA_VALUE_{TRUE_FALSE} - whether the property is found
403 */
404 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_object_has_property(ecma_object_t * object_p,ecma_string_t * property_name_p)405 ecma_op_object_has_property (ecma_object_t *object_p, /**< the object */
406 ecma_string_t *property_name_p) /**< property name */
407 {
408 while (true)
409 {
410 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
411 if (ECMA_OBJECT_IS_PROXY (object_p))
412 {
413 return ecma_proxy_object_has (object_p, property_name_p);
414 }
415 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
416
417 /* 2 - 3. */
418 if (ecma_op_ordinary_object_has_own_property (object_p, property_name_p))
419 {
420 return ECMA_VALUE_TRUE;
421 }
422
423 jmem_cpointer_t proto_cp = ecma_op_ordinary_object_get_prototype_of (object_p);
424
425 /* 7. */
426 if (proto_cp == JMEM_CP_NULL)
427 {
428 return ECMA_VALUE_FALSE;
429 }
430
431 object_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, proto_cp);
432 }
433 } /* ecma_op_object_has_property */
434
435 /**
436 * Search the value corresponding to a property name
437 *
438 * Note: search includes prototypes
439 *
440 * @return ecma value if property is found
441 * ECMA_VALUE_NOT_FOUND if property is not found
442 * Returned value must be freed with ecma_free_value
443 */
444 ecma_value_t
ecma_op_object_find_own(ecma_value_t base_value,ecma_object_t * object_p,ecma_string_t * property_name_p)445 ecma_op_object_find_own (ecma_value_t base_value, /**< base value */
446 ecma_object_t *object_p, /**< target object */
447 ecma_string_t *property_name_p) /**< property name */
448 {
449 JERRY_ASSERT (object_p != NULL
450 && !ecma_is_lexical_environment (object_p));
451 JERRY_ASSERT (property_name_p != NULL);
452 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
453
454 ecma_object_type_t type = ecma_get_object_type (object_p);
455
456 switch (type)
457 {
458 case ECMA_OBJECT_TYPE_CLASS:
459 {
460 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
461
462 if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL)
463 {
464 if (ecma_string_is_length (property_name_p))
465 {
466 ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
467
468 ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
469 ecma_length_t length = ecma_string_get_length (prim_value_str_p);
470
471 return ecma_make_uint32_value (length);
472 }
473
474 uint32_t index = ecma_string_get_array_index (property_name_p);
475
476 if (index != ECMA_STRING_NOT_ARRAY_INDEX)
477 {
478 ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
479
480 ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
481
482 if (index < ecma_string_get_length (prim_value_str_p))
483 {
484 ecma_char_t char_at_idx = ecma_string_get_char_at_pos (prim_value_str_p, index);
485 return ecma_make_string_value (ecma_new_ecma_string_from_code_unit (char_at_idx));
486 }
487 }
488 }
489
490 break;
491 }
492 case ECMA_OBJECT_TYPE_ARRAY:
493 {
494 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
495
496 if (ecma_string_is_length (property_name_p))
497 {
498 return ecma_make_uint32_value (ext_object_p->u.array.length);
499 }
500
501 if (JERRY_LIKELY (ecma_op_array_is_fast_array (ext_object_p)))
502 {
503 uint32_t index = ecma_string_get_array_index (property_name_p);
504
505 if (JERRY_LIKELY (index != ECMA_STRING_NOT_ARRAY_INDEX))
506 {
507 if (JERRY_LIKELY (index < ext_object_p->u.array.length))
508 {
509 ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, object_p->u1.property_list_cp);
510
511 return (ecma_is_value_array_hole (values_p[index]) ? ECMA_VALUE_NOT_FOUND
512 : ecma_fast_copy_value (values_p[index]));
513 }
514 }
515 return ECMA_VALUE_NOT_FOUND;
516 }
517
518 break;
519 }
520 case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
521 {
522 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
523
524 if (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS)
525 {
526 uint32_t index = ecma_string_get_array_index (property_name_p);
527
528 if (index != ECMA_STRING_NOT_ARRAY_INDEX
529 && index < ext_object_p->u.pseudo_array.u1.length)
530 {
531 ecma_value_t *arg_Literal_p = (ecma_value_t *) (ext_object_p + 1);
532
533 if (arg_Literal_p[index] != ECMA_VALUE_EMPTY)
534 {
535 ecma_string_t *arg_name_p = ecma_get_string_from_value (arg_Literal_p[index]);
536
537 ecma_object_t *lex_env_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
538 ext_object_p->u.pseudo_array.u2.lex_env_cp);
539
540 JERRY_ASSERT (lex_env_p != NULL
541 && ecma_is_lexical_environment (lex_env_p));
542
543 return ecma_op_get_binding_value (lex_env_p, arg_name_p, true);
544 }
545 }
546 }
547 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
548 /* ES2015 9.4.5.4 */
549 if (ecma_object_is_typedarray (object_p))
550 {
551 #if ENABLED (JERRY_ES2015)
552 if (ecma_prop_name_is_symbol (property_name_p))
553 {
554 break;
555 }
556 #endif /* ENABLED (JERRY_ES2015) */
557
558 uint32_t array_index = ecma_string_get_array_index (property_name_p);
559
560 if (array_index != ECMA_STRING_NOT_ARRAY_INDEX)
561 {
562 ecma_typedarray_info_t info = ecma_typedarray_get_info (object_p);
563
564 if (array_index >= info.length)
565 {
566 return ECMA_VALUE_UNDEFINED;
567 }
568
569 ecma_length_t byte_pos = array_index << info.shift;
570 ecma_number_t num = ecma_get_typedarray_element (info.buffer_p + byte_pos, info.id);
571 return ecma_make_number_value (num);
572 }
573
574 ecma_number_t num = ecma_string_to_number (property_name_p);
575 ecma_string_t *num_to_str = ecma_new_ecma_string_from_number (num);
576
577 if (ecma_compare_ecma_strings (property_name_p, num_to_str))
578 {
579 ecma_deref_ecma_string (num_to_str);
580
581 return ECMA_VALUE_UNDEFINED;
582 }
583
584 ecma_deref_ecma_string (num_to_str);
585 }
586 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
587
588 break;
589 }
590 default:
591 {
592 break;
593 }
594 }
595
596 ecma_property_t *property_p = ecma_find_named_property (object_p, property_name_p);
597
598 if (property_p == NULL)
599 {
600 if (ecma_get_object_is_builtin (object_p))
601 {
602 if (type == ECMA_OBJECT_TYPE_FUNCTION && ecma_builtin_function_is_routine (object_p))
603 {
604 property_p = ecma_builtin_routine_try_to_instantiate_property (object_p, property_name_p);
605 }
606 else
607 {
608 property_p = ecma_builtin_try_to_instantiate_property (object_p, property_name_p);
609 }
610 }
611 else if (type == ECMA_OBJECT_TYPE_FUNCTION)
612 {
613 #if !ENABLED (JERRY_ES2015)
614 if (ecma_string_is_length (property_name_p))
615 {
616 /* Get length virtual property. */
617 ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) object_p;
618 const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
619
620 uint32_t len;
621 if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
622 {
623 cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_data_p;
624 len = args_p->argument_end;
625 }
626 else
627 {
628 cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_data_p;
629 len = args_p->argument_end;
630 }
631
632 return ecma_make_uint32_value (len);
633 }
634 #endif /* !ENABLED (JERRY_ES2015) */
635
636 /* Get prototype physical property. */
637 property_p = ecma_op_function_try_to_lazy_instantiate_property (object_p, property_name_p);
638 }
639 else if (type == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION)
640 {
641 property_p = ecma_op_external_function_try_to_lazy_instantiate_property (object_p, property_name_p);
642 }
643 else if (type == ECMA_OBJECT_TYPE_BOUND_FUNCTION)
644 {
645 property_p = ecma_op_bound_function_try_to_lazy_instantiate_property (object_p, property_name_p);
646 }
647
648 if (property_p == NULL)
649 {
650 return ECMA_VALUE_NOT_FOUND;
651 }
652 }
653
654 ecma_property_value_t *prop_value_p = ECMA_PROPERTY_VALUE_PTR (property_p);
655
656 if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA)
657 {
658 return ecma_fast_copy_value (prop_value_p->value);
659 }
660
661 JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR);
662
663 ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (prop_value_p);
664
665 if (get_set_pair_p->getter_cp == JMEM_CP_NULL)
666 {
667 return ECMA_VALUE_UNDEFINED;
668 }
669
670 ecma_object_t *getter_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->getter_cp);
671
672 return ecma_op_function_call (getter_p, base_value, NULL, 0);
673 } /* ecma_op_object_find_own */
674
675 /**
676 * Search the value corresponding to an uint32_t property index
677 *
678 * Note: this method falls back to the general ecma_op_object_find
679 *
680 * @return ecma value if property is found
681 * ECMA_VALUE_NOT_FOUND if property is not found
682 * Returned value must be freed with ecma_free_value
683 */
684 ecma_value_t
ecma_op_object_find_by_uint32_index(ecma_object_t * object_p,uint32_t index)685 ecma_op_object_find_by_uint32_index (ecma_object_t *object_p, /**< the object */
686 uint32_t index) /**< property index */
687 {
688 if (JERRY_LIKELY (index <= ECMA_DIRECT_STRING_MAX_IMM))
689 {
690 return ecma_op_object_find (object_p, ECMA_CREATE_DIRECT_UINT32_STRING (index));
691 }
692
693 ecma_string_t *index_str_p = ecma_new_non_direct_string_from_uint32 (index);
694 ecma_value_t ret_value = ecma_op_object_find (object_p, index_str_p);
695 ecma_deref_ecma_string (index_str_p);
696
697 return ret_value;
698 } /* ecma_op_object_find_by_uint32_index */
699
700 /**
701 * Search the value corresponding to an ecma_number_t property index
702 *
703 * Note: this method falls back to the general ecma_op_object_find
704 *
705 * @return ecma value if property is found
706 * ECMA_VALUE_NOT_FOUND if property is not found
707 * Returned value must be freed with ecma_free_value
708 */
709 ecma_value_t
ecma_op_object_find_by_number_index(ecma_object_t * object_p,ecma_number_t index)710 ecma_op_object_find_by_number_index (ecma_object_t *object_p, /**< the object */
711 ecma_number_t index) /**< property index */
712 {
713 ecma_string_t *index_str_p = ecma_new_ecma_string_from_number (index);
714 ecma_value_t ret_value = ecma_op_object_find (object_p, index_str_p);
715 ecma_deref_ecma_string (index_str_p);
716
717 return ret_value;
718 } /* ecma_op_object_find_by_number_index */
719
720 /**
721 * Search the value corresponding to a property name
722 *
723 * Note: search includes prototypes
724 *
725 * @return ecma value if property is found
726 * ECMA_VALUE_NOT_FOUND if property is not found
727 * Returned value must be freed with ecma_free_value
728 */
729 ecma_value_t
ecma_op_object_find(ecma_object_t * object_p,ecma_string_t * property_name_p)730 ecma_op_object_find (ecma_object_t *object_p, /**< the object */
731 ecma_string_t *property_name_p) /**< property name */
732 {
733 ecma_value_t base_value = ecma_make_object_value (object_p);
734
735 while (true)
736 {
737 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
738 if (ECMA_OBJECT_IS_PROXY (object_p))
739 {
740 return ecma_proxy_object_find (object_p, property_name_p);
741 }
742 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
743
744 ecma_value_t value = ecma_op_object_find_own (base_value, object_p, property_name_p);
745
746 if (ecma_is_value_found (value))
747 {
748 return value;
749 }
750
751 if (object_p->u2.prototype_cp == JMEM_CP_NULL)
752 {
753 break;
754 }
755
756 object_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, object_p->u2.prototype_cp);
757 }
758
759 return ECMA_VALUE_NOT_FOUND;
760 } /* ecma_op_object_find */
761
762 /**
763 * Get own property by name
764 *
765 * Note: property must be an existing data property
766 *
767 * @return ecma value
768 * Returned value must be freed with ecma_free_value
769 */
770 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_object_get_own_data_prop(ecma_object_t * object_p,ecma_string_t * property_name_p)771 ecma_op_object_get_own_data_prop (ecma_object_t *object_p, /**< the object */
772 ecma_string_t *property_name_p) /**< property name */
773 {
774 JERRY_ASSERT (ecma_is_lexical_environment (object_p)
775 || !ecma_op_object_is_fast_array (object_p));
776
777 ecma_value_t result = ecma_op_object_find_own (ecma_make_object_value (object_p),
778 object_p,
779 property_name_p);
780
781 #ifndef JERRY_NDEBUG
782 /* Because ecma_op_object_find_own might create a property
783 * this check is executed after the function return. */
784 ecma_property_t *property_p = ecma_find_named_property (object_p,
785 property_name_p);
786
787 JERRY_ASSERT (property_p != NULL
788 && ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA
789 && !ecma_is_property_configurable (*property_p));
790 #endif /* !JERRY_NDEBUG */
791
792 return result;
793 } /* ecma_op_object_get_own_data_prop */
794
795 /**
796 * [[Get]] operation of ecma object
797 *
798 * This function returns the value of a named property, or undefined
799 * if the property is not found in the prototype chain. If the property
800 * is an accessor, it calls the "get" callback function and returns
801 * with its result (including error throws).
802 *
803 * See also:
804 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
805 *
806 * @return ecma value
807 * Returned value must be freed with ecma_free_value
808 */
809 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_object_get(ecma_object_t * object_p,ecma_string_t * property_name_p)810 ecma_op_object_get (ecma_object_t *object_p, /**< the object */
811 ecma_string_t *property_name_p) /**< property name */
812 {
813 return ecma_op_object_get_with_receiver (object_p, property_name_p, ecma_make_object_value (object_p));
814 } /* ecma_op_object_get */
815
816 /**
817 * [[Get]] operation of ecma object with the specified receiver
818 *
819 * This function returns the value of a named property, or undefined
820 * if the property is not found in the prototype chain. If the property
821 * is an accessor, it calls the "get" callback function and returns
822 * with its result (including error throws).
823 *
824 * See also:
825 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
826 *
827 * @return ecma value
828 * Returned value must be freed with ecma_free_value
829 */
830 ecma_value_t
ecma_op_object_get_with_receiver(ecma_object_t * object_p,ecma_string_t * property_name_p,ecma_value_t receiver)831 ecma_op_object_get_with_receiver (ecma_object_t *object_p, /**< the object */
832 ecma_string_t *property_name_p, /**< property name */
833 ecma_value_t receiver) /**< receiver to invoke getter function */
834 {
835 while (true)
836 {
837 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
838 if (ECMA_OBJECT_IS_PROXY (object_p))
839 {
840 return ecma_proxy_object_get (object_p, property_name_p, receiver);
841 }
842 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
843
844 ecma_value_t value = ecma_op_object_find_own (receiver, object_p, property_name_p);
845
846 if (ecma_is_value_found (value))
847 {
848 return value;
849 }
850
851 jmem_cpointer_t proto_cp = ecma_op_ordinary_object_get_prototype_of (object_p);
852
853 if (proto_cp == JMEM_CP_NULL)
854 {
855 break;
856 }
857
858 object_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, proto_cp);
859 }
860
861 return ECMA_VALUE_UNDEFINED;
862 } /* ecma_op_object_get_with_receiver */
863
864 /**
865 * [[Get]] operation of ecma object specified for uint32_t property index
866 *
867 * @return ecma value
868 * Returned value must be freed with ecma_free_value
869 */
870 ecma_value_t
ecma_op_object_get_by_uint32_index(ecma_object_t * object_p,uint32_t index)871 ecma_op_object_get_by_uint32_index (ecma_object_t *object_p, /**< the object */
872 uint32_t index) /**< property index */
873 {
874 if (JERRY_LIKELY (index <= ECMA_DIRECT_STRING_MAX_IMM))
875 {
876 return ecma_op_object_get (object_p, ECMA_CREATE_DIRECT_UINT32_STRING (index));
877 }
878
879 ecma_string_t *index_str_p = ecma_new_non_direct_string_from_uint32 (index);
880 ecma_value_t ret_value = ecma_op_object_get (object_p, index_str_p);
881 ecma_deref_ecma_string (index_str_p);
882
883 return ret_value;
884 } /* ecma_op_object_get_by_uint32_index */
885
886 /**
887 * Perform ToLength(O.[[Get]]("length")) operation
888 *
889 * The property is converted to uint32 during the operation
890 *
891 * @return ECMA_VALUE_ERROR - if there was any error during the operation
892 * ECMA_VALUE_EMPTY - otherwise
893 */
894 ecma_value_t
ecma_op_object_get_length(ecma_object_t * object_p,uint32_t * length_p)895 ecma_op_object_get_length (ecma_object_t *object_p, /**< the object */
896 uint32_t *length_p) /**< [out] length value converted to uint32 */
897 {
898 if (JERRY_LIKELY (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_ARRAY))
899 {
900 *length_p = ecma_array_get_length (object_p);
901 return ECMA_VALUE_EMPTY;
902 }
903
904 ecma_value_t len_value = ecma_op_object_get_by_magic_id (object_p, LIT_MAGIC_STRING_LENGTH);
905 ecma_value_t len_number = ecma_op_to_length (len_value, length_p);
906 ecma_free_value (len_value);
907
908 JERRY_ASSERT (ECMA_IS_VALUE_ERROR (len_number) || ecma_is_value_empty (len_number));
909
910 return len_number;
911 } /* ecma_op_object_get_length */
912
913 /**
914 * [[Get]] operation of ecma object where the property name is a magic string
915 *
916 * This function returns the value of a named property, or undefined
917 * if the property is not found in the prototype chain. If the property
918 * is an accessor, it calls the "get" callback function and returns
919 * with its result (including error throws).
920 *
921 * See also:
922 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
923 *
924 * @return ecma value
925 * Returned value must be freed with ecma_free_value
926 */
927 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_object_get_by_magic_id(ecma_object_t * object_p,lit_magic_string_id_t property_id)928 ecma_op_object_get_by_magic_id (ecma_object_t *object_p, /**< the object */
929 lit_magic_string_id_t property_id) /**< property magic string id */
930 {
931 return ecma_op_object_get (object_p, ecma_get_magic_string (property_id));
932 } /* ecma_op_object_get_by_magic_id */
933
934 #if ENABLED (JERRY_ES2015)
935 /**
936 * [[Get]] a well-known symbol by the given property id
937 *
938 * @return pointer to the requested well-known symbol
939 */
940 ecma_string_t *
ecma_op_get_global_symbol(lit_magic_string_id_t property_id)941 ecma_op_get_global_symbol (lit_magic_string_id_t property_id) /**< property symbol id */
942 {
943 ecma_value_t symbol_value = ecma_op_object_get_by_magic_id (ecma_builtin_get (ECMA_BUILTIN_ID_INTRINSIC_OBJECT),
944 property_id);
945 JERRY_ASSERT (ecma_is_value_symbol (symbol_value));
946
947 return ecma_get_symbol_from_value (symbol_value);
948 } /* ecma_op_get_global_symbol */
949
950 /**
951 * [[Get]] operation of ecma object where the property is a well-known symbol
952 *
953 * @return ecma value
954 * Returned value must be freed with ecma_free_value
955 */
956 ecma_value_t
ecma_op_object_get_by_symbol_id(ecma_object_t * object_p,lit_magic_string_id_t property_id)957 ecma_op_object_get_by_symbol_id (ecma_object_t *object_p, /**< the object */
958 lit_magic_string_id_t property_id) /**< property symbol id */
959 {
960 ecma_string_t *symbol_p = ecma_op_get_global_symbol (property_id);
961 ecma_value_t ret_value = ecma_op_object_get (object_p, symbol_p);
962 ecma_deref_ecma_string (symbol_p);
963
964 return ret_value;
965 } /* ecma_op_object_get_by_symbol_id */
966
967 /**
968 * GetMethod operation
969 *
970 * See also: ECMA-262 v6, 7.3.9
971 *
972 * Note:
973 * Returned value must be freed with ecma_free_value.
974 *
975 * @return iterator function object - if success
976 * raised error - otherwise
977 */
978 static ecma_value_t
ecma_op_get_method(ecma_value_t value,ecma_string_t * prop_name_p)979 ecma_op_get_method (ecma_value_t value, /**< ecma value */
980 ecma_string_t *prop_name_p) /** property name */
981 {
982 /* 2. */
983 ecma_value_t obj_value = ecma_op_to_object (value);
984
985 if (ECMA_IS_VALUE_ERROR (obj_value))
986 {
987 return obj_value;
988 }
989
990 ecma_object_t *obj_p = ecma_get_object_from_value (obj_value);
991 ecma_value_t func;
992
993 func = ecma_op_object_get (obj_p, prop_name_p);
994 ecma_deref_object (obj_p);
995
996 /* 3. */
997 if (ECMA_IS_VALUE_ERROR (func))
998 {
999 return func;
1000 }
1001
1002 /* 4. */
1003 if (ecma_is_value_undefined (func) || ecma_is_value_null (func))
1004 {
1005 return ECMA_VALUE_UNDEFINED;
1006 }
1007
1008 /* 5. */
1009 if (!ecma_op_is_callable (func))
1010 {
1011 ecma_free_value (func);
1012 return ecma_raise_type_error (ECMA_ERR_MSG ("Iterator is not callable."));
1013 }
1014
1015 /* 6. */
1016 return func;
1017 } /* ecma_op_get_method */
1018
1019 /**
1020 * GetMethod operation when the property is a well-known symbol
1021 *
1022 * See also: ECMA-262 v6, 7.3.9
1023 *
1024 * Note:
1025 * Returned value must be freed with ecma_free_value.
1026 *
1027 * @return iterator function object - if success
1028 * raised error - otherwise
1029 */
1030 ecma_value_t
ecma_op_get_method_by_symbol_id(ecma_value_t value,lit_magic_string_id_t symbol_id)1031 ecma_op_get_method_by_symbol_id (ecma_value_t value, /**< ecma value */
1032 lit_magic_string_id_t symbol_id) /**< property symbol id */
1033 {
1034 ecma_string_t *prop_name_p = ecma_op_get_global_symbol (symbol_id);
1035 ecma_value_t ret_value = ecma_op_get_method (value, prop_name_p);
1036 ecma_deref_ecma_string (prop_name_p);
1037
1038 return ret_value;
1039 } /* ecma_op_get_method_by_symbol_id */
1040
1041 /**
1042 * GetMethod operation when the property is a magic string
1043 *
1044 * See also: ECMA-262 v6, 7.3.9
1045 *
1046 * Note:
1047 * Returned value must be freed with ecma_free_value.
1048 *
1049 * @return iterator function object - if success
1050 * raised error - otherwise
1051 */
1052 ecma_value_t
ecma_op_get_method_by_magic_id(ecma_value_t value,lit_magic_string_id_t magic_id)1053 ecma_op_get_method_by_magic_id (ecma_value_t value, /**< ecma value */
1054 lit_magic_string_id_t magic_id) /**< property magic id */
1055 {
1056 return ecma_op_get_method (value, ecma_get_magic_string (magic_id));
1057 } /* ecma_op_get_method_by_magic_id */
1058 #endif /* ENABLED (JERRY_ES2015) */
1059
1060 /**
1061 * [[Put]] ecma general object's operation specialized for uint32_ property index
1062 *
1063 * Note: This function falls back to the general ecma_op_object_put
1064 *
1065 * @return ecma value
1066 * The returned value must be freed with ecma_free_value.
1067 */
1068 ecma_value_t
ecma_op_object_put_by_uint32_index(ecma_object_t * object_p,uint32_t index,ecma_value_t value,bool is_throw)1069 ecma_op_object_put_by_uint32_index (ecma_object_t *object_p, /**< the object */
1070 uint32_t index, /**< property index */
1071 ecma_value_t value, /**< ecma value */
1072 bool is_throw) /**< flag that controls failure handling */
1073 {
1074 if (JERRY_LIKELY (index <= ECMA_DIRECT_STRING_MAX_IMM))
1075 {
1076 return ecma_op_object_put (object_p,
1077 ECMA_CREATE_DIRECT_UINT32_STRING (index),
1078 value,
1079 is_throw);
1080 }
1081
1082 ecma_string_t *index_str_p = ecma_new_non_direct_string_from_uint32 (index);
1083 ecma_value_t ret_value = ecma_op_object_put (object_p, index_str_p, value, is_throw);
1084 ecma_deref_ecma_string (index_str_p);
1085
1086 return ret_value;
1087 } /* ecma_op_object_put_by_uint32_index */
1088
1089 /**
1090 * [[Put]] ecma general object's operation specialized for ecma_number_ property index
1091 *
1092 * Note: This function falls back to the general ecma_op_object_put
1093 *
1094 * @return ecma value
1095 * The returned value must be freed with ecma_free_value.
1096 */
1097 ecma_value_t
ecma_op_object_put_by_number_index(ecma_object_t * object_p,ecma_number_t index,ecma_value_t value,bool is_throw)1098 ecma_op_object_put_by_number_index (ecma_object_t *object_p, /**< the object */
1099 ecma_number_t index, /**< property index */
1100 ecma_value_t value, /**< ecma value */
1101 bool is_throw) /**< flag that controls failure handling */
1102 {
1103 ecma_string_t *index_str_p = ecma_new_ecma_string_from_number (index);
1104 ecma_value_t ret_value = ecma_op_object_put (object_p, index_str_p, value, is_throw);
1105 ecma_deref_ecma_string (index_str_p);
1106
1107 return ret_value;
1108 } /* ecma_op_object_put_by_number_index */
1109
1110 /**
1111 * [[Put]] ecma general object's operation
1112 *
1113 * See also:
1114 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
1115 * ECMA-262 v5, 8.12.5
1116 * Also incorporates [[CanPut]] ECMA-262 v5, 8.12.4
1117 *
1118 * @return ecma value
1119 * The returned value must be freed with ecma_free_value.
1120 *
1121 * Returns with ECMA_VALUE_TRUE if the operation is
1122 * successful. Otherwise it returns with an error object
1123 * or ECMA_VALUE_FALSE.
1124 *
1125 * Note: even if is_throw is false, the setter can throw an
1126 * error, and this function returns with that error.
1127 */
1128 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_object_put(ecma_object_t * object_p,ecma_string_t * property_name_p,ecma_value_t value,bool is_throw)1129 ecma_op_object_put (ecma_object_t *object_p, /**< the object */
1130 ecma_string_t *property_name_p, /**< property name */
1131 ecma_value_t value, /**< ecma value */
1132 bool is_throw) /**< flag that controls failure handling */
1133 {
1134 return ecma_op_object_put_with_receiver (object_p,
1135 property_name_p,
1136 value,
1137 ecma_make_object_value (object_p),
1138 is_throw);
1139 } /* ecma_op_object_put */
1140
1141 #if ENABLED (JERRY_ES2015)
1142 /**
1143 * [[Set]] ( P, V, Receiver) operation part for ordinary objects
1144 *
1145 * See also: ECMAScript v6, 9.19.9
1146 *
1147 * @return ecma value
1148 * The returned value must be freed with ecma_free_value.
1149 */
1150 static ecma_value_t
ecma_op_object_put_apply_receiver(ecma_value_t receiver,ecma_string_t * property_name_p,ecma_value_t value,bool is_throw)1151 ecma_op_object_put_apply_receiver (ecma_value_t receiver, /**< receiver */
1152 ecma_string_t *property_name_p, /**< property name */
1153 ecma_value_t value, /**< value to set */
1154 bool is_throw) /**< flag that controls failure handling */
1155 {
1156 /* 5.b */
1157 if (!ecma_is_value_object (receiver))
1158 {
1159 return ecma_reject (is_throw);
1160 }
1161
1162 ecma_object_t *receiver_obj_p = ecma_get_object_from_value (receiver);
1163
1164 ecma_property_descriptor_t prop_desc;
1165 /* 5.c */
1166 ecma_value_t status = ecma_op_object_get_own_property_descriptor (receiver_obj_p,
1167 property_name_p,
1168 &prop_desc);
1169
1170 /* 5.d */
1171 if (ECMA_IS_VALUE_ERROR (status))
1172 {
1173 return status;
1174 }
1175
1176 /* 5.e */
1177 if (ecma_is_value_true (status))
1178 {
1179 ecma_value_t result;
1180
1181 /* 5.e.i - 5.e.ii */
1182 if (prop_desc.flags & (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED)
1183 || !(prop_desc.flags & ECMA_PROP_IS_WRITABLE))
1184 {
1185 result = ecma_reject (is_throw);
1186 }
1187 else
1188 {
1189 /* 5.e.iii */
1190 JERRY_ASSERT (prop_desc.flags & ECMA_PROP_IS_VALUE_DEFINED);
1191 ecma_free_value (prop_desc.value);
1192 prop_desc.value = ecma_copy_value (value);
1193
1194 /* 5.e.iv */
1195 result = ecma_op_object_define_own_property (receiver_obj_p, property_name_p, &prop_desc);
1196 }
1197
1198 ecma_free_property_descriptor (&prop_desc);
1199
1200 return result;
1201 }
1202
1203 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
1204 if (ECMA_OBJECT_IS_PROXY (receiver_obj_p))
1205 {
1206 ecma_property_descriptor_t desc;
1207 desc.flags = ECMA_NAME_DATA_PROPERTY_DESCRIPTOR_BITS;
1208 desc.value = value;
1209 return ecma_proxy_object_define_own_property (receiver_obj_p, property_name_p, &desc);
1210 }
1211 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
1212
1213 if (JERRY_UNLIKELY (ecma_op_object_is_fast_array (receiver_obj_p)))
1214 {
1215 ecma_fast_array_convert_to_normal (receiver_obj_p);
1216 }
1217
1218 /* 5.f.i */
1219 ecma_property_value_t *new_prop_value_p;
1220 new_prop_value_p = ecma_create_named_data_property (receiver_obj_p,
1221 property_name_p,
1222 ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
1223 NULL);
1224 JERRY_ASSERT (ecma_is_value_undefined (new_prop_value_p->value));
1225 new_prop_value_p->value = ecma_copy_value_if_not_object (value);
1226
1227 return ECMA_VALUE_TRUE;
1228 } /* ecma_op_object_put_apply_receiver */
1229 #endif /* ENABLED (JERRY_ES2015) */
1230
1231 /**
1232 * [[Put]] ecma general object's operation with given receiver
1233 *
1234 * See also:
1235 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
1236 * ECMA-262 v5, 8.12.5
1237 * ECMA-262 v6, 9.1.9
1238 * Also incorporates [[CanPut]] ECMA-262 v5, 8.12.4
1239 *
1240 * @return ecma value
1241 * The returned value must be freed with ecma_free_value.
1242 *
1243 * Returns with ECMA_VALUE_TRUE if the operation is
1244 * successful. Otherwise it returns with an error object
1245 * or ECMA_VALUE_FALSE.
1246 *
1247 * Note: even if is_throw is false, the setter can throw an
1248 * error, and this function returns with that error.
1249 */
1250 ecma_value_t
ecma_op_object_put_with_receiver(ecma_object_t * object_p,ecma_string_t * property_name_p,ecma_value_t value,ecma_value_t receiver,bool is_throw)1251 ecma_op_object_put_with_receiver (ecma_object_t *object_p, /**< the object */
1252 ecma_string_t *property_name_p, /**< property name */
1253 ecma_value_t value, /**< ecma value */
1254 ecma_value_t receiver, /**< receiver */
1255 bool is_throw) /**< flag that controls failure handling */
1256 {
1257 JERRY_ASSERT (object_p != NULL
1258 && !ecma_is_lexical_environment (object_p));
1259 JERRY_ASSERT (property_name_p != NULL);
1260
1261 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
1262 if (ECMA_OBJECT_IS_PROXY (object_p))
1263 {
1264 return ecma_proxy_object_set (object_p, property_name_p, value, receiver);
1265 }
1266 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
1267
1268 #if defined(JERRY_FUNCTION_NAME) && !defined(__APPLE__)
1269 if (ecma_is_value_object(value)) {
1270 ecma_object_t* obj = ecma_get_object_from_value(value);
1271 if (ecma_get_object_type(obj) == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION) {
1272 ecma_string_t* property_name = ecma_get_magic_string (LIT_MAGIC_STRING_NAME);
1273 if (ecma_find_named_property (obj, property_name) == NULL) {
1274 ecma_property_value_t* prop_val = ecma_create_named_data_property(obj,
1275 property_name,
1276 ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
1277 NULL);
1278 prop_val->value = ecma_copy_value(ecma_make_string_value(property_name_p));
1279 } else {
1280 ecma_deref_ecma_string (property_name);
1281 }
1282 }
1283 }
1284 #endif
1285
1286 ecma_object_type_t type = ecma_get_object_type (object_p);
1287
1288 switch (type)
1289 {
1290 case ECMA_OBJECT_TYPE_ARRAY:
1291 {
1292 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
1293
1294 if (ecma_string_is_length (property_name_p))
1295 {
1296 if (ecma_is_property_writable (ext_object_p->u.array.u.length_prop))
1297 {
1298 return ecma_op_array_object_set_length (object_p, value, 0);
1299 }
1300
1301 return ecma_reject (is_throw);
1302 }
1303
1304 if (JERRY_LIKELY (ecma_op_array_is_fast_array (ext_object_p)))
1305 {
1306 if (JERRY_UNLIKELY (!ecma_op_ordinary_object_is_extensible (object_p)))
1307 {
1308 return ecma_reject (is_throw);
1309 }
1310
1311 uint32_t index = ecma_string_get_array_index (property_name_p);
1312
1313 if (JERRY_UNLIKELY (index == ECMA_STRING_NOT_ARRAY_INDEX))
1314 {
1315 ecma_fast_array_convert_to_normal (object_p);
1316 }
1317 else if (ecma_fast_array_set_property (object_p, index, value))
1318 {
1319 return ECMA_VALUE_TRUE;
1320 }
1321 }
1322
1323 JERRY_ASSERT (!ecma_op_object_is_fast_array (object_p));
1324
1325 break;
1326 }
1327 case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
1328 {
1329 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
1330
1331 if (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS)
1332 {
1333 uint32_t index = ecma_string_get_array_index (property_name_p);
1334
1335 if (index != ECMA_STRING_NOT_ARRAY_INDEX
1336 && index < ext_object_p->u.pseudo_array.u1.length)
1337 {
1338 ecma_value_t *arg_Literal_p = (ecma_value_t *) (ext_object_p + 1);
1339
1340 if (arg_Literal_p[index] != ECMA_VALUE_EMPTY)
1341 {
1342 ecma_string_t *arg_name_p = ecma_get_string_from_value (arg_Literal_p[index]);
1343
1344 ecma_object_t *lex_env_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
1345 ext_object_p->u.pseudo_array.u2.lex_env_cp);
1346
1347 JERRY_ASSERT (lex_env_p != NULL
1348 && ecma_is_lexical_environment (lex_env_p));
1349
1350 ecma_op_set_mutable_binding (lex_env_p, arg_name_p, value, true);
1351 return ECMA_VALUE_TRUE;
1352 }
1353 }
1354 }
1355 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
1356 /* ES2015 9.4.5.5 */
1357 if (ecma_object_is_typedarray (object_p))
1358 {
1359 #if ENABLED (JERRY_ES2015)
1360 if (ecma_prop_name_is_symbol (property_name_p))
1361 {
1362 break;
1363 }
1364 #endif /* ENABLED (JERRY_ES2015) */
1365
1366 uint32_t array_index = ecma_string_get_array_index (property_name_p);
1367
1368 if (array_index != ECMA_STRING_NOT_ARRAY_INDEX)
1369 {
1370 ecma_number_t num_var;
1371 ecma_value_t error = ecma_get_number (value, &num_var);
1372
1373 if (ECMA_IS_VALUE_ERROR (error))
1374 {
1375 jcontext_release_exception ();
1376 return ecma_reject (is_throw);
1377 }
1378
1379 ecma_typedarray_info_t info = ecma_typedarray_get_info (object_p);
1380
1381 if (array_index >= info.length)
1382 {
1383 return ecma_reject (is_throw);
1384 }
1385
1386 ecma_length_t byte_pos = array_index << info.shift;
1387 ecma_set_typedarray_element (info.buffer_p + byte_pos, num_var, info.id);
1388
1389 return ECMA_VALUE_TRUE;
1390 }
1391
1392 ecma_number_t num = ecma_string_to_number (property_name_p);
1393 ecma_string_t *num_to_str = ecma_new_ecma_string_from_number (num);
1394
1395 if (ecma_compare_ecma_strings (property_name_p, num_to_str))
1396 {
1397 ecma_deref_ecma_string (num_to_str);
1398
1399 return ecma_reject (is_throw);
1400 }
1401
1402 ecma_deref_ecma_string (num_to_str);
1403 }
1404 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
1405 break;
1406 }
1407 default:
1408 {
1409 break;
1410 }
1411 }
1412
1413 ecma_property_t *property_p = ecma_find_named_property (object_p, property_name_p);
1414
1415 if (property_p == NULL)
1416 {
1417 if (type == ECMA_OBJECT_TYPE_CLASS)
1418 {
1419 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
1420
1421 if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL)
1422 {
1423 uint32_t index = ecma_string_get_array_index (property_name_p);
1424
1425 if (index != ECMA_STRING_NOT_ARRAY_INDEX)
1426 {
1427 ecma_value_t prim_value_p = ext_object_p->u.class_prop.u.value;
1428 ecma_string_t *prim_value_str_p = ecma_get_string_from_value (prim_value_p);
1429
1430 if (index < ecma_string_get_length (prim_value_str_p))
1431 {
1432 return ecma_reject (is_throw);
1433 }
1434 }
1435 }
1436 }
1437
1438 if (ecma_get_object_is_builtin (object_p))
1439 {
1440 if (type == ECMA_OBJECT_TYPE_FUNCTION && ecma_builtin_function_is_routine (object_p))
1441 {
1442 property_p = ecma_builtin_routine_try_to_instantiate_property (object_p, property_name_p);
1443 }
1444 else
1445 {
1446 property_p = ecma_builtin_try_to_instantiate_property (object_p, property_name_p);
1447 }
1448 }
1449 else if (type == ECMA_OBJECT_TYPE_FUNCTION)
1450 {
1451 #if ENABLED (JERRY_ES2015)
1452 /* Uninitialized 'length' property is non-writable (ECMA-262 v6, 19.2.4.1) */
1453 if ((ecma_string_is_length (property_name_p))
1454 && (!ECMA_GET_FIRST_BIT_FROM_POINTER_TAG (((ecma_extended_object_t *) object_p)->u.function.scope_cp)))
1455 {
1456 return ecma_reject (is_throw);
1457 }
1458 #else /* !ENABLED (JERRY_ES2015) */
1459 if (ecma_string_is_length (property_name_p))
1460 {
1461 return ecma_reject (is_throw);
1462 }
1463 #endif /* ENABLED (JERRY_ES2015) */
1464
1465 /* Get prototype physical property. */
1466 property_p = ecma_op_function_try_to_lazy_instantiate_property (object_p, property_name_p);
1467 }
1468 else if (type == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION)
1469 {
1470 property_p = ecma_op_external_function_try_to_lazy_instantiate_property (object_p, property_name_p);
1471 }
1472 else if (type == ECMA_OBJECT_TYPE_BOUND_FUNCTION)
1473 {
1474 property_p = ecma_op_bound_function_try_to_lazy_instantiate_property (object_p, property_name_p);
1475 }
1476 }
1477
1478 jmem_cpointer_t setter_cp = JMEM_CP_NULL;
1479
1480 if (property_p != NULL)
1481 {
1482 if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA)
1483 {
1484 if (ecma_is_property_writable (*property_p))
1485 {
1486 #if ENABLED (JERRY_ES2015)
1487 if (ecma_make_object_value (object_p) != receiver)
1488 {
1489 return ecma_op_object_put_apply_receiver (receiver, property_name_p, value, is_throw);
1490 }
1491 #endif /* ENABLED (JERRY_ES2015) */
1492
1493 /* There is no need for special casing arrays here because changing the
1494 * value of an existing property never changes the length of an array. */
1495 ecma_named_data_property_assign_value (object_p,
1496 ECMA_PROPERTY_VALUE_PTR (property_p),
1497 value);
1498 return ECMA_VALUE_TRUE;
1499 }
1500 }
1501 else
1502 {
1503 JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR);
1504
1505 ecma_getter_setter_pointers_t *get_set_pair_p;
1506 get_set_pair_p = ecma_get_named_accessor_property (ECMA_PROPERTY_VALUE_PTR (property_p));
1507 setter_cp = get_set_pair_p->setter_cp;
1508 }
1509 }
1510 else
1511 {
1512 bool create_new_property = true;
1513
1514 jmem_cpointer_t proto_cp = ecma_op_ordinary_object_get_prototype_of (object_p);
1515
1516 if (proto_cp != JMEM_CP_NULL)
1517 {
1518 ecma_property_ref_t property_ref = { NULL };
1519 ecma_object_t *proto_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, proto_cp);
1520
1521 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
1522 if (ECMA_OBJECT_IS_PROXY (proto_p))
1523 {
1524 return ecma_op_object_put_with_receiver (proto_p,
1525 property_name_p,
1526 value,
1527 receiver,
1528 is_throw);
1529 }
1530 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
1531
1532 ecma_property_t inherited_property = ecma_op_object_get_property (proto_p,
1533 property_name_p,
1534 &property_ref,
1535 ECMA_PROPERTY_GET_NO_OPTIONS);
1536
1537 if (inherited_property != ECMA_PROPERTY_TYPE_NOT_FOUND)
1538 {
1539 if (ECMA_PROPERTY_GET_TYPE (inherited_property) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR)
1540 {
1541 setter_cp = ecma_get_named_accessor_property (property_ref.value_p)->setter_cp;
1542 create_new_property = false;
1543 }
1544 else
1545 {
1546 create_new_property = ecma_is_property_writable (inherited_property);
1547 }
1548 }
1549 }
1550
1551 if (create_new_property
1552 && ecma_op_ordinary_object_is_extensible (object_p))
1553 {
1554 const ecma_object_type_t obj_type = ecma_get_object_type (object_p);
1555
1556 if (obj_type == ECMA_OBJECT_TYPE_PSEUDO_ARRAY)
1557 {
1558 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
1559
1560 if (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS)
1561 {
1562 return ecma_builtin_helper_def_prop (object_p,
1563 property_name_p,
1564 value,
1565 ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE | ECMA_IS_THROW);
1566 }
1567 }
1568
1569 uint32_t index = ecma_string_get_array_index (property_name_p);
1570
1571 if (obj_type == ECMA_OBJECT_TYPE_ARRAY
1572 && index != ECMA_STRING_NOT_ARRAY_INDEX)
1573 {
1574 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
1575
1576 if (index < UINT32_MAX
1577 && index >= ext_object_p->u.array.length)
1578 {
1579 if (!ecma_is_property_writable (ext_object_p->u.array.u.length_prop))
1580 {
1581 return ecma_reject (is_throw);
1582 }
1583
1584 ext_object_p->u.array.length = index + 1;
1585 }
1586 }
1587
1588 #if ENABLED (JERRY_ES2015)
1589 return ecma_op_object_put_apply_receiver (receiver, property_name_p, value, is_throw);
1590 #endif /* ENABLED (JERRY_ES2015) */
1591
1592 ecma_property_value_t *new_prop_value_p;
1593 new_prop_value_p = ecma_create_named_data_property (object_p,
1594 property_name_p,
1595 ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
1596 NULL);
1597
1598 JERRY_ASSERT (ecma_is_value_undefined (new_prop_value_p->value));
1599 new_prop_value_p->value = ecma_copy_value_if_not_object (value);
1600 return ECMA_VALUE_TRUE;
1601 }
1602 }
1603
1604 if (setter_cp == JMEM_CP_NULL)
1605 {
1606 return ecma_reject (is_throw);
1607 }
1608
1609 ecma_value_t ret_value = ecma_op_function_call (ECMA_GET_NON_NULL_POINTER (ecma_object_t, setter_cp),
1610 receiver,
1611 &value,
1612 1);
1613
1614 if (!ECMA_IS_VALUE_ERROR (ret_value))
1615 {
1616 ecma_fast_free_value (ret_value);
1617 ret_value = ECMA_VALUE_TRUE;
1618 }
1619
1620 return ret_value;
1621 } /* ecma_op_object_put_with_receiver */
1622
1623 /**
1624 * [[Delete]] ecma object's operation specialized for uint32_t property index
1625 *
1626 * Note:
1627 * This method falls back to the general ecma_op_object_delete
1628 *
1629 * @return true - if deleted successfully
1630 * false - or type error otherwise (based in 'is_throw')
1631 */
1632 ecma_value_t
ecma_op_object_delete_by_uint32_index(ecma_object_t * obj_p,uint32_t index,bool is_throw)1633 ecma_op_object_delete_by_uint32_index (ecma_object_t *obj_p, /**< the object */
1634 uint32_t index, /**< property index */
1635 bool is_throw) /**< flag that controls failure handling */
1636 {
1637 if (JERRY_LIKELY (index <= ECMA_DIRECT_STRING_MAX_IMM))
1638 {
1639 return ecma_op_object_delete (obj_p, ECMA_CREATE_DIRECT_UINT32_STRING (index), is_throw);
1640 }
1641
1642 ecma_string_t *index_str_p = ecma_new_non_direct_string_from_uint32 (index);
1643 ecma_value_t ret_value = ecma_op_object_delete (obj_p, index_str_p, is_throw);
1644 ecma_deref_ecma_string (index_str_p);
1645
1646 return ret_value;
1647 } /* ecma_op_object_delete_by_uint32_index */
1648
1649 /**
1650 * [[Delete]] ecma object's operation specialized for ecma_number_t property index
1651 *
1652 * Note:
1653 * This method falls back to the general ecma_op_object_delete
1654 *
1655 * @return true - if deleted successfully
1656 * false - or type error otherwise (based in 'is_throw')
1657 */
1658 ecma_value_t
ecma_op_object_delete_by_number_index(ecma_object_t * obj_p,ecma_number_t index,bool is_throw)1659 ecma_op_object_delete_by_number_index (ecma_object_t *obj_p, /**< the object */
1660 ecma_number_t index, /**< property index */
1661 bool is_throw) /**< flag that controls failure handling */
1662 {
1663 ecma_string_t *index_str_p = ecma_new_ecma_string_from_number (index);
1664 ecma_value_t ret_value = ecma_op_object_delete (obj_p, index_str_p, is_throw);
1665 ecma_deref_ecma_string (index_str_p);
1666
1667 return ret_value;
1668 } /* ecma_op_object_delete_by_number_index */
1669
1670 /**
1671 * [[Delete]] ecma object's operation
1672 *
1673 * See also:
1674 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
1675 *
1676 * Note:
1677 * returned value must be freed with ecma_free_value
1678 *
1679 * @return true - if deleted successfully
1680 * false - or type error otherwise (based in 'is_throw')
1681 */
1682 ecma_value_t
ecma_op_object_delete(ecma_object_t * obj_p,ecma_string_t * property_name_p,bool is_throw)1683 ecma_op_object_delete (ecma_object_t *obj_p, /**< the object */
1684 ecma_string_t *property_name_p, /**< property name */
1685 bool is_throw) /**< flag that controls failure handling */
1686 {
1687 JERRY_ASSERT (obj_p != NULL
1688 && !ecma_is_lexical_environment (obj_p));
1689 JERRY_ASSERT (property_name_p != NULL);
1690
1691 if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_PSEUDO_ARRAY)
1692 {
1693 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
1694
1695 if (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS)
1696 {
1697 return ecma_op_arguments_object_delete (obj_p,
1698 property_name_p,
1699 is_throw);
1700 }
1701 }
1702
1703 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
1704 if (ECMA_OBJECT_IS_PROXY (obj_p))
1705 {
1706 return ecma_proxy_object_delete_property (obj_p, property_name_p);
1707 }
1708 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
1709
1710 JERRY_ASSERT_OBJECT_TYPE_IS_VALID (ecma_get_object_type (obj_p));
1711
1712 return ecma_op_general_object_delete (obj_p,
1713 property_name_p,
1714 is_throw);
1715 } /* ecma_op_object_delete */
1716
1717 /**
1718 * [[DefaultValue]] ecma object's operation
1719 *
1720 * See also:
1721 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
1722 *
1723 * @return ecma value
1724 * Returned value must be freed with ecma_free_value
1725 */
1726 ecma_value_t
ecma_op_object_default_value(ecma_object_t * obj_p,ecma_preferred_type_hint_t hint)1727 ecma_op_object_default_value (ecma_object_t *obj_p, /**< the object */
1728 ecma_preferred_type_hint_t hint) /**< hint on preferred result type */
1729 {
1730 JERRY_ASSERT (obj_p != NULL
1731 && !ecma_is_lexical_environment (obj_p));
1732
1733 JERRY_ASSERT_OBJECT_TYPE_IS_VALID (ecma_get_object_type (obj_p));
1734
1735 /*
1736 * typedef ecma_property_t * (*default_value_ptr_t) (ecma_object_t *, ecma_string_t *);
1737 * static const default_value_ptr_t default_value [ECMA_OBJECT_TYPE__COUNT] =
1738 * {
1739 * [ECMA_OBJECT_TYPE_GENERAL] = &ecma_op_general_object_default_value,
1740 * [ECMA_OBJECT_TYPE_CLASS] = &ecma_op_general_object_default_value,
1741 * [ECMA_OBJECT_TYPE_FUNCTION] = &ecma_op_general_object_default_value,
1742 * [ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION] = &ecma_op_general_object_default_value,
1743 * [ECMA_OBJECT_TYPE_ARRAY] = &ecma_op_general_object_default_value,
1744 * [ECMA_OBJECT_TYPE_BOUND_FUNCTION] = &ecma_op_general_object_default_value,
1745 * [ECMA_OBJECT_TYPE_PSEUDO_ARRAY] = &ecma_op_general_object_default_value
1746 * };
1747 *
1748 * return default_value[type] (obj_p, property_name_p);
1749 */
1750
1751 return ecma_op_general_object_default_value (obj_p, hint);
1752 } /* ecma_op_object_default_value */
1753
1754 /**
1755 * [[DefineOwnProperty]] ecma object's operation
1756 *
1757 * See also:
1758 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 8
1759 *
1760 * @return ecma value
1761 * Returned value must be freed with ecma_free_value
1762 */
1763 ecma_value_t
ecma_op_object_define_own_property(ecma_object_t * obj_p,ecma_string_t * property_name_p,const ecma_property_descriptor_t * property_desc_p)1764 ecma_op_object_define_own_property (ecma_object_t *obj_p, /**< the object */
1765 ecma_string_t *property_name_p, /**< property name */
1766 const ecma_property_descriptor_t *property_desc_p) /**< property
1767 * descriptor */
1768 {
1769 JERRY_ASSERT (obj_p != NULL
1770 && !ecma_is_lexical_environment (obj_p));
1771 JERRY_ASSERT (property_name_p != NULL);
1772
1773 const ecma_object_type_t type = ecma_get_object_type (obj_p);
1774
1775 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
1776 if (ECMA_OBJECT_IS_PROXY (obj_p))
1777 {
1778 return ecma_proxy_object_define_own_property (obj_p, property_name_p, property_desc_p);
1779 }
1780 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
1781
1782 switch (type)
1783 {
1784 case ECMA_OBJECT_TYPE_GENERAL:
1785 case ECMA_OBJECT_TYPE_CLASS:
1786 case ECMA_OBJECT_TYPE_FUNCTION:
1787 case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION:
1788 case ECMA_OBJECT_TYPE_BOUND_FUNCTION:
1789 {
1790 return ecma_op_general_object_define_own_property (obj_p,
1791 property_name_p,
1792 property_desc_p);
1793 }
1794
1795 case ECMA_OBJECT_TYPE_ARRAY:
1796 {
1797 return ecma_op_array_object_define_own_property (obj_p,
1798 property_name_p,
1799 property_desc_p);
1800 }
1801
1802 default:
1803 {
1804 JERRY_ASSERT (type == ECMA_OBJECT_TYPE_PSEUDO_ARRAY);
1805
1806 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
1807
1808 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
1809 if (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS)
1810 {
1811 #else /* !ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
1812 JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);
1813 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
1814 return ecma_op_arguments_object_define_own_property (obj_p,
1815 property_name_p,
1816 property_desc_p);
1817 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
1818 }
1819 /* ES2015 9.4.5.3 */
1820 if (ecma_object_is_typedarray (obj_p))
1821 {
1822 #if ENABLED (JERRY_ES2015)
1823 if (ecma_prop_name_is_symbol (property_name_p))
1824 {
1825 return ecma_op_general_object_define_own_property (obj_p,
1826 property_name_p,
1827 property_desc_p);
1828 }
1829 #endif /* ENABLED (JERRY_ES2015) */
1830 uint32_t array_index = ecma_string_get_array_index (property_name_p);
1831
1832 if (array_index != ECMA_STRING_NOT_ARRAY_INDEX)
1833 {
1834 bool define_status = ecma_op_typedarray_define_index_prop (obj_p,
1835 array_index,
1836 property_desc_p);
1837
1838 if (define_status)
1839 {
1840 return ECMA_VALUE_TRUE;
1841 }
1842
1843 return ecma_reject (property_desc_p->flags & ECMA_PROP_IS_THROW);
1844 }
1845
1846 ecma_number_t num = ecma_string_to_number (property_name_p);
1847 ecma_string_t *num_to_str = ecma_new_ecma_string_from_number (num);
1848
1849 if (ecma_compare_ecma_strings (property_name_p, num_to_str))
1850 {
1851 ecma_deref_ecma_string (num_to_str);
1852
1853 return ecma_reject (property_desc_p->flags & ECMA_PROP_IS_THROW);
1854 }
1855
1856 ecma_deref_ecma_string (num_to_str);
1857 }
1858
1859 return ecma_op_general_object_define_own_property (obj_p,
1860 property_name_p,
1861 property_desc_p);
1862 #else /* !ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
1863 break;
1864 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
1865 }
1866 }
1867 } /* ecma_op_object_define_own_property */
1868
1869 /**
1870 * Get property descriptor from specified property
1871 *
1872 * depending on the property type the following fields are set:
1873 * - for named data properties: { [Value], [Writable], [Enumerable], [Configurable] };
1874 * - for named accessor properties: { [Get] - if defined,
1875 * [Set] - if defined,
1876 * [Enumerable], [Configurable]
1877 * }.
1878 *
1879 * The output property descriptor will always be initialized to an empty descriptor.
1880 *
1881 * @return true - if property found
1882 * false - otherwise
1883 */
1884 ecma_value_t
ecma_op_object_get_own_property_descriptor(ecma_object_t * object_p,ecma_string_t * property_name_p,ecma_property_descriptor_t * prop_desc_p)1885 ecma_op_object_get_own_property_descriptor (ecma_object_t *object_p, /**< the object */
1886 ecma_string_t *property_name_p, /**< property name */
1887 ecma_property_descriptor_t *prop_desc_p) /**< property descriptor */
1888 {
1889 *prop_desc_p = ecma_make_empty_property_descriptor ();
1890
1891 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
1892 if (ECMA_OBJECT_IS_PROXY (object_p))
1893 {
1894 return ecma_proxy_object_get_own_property_descriptor (object_p, property_name_p, prop_desc_p);
1895 }
1896 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
1897
1898 ecma_property_ref_t property_ref;
1899
1900 ecma_property_t property = ecma_op_object_get_own_property (object_p,
1901 property_name_p,
1902 &property_ref,
1903 ECMA_PROPERTY_GET_VALUE);
1904
1905 if (property == ECMA_PROPERTY_TYPE_NOT_FOUND || property == ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP)
1906 {
1907 return ECMA_VALUE_FALSE;
1908 }
1909
1910 uint32_t flags = ecma_is_property_enumerable (property) ? ECMA_PROP_IS_ENUMERABLE : ECMA_PROP_NO_OPTS;
1911 flags |= ecma_is_property_configurable (property) ? ECMA_PROP_IS_CONFIGURABLE: ECMA_PROP_NO_OPTS;
1912
1913 prop_desc_p->flags = (uint16_t) (ECMA_PROP_IS_ENUMERABLE_DEFINED | ECMA_PROP_IS_CONFIGURABLE_DEFINED | flags);
1914
1915 ecma_property_types_t type = ECMA_PROPERTY_GET_TYPE (property);
1916
1917 if (type != ECMA_PROPERTY_TYPE_NAMEDACCESSOR)
1918 {
1919 if (type == ECMA_PROPERTY_TYPE_NAMEDDATA)
1920 {
1921 prop_desc_p->value = ecma_copy_value (property_ref.value_p->value);
1922 }
1923 else
1924 {
1925 JERRY_ASSERT (type == ECMA_PROPERTY_TYPE_VIRTUAL);
1926 prop_desc_p->value = property_ref.virtual_value;
1927 }
1928
1929 prop_desc_p->flags |= (ECMA_PROP_IS_VALUE_DEFINED | ECMA_PROP_IS_WRITABLE_DEFINED);
1930 prop_desc_p->flags = (uint16_t) (prop_desc_p->flags | (ecma_is_property_writable (property) ? ECMA_PROP_IS_WRITABLE
1931 : ECMA_PROP_NO_OPTS));
1932 }
1933 else
1934 {
1935
1936 ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (property_ref.value_p);
1937 prop_desc_p->flags |= (ECMA_PROP_IS_GET_DEFINED | ECMA_PROP_IS_SET_DEFINED);
1938
1939 if (get_set_pair_p->getter_cp == JMEM_CP_NULL)
1940 {
1941 prop_desc_p->get_p = NULL;
1942 }
1943 else
1944 {
1945 prop_desc_p->get_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->getter_cp);
1946 ecma_ref_object (prop_desc_p->get_p);
1947 }
1948
1949 if (get_set_pair_p->setter_cp == JMEM_CP_NULL)
1950 {
1951 prop_desc_p->set_p = NULL;
1952 }
1953 else
1954 {
1955 prop_desc_p->set_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp);
1956 ecma_ref_object (prop_desc_p->set_p);
1957 }
1958 }
1959
1960 return ECMA_VALUE_TRUE;
1961 } /* ecma_op_object_get_own_property_descriptor */
1962
1963 /**
1964 * [[HasInstance]] ecma object's operation
1965 *
1966 * See also:
1967 * ECMA-262 v5, 8.6.2; ECMA-262 v5, Table 9
1968 *
1969 * @return ecma value containing a boolean value or an error
1970 * Returned value must be freed with ecma_free_value
1971 */
1972 ecma_value_t
ecma_op_object_has_instance(ecma_object_t * obj_p,ecma_value_t value)1973 ecma_op_object_has_instance (ecma_object_t *obj_p, /**< the object */
1974 ecma_value_t value) /**< argument 'V' */
1975 {
1976 JERRY_ASSERT (obj_p != NULL
1977 && !ecma_is_lexical_environment (obj_p));
1978
1979 JERRY_ASSERT_OBJECT_TYPE_IS_VALID (ecma_get_object_type (obj_p));
1980
1981 if (ecma_op_object_is_callable (obj_p))
1982 {
1983 return ecma_op_function_has_instance (obj_p, value);
1984 }
1985
1986 return ecma_raise_type_error (ECMA_ERR_MSG ("Expected a function object."));
1987 } /* ecma_op_object_has_instance */
1988
1989 /**
1990 * Object's isPrototypeOf operation
1991 *
1992 * See also:
1993 * ECMA-262 v5, 15.2.4.6; 3
1994 *
1995 * @return ECMA_VALUE_ERROR - if the operation fails
1996 * ECMA_VALUE_TRUE - if the target object is prototype of the base object
1997 * ECMA_VALUE_FALSE - if the target object is not prototype of the base object
1998 */
1999 ecma_value_t
ecma_op_object_is_prototype_of(ecma_object_t * base_p,ecma_object_t * target_p)2000 ecma_op_object_is_prototype_of (ecma_object_t *base_p, /**< base object */
2001 ecma_object_t *target_p) /**< target object */
2002 {
2003 do
2004 {
2005 jmem_cpointer_t target_cp;
2006 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
2007 if (ECMA_OBJECT_IS_PROXY (target_p))
2008 {
2009 ecma_value_t target_proto = ecma_proxy_object_get_prototype_of (target_p);
2010
2011 if (ECMA_IS_VALUE_ERROR (target_proto))
2012 {
2013 return target_proto;
2014 }
2015 target_cp = ecma_proxy_object_prototype_to_cp (target_proto);
2016 }
2017 else
2018 {
2019 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
2020 target_cp = ecma_op_ordinary_object_get_prototype_of (target_p);
2021 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
2022 }
2023 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
2024
2025 if (target_cp == JMEM_CP_NULL)
2026 {
2027 return ECMA_VALUE_FALSE;
2028 }
2029
2030 target_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, target_cp);
2031
2032 if (target_p == base_p)
2033 {
2034 return ECMA_VALUE_TRUE;
2035 }
2036 } while (true);
2037 } /* ecma_op_object_is_prototype_of */
2038
2039 /**
2040 * Get collection of property names
2041 *
2042 * Order of names in the collection:
2043 * - integer indices in ascending order
2044 * - other indices in creation order (for built-ins: the order of the properties are listed in specification).
2045 *
2046 * Note:
2047 * Implementation of the routine assumes that new properties are appended to beginning of corresponding object's
2048 * property list, and the list is not reordered (in other words, properties are stored in order that is reversed
2049 * to the properties' addition order).
2050 *
2051 * @return NULL - if the Proxy.[[OwnPropertyKeys]] operation raises error
2052 * collection of property names - otherwise
2053 */
2054 ecma_collection_t *
ecma_op_object_get_property_names(ecma_object_t * obj_p,uint32_t opts)2055 ecma_op_object_get_property_names (ecma_object_t *obj_p, /**< object */
2056 uint32_t opts) /**< any combination of ecma_list_properties_options_t values */
2057 {
2058 JERRY_ASSERT (obj_p != NULL
2059 && !ecma_is_lexical_environment (obj_p));
2060
2061 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
2062 if (ECMA_OBJECT_IS_PROXY (obj_p))
2063 {
2064 /* Integrated a part of ECMA 262 v6 7.3.21 EnumerableOwnNames operation. */
2065 ecma_collection_t *proxy_keys = ecma_proxy_object_own_property_keys (obj_p);
2066 if (JERRY_UNLIKELY (proxy_keys == NULL))
2067 {
2068 return proxy_keys;
2069 }
2070 ecma_collection_t *return_keys = ecma_new_collection ();
2071
2072 /* Move valid elements to the output collection */
2073 for (uint32_t i = 0; i < proxy_keys->item_count; i++)
2074 {
2075 ecma_value_t entry = proxy_keys->buffer_p[i];
2076 ecma_string_t *prop_name_p = ecma_get_prop_name_from_value (entry);
2077 bool prop_is_symbol = ecma_prop_name_is_symbol (prop_name_p);
2078
2079 if (prop_is_symbol && ((opts & (ECMA_LIST_SYMBOLS | ECMA_LIST_SYMBOLS_ONLY)) != 0))
2080 {
2081 ecma_collection_push_back (return_keys, entry);
2082 }
2083 else if (!prop_is_symbol && (opts & ECMA_LIST_SYMBOLS_ONLY) == 0)
2084 {
2085 ecma_collection_push_back (return_keys, entry);
2086 }
2087 else
2088 {
2089 ecma_free_value (entry);
2090 }
2091 }
2092
2093 ecma_collection_destroy (proxy_keys);
2094 return return_keys;
2095 }
2096 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
2097
2098 if (ecma_op_object_is_fast_array (obj_p))
2099 {
2100 return ecma_fast_array_get_property_names (obj_p, opts);
2101 }
2102
2103 ecma_collection_t *ret_p = ecma_new_collection ();
2104 ecma_collection_t *skipped_non_enumerable_p = ecma_new_collection ();
2105
2106 const bool is_enumerable_only = (opts & ECMA_LIST_ENUMERABLE) != 0;
2107 const bool is_array_indices_only = (opts & ECMA_LIST_ARRAY_INDICES) != 0;
2108 const bool is_with_prototype_chain = (opts & ECMA_LIST_PROTOTYPE) != 0;
2109 #if ENABLED (JERRY_ES2015)
2110 const bool is_symbols = (opts & ECMA_LIST_SYMBOLS) != 0;
2111 const bool is_symbols_only = (opts & ECMA_LIST_SYMBOLS_ONLY) != 0;
2112 #endif /* ENABLED (JERRY_ES2015) */
2113
2114 const size_t bitmap_row_size = sizeof (uint32_t) * JERRY_BITSINBYTE;
2115 const size_t names_hashes_bitmap_size = ECMA_OBJECT_HASH_BITMAP_SIZE / bitmap_row_size;
2116 JERRY_VLA (uint32_t, names_hashes_bitmap, names_hashes_bitmap_size);
2117
2118 memset (names_hashes_bitmap, 0, names_hashes_bitmap_size * sizeof (names_hashes_bitmap[0]));
2119
2120 while (true)
2121 {
2122 const ecma_object_type_t type = ecma_get_object_type (obj_p);
2123 const bool obj_is_builtin = ecma_get_object_is_builtin (obj_p);
2124 ecma_length_t string_named_properties_count = 0;
2125 ecma_length_t array_index_named_properties_count = 0;
2126 #if ENABLED (JERRY_ES2015)
2127 ecma_length_t symbol_named_properties_count = 0;
2128 #endif /* ENABLED (JERRY_ES2015) */
2129 ecma_collection_t *prop_names_p = ecma_new_collection ();
2130
2131 #if ENABLED (JERRY_ES2015)
2132 if (JERRY_LIKELY (!is_symbols_only))
2133 {
2134 #endif /* ENABLED (JERRY_ES2015) */
2135
2136 if (obj_is_builtin)
2137 {
2138 if (type == ECMA_OBJECT_TYPE_FUNCTION && ecma_builtin_function_is_routine (obj_p))
2139 {
2140 ecma_builtin_routine_list_lazy_property_names (obj_p,
2141 opts,
2142 prop_names_p,
2143 skipped_non_enumerable_p);
2144 }
2145 else
2146 {
2147 ecma_builtin_list_lazy_property_names (obj_p,
2148 opts,
2149 prop_names_p,
2150 skipped_non_enumerable_p);
2151 }
2152 }
2153 else
2154 {
2155 switch (type)
2156 {
2157 case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
2158 {
2159 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
2160 if (ecma_object_is_typedarray (obj_p))
2161 {
2162 ecma_op_typedarray_list_lazy_property_names (obj_p, prop_names_p);
2163 }
2164 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
2165 break;
2166 }
2167 case ECMA_OBJECT_TYPE_FUNCTION:
2168 {
2169 if (!is_array_indices_only)
2170 {
2171 ecma_op_function_list_lazy_property_names (obj_p,
2172 opts,
2173 prop_names_p,
2174 skipped_non_enumerable_p);
2175 }
2176 break;
2177 }
2178 case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION:
2179 {
2180 if (!is_array_indices_only)
2181 {
2182 ecma_op_external_function_list_lazy_property_names (obj_p,
2183 opts,
2184 prop_names_p,
2185 skipped_non_enumerable_p);
2186 }
2187 break;
2188 }
2189 case ECMA_OBJECT_TYPE_BOUND_FUNCTION:
2190 {
2191 if (!is_array_indices_only)
2192 {
2193 ecma_op_bound_function_list_lazy_property_names (obj_p,
2194 opts,
2195 prop_names_p,
2196 skipped_non_enumerable_p);
2197 }
2198 break;
2199 }
2200 case ECMA_OBJECT_TYPE_CLASS:
2201 {
2202 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
2203
2204 if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_STRING_UL)
2205 {
2206 ecma_op_string_list_lazy_property_names (obj_p,
2207 opts,
2208 prop_names_p,
2209 skipped_non_enumerable_p);
2210 }
2211
2212 break;
2213 }
2214 case ECMA_OBJECT_TYPE_ARRAY:
2215 {
2216 ecma_op_array_list_lazy_property_names (obj_p,
2217 opts,
2218 prop_names_p,
2219 skipped_non_enumerable_p);
2220 break;
2221 }
2222 default:
2223 {
2224 JERRY_ASSERT (type == ECMA_OBJECT_TYPE_GENERAL);
2225
2226 break;
2227 }
2228 }
2229 }
2230 #if ENABLED (JERRY_ES2015)
2231 }
2232 #endif /* ENABLED (JERRY_ES2015) */
2233
2234 ecma_value_t *buffer_p = prop_names_p->buffer_p;
2235 uint32_t lazy_prop_name_count = prop_names_p->item_count;
2236
2237 const size_t own_names_hashes_bitmap_size = ECMA_OBJECT_HASH_BITMAP_SIZE / bitmap_row_size;
2238 JERRY_VLA (uint32_t, own_names_hashes_bitmap, own_names_hashes_bitmap_size);
2239 memset (own_names_hashes_bitmap, 0, own_names_hashes_bitmap_size * sizeof (own_names_hashes_bitmap[0]));
2240
2241 for (uint32_t i = 0; i < prop_names_p->item_count; i++)
2242 {
2243 ecma_string_t *name_p = ecma_get_string_from_value (buffer_p[i]);
2244
2245 if (ecma_string_get_array_index (name_p) != ECMA_STRING_NOT_ARRAY_INDEX)
2246 {
2247 array_index_named_properties_count++;
2248 }
2249 #if ENABLED (JERRY_ES2015)
2250 else if (ecma_prop_name_is_symbol (name_p))
2251 {
2252 symbol_named_properties_count++;
2253 }
2254 #endif /* ENABLED (JERRY_ES2015) */
2255 else
2256 {
2257 string_named_properties_count++;
2258 }
2259
2260 #if ENABLED (JERRY_ES2015)
2261 /* Symbols are never lazy listed */
2262 JERRY_ASSERT (!ecma_prop_name_is_symbol (name_p));
2263 #endif /* ENABLED (JERRY_ES2015) */
2264
2265 uint8_t hash = (uint8_t) ecma_string_hash (name_p);
2266 uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
2267 uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
2268
2269 if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) == 0)
2270 {
2271 own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
2272 }
2273 }
2274
2275 jmem_cpointer_t prop_iter_cp = obj_p->u1.property_list_cp;
2276
2277 if (ecma_op_object_is_fast_array (obj_p) && prop_iter_cp != JMEM_CP_NULL)
2278 {
2279 ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
2280
2281 uint32_t length = ext_obj_p->u.array.length;
2282 array_index_named_properties_count = length - ecma_fast_array_get_hole_count (obj_p);
2283
2284 ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, prop_iter_cp);
2285
2286 for (uint32_t i = 0; i < length; i++)
2287 {
2288 if (ecma_is_value_array_hole (values_p[i]))
2289 {
2290 continue;
2291 }
2292
2293 ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (i);
2294
2295 uint8_t hash = (uint8_t) ecma_string_hash (index_str_p);
2296 uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
2297 uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
2298
2299 bool is_add = true;
2300
2301 if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0)
2302 {
2303 buffer_p = prop_names_p->buffer_p;
2304
2305 for (uint32_t j = 0; j < prop_names_p->item_count; j++)
2306 {
2307 ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]);
2308
2309 if (ecma_compare_ecma_strings (index_str_p, current_name_p))
2310 {
2311 is_add = false;
2312 break;
2313 }
2314 }
2315 }
2316
2317 if (is_add)
2318 {
2319 own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
2320
2321 ecma_collection_push_back (prop_names_p, ecma_make_string_value (index_str_p));
2322 }
2323 }
2324 }
2325 else
2326 {
2327 #if ENABLED (JERRY_PROPRETY_HASHMAP)
2328 if (prop_iter_cp != JMEM_CP_NULL)
2329 {
2330 ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
2331
2332 if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
2333 {
2334 prop_iter_cp = prop_iter_p->next_property_cp;
2335 }
2336 }
2337 #endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */
2338
2339 while (prop_iter_cp != JMEM_CP_NULL)
2340 {
2341 ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
2342 JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p));
2343
2344 for (int i = 0; i < ECMA_PROPERTY_PAIR_ITEM_COUNT; i++)
2345 {
2346 ecma_property_t *property_p = prop_iter_p->types + i;
2347
2348 if (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA
2349 || ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR)
2350 {
2351 ecma_property_pair_t *prop_pair_p = (ecma_property_pair_t *) prop_iter_p;
2352
2353 if (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC
2354 && prop_pair_p->names_cp[i] >= LIT_NON_INTERNAL_MAGIC_STRING__COUNT
2355 && prop_pair_p->names_cp[i] < LIT_MAGIC_STRING__COUNT)
2356 {
2357 /* Internal properties are never enumerated. */
2358 continue;
2359 }
2360
2361 ecma_string_t *name_p = ecma_string_from_property_name (*property_p,
2362 prop_pair_p->names_cp[i]);
2363
2364 if (!(is_enumerable_only && !ecma_is_property_enumerable (*property_p)))
2365 {
2366 #if ENABLED (JERRY_ES2015)
2367 /* We skip the current property in the following cases:
2368 1. We don't want to list symbols (is_symbols and is_symbols_only are false)
2369 and the current property is a symbol.
2370 2. We only want to list symbols (is_symbols_only is true) and the current
2371 property is NOT a symbol. */
2372 bool is_symbol = ecma_prop_name_is_symbol (name_p);
2373 if ((!(is_symbols || is_symbols_only) && is_symbol) || (is_symbols_only && !is_symbol))
2374 {
2375 ecma_deref_ecma_string (name_p);
2376 continue;
2377 }
2378 #endif /* ENABLED (JERRY_ES2015) */
2379
2380 uint8_t hash = (uint8_t) ecma_string_hash (name_p);
2381 uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
2382 uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
2383
2384 bool is_add = true;
2385
2386 if ((own_names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0)
2387 {
2388 buffer_p = prop_names_p->buffer_p;
2389
2390 for (uint32_t j = 0; j < prop_names_p->item_count; j++)
2391 {
2392 ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]);
2393
2394 if (ecma_compare_ecma_strings (name_p, current_name_p))
2395 {
2396 is_add = false;
2397 break;
2398 }
2399 }
2400 }
2401
2402 if (is_add)
2403 {
2404 if (ecma_string_get_array_index (name_p) != ECMA_STRING_NOT_ARRAY_INDEX)
2405 {
2406 /* The name is a valid array index. */
2407 array_index_named_properties_count++;
2408 }
2409 else if (!is_array_indices_only)
2410 {
2411 #if ENABLED (JERRY_ES2015)
2412 if (ecma_prop_name_is_symbol (name_p))
2413 {
2414 symbol_named_properties_count++;
2415 }
2416 else
2417 {
2418 #endif /* ENABLED (JERRY_ES2015) */
2419 string_named_properties_count++;
2420 #if ENABLED (JERRY_ES2015)
2421 }
2422 #endif /* ENABLED (JERRY_ES2015) */
2423 }
2424 else
2425 {
2426 ecma_deref_ecma_string (name_p);
2427 continue;
2428 }
2429
2430 own_names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
2431
2432 ecma_collection_push_back (prop_names_p, ecma_make_prop_name_value (name_p));
2433 }
2434 else
2435 {
2436 ecma_deref_ecma_string (name_p);
2437 }
2438 }
2439 else
2440 {
2441 JERRY_ASSERT (is_enumerable_only && !ecma_is_property_enumerable (*property_p));
2442
2443 ecma_collection_push_back (skipped_non_enumerable_p, ecma_make_prop_name_value (name_p));
2444 }
2445 }
2446 }
2447
2448 prop_iter_cp = prop_iter_p->next_property_cp;
2449 }
2450 }
2451
2452 ecma_length_t all_properties_count = array_index_named_properties_count + string_named_properties_count;
2453
2454 #if ENABLED (JERRY_ES2015)
2455 all_properties_count += symbol_named_properties_count;
2456 #endif /* ENABLED (JERRY_ES2015) */
2457
2458 /* Second pass: collecting property names into an array. */
2459 JMEM_DEFINE_LOCAL_ARRAY (names_p, all_properties_count, ecma_string_t *);
2460
2461 ecma_string_t **string_names_p = names_p + array_index_named_properties_count;
2462 #if ENABLED (JERRY_ES2015)
2463 ecma_string_t **symbol_names_p = string_names_p + string_named_properties_count;
2464 #endif /* ENABLED (JERRY_ES2015) */
2465
2466 uint32_t array_index_name_pos = 0;
2467 uint32_t string_name_pos = string_named_properties_count;
2468 uint32_t lazy_string_name_pos = 0;
2469 #if ENABLED (JERRY_ES2015)
2470 uint32_t symbol_name_pos = symbol_named_properties_count;
2471 #endif /* ENABLED (JERRY_ES2015) */
2472
2473 buffer_p = prop_names_p->buffer_p;
2474
2475 for (uint32_t i = 0; i < prop_names_p->item_count; i++)
2476 {
2477 ecma_string_t *name_p = ecma_get_prop_name_from_value (buffer_p[i]);
2478 ecma_ref_ecma_string (name_p);
2479
2480 uint32_t index = ecma_string_get_array_index (name_p);
2481
2482 if (index != ECMA_STRING_NOT_ARRAY_INDEX)
2483 {
2484 JERRY_ASSERT (array_index_name_pos < array_index_named_properties_count);
2485
2486 uint32_t insertion_pos = 0;
2487 while (insertion_pos < array_index_name_pos
2488 && index > ecma_string_get_array_index (names_p[insertion_pos]))
2489 {
2490 insertion_pos++;
2491 }
2492
2493 if (insertion_pos == array_index_name_pos)
2494 {
2495 names_p[array_index_name_pos++] = name_p;
2496 }
2497 else
2498 {
2499 JERRY_ASSERT (insertion_pos < array_index_name_pos);
2500 JERRY_ASSERT (index <= ecma_string_get_array_index (names_p[insertion_pos]));
2501
2502 uint32_t move_pos = array_index_name_pos++;
2503
2504 while (move_pos > insertion_pos)
2505 {
2506 names_p[move_pos] = names_p[move_pos - 1u];
2507
2508 move_pos--;
2509 }
2510
2511 names_p[insertion_pos] = name_p;
2512 }
2513 }
2514 #if ENABLED (JERRY_ES2015)
2515 else if (ecma_prop_name_is_symbol (name_p))
2516 {
2517 // Put in the symbols in reverse order.
2518 JERRY_ASSERT (symbol_name_pos > 0);
2519 JERRY_ASSERT (symbol_name_pos <= symbol_named_properties_count);
2520
2521 symbol_names_p[--symbol_name_pos] = name_p;
2522 }
2523 #endif /* ENABLED (JERRY_ES2015) */
2524 else
2525 {
2526 // Put in the strings in reverse order.
2527 JERRY_ASSERT (string_name_pos > 0);
2528 JERRY_ASSERT (string_name_pos <= string_named_properties_count);
2529
2530 if (i < lazy_prop_name_count)
2531 {
2532 string_names_p[lazy_string_name_pos++] = name_p;
2533 }
2534 else
2535 {
2536 string_names_p[--string_name_pos] = name_p;
2537 }
2538 }
2539 }
2540
2541 JERRY_ASSERT (array_index_name_pos == array_index_named_properties_count);
2542 JERRY_ASSERT (string_name_pos - lazy_string_name_pos == 0);
2543 #if ENABLED (JERRY_ES2015)
2544 JERRY_ASSERT (symbol_name_pos == 0);
2545 #endif /* ENABLED (JERRY_ES2015) */
2546
2547 ecma_collection_free (prop_names_p);
2548
2549 /* Third pass:
2550 * embedding own property names of current object of prototype chain to aggregate property names collection */
2551 for (uint32_t i = 0; i < all_properties_count; i++)
2552 {
2553 bool is_append = true;
2554
2555 ecma_string_t *name_p = names_p[i];
2556
2557 uint8_t hash = (uint8_t) ecma_string_hash (name_p);
2558 uint32_t bitmap_row = (uint32_t) (hash / bitmap_row_size);
2559 uint32_t bitmap_column = (uint32_t) (hash % bitmap_row_size);
2560
2561 if ((names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) == 0)
2562 {
2563 /* This hash has not been used before (for non-skipped). */
2564 names_hashes_bitmap[bitmap_row] |= (1u << bitmap_column);
2565 }
2566 else
2567 {
2568 /* Name with same hash has already occured. */
2569 buffer_p = ret_p->buffer_p;
2570
2571 for (uint32_t j = 0; j < ret_p->item_count; j++)
2572 {
2573 ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]);
2574
2575 if (ecma_compare_ecma_strings (name_p, current_name_p))
2576 {
2577 is_append = false;
2578 break;
2579 }
2580 }
2581 }
2582
2583 if (is_append)
2584 {
2585 buffer_p = skipped_non_enumerable_p->buffer_p;
2586
2587 for (uint32_t j = 0; j < skipped_non_enumerable_p->item_count; j++)
2588 {
2589 ecma_string_t *current_name_p = ecma_get_prop_name_from_value (buffer_p[j]);
2590
2591 if (ecma_compare_ecma_strings (name_p, current_name_p))
2592 {
2593 is_append = false;
2594 break;
2595 }
2596 }
2597 }
2598
2599 if (is_append)
2600 {
2601 JERRY_ASSERT ((names_hashes_bitmap[bitmap_row] & (1u << bitmap_column)) != 0);
2602
2603 ecma_collection_push_back (ret_p, ecma_make_prop_name_value (name_p));
2604 }
2605 else
2606 {
2607 ecma_deref_ecma_string (name_p);
2608 }
2609
2610 }
2611
2612 JMEM_FINALIZE_LOCAL_ARRAY (names_p);
2613
2614 if (!is_with_prototype_chain || obj_p->u2.prototype_cp == JMEM_CP_NULL)
2615 {
2616 break;
2617 }
2618
2619 obj_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, obj_p->u2.prototype_cp);
2620 }
2621
2622 ecma_collection_free (skipped_non_enumerable_p);
2623
2624 return ret_p;
2625 } /* ecma_op_object_get_property_names */
2626
2627 /**
2628 * The function is used in the assert of ecma_object_get_class_name
2629 *
2630 * @return true - if class name is an object
2631 * false - otherwise
2632 */
2633 inline static bool
ecma_object_check_class_name_is_object(ecma_object_t * obj_p)2634 ecma_object_check_class_name_is_object (ecma_object_t *obj_p) /**< object */
2635 {
2636 #ifndef JERRY_NDEBUG
2637 return (ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_GLOBAL)
2638 #if ENABLED (JERRY_ES2015_BUILTIN_PROMISE)
2639 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_PROMISE_PROTOTYPE)
2640 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */
2641 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
2642 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE)
2643 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_TYPEDARRAY_PROTOTYPE)
2644 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_INT8ARRAY_PROTOTYPE)
2645 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_UINT8ARRAY_PROTOTYPE)
2646 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_INT16ARRAY_PROTOTYPE)
2647 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_UINT16ARRAY_PROTOTYPE)
2648 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_INT32ARRAY_PROTOTYPE)
2649 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_UINT32ARRAY_PROTOTYPE)
2650 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_FLOAT32ARRAY_PROTOTYPE)
2651 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_UINT8CLAMPEDARRAY_PROTOTYPE)
2652 #if ENABLED (JERRY_NUMBER_TYPE_FLOAT64)
2653 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_FLOAT64ARRAY_PROTOTYPE)
2654 #endif /* ENABLED (JERRY_NUMBER_TYPE_FLOAT64) */
2655 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
2656 #if ENABLED (JERRY_ES2015)
2657 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ARRAY_PROTOTYPE_UNSCOPABLES)
2658 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE)
2659 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ITERATOR_PROTOTYPE)
2660 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_STRING_ITERATOR_PROTOTYPE)
2661 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE)
2662 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE)
2663 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE)
2664 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE)
2665 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_GENERATOR_PROTOTYPE)
2666 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE)
2667 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE)
2668 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ERROR_PROTOTYPE)
2669 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_STRING_PROTOTYPE)
2670 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE)
2671 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_NUMBER_PROTOTYPE)
2672 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_DATE_PROTOTYPE)
2673 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_REGEXP_PROTOTYPE)
2674 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE)
2675 #endif /* ENABLED (JERRY_ES2015) */
2676 #if ENABLED (JERRY_ES2015_BUILTIN_MAP)
2677 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_MAP_PROTOTYPE)
2678 #if ENABLED (JERRY_ES2015)
2679 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_MAP_ITERATOR_PROTOTYPE)
2680 #endif /* ENABLED (JERRY_ES2015) */
2681 #endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) */
2682 #if ENABLED (JERRY_ES2015_BUILTIN_SET)
2683 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_SET_PROTOTYPE)
2684 #if ENABLED (JERRY_ES2015)
2685 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_SET_ITERATOR_PROTOTYPE)
2686 #endif /* ENABLED (JERRY_ES2015) */
2687 #endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */
2688 #if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP)
2689 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_WEAKMAP_PROTOTYPE)
2690 #endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) */
2691 #if ENABLED (JERRY_ES2015_BUILTIN_WEAKSET)
2692 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_WEAKSET_PROTOTYPE)
2693 #endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) */
2694 #if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW)
2695 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_DATAVIEW_PROTOTYPE)
2696 #endif /* ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) */
2697 || ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_OBJECT_PROTOTYPE));
2698 #else /* JERRY_NDEBUG */
2699 JERRY_UNUSED (obj_p);
2700 return true;
2701 #endif /* !JERRY_NDEBUG */
2702 } /* ecma_object_check_class_name_is_object */
2703
2704 /**
2705 * Get [[Class]] string of specified object
2706 *
2707 * @return class name magic string
2708 */
2709 lit_magic_string_id_t
ecma_object_get_class_name(ecma_object_t * obj_p)2710 ecma_object_get_class_name (ecma_object_t *obj_p) /**< object */
2711 {
2712 ecma_object_type_t type = ecma_get_object_type (obj_p);
2713
2714 switch (type)
2715 {
2716 case ECMA_OBJECT_TYPE_ARRAY:
2717 {
2718 return LIT_MAGIC_STRING_ARRAY_UL;
2719 }
2720 case ECMA_OBJECT_TYPE_CLASS:
2721 {
2722 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) obj_p;
2723
2724 return (lit_magic_string_id_t) ext_object_p->u.class_prop.class_id;
2725 }
2726 case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
2727 {
2728 ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
2729
2730 switch (ext_obj_p->u.pseudo_array.type)
2731 {
2732 #if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
2733 case ECMA_PSEUDO_ARRAY_TYPEDARRAY:
2734 case ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO:
2735 {
2736 return (lit_magic_string_id_t) ext_obj_p->u.pseudo_array.u1.class_id;
2737 }
2738 #endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
2739 #if ENABLED (JERRY_ES2015)
2740 case ECMA_PSEUDO_ARRAY_ITERATOR:
2741 {
2742 return LIT_MAGIC_STRING_ARRAY_ITERATOR_UL;
2743 }
2744 case ECMA_PSEUDO_SET_ITERATOR:
2745 {
2746 return LIT_MAGIC_STRING_SET_ITERATOR_UL;
2747 }
2748 case ECMA_PSEUDO_MAP_ITERATOR:
2749 {
2750 return LIT_MAGIC_STRING_MAP_ITERATOR_UL;
2751 }
2752 #endif /* ENABLED (JERRY_ES2015) */
2753 #if ENABLED (JERRY_ES2015)
2754 case ECMA_PSEUDO_STRING_ITERATOR:
2755 {
2756 return LIT_MAGIC_STRING_STRING_ITERATOR_UL;
2757 }
2758 #endif /* ENABLED (JERRY_ES2015) */
2759 default:
2760 {
2761 JERRY_ASSERT (ext_obj_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);
2762
2763 return LIT_MAGIC_STRING_ARGUMENTS_UL;
2764 }
2765 }
2766
2767 break;
2768 }
2769 case ECMA_OBJECT_TYPE_FUNCTION:
2770 case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION:
2771 case ECMA_OBJECT_TYPE_BOUND_FUNCTION:
2772 {
2773 return LIT_MAGIC_STRING_FUNCTION_UL;
2774 }
2775 default:
2776 {
2777 JERRY_ASSERT (type == ECMA_OBJECT_TYPE_GENERAL || type == ECMA_OBJECT_TYPE_PROXY);
2778
2779 if (ecma_get_object_is_builtin (obj_p))
2780 {
2781 ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
2782
2783 switch (ext_obj_p->u.built_in.id)
2784 {
2785 #if ENABLED (JERRY_BUILTIN_MATH)
2786 case ECMA_BUILTIN_ID_MATH:
2787 {
2788 return LIT_MAGIC_STRING_MATH_UL;
2789 }
2790 #endif /* ENABLED (JERRY_BUILTIN_MATH) */
2791 #if ENABLED (JERRY_ES2015_BUILTIN_REFLECT)
2792 case ECMA_BUILTIN_ID_REFLECT:
2793 {
2794 return LIT_MAGIC_STRING_REFLECT_UL;
2795 }
2796 #endif /* ENABLED (JERRY_ES2015_BUILTIN_REFLECT) */
2797 #if ENABLED (JERRY_ES2015)
2798 case ECMA_BUILTIN_ID_GENERATOR:
2799 {
2800 return LIT_MAGIC_STRING_GENERATOR_UL;
2801 }
2802 #endif /* ENABLED (JERRY_ES2015) */
2803 #if ENABLED (JERRY_BUILTIN_JSON)
2804 case ECMA_BUILTIN_ID_JSON:
2805 {
2806 return LIT_MAGIC_STRING_JSON_U;
2807 }
2808 #endif /* ENABLED (JERRY_BUILTIN_JSON) */
2809 #if !ENABLED (JERRY_ES2015)
2810 #if ENABLED (JERRY_BUILTIN_ERRORS)
2811 case ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE:
2812 case ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE:
2813 case ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE:
2814 case ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE:
2815 case ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE:
2816 case ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE:
2817 #endif /* ENABLED (JERRY_BUILTIN_ERRORS) */
2818 case ECMA_BUILTIN_ID_ERROR_PROTOTYPE:
2819 {
2820 return LIT_MAGIC_STRING_ERROR_UL;
2821 }
2822 #endif /* !ENABLED (JERRY_ES2015) */
2823 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
2824 case ECMA_BUILTIN_ID_PROXY:
2825 {
2826 return LIT_MAGIC_STRING_FUNCTION_UL;
2827 }
2828 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
2829 default:
2830 {
2831 JERRY_ASSERT (ecma_object_check_class_name_is_object (obj_p));
2832
2833 return LIT_MAGIC_STRING_OBJECT_UL;
2834 }
2835 }
2836 }
2837 else
2838 {
2839 return LIT_MAGIC_STRING_OBJECT_UL;
2840 }
2841 }
2842 }
2843 } /* ecma_object_get_class_name */
2844
2845 /**
2846 * Get value of an object if the class matches
2847 *
2848 * @return value of the object if the class matches
2849 * ECMA_VALUE_NOT_FOUND otherwise
2850 */
2851 inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_object_class_is(ecma_object_t * object_p,uint32_t class_id)2852 ecma_object_class_is (ecma_object_t *object_p, /**< object */
2853 uint32_t class_id) /**< class id */
2854 {
2855 if (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_CLASS)
2856 {
2857 ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
2858
2859 if (ext_object_p->u.class_prop.class_id == class_id)
2860 {
2861 return true;
2862 }
2863 }
2864
2865 return false;
2866 } /* ecma_object_class_is */
2867
2868 /**
2869 * Checks if the given argument has [[RegExpMatcher]] internal slot
2870 *
2871 * @return true - if the given argument is a regexp
2872 * false - otherwise
2873 */
2874 inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_object_is_regexp_object(ecma_value_t arg)2875 ecma_object_is_regexp_object (ecma_value_t arg) /**< argument */
2876 {
2877 return (ecma_is_value_object (arg)
2878 && ecma_object_class_is (ecma_get_object_from_value (arg), LIT_MAGIC_STRING_REGEXP_UL));
2879 } /* ecma_object_is_regexp_object */
2880
2881 #if ENABLED (JERRY_ES2015)
2882 /**
2883 * Object's IsConcatSpreadable operation, used for Array.prototype.concat
2884 * It checks the argument's [Symbol.isConcatSpreadable] property value
2885 *
2886 * See also:
2887 * ECMA-262 v6, 22.1.3.1.1;
2888 *
2889 * @return ECMA_VALUE_ERROR - if the operation fails
2890 * ECMA_VALUE_TRUE - if the argument is concatSpreadable
2891 * ECMA_VALUE_FALSE - otherwise
2892 */
2893 ecma_value_t
ecma_op_is_concat_spreadable(ecma_value_t arg)2894 ecma_op_is_concat_spreadable (ecma_value_t arg) /**< argument */
2895 {
2896 if (!ecma_is_value_object (arg))
2897 {
2898 return ECMA_VALUE_FALSE;
2899 }
2900
2901 ecma_value_t spreadable = ecma_op_object_get_by_symbol_id (ecma_get_object_from_value (arg),
2902 LIT_GLOBAL_SYMBOL_IS_CONCAT_SPREADABLE);
2903
2904 if (ECMA_IS_VALUE_ERROR (spreadable))
2905 {
2906 return spreadable;
2907 }
2908
2909 if (!ecma_is_value_undefined (spreadable))
2910 {
2911 const bool to_bool = ecma_op_to_boolean (spreadable);
2912 ecma_free_value (spreadable);
2913 return ecma_make_boolean_value (to_bool);
2914 }
2915
2916 return ecma_is_value_array (arg);
2917 } /* ecma_op_is_concat_spreadable */
2918
2919 /**
2920 * IsRegExp operation
2921 *
2922 * See also:
2923 * ECMA-262 v6, 22.1.3.1.1;
2924 *
2925 * @return ECMA_VALUE_ERROR - if the operation fails
2926 * ECMA_VALUE_TRUE - if the argument is regexp
2927 * ECMA_VALUE_FALSE - otherwise
2928 */
2929 ecma_value_t
ecma_op_is_regexp(ecma_value_t arg)2930 ecma_op_is_regexp (ecma_value_t arg) /**< argument */
2931 {
2932 if (!ecma_is_value_object (arg))
2933 {
2934 return ECMA_VALUE_FALSE;
2935 }
2936
2937 ecma_value_t is_regexp = ecma_op_object_get_by_symbol_id (ecma_get_object_from_value (arg),
2938 LIT_GLOBAL_SYMBOL_MATCH);
2939
2940 if (ECMA_IS_VALUE_ERROR (is_regexp))
2941 {
2942 return is_regexp;
2943 }
2944
2945 if (!ecma_is_value_undefined (is_regexp))
2946 {
2947 const bool to_bool = ecma_op_to_boolean (is_regexp);
2948 ecma_free_value (is_regexp);
2949 return ecma_make_boolean_value (to_bool);
2950 }
2951
2952 return ecma_make_boolean_value (ecma_object_is_regexp_object (arg));
2953 } /* ecma_op_is_regexp */
2954
2955 /**
2956 * SpeciesConstructor operation
2957 * See also:
2958 * ECMA-262 v6, 7.3.20;
2959 *
2960 * @return ecma_value
2961 * returned value must be freed with ecma_free_value
2962 */
2963 ecma_value_t
ecma_op_species_constructor(ecma_object_t * this_value,ecma_builtin_id_t default_constructor_id)2964 ecma_op_species_constructor (ecma_object_t *this_value, /**< This Value */
2965 ecma_builtin_id_t default_constructor_id) /**< Builtin ID of default constructor */
2966 {
2967 ecma_object_t *default_constructor_p = ecma_builtin_get (default_constructor_id);
2968 ecma_value_t constructor = ecma_op_object_get_by_magic_id (this_value, LIT_MAGIC_STRING_CONSTRUCTOR);
2969 if (ECMA_IS_VALUE_ERROR (constructor))
2970 {
2971 return constructor;
2972 }
2973
2974 if (ecma_is_value_undefined (constructor))
2975 {
2976 ecma_ref_object (default_constructor_p);
2977 return ecma_make_object_value (default_constructor_p);
2978 }
2979
2980 if (!ecma_is_value_object (constructor))
2981 {
2982 ecma_free_value (constructor);
2983 return ecma_raise_type_error (ECMA_ERR_MSG ("Constructor must be an Object"));
2984 }
2985
2986 ecma_object_t *ctor_object_p = ecma_get_object_from_value (constructor);
2987 ecma_value_t species = ecma_op_object_get_by_symbol_id (ctor_object_p, LIT_GLOBAL_SYMBOL_SPECIES);
2988 ecma_deref_object (ctor_object_p);
2989
2990 if (ECMA_IS_VALUE_ERROR (species))
2991 {
2992 return species;
2993 }
2994
2995 if (ecma_is_value_undefined (species) || ecma_is_value_null (species))
2996 {
2997 ecma_ref_object (default_constructor_p);
2998 return ecma_make_object_value (default_constructor_p);
2999 }
3000
3001 if (!ecma_is_constructor (species))
3002 {
3003 ecma_free_value (species);
3004 return ecma_raise_type_error (ECMA_ERR_MSG ("Species must be a Constructor"));
3005 }
3006
3007 return species;
3008 } /* ecma_op_species_constructor */
3009
3010 /**
3011 * 7.3.18 Abstract operation Invoke when property name is a magic string
3012 *
3013 * @return ecma_value result of the invoked function or raised error
3014 * note: returned value must be freed with ecma_free_value
3015 */
3016 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_invoke_by_symbol_id(ecma_value_t object,lit_magic_string_id_t symbol_id,ecma_value_t * args_p,ecma_length_t args_len)3017 ecma_op_invoke_by_symbol_id (ecma_value_t object, /**< Object value */
3018 lit_magic_string_id_t symbol_id, /**< Symbol ID */
3019 ecma_value_t *args_p, /**< Argument list */
3020 ecma_length_t args_len) /**< Argument list length */
3021 {
3022 ecma_string_t *symbol_p = ecma_op_get_global_symbol (symbol_id);
3023 ecma_value_t ret_value = ecma_op_invoke (object, symbol_p, args_p, args_len);
3024 ecma_deref_ecma_string (symbol_p);
3025
3026 return ret_value;
3027 } /* ecma_op_invoke_by_symbol_id */
3028 #endif /* ENABLED (JERRY_ES2015) */
3029
3030 /**
3031 * 7.3.18 Abstract operation Invoke when property name is a magic string
3032 *
3033 * @return ecma_value result of the invoked function or raised error
3034 * note: returned value must be freed with ecma_free_value
3035 */
3036 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_invoke_by_magic_id(ecma_value_t object,lit_magic_string_id_t magic_string_id,ecma_value_t * args_p,ecma_length_t args_len)3037 ecma_op_invoke_by_magic_id (ecma_value_t object, /**< Object value */
3038 lit_magic_string_id_t magic_string_id, /**< Magic string ID */
3039 ecma_value_t *args_p, /**< Argument list */
3040 ecma_length_t args_len) /**< Argument list length */
3041 {
3042 return ecma_op_invoke (object, ecma_get_magic_string (magic_string_id), args_p, args_len);
3043 } /* ecma_op_invoke_by_magic_id */
3044
3045 /**
3046 * 7.3.18 Abstract operation Invoke
3047 *
3048 * @return ecma_value result of the invoked function or raised error
3049 * note: returned value must be freed with ecma_free_value
3050 */
3051 ecma_value_t
ecma_op_invoke(ecma_value_t object,ecma_string_t * property_name_p,ecma_value_t * args_p,ecma_length_t args_len)3052 ecma_op_invoke (ecma_value_t object, /**< Object value */
3053 ecma_string_t *property_name_p, /**< Property name */
3054 ecma_value_t *args_p, /**< Argument list */
3055 ecma_length_t args_len) /**< Argument list length */
3056 {
3057 /* 3. */
3058 ecma_value_t object_value = ecma_op_to_object (object);
3059 if (ECMA_IS_VALUE_ERROR (object_value))
3060 {
3061 return object_value;
3062 }
3063
3064 ecma_object_t *object_p = ecma_get_object_from_value (object_value);
3065 ecma_value_t func = ecma_op_object_get (object_p, property_name_p);
3066
3067 if (ECMA_IS_VALUE_ERROR (func))
3068 {
3069 ecma_deref_object (object_p);
3070 return func;
3071 }
3072
3073 /* 4. */
3074 if (!ecma_op_is_callable (func))
3075 {
3076 ecma_free_value (func);
3077 ecma_deref_object (object_p);
3078 return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not callable"));
3079 }
3080
3081 ecma_object_t *func_obj_p = ecma_get_object_from_value (func);
3082 ecma_value_t call_result = ecma_op_function_call (func_obj_p, object, args_p, args_len);
3083 ecma_deref_object (object_p);
3084 ecma_deref_object (func_obj_p);
3085
3086 return call_result;
3087 } /* ecma_op_invoke */
3088
3089 /**
3090 * Ordinary object [[GetPrototypeOf]] operation
3091 *
3092 * See also:
3093 * ECMAScript v6, 9.1.1
3094 *
3095 * @return the value of the [[Prototype]] internal slot of the given object.
3096 */
3097 inline jmem_cpointer_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_ordinary_object_get_prototype_of(ecma_object_t * obj_p)3098 ecma_op_ordinary_object_get_prototype_of (ecma_object_t *obj_p) /**< object */
3099 {
3100 JERRY_ASSERT (!ecma_is_lexical_environment (obj_p));
3101 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (obj_p));
3102
3103 return obj_p->u2.prototype_cp;
3104 } /* ecma_op_ordinary_object_get_prototype_of */
3105
3106 /**
3107 * Ordinary object [[SetPrototypeOf]] operation
3108 *
3109 * See also:
3110 * ECMAScript v6, 9.1.2
3111 *
3112 * @return ECMA_VALUE_FALSE - if the operation fails
3113 * ECMA_VALUE_TRUE - otherwise
3114 */
3115 inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_op_ordinary_object_set_prototype_of(ecma_object_t * obj_p,ecma_value_t proto)3116 ecma_op_ordinary_object_set_prototype_of (ecma_object_t *obj_p, /**< base object */
3117 ecma_value_t proto) /**< prototype object */
3118 {
3119 JERRY_ASSERT (!ecma_is_lexical_environment (obj_p));
3120 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (obj_p));
3121
3122 /* 1. */
3123 JERRY_ASSERT (ecma_is_value_object (proto) || ecma_is_value_null (proto));
3124
3125 /* 3. */
3126 ecma_object_t *current_proto_p = ECMA_GET_POINTER (ecma_object_t, ecma_op_ordinary_object_get_prototype_of (obj_p));
3127 ecma_object_t *new_proto_p = ecma_is_value_null (proto) ? NULL : ecma_get_object_from_value (proto);
3128
3129 /* 4. */
3130 if (new_proto_p == current_proto_p)
3131 {
3132 return ECMA_VALUE_TRUE;
3133 }
3134
3135 /* 2 - 5. */
3136 if (!ecma_op_ordinary_object_is_extensible (obj_p))
3137 {
3138 return ECMA_VALUE_FALSE;
3139 }
3140
3141 /* 6. */
3142 ecma_object_t *iter_p = new_proto_p;
3143
3144 /* 7 - 8. */
3145 while (true)
3146 {
3147 /* 8.a */
3148 if (iter_p == NULL)
3149 {
3150 break;
3151 }
3152
3153 /* 8.b */
3154 if (obj_p == iter_p)
3155 {
3156 return ECMA_VALUE_FALSE;
3157 }
3158
3159 /* 8.c.i */
3160 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
3161 if (ECMA_OBJECT_IS_PROXY (iter_p))
3162 {
3163 break;
3164 }
3165 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
3166
3167 /* 8.c.ii */
3168 iter_p = ECMA_GET_POINTER (ecma_object_t, ecma_op_ordinary_object_get_prototype_of (iter_p));
3169 }
3170
3171 /* 9. */
3172 ECMA_SET_POINTER (obj_p->u2.prototype_cp, new_proto_p);
3173
3174 /* 10. */
3175 return ECMA_VALUE_TRUE;
3176 } /* ecma_op_ordinary_object_set_prototype_of */
3177
3178 /**
3179 * [[IsExtensible]] operation for Ordinary object.
3180 *
3181 * See also:
3182 * ECMAScript v6, 9.1.2
3183 *
3184 * @return true - if object is extensible
3185 * false - otherwise
3186 */
3187 inline bool JERRY_ATTR_PURE
ecma_op_ordinary_object_is_extensible(ecma_object_t * object_p)3188 ecma_op_ordinary_object_is_extensible (ecma_object_t *object_p) /**< object */
3189 {
3190 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
3191
3192 return (object_p->type_flags_refs & ECMA_OBJECT_FLAG_EXTENSIBLE) != 0;
3193 } /* ecma_op_ordinary_object_is_extensible */
3194
3195 /**
3196 * Set value of [[Extensible]] object's internal property.
3197 */
3198 void JERRY_ATTR_NOINLINE
ecma_op_ordinary_object_prevent_extensions(ecma_object_t * object_p)3199 ecma_op_ordinary_object_prevent_extensions (ecma_object_t *object_p) /**< object */
3200 {
3201 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
3202 object_p->type_flags_refs = (uint16_t) (object_p->type_flags_refs & ~ECMA_OBJECT_FLAG_EXTENSIBLE);
3203 } /* ecma_op_ordinary_object_prevent_extensions */
3204
3205 /**
3206 * Checks whether an object (excluding prototypes) has a named property
3207 *
3208 * @return true - if property is found
3209 * false - otherwise
3210 */
3211 inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_op_ordinary_object_has_own_property(ecma_object_t * object_p,ecma_string_t * property_name_p)3212 ecma_op_ordinary_object_has_own_property (ecma_object_t *object_p, /**< the object */
3213 ecma_string_t *property_name_p) /**< property name */
3214 {
3215 JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
3216
3217 ecma_property_t property = ecma_op_object_get_own_property (object_p,
3218 property_name_p,
3219 NULL,
3220 ECMA_PROPERTY_GET_HAS_OWN_PROP);
3221
3222 return property != ECMA_PROPERTY_TYPE_NOT_FOUND && property != ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP;
3223 } /* ecma_op_ordinary_object_has_own_property */
3224
3225 /**
3226 * @}
3227 * @}
3228 */
3229