-
Notifications
You must be signed in to change notification settings - Fork 680
/
Copy pathvm.c
5337 lines (4471 loc) · 177 KB
/
vm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "vm.h"
#include "ecma-alloc.h"
#include "ecma-arguments-object.h"
#include "ecma-array-object.h"
#include "ecma-bigint.h"
#include "ecma-builtin-object.h"
#include "ecma-builtins.h"
#include "ecma-comparison.h"
#include "ecma-conversion.h"
#include "ecma-errors.h"
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-helpers.h"
#include "ecma-iterator-object.h"
#include "ecma-lcache.h"
#include "ecma-lex-env.h"
#include "ecma-objects-general.h"
#include "ecma-objects.h"
#include "ecma-promise-object.h"
#include "ecma-regexp-object.h"
#include "common.h"
#include "jcontext.h"
#include "opcodes.h"
#include "vm-stack.h"
/** \addtogroup vm Virtual machine
* @{
*
* \addtogroup vm_executor Executor
* @{
*/
JERRY_STATIC_ASSERT ((sizeof (vm_frame_ctx_t) % sizeof (ecma_value_t)) == 0,
sizeof_vm_frame_ctx_must_be_sizeof_ecma_value_t_aligned);
/**
* Get the value of object[property].
*
* @return ecma value
*/
static ecma_value_t
vm_op_get_value (ecma_value_t object, /**< base object */
ecma_value_t property) /**< property name */
{
if (ecma_is_value_object (object))
{
ecma_object_t *object_p = ecma_get_object_from_value (object);
ecma_string_t *property_name_p = NULL;
if (ecma_is_value_integer_number (property))
{
ecma_integer_value_t int_value = ecma_get_integer_from_value (property);
if (int_value >= 0 && int_value <= ECMA_DIRECT_STRING_MAX_IMM)
{
if (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_ARRAY)
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
if (JERRY_LIKELY (ecma_op_array_is_fast_array (ext_object_p)
&& (uint32_t) int_value < ext_object_p->u.array.length))
{
ecma_value_t *values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, object_p->u1.property_list_cp);
if (JERRY_LIKELY (!ecma_is_value_array_hole (values_p[int_value])))
{
return ecma_fast_copy_value (values_p[int_value]);
}
}
}
property_name_p = (ecma_string_t *) ECMA_CREATE_DIRECT_STRING (ECMA_DIRECT_STRING_UINT, (uintptr_t) int_value);
}
}
else if (ecma_is_value_string (property))
{
property_name_p = ecma_get_string_from_value (property);
}
if (ecma_is_value_symbol (property))
{
property_name_p = ecma_get_symbol_from_value (property);
}
if (property_name_p != NULL)
{
#if JERRY_LCACHE
ecma_property_t *property_p = ecma_lcache_lookup (object_p, property_name_p);
if (property_p != NULL && (*property_p & ECMA_PROPERTY_FLAG_DATA))
{
JERRY_ASSERT (!ECMA_PROPERTY_IS_INTERNAL (*property_p));
return ecma_fast_copy_value (ECMA_PROPERTY_VALUE_PTR (property_p)->value);
}
#endif /* JERRY_LCACHE */
/* There is no need to free the name. */
return ecma_op_object_get (object_p, property_name_p);
}
}
if (JERRY_UNLIKELY (ecma_is_value_undefined (object) || ecma_is_value_null (object)))
{
#if JERRY_ERROR_MESSAGES
ecma_value_t error_value =
ecma_raise_standard_error_with_format (JERRY_ERROR_TYPE, "Cannot read property '%' of %", property, object);
#else /* !JERRY_ERROR_MESSAGES */
ecma_value_t error_value = ecma_raise_type_error (ECMA_ERR_EMPTY);
#endif /* JERRY_ERROR_MESSAGES */
return error_value;
}
ecma_string_t *property_name_p = ecma_op_to_property_key (property);
if (property_name_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_value_t get_value_result = ecma_op_get_value_object_base (object, property_name_p);
ecma_deref_ecma_string (property_name_p);
return get_value_result;
} /* vm_op_get_value */
/**
* Set the value of object[property].
*
* Note:
* this function frees its object and property arguments
*
* @return an ecma value which contains an error
* if the property setting is unsuccessful
*/
static ecma_value_t
vm_op_set_value (ecma_value_t base, /**< base object */
ecma_value_t property, /**< property name */
ecma_value_t value, /**< ecma value */
bool is_strict) /**< strict mode */
{
ecma_value_t result = ECMA_VALUE_EMPTY;
ecma_object_t *object_p;
ecma_string_t *property_p;
if (JERRY_UNLIKELY (!ecma_is_value_object (base)))
{
if (JERRY_UNLIKELY (ecma_is_value_null (base) || ecma_is_value_undefined (base)))
{
#if JERRY_ERROR_MESSAGES
result = ecma_raise_standard_error_with_format (JERRY_ERROR_TYPE, "Cannot set property '%' of %", property, base);
#else /* !JERRY_ERROR_MESSAGES */
result = ecma_raise_type_error (ECMA_ERR_EMPTY);
#endif /* JERRY_ERROR_MESSAGES */
ecma_free_value (property);
return result;
}
if (JERRY_UNLIKELY (!ecma_is_value_prop_name (property)))
{
property_p = ecma_op_to_string (property);
ecma_fast_free_value (property);
if (JERRY_UNLIKELY (property_p == NULL))
{
ecma_free_value (base);
return ECMA_VALUE_ERROR;
}
}
else
{
property_p = ecma_get_prop_name_from_value (property);
}
ecma_value_t object = ecma_op_to_object (base);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (object));
object_p = ecma_get_object_from_value (object);
ecma_op_ordinary_object_prevent_extensions (object_p);
result = ecma_op_object_put_with_receiver (object_p, property_p, value, base, is_strict);
ecma_free_value (base);
}
else
{
object_p = ecma_get_object_from_value (base);
if (JERRY_UNLIKELY (!ecma_is_value_prop_name (property)))
{
property_p = ecma_op_to_string (property);
ecma_fast_free_value (property);
if (JERRY_UNLIKELY (property_p == NULL))
{
ecma_deref_object (object_p);
return ECMA_VALUE_ERROR;
}
}
else
{
property_p = ecma_get_prop_name_from_value (property);
}
if (!ecma_is_lexical_environment (object_p))
{
result = ecma_op_object_put_with_receiver (object_p, property_p, value, base, is_strict);
}
else
{
result = ecma_op_set_mutable_binding (object_p, property_p, value, is_strict);
}
}
ecma_deref_object (object_p);
ecma_deref_ecma_string (property_p);
return result;
} /* vm_op_set_value */
/** Compact bytecode define */
#define CBC_OPCODE(arg1, arg2, arg3, arg4) (uint16_t) arg4,
/**
* Decode table for both opcodes and extended opcodes.
*/
static const uint16_t vm_decode_table[] JERRY_ATTR_CONST_DATA = { CBC_OPCODE_LIST CBC_EXT_OPCODE_LIST };
#undef CBC_OPCODE
/**
* Run global code
*
* Note:
* returned value must be freed with ecma_free_value, when it is no longer needed.
*
* @return ecma value
*/
ecma_value_t
vm_run_global (const ecma_compiled_code_t *bytecode_p, /**< pointer to bytecode to run */
ecma_object_t *function_object_p) /**< function object if available */
{
#if JERRY_BUILTIN_REALMS
ecma_object_t *global_obj_p = (ecma_object_t *) ecma_op_function_get_realm (bytecode_p);
#else /* !JERRY_BUILTIN_REALMS */
ecma_object_t *global_obj_p = ecma_builtin_get_global ();
#endif /* JERRY_BUILTIN_REALMS */
if (bytecode_p->status_flags & CBC_CODE_FLAGS_LEXICAL_BLOCK_NEEDED)
{
ecma_create_global_lexical_block (global_obj_p);
}
ecma_object_t *const global_scope_p = ecma_get_global_scope (global_obj_p);
vm_frame_ctx_shared_t shared;
shared.bytecode_header_p = bytecode_p;
shared.function_object_p = function_object_p;
shared.status_flags = 0;
#if JERRY_BUILTIN_REALMS
ecma_value_t this_binding = ((ecma_global_object_t *) global_obj_p)->this_binding;
ecma_global_object_t *saved_global_object_p = JERRY_CONTEXT (global_object_p);
JERRY_CONTEXT (global_object_p) = (ecma_global_object_t *) global_obj_p;
#else /* !JERRY_BUILTIN_REALMS */
ecma_value_t this_binding = ecma_make_object_value (global_obj_p);
#endif /* JERRY_BUILTIN_REALMS */
ecma_value_t result = vm_run (&shared, this_binding, global_scope_p);
#if JERRY_BUILTIN_REALMS
JERRY_CONTEXT (global_object_p) = saved_global_object_p;
#endif /* JERRY_BUILTIN_REALMS */
return result;
} /* vm_run_global */
/**
* Run specified eval-mode bytecode
*
* @return ecma value
*/
ecma_value_t
vm_run_eval (ecma_compiled_code_t *bytecode_data_p, /**< byte-code data */
uint32_t parse_opts) /**< ecma_parse_opts_t option bits */
{
ecma_value_t this_binding;
ecma_object_t *lex_env_p;
/* ECMA-262 v5, 10.4.2 */
if (parse_opts & ECMA_PARSE_DIRECT_EVAL)
{
this_binding = ecma_copy_value (JERRY_CONTEXT (vm_top_context_p)->this_binding);
lex_env_p = JERRY_CONTEXT (vm_top_context_p)->lex_env_p;
#if JERRY_DEBUGGER
uint32_t chain_index = JERRY_CONTEXT (debugger_eval_chain_index);
JERRY_CONTEXT (debugger_eval_chain_index) = 0;
while (chain_index != 0)
{
if (JERRY_UNLIKELY (lex_env_p->u2.outer_reference_cp == JMEM_CP_NULL))
{
ecma_bytecode_deref (bytecode_data_p);
ecma_free_value (this_binding);
return ecma_raise_range_error (ECMA_ERR_INVALID_SCOPE_CHAIN_INDEX_FOR_EVAL);
}
lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, lex_env_p->u2.outer_reference_cp);
if ((ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND)
|| (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE))
{
chain_index--;
}
}
#endif /* JERRY_DEBUGGER */
}
else
{
#if JERRY_BUILTIN_REALMS
ecma_object_t *global_obj_p = (ecma_object_t *) ecma_op_function_get_realm (bytecode_data_p);
this_binding = ((ecma_global_object_t *) global_obj_p)->this_binding;
ecma_ref_object (ecma_get_object_from_value (this_binding));
#else /* !JERRY_BUILTIN_REALMS */
ecma_object_t *global_obj_p = ecma_builtin_get_global ();
ecma_ref_object (global_obj_p);
this_binding = ecma_make_object_value (global_obj_p);
#endif /* JERRY_BUILTIN_REALMS */
lex_env_p = ecma_get_global_scope (global_obj_p);
}
ecma_ref_object (lex_env_p);
if ((bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE) != 0)
{
ecma_object_t *strict_lex_env_p = ecma_create_decl_lex_env (lex_env_p);
ecma_deref_object (lex_env_p);
lex_env_p = strict_lex_env_p;
}
if ((bytecode_data_p->status_flags & CBC_CODE_FLAGS_LEXICAL_BLOCK_NEEDED) != 0)
{
ecma_object_t *lex_block_p = ecma_create_decl_lex_env (lex_env_p);
lex_block_p->type_flags_refs |= ECMA_OBJECT_FLAG_BLOCK;
ecma_deref_object (lex_env_p);
lex_env_p = lex_block_p;
}
vm_frame_ctx_shared_t shared;
shared.bytecode_header_p = bytecode_data_p;
shared.function_object_p = NULL;
shared.status_flags = (parse_opts & ECMA_PARSE_DIRECT_EVAL) ? VM_FRAME_CTX_SHARED_DIRECT_EVAL : 0;
ecma_value_t completion_value = vm_run (&shared, this_binding, lex_env_p);
ecma_deref_object (lex_env_p);
ecma_free_value (this_binding);
#if JERRY_SNAPSHOT_EXEC
if (!(bytecode_data_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
{
ecma_bytecode_deref (bytecode_data_p);
}
#else /* !JERRY_SNAPSHOT_EXEC */
ecma_bytecode_deref (bytecode_data_p);
#endif /* JERRY_SNAPSHOT_EXEC */
return completion_value;
} /* vm_run_eval */
#if JERRY_MODULE_SYSTEM
/**
* Run module code
*
* Note:
* returned value must be freed with ecma_free_value, when it is no longer needed.
*
* @return ecma value
*/
ecma_value_t
vm_run_module (ecma_module_t *module_p) /**< module to be executed */
{
const ecma_value_t module_init_result = ecma_module_initialize (module_p);
if (ECMA_IS_VALUE_ERROR (module_init_result))
{
return module_init_result;
}
vm_frame_ctx_shared_t shared;
shared.bytecode_header_p = module_p->u.compiled_code_p;
shared.function_object_p = &module_p->header.object;
shared.status_flags = 0;
return vm_run (&shared, ECMA_VALUE_UNDEFINED, module_p->scope_p);
} /* vm_run_module */
#endif /* JERRY_MODULE_SYSTEM */
/**
* Construct object
*
* @return object value
*/
static ecma_value_t
vm_construct_literal_object (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_value_t lit_value) /**< literal */
{
ecma_compiled_code_t *bytecode_p;
#if JERRY_SNAPSHOT_EXEC
if (JERRY_LIKELY (!(frame_ctx_p->shared_p->bytecode_header_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION)))
{
#endif /* JERRY_SNAPSHOT_EXEC */
bytecode_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t, lit_value);
#if JERRY_SNAPSHOT_EXEC
}
else
{
uint8_t *byte_p = ((uint8_t *) frame_ctx_p->shared_p->bytecode_header_p) + lit_value;
bytecode_p = (ecma_compiled_code_t *) byte_p;
}
#endif /* JERRY_SNAPSHOT_EXEC */
#if JERRY_BUILTIN_REGEXP
if (JERRY_UNLIKELY (!CBC_IS_FUNCTION (bytecode_p->status_flags)))
{
ecma_object_t *regexp_obj_p = ecma_op_regexp_alloc (NULL);
if (JERRY_UNLIKELY (regexp_obj_p == NULL))
{
return ECMA_VALUE_ERROR;
}
return ecma_op_create_regexp_from_bytecode (regexp_obj_p, (re_compiled_code_t *) bytecode_p);
}
#else /* !JERRY_BUILTIN_REGEXP */
JERRY_ASSERT (CBC_IS_FUNCTION (bytecode_p->status_flags));
#endif /* JERRY_BUILTIN_REGEXP */
ecma_object_t *func_obj_p;
if (JERRY_UNLIKELY (CBC_FUNCTION_IS_ARROW (bytecode_p->status_flags)))
{
func_obj_p = ecma_op_create_arrow_function_object (frame_ctx_p->lex_env_p, bytecode_p, frame_ctx_p->this_binding);
}
else
{
func_obj_p = ecma_op_create_any_function_object (frame_ctx_p->lex_env_p, bytecode_p);
}
return ecma_make_object_value (func_obj_p);
} /* vm_construct_literal_object */
/**
* Get implicit this value
*
* @return true - if the implicit 'this' value is updated,
* false - otherwise
*/
static inline bool JERRY_ATTR_ALWAYS_INLINE
vm_get_implicit_this_value (ecma_value_t *this_value_p) /**< [in,out] this value */
{
if (ecma_is_value_object (*this_value_p))
{
ecma_object_t *this_obj_p = ecma_get_object_from_value (*this_value_p);
if (ecma_is_lexical_environment (this_obj_p))
{
ecma_value_t completion_value = ecma_op_implicit_this_value (this_obj_p);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (completion_value));
*this_value_p = completion_value;
return true;
}
}
return false;
} /* vm_get_implicit_this_value */
/**
* Special bytecode sequence for error handling while the vm_loop
* is preserved for an execute operation
*/
static const uint8_t vm_error_byte_code_p[] = { CBC_EXT_OPCODE, CBC_EXT_ERROR };
/**
* Get class function object
*
* @return the pointer to the object
*/
static ecma_object_t *
vm_get_class_function (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
JERRY_ASSERT (frame_ctx_p != NULL);
if (frame_ctx_p->shared_p->status_flags & VM_FRAME_CTX_SHARED_NON_ARROW_FUNC)
{
return frame_ctx_p->shared_p->function_object_p;
}
ecma_environment_record_t *environment_record_p = ecma_op_get_environment_record (frame_ctx_p->lex_env_p);
JERRY_ASSERT (environment_record_p != NULL);
return ecma_get_object_from_value (environment_record_p->function_object);
} /* vm_get_class_function */
/**
* 'super(...)' function call handler.
*/
static void
vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
JERRY_ASSERT (frame_ctx_p->call_operation == VM_EXEC_SUPER_CALL);
JERRY_ASSERT (frame_ctx_p->byte_code_p[0] == CBC_EXT_OPCODE);
const uint8_t *byte_code_p = frame_ctx_p->byte_code_p + 3;
uint8_t opcode = byte_code_p[-2];
uint32_t arguments_list_len;
bool spread_arguments = opcode >= CBC_EXT_SPREAD_SUPER_CALL;
ecma_collection_t *collection_p = NULL;
ecma_value_t *arguments_p;
if (spread_arguments)
{
ecma_value_t collection = *(--frame_ctx_p->stack_top_p);
collection_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, collection);
arguments_p = collection_p->buffer_p;
arguments_list_len = collection_p->item_count;
}
else
{
arguments_list_len = byte_code_p[-1];
arguments_p = frame_ctx_p->stack_top_p;
}
ecma_value_t func_value = *(--frame_ctx_p->stack_top_p);
ecma_value_t completion_value;
ecma_environment_record_t *environment_record_p = ecma_op_get_environment_record (frame_ctx_p->lex_env_p);
JERRY_ASSERT (environment_record_p);
if (!ecma_is_constructor (func_value))
{
completion_value = ecma_raise_type_error (ECMA_ERR_VALUE_FOR_CLASS_HERITAGE_IS_NOT_A_CONSTRUCTOR);
}
else
{
ecma_object_t *func_obj_p = ecma_get_object_from_value (func_value);
completion_value =
ecma_op_function_construct (func_obj_p, JERRY_CONTEXT (current_new_target_p), arguments_p, arguments_list_len);
if (!ECMA_IS_VALUE_ERROR (completion_value) && ecma_op_this_binding_is_initialized (environment_record_p))
{
ecma_free_value (completion_value);
completion_value = ecma_raise_reference_error (ECMA_ERR_SUPER_CONSTRUCTOR_MAY_ONLY_BE_CALLED_ONCE);
}
}
/* Free registers. */
for (uint32_t i = 0; i < arguments_list_len; i++)
{
ecma_fast_free_value (arguments_p[i]);
}
if (collection_p != NULL)
{
ecma_collection_destroy (collection_p);
}
if (ecma_is_value_object (completion_value))
{
ecma_op_bind_this_value (environment_record_p, completion_value);
frame_ctx_p->this_binding = completion_value;
ecma_value_t fields_value = opfunc_init_class_fields (vm_get_class_function (frame_ctx_p), completion_value);
if (ECMA_IS_VALUE_ERROR (fields_value))
{
ecma_free_value (completion_value);
completion_value = ECMA_VALUE_ERROR;
}
}
ecma_free_value (func_value);
if (JERRY_UNLIKELY (ECMA_IS_VALUE_ERROR (completion_value)))
{
#if JERRY_DEBUGGER
JERRY_CONTEXT (debugger_exception_byte_code_p) = frame_ctx_p->byte_code_p;
#endif /* JERRY_DEBUGGER */
frame_ctx_p->byte_code_p = (uint8_t *) vm_error_byte_code_p;
}
else
{
frame_ctx_p->byte_code_p = byte_code_p;
uint32_t opcode_data = vm_decode_table[(CBC_END + 1) + opcode];
if (!(opcode_data & (VM_OC_PUT_STACK | VM_OC_PUT_BLOCK)))
{
ecma_fast_free_value (completion_value);
}
else if (opcode_data & VM_OC_PUT_STACK)
{
*frame_ctx_p->stack_top_p++ = completion_value;
}
else
{
ecma_fast_free_value (VM_GET_REGISTER (frame_ctx_p, 0));
VM_GET_REGISTERS (frame_ctx_p)[0] = completion_value;
}
}
} /* vm_super_call */
/**
* Perform one of the following call/construct operation with spreaded argument list
* - f(...args)
* - o.f(...args)
* - new O(...args)
*/
static void
vm_spread_operation (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
JERRY_ASSERT (frame_ctx_p->byte_code_p[0] == CBC_EXT_OPCODE);
uint8_t opcode = frame_ctx_p->byte_code_p[1];
ecma_value_t completion_value;
ecma_value_t collection = *(--frame_ctx_p->stack_top_p);
ecma_collection_t *collection_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, collection);
ecma_value_t func_value = *(--frame_ctx_p->stack_top_p);
bool is_call_prop = opcode >= CBC_EXT_SPREAD_CALL_PROP;
if (frame_ctx_p->byte_code_p[1] == CBC_EXT_SPREAD_NEW)
{
ecma_error_msg_t constructor_message_id = ecma_check_constructor (func_value);
if (constructor_message_id != ECMA_IS_VALID_CONSTRUCTOR)
{
completion_value = ecma_raise_type_error (constructor_message_id);
}
else
{
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (func_value);
completion_value = ecma_op_function_construct (constructor_obj_p,
constructor_obj_p,
collection_p->buffer_p,
collection_p->item_count);
}
}
else
{
ecma_value_t this_value = is_call_prop ? frame_ctx_p->stack_top_p[-2] : ECMA_VALUE_UNDEFINED;
if (!ecma_is_value_object (func_value) || !ecma_op_object_is_callable (ecma_get_object_from_value (func_value)))
{
completion_value = ecma_raise_type_error (ECMA_ERR_EXPECTED_A_FUNCTION);
}
else
{
ecma_object_t *func_obj_p = ecma_get_object_from_value (func_value);
completion_value =
ecma_op_function_call (func_obj_p, this_value, collection_p->buffer_p, collection_p->item_count);
}
if (is_call_prop)
{
ecma_free_value (*(--frame_ctx_p->stack_top_p));
ecma_free_value (*(--frame_ctx_p->stack_top_p));
}
}
ecma_collection_free (collection_p);
ecma_free_value (func_value);
if (JERRY_UNLIKELY (ECMA_IS_VALUE_ERROR (completion_value)))
{
#if JERRY_DEBUGGER
JERRY_CONTEXT (debugger_exception_byte_code_p) = frame_ctx_p->byte_code_p;
#endif /* JERRY_DEBUGGER */
frame_ctx_p->byte_code_p = (uint8_t *) vm_error_byte_code_p;
}
else
{
uint32_t opcode_data = vm_decode_table[(CBC_END + 1) + opcode];
if (!(opcode_data & (VM_OC_PUT_STACK | VM_OC_PUT_BLOCK)))
{
ecma_fast_free_value (completion_value);
}
else if (opcode_data & VM_OC_PUT_STACK)
{
*frame_ctx_p->stack_top_p++ = completion_value;
}
else
{
ecma_fast_free_value (VM_GET_REGISTER (frame_ctx_p, 0));
VM_GET_REGISTERS (frame_ctx_p)[0] = completion_value;
}
/* EXT_OPCODE, SPREAD_OPCODE, BYTE_ARG */
frame_ctx_p->byte_code_p += 3;
}
} /* vm_spread_operation */
/**
* 'Function call' opcode handler.
*
* See also: ECMA-262 v5, 11.2.3
*/
static void
opfunc_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
const uint8_t *byte_code_p = frame_ctx_p->byte_code_p + 1;
uint8_t opcode = byte_code_p[-1];
uint32_t arguments_list_len;
if (opcode >= CBC_CALL0)
{
arguments_list_len = (unsigned int) ((opcode - CBC_CALL0) / 6);
}
else
{
arguments_list_len = *byte_code_p++;
}
bool is_call_prop = ((opcode - CBC_CALL) % 6) >= 3;
ecma_value_t *stack_top_p = frame_ctx_p->stack_top_p - arguments_list_len;
ecma_value_t this_value = is_call_prop ? stack_top_p[-3] : ECMA_VALUE_UNDEFINED;
ecma_value_t func_value = stack_top_p[-1];
ecma_value_t completion_value =
ecma_op_function_validated_call (func_value, this_value, stack_top_p, arguments_list_len);
JERRY_CONTEXT (status_flags) &= (uint32_t) ~ECMA_STATUS_DIRECT_EVAL;
/* Free registers. */
for (uint32_t i = 0; i < arguments_list_len; i++)
{
ecma_fast_free_value (stack_top_p[i]);
}
if (is_call_prop)
{
ecma_free_value (*(--stack_top_p));
ecma_free_value (*(--stack_top_p));
}
if (JERRY_UNLIKELY (ECMA_IS_VALUE_ERROR (completion_value)))
{
#if JERRY_DEBUGGER
JERRY_CONTEXT (debugger_exception_byte_code_p) = frame_ctx_p->byte_code_p;
#endif /* JERRY_DEBUGGER */
frame_ctx_p->byte_code_p = (uint8_t *) vm_error_byte_code_p;
}
else
{
frame_ctx_p->byte_code_p = byte_code_p;
ecma_free_value (*(--stack_top_p));
uint32_t opcode_data = vm_decode_table[opcode];
if (!(opcode_data & (VM_OC_PUT_STACK | VM_OC_PUT_BLOCK)))
{
ecma_fast_free_value (completion_value);
}
else if (opcode_data & VM_OC_PUT_STACK)
{
*stack_top_p++ = completion_value;
}
else
{
ecma_fast_free_value (VM_GET_REGISTER (frame_ctx_p, 0));
VM_GET_REGISTERS (frame_ctx_p)[0] = completion_value;
}
}
frame_ctx_p->stack_top_p = stack_top_p;
} /* opfunc_call */
/**
* 'Constructor call' opcode handler.
*
* See also: ECMA-262 v5, 11.2.2
*/
static void
opfunc_construct (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
const uint8_t *byte_code_p = frame_ctx_p->byte_code_p + 1;
uint8_t opcode = byte_code_p[-1];
unsigned int arguments_list_len;
if (opcode >= CBC_NEW0)
{
arguments_list_len = (unsigned int) (opcode - CBC_NEW0);
}
else
{
arguments_list_len = *byte_code_p++;
}
ecma_value_t *stack_top_p = frame_ctx_p->stack_top_p - arguments_list_len;
ecma_value_t constructor_value = stack_top_p[-1];
ecma_value_t completion_value;
ecma_error_msg_t constructor_message_id = ecma_check_constructor (constructor_value);
if (constructor_message_id != ECMA_IS_VALID_CONSTRUCTOR)
{
completion_value = ecma_raise_type_error (constructor_message_id);
}
else
{
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor_value);
completion_value =
ecma_op_function_construct (constructor_obj_p, constructor_obj_p, stack_top_p, arguments_list_len);
}
/* Free registers. */
for (uint32_t i = 0; i < arguments_list_len; i++)
{
ecma_fast_free_value (stack_top_p[i]);
}
if (JERRY_UNLIKELY (ECMA_IS_VALUE_ERROR (completion_value)))
{
#if JERRY_DEBUGGER
JERRY_CONTEXT (debugger_exception_byte_code_p) = frame_ctx_p->byte_code_p;
#endif /* JERRY_DEBUGGER */
frame_ctx_p->byte_code_p = (uint8_t *) vm_error_byte_code_p;
}
else
{
ecma_free_value (stack_top_p[-1]);
frame_ctx_p->byte_code_p = byte_code_p;
stack_top_p[-1] = completion_value;
}
frame_ctx_p->stack_top_p = stack_top_p;
} /* opfunc_construct */
/**
* Read literal index from the byte code stream into destination.
*
* @param destination destination
*/
#define READ_LITERAL_INDEX(destination) \
do \
{ \
(destination) = *byte_code_p++; \
if ((destination) >= encoding_limit) \
{ \
(destination) = (uint16_t) ((((destination) << 8) | *byte_code_p++) - encoding_delta); \
} \
} while (0)
/**
* Get literal value by literal index.
*
* @param literal_index literal index
* @param target_value target value
*
* TODO: For performance reasons, we define this as a macro.
* When we are able to construct a function with similar speed,
* we can remove this macro.
*/
#define READ_LITERAL(literal_index, target_value) \
do \
{ \
if ((literal_index) < ident_end) \
{ \
if ((literal_index) < register_end) \
{ \
/* Note: There should be no specialization for arguments. */ \
(target_value) = ecma_fast_copy_value (VM_GET_REGISTER (frame_ctx_p, literal_index)); \
} \
else \
{ \
ecma_string_t *name_p = ecma_get_string_from_value (literal_start_p[literal_index]); \
\
result = ecma_op_resolve_reference_value (frame_ctx_p->lex_env_p, name_p); \
\
if (ECMA_IS_VALUE_ERROR (result)) \
{ \
goto error; \
} \
(target_value) = result; \
} \
} \
else if (literal_index < const_literal_end) \
{ \
(target_value) = ecma_fast_copy_value (literal_start_p[literal_index]); \
} \
else \
{ \
/* Object construction. */ \
(target_value) = vm_construct_literal_object (frame_ctx_p, literal_start_p[literal_index]); \
} \
} while (0)
/**
* Store the original value for post increase/decrease operators
*
* @param value original value
*/
#define POST_INCREASE_DECREASE_PUT_RESULT(value) \
if (opcode_data & VM_OC_PUT_STACK) \
{ \
if (opcode_flags & VM_OC_IDENT_INCR_DECR_OPERATOR_FLAG) \
{ \
JERRY_ASSERT (opcode == CBC_POST_INCR_IDENT_PUSH_RESULT || opcode == CBC_POST_DECR_IDENT_PUSH_RESULT); \
*stack_top_p++ = (value); \
} \
else \
{ \
/* The parser ensures there is enough space for the \
* extra value on the stack. See js-parser-expr.c. */ \
JERRY_ASSERT (opcode == CBC_POST_INCR_PUSH_RESULT || opcode == CBC_POST_DECR_PUSH_RESULT); \
stack_top_p++; \
stack_top_p[-1] = stack_top_p[-2]; \
stack_top_p[-2] = stack_top_p[-3]; \
stack_top_p[-3] = (value); \
} \
opcode_data &= (uint32_t) ~VM_OC_PUT_STACK; \
} \
else \
{ \
JERRY_ASSERT (opcode_data &VM_OC_PUT_BLOCK); \
ecma_free_value (VM_GET_REGISTER (frame_ctx_p, 0)); \
VM_GET_REGISTERS (frame_ctx_p)[0] = (value); \
opcode_data &= (uint32_t) ~VM_OC_PUT_BLOCK; \
}
/**
* Get the end of the existing topmost context
*/
#define VM_LAST_CONTEXT_END() (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth)
/**
* Run generic byte code.
*
* @return ecma value
*/
static ecma_value_t JERRY_ATTR_NOINLINE
vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
const ecma_compiled_code_t *bytecode_header_p = frame_ctx_p->shared_p->bytecode_header_p;
const uint8_t *byte_code_p = frame_ctx_p->byte_code_p;
ecma_value_t *literal_start_p = frame_ctx_p->literal_start_p;
ecma_value_t *stack_top_p;
uint16_t encoding_limit;
uint16_t encoding_delta;
uint16_t register_end;
uint16_t ident_end;
uint16_t const_literal_end;
int32_t branch_offset = 0;
uint8_t branch_offset_length = 0;
ecma_value_t left_value;
ecma_value_t right_value;
ecma_value_t result = ECMA_VALUE_EMPTY;
bool is_strict = ((bytecode_header_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE) != 0);
/* Prepare for byte code execution. */
if (!(bytecode_header_p->status_flags & CBC_CODE_FLAGS_FULL_LITERAL_ENCODING))
{
encoding_limit = CBC_SMALL_LITERAL_ENCODING_LIMIT;
encoding_delta = CBC_SMALL_LITERAL_ENCODING_DELTA;
}
else
{
encoding_limit = CBC_FULL_LITERAL_ENCODING_LIMIT;
encoding_delta = CBC_FULL_LITERAL_ENCODING_DELTA;
}
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)