-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathecma-gc.c
More file actions
2349 lines (1999 loc) · 74.9 KB
/
Copy pathecma-gc.c
File metadata and controls
2349 lines (1999 loc) · 74.9 KB
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.
*/
/**
* Garbage collector implementation
*/
#include "ecma-gc.h"
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-arraybuffer-object.h"
#include "ecma-builtin-handlers.h"
#include "ecma-container-object.h"
#include "ecma-function-object.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-lcache.h"
#include "ecma-objects.h"
#include "ecma-property-hashmap.h"
#include "ecma-proxy-object.h"
#include "jcontext.h"
#include "jrt-bit-fields.h"
#include "jrt-libc-includes.h"
#include "jrt.h"
#include "re-compiler.h"
#include "vm-defines.h"
#include "vm-stack.h"
#if JERRY_BUILTIN_TYPEDARRAY
#include "ecma-typedarray-object.h"
#endif /* JERRY_BUILTIN_TYPEDARRAY */
#include "ecma-promise-object.h"
/* TODO: Extract GC to a separate component */
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmagc Garbage collector
* @{
*/
/*
* The garbage collector uses the reference counter
* of object: it increases the counter by one when
* the object is marked at the first time.
*/
/**
* Get visited flag of the object.
*
* @return true - if visited
* false - otherwise
*/
static inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_gc_is_object_visited (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (object_p != NULL);
return (object_p->type_flags_refs < ECMA_OBJECT_NON_VISITED);
} /* ecma_gc_is_object_visited */
/**
* Mark objects as visited starting from specified object as root
*/
static void ecma_gc_mark (ecma_object_t *object_p);
/**
* Set visited flag of the object.
*/
static void
ecma_gc_set_object_visited (ecma_object_t *object_p) /**< object */
{
if (object_p->type_flags_refs >= ECMA_OBJECT_NON_VISITED)
{
#if (JERRY_GC_MARK_LIMIT != 0)
if (JERRY_CONTEXT (ecma_gc_mark_recursion_limit) != 0)
{
JERRY_CONTEXT (ecma_gc_mark_recursion_limit)--;
/* Set the reference count of gray object to 0 */
object_p->type_flags_refs &= (ecma_object_descriptor_t) (ECMA_OBJECT_REF_ONE - 1);
ecma_gc_mark (object_p);
JERRY_CONTEXT (ecma_gc_mark_recursion_limit)++;
}
else
{
/* Set the reference count of the non-marked gray object to 1 */
object_p->type_flags_refs &= (ecma_object_descriptor_t) ((ECMA_OBJECT_REF_ONE << 1) - 1);
JERRY_ASSERT (object_p->type_flags_refs >= ECMA_OBJECT_REF_ONE);
}
#else /* (JERRY_GC_MARK_LIMIT == 0) */
/* Set the reference count of gray object to 0 */
object_p->type_flags_refs &= (ecma_object_descriptor_t) (ECMA_OBJECT_REF_ONE - 1);
#endif /* (JERRY_GC_MARK_LIMIT != 0) */
}
} /* ecma_gc_set_object_visited */
/**
* Initialize GC information for the object
*/
extern inline void
ecma_init_gc_info (ecma_object_t *object_p) /**< object */
{
JERRY_CONTEXT (ecma_gc_objects_number)++;
JERRY_CONTEXT (ecma_gc_new_objects)++;
JERRY_ASSERT (object_p->type_flags_refs < ECMA_OBJECT_REF_ONE);
object_p->type_flags_refs |= ECMA_OBJECT_REF_ONE;
object_p->gc_next_cp = JERRY_CONTEXT (ecma_gc_objects_cp);
ECMA_SET_NON_NULL_POINTER (JERRY_CONTEXT (ecma_gc_objects_cp), object_p);
} /* ecma_init_gc_info */
/**
* Increase reference counter of an object
*/
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_ref_object_inline (ecma_object_t *object_p) /**< object */
{
if (JERRY_LIKELY (object_p->type_flags_refs < ECMA_OBJECT_MAX_REF))
{
object_p->type_flags_refs = (ecma_object_descriptor_t) (object_p->type_flags_refs + ECMA_OBJECT_REF_ONE);
}
else
{
jerry_fatal (JERRY_FATAL_REF_COUNT_LIMIT);
}
} /* ecma_ref_object_inline */
/**
* Increase reference counter of an object
*/
void
ecma_ref_object (ecma_object_t *object_p) /**< object */
{
ecma_ref_object_inline (object_p);
} /* ecma_ref_object */
/**
* Decrease reference counter of an object
*/
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_deref_object (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (object_p->type_flags_refs >= ECMA_OBJECT_REF_ONE);
object_p->type_flags_refs = (ecma_object_descriptor_t) (object_p->type_flags_refs - ECMA_OBJECT_REF_ONE);
} /* ecma_deref_object */
/**
* Mark objects referenced by global object
*/
static void
ecma_gc_mark_global_object (ecma_global_object_t *global_object_p) /**< global object */
{
JERRY_ASSERT (global_object_p->extended_object.u.built_in.routine_id == 0);
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, global_object_p->global_env_cp));
#if JERRY_BUILTIN_REALMS
ecma_gc_set_object_visited (ecma_get_object_from_value (global_object_p->this_binding));
#endif /* JERRY_BUILTIN_REALMS */
if (global_object_p->global_scope_cp != global_object_p->global_env_cp)
{
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, global_object_p->global_scope_cp));
}
jmem_cpointer_t *builtin_objects_p = global_object_p->builtin_objects;
for (int i = 0; i < ECMA_BUILTIN_OBJECTS_COUNT; i++)
{
if (builtin_objects_p[i] != JMEM_CP_NULL)
{
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, builtin_objects_p[i]));
}
}
} /* ecma_gc_mark_global_object */
/**
* Mark objects referenced by arguments object
*/
static void
ecma_gc_mark_arguments_object (ecma_extended_object_t *ext_object_p) /**< arguments object */
{
JERRY_ASSERT (ecma_get_object_type ((ecma_object_t *) ext_object_p) == ECMA_OBJECT_TYPE_CLASS);
ecma_unmapped_arguments_t *arguments_p = (ecma_unmapped_arguments_t *) ext_object_p;
ecma_gc_set_object_visited (ecma_get_object_from_value (arguments_p->callee));
ecma_value_t *argv_p = (ecma_value_t *) (arguments_p + 1);
if (ext_object_p->u.cls.u1.arguments_flags & ECMA_ARGUMENTS_OBJECT_MAPPED)
{
ecma_mapped_arguments_t *mapped_arguments_p = (ecma_mapped_arguments_t *) ext_object_p;
argv_p = (ecma_value_t *) (mapped_arguments_p + 1);
ecma_gc_set_object_visited (ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, mapped_arguments_p->lex_env));
}
uint32_t arguments_number = arguments_p->header.u.cls.u3.arguments_number;
for (uint32_t i = 0; i < arguments_number; i++)
{
if (ecma_is_value_object (argv_p[i]))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (argv_p[i]));
}
}
} /* ecma_gc_mark_arguments_object */
/**
* Mark referenced object from property
*/
static void
ecma_gc_mark_properties (ecma_object_t *object_p, /**< object */
bool mark_references) /**< mark references */
{
JERRY_UNUSED (mark_references);
#if !JERRY_MODULE_SYSTEM
JERRY_ASSERT (!mark_references);
#endif /* !JERRY_MODULE_SYSTEM */
jmem_cpointer_t prop_iter_cp = object_p->u1.property_list_cp;
#if JERRY_PROPERTY_HASHMAP
if (prop_iter_cp != JMEM_CP_NULL)
{
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
{
prop_iter_cp = prop_iter_p->next_property_cp;
}
}
#endif /* JERRY_PROPERTY_HASHMAP */
while (prop_iter_cp != JMEM_CP_NULL)
{
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p));
ecma_property_pair_t *property_pair_p = (ecma_property_pair_t *) prop_iter_p;
for (uint32_t index = 0; index < ECMA_PROPERTY_PAIR_ITEM_COUNT; index++)
{
uint8_t property = property_pair_p->header.types[index];
if (JERRY_LIKELY (ECMA_PROPERTY_IS_RAW (property)))
{
if (property & ECMA_PROPERTY_FLAG_DATA)
{
ecma_value_t value = property_pair_p->values[index].value;
if (ecma_is_value_object (value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (value));
}
continue;
}
#if JERRY_MODULE_SYSTEM
if (mark_references)
{
continue;
}
#endif /* JERRY_MODULE_SYSTEM */
ecma_property_value_t *accessor_objs_p = property_pair_p->values + index;
ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (accessor_objs_p);
if (get_set_pair_p->getter_cp != JMEM_CP_NULL)
{
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->getter_cp));
}
if (get_set_pair_p->setter_cp != JMEM_CP_NULL)
{
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp));
}
continue;
}
if (!ECMA_PROPERTY_IS_INTERNAL (property))
{
JERRY_ASSERT (property == ECMA_PROPERTY_TYPE_DELETED || property == ECMA_PROPERTY_TYPE_HASHMAP);
continue;
}
JERRY_ASSERT (property_pair_p->names_cp[index] >= LIT_INTERNAL_MAGIC_STRING_FIRST_DATA
&& property_pair_p->names_cp[index] < LIT_MAGIC_STRING__COUNT);
switch (property_pair_p->names_cp[index])
{
case LIT_INTERNAL_MAGIC_STRING_ENVIRONMENT_RECORD:
{
ecma_environment_record_t *environment_record_p;
environment_record_p =
ECMA_GET_INTERNAL_VALUE_POINTER (ecma_environment_record_t, property_pair_p->values[index].value);
if (environment_record_p->this_binding != ECMA_VALUE_UNINITIALIZED)
{
JERRY_ASSERT (ecma_is_value_object (environment_record_p->this_binding));
ecma_gc_set_object_visited (ecma_get_object_from_value (environment_record_p->this_binding));
}
JERRY_ASSERT (ecma_is_value_object (environment_record_p->function_object));
ecma_gc_set_object_visited (ecma_get_object_from_value (environment_record_p->function_object));
break;
}
case LIT_INTERNAL_MAGIC_STRING_CLASS_PRIVATE_ELEMENTS:
{
ecma_value_t *compact_collection_p;
compact_collection_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_value_t, property_pair_p->values[index].value);
ecma_value_t *end_p = ecma_compact_collection_end (compact_collection_p);
ecma_value_t *current_p = compact_collection_p + 1;
while (end_p - current_p >= ECMA_PRIVATE_ELEMENT_LIST_SIZE)
{
current_p++; /* skip the type */
current_p++; /* skip the name */
ecma_value_t value = *current_p++;
if (!ecma_is_value_undefined (value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (value));
}
}
break;
}
#if JERRY_BUILTIN_CONTAINER
case LIT_INTERNAL_MAGIC_STRING_WEAK_REFS:
{
ecma_value_t key_arg = ecma_make_object_value (object_p);
ecma_collection_t *refs_p;
refs_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, property_pair_p->values[index].value);
for (uint32_t j = 0; j < refs_p->item_count; j++)
{
const ecma_value_t reference_value = refs_p->buffer_p[j];
if (ecma_is_value_empty (reference_value))
{
continue;
}
ecma_object_t *reference_object_p = ecma_get_object_from_value (reference_value);
JERRY_ASSERT (ecma_get_object_type (reference_object_p) == ECMA_OBJECT_TYPE_CLASS);
ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) reference_object_p;
if (map_object_p->u.cls.type != ECMA_OBJECT_CLASS_CONTAINER
|| map_object_p->u.cls.u2.container_id != LIT_MAGIC_STRING_WEAKMAP_UL
|| !ecma_gc_is_object_visited (reference_object_p))
{
continue;
}
ecma_value_t value = ecma_op_container_find_weak_value (reference_object_p, key_arg);
if (ecma_is_value_object (value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (value));
}
}
break;
}
#endif /* JERRY_BUILTIN_CONTAINER */
case LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER_WITH_REFERENCES:
{
jerry_value_t value = property_pair_p->values[index].value;
if (value == JMEM_CP_NULL)
{
JERRY_ASSERT (!(property & ECMA_PROPERTY_FLAG_SINGLE_EXTERNAL));
break;
}
ecma_native_pointer_t *item_p;
item_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_native_pointer_t, value);
do
{
jerry_object_native_info_t *native_info_p = item_p->native_info_p;
JERRY_ASSERT (native_info_p != NULL && native_info_p->number_of_references > 0);
uint8_t *start_p = ((uint8_t *) item_p->native_p) + native_info_p->offset_of_references;
ecma_value_t *value_p = (ecma_value_t *) start_p;
ecma_value_t *end_p = value_p + native_info_p->number_of_references;
do
{
if (ecma_is_value_object (*value_p))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*value_p));
}
} while (++value_p < end_p);
if (property & ECMA_PROPERTY_FLAG_SINGLE_EXTERNAL)
{
break;
}
item_p = &(((ecma_native_pointer_chain_t *) item_p)->next_p->data);
} while (item_p != NULL);
break;
}
}
}
prop_iter_cp = prop_iter_p->next_property_cp;
}
} /* ecma_gc_mark_properties */
/**
* Mark compiled code.
*/
static void
ecma_gc_mark_compiled_code (ecma_value_t script_value) /**< script value */
{
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
if (script_p->refs_and_type & CBC_SCRIPT_USER_VALUE_IS_OBJECT)
{
ecma_value_t user_value = CBC_SCRIPT_GET_USER_VALUE (script_p);
JERRY_ASSERT (ecma_is_value_object (user_value));
ecma_gc_set_object_visited (ecma_get_object_from_value (user_value));
}
#if JERRY_MODULE_SYSTEM
if (script_p->refs_and_type & CBC_SCRIPT_HAS_IMPORT_META)
{
ecma_value_t import_meta = CBC_SCRIPT_GET_IMPORT_META (script_p, script_p->refs_and_type);
JERRY_ASSERT (ecma_is_value_object (import_meta));
ecma_gc_set_object_visited (ecma_get_object_from_value (import_meta));
}
#endif /* JERRY_MODULE_SYSTEM */
#if JERRY_BUILTIN_REALMS
ecma_gc_set_object_visited (script_p->realm_p);
#endif /* JERRY_BUILTIN_REALMS */
} /* ecma_gc_mark_compiled_code */
/**
* Mark objects referenced by bound function object.
*/
static void JERRY_ATTR_NOINLINE
ecma_gc_mark_bound_function_object (ecma_object_t *object_p) /**< bound function object */
{
JERRY_ASSERT (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION);
ecma_bound_function_t *bound_func_p = (ecma_bound_function_t *) object_p;
ecma_object_t *target_func_p;
target_func_p =
ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t, bound_func_p->header.u.bound_function.target_function);
ecma_gc_set_object_visited (target_func_p);
ecma_value_t args_len_or_this = bound_func_p->header.u.bound_function.args_len_or_this;
if (!ecma_is_value_integer_number (args_len_or_this))
{
if (ecma_is_value_object (args_len_or_this))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (args_len_or_this));
}
return;
}
ecma_integer_value_t args_length = ecma_get_integer_from_value (args_len_or_this);
ecma_value_t *args_p = (ecma_value_t *) (bound_func_p + 1);
JERRY_ASSERT (args_length > 0);
for (ecma_integer_value_t i = 0; i < args_length; i++)
{
if (ecma_is_value_object (args_p[i]))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (args_p[i]));
}
}
} /* ecma_gc_mark_bound_function_object */
/**
* Mark objects referenced by Promise built-in.
*/
static void
ecma_gc_mark_promise_object (ecma_extended_object_t *ext_object_p) /**< extended object */
{
/* Mark promise result. */
ecma_value_t result = ext_object_p->u.cls.u3.value;
if (ecma_is_value_object (result))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (result));
}
/* Mark all reactions. */
ecma_promise_object_t *promise_object_p = (ecma_promise_object_t *) ext_object_p;
ecma_collection_t *collection_p = promise_object_p->reactions;
if (collection_p != NULL)
{
ecma_value_t *buffer_p = collection_p->buffer_p;
ecma_value_t *buffer_end_p = buffer_p + collection_p->item_count;
while (buffer_p < buffer_end_p)
{
ecma_value_t value = *buffer_p++;
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t, value));
if (JMEM_CP_GET_FIRST_BIT_FROM_POINTER_TAG (value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*buffer_p++));
}
if (JMEM_CP_GET_SECOND_BIT_FROM_POINTER_TAG (value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*buffer_p++));
}
}
}
} /* ecma_gc_mark_promise_object */
#if JERRY_BUILTIN_CONTAINER
/**
* Mark objects referenced by Map built-in.
*/
static void
ecma_gc_mark_map_object (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (object_p != NULL);
ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) object_p;
ecma_collection_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, map_object_p->u.cls.u3.value);
ecma_value_t *start_p = ECMA_CONTAINER_START (container_p);
uint32_t entry_count = ECMA_CONTAINER_ENTRY_COUNT (container_p);
for (uint32_t i = 0; i < entry_count; i += ECMA_CONTAINER_PAIR_SIZE)
{
ecma_container_pair_t *entry_p = (ecma_container_pair_t *) (start_p + i);
if (ecma_is_value_empty (entry_p->key))
{
continue;
}
if (ecma_is_value_object (entry_p->key))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (entry_p->key));
}
if (ecma_is_value_object (entry_p->value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (entry_p->value));
}
}
} /* ecma_gc_mark_map_object */
/**
* Mark objects referenced by WeakMap built-in.
*/
static void
ecma_gc_mark_weakmap_object (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (object_p != NULL);
ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) object_p;
ecma_collection_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, map_object_p->u.cls.u3.value);
ecma_value_t *start_p = ECMA_CONTAINER_START (container_p);
uint32_t entry_count = ECMA_CONTAINER_ENTRY_COUNT (container_p);
for (uint32_t i = 0; i < entry_count; i += ECMA_CONTAINER_PAIR_SIZE)
{
ecma_container_pair_t *entry_p = (ecma_container_pair_t *) (start_p + i);
if (ecma_is_value_empty (entry_p->key))
{
continue;
}
JERRY_ASSERT (ecma_is_value_object (entry_p->key));
if (ecma_is_value_object (entry_p->value) && ecma_gc_is_object_visited (ecma_get_object_from_value (entry_p->key)))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (entry_p->value));
}
}
} /* ecma_gc_mark_weakmap_object */
/**
* Mark objects referenced by Set built-in.
*/
static void
ecma_gc_mark_set_object (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (object_p != NULL);
ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) object_p;
ecma_collection_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, map_object_p->u.cls.u3.value);
ecma_value_t *start_p = ECMA_CONTAINER_START (container_p);
uint32_t entry_count = ECMA_CONTAINER_ENTRY_COUNT (container_p);
for (uint32_t i = 0; i < entry_count; i += ECMA_CONTAINER_VALUE_SIZE)
{
ecma_value_t *entry_p = start_p + i;
if (ecma_is_value_empty (*entry_p))
{
continue;
}
if (ecma_is_value_object (*entry_p))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*entry_p));
}
}
} /* ecma_gc_mark_set_object */
#endif /* JERRY_BUILTIN_CONTAINER */
/**
* Mark objects referenced by inactive generator functions, async functions, etc.
*/
static void
ecma_gc_mark_executable_object (ecma_object_t *object_p) /**< object */
{
vm_executable_object_t *executable_object_p = (vm_executable_object_t *) object_p;
if (executable_object_p->extended_object.u.cls.u2.executable_obj_flags & ECMA_ASYNC_GENERATOR_CALLED)
{
ecma_value_t task = executable_object_p->extended_object.u.cls.u3.head;
while (!ECMA_IS_INTERNAL_VALUE_NULL (task))
{
ecma_async_generator_task_t *task_p;
task_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_async_generator_task_t, task);
JERRY_ASSERT (ecma_is_value_object (task_p->promise));
ecma_gc_set_object_visited (ecma_get_object_from_value (task_p->promise));
if (ecma_is_value_object (task_p->operation_value))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (task_p->operation_value));
}
task = task_p->next;
}
}
ecma_gc_set_object_visited (executable_object_p->frame_ctx.lex_env_p);
ecma_gc_set_object_visited (executable_object_p->shared.function_object_p);
if (!ECMA_EXECUTABLE_OBJECT_IS_SUSPENDED (executable_object_p)
&& !(executable_object_p->extended_object.u.cls.u2.executable_obj_flags
& ECMA_EXECUTABLE_OBJECT_DO_AWAIT_OR_YIELD))
{
/* All objects referenced by running executable objects are strong roots,
* and a finished executable object cannot refer to other values. */
return;
}
if (ecma_is_value_object (executable_object_p->frame_ctx.this_binding))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (executable_object_p->frame_ctx.this_binding));
}
const ecma_compiled_code_t *bytecode_header_p = executable_object_p->shared.bytecode_header_p;
size_t register_end;
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;
register_end = args_p->register_end;
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;
register_end = args_p->register_end;
}
ecma_value_t *register_p = VM_GET_REGISTERS (&executable_object_p->frame_ctx);
ecma_value_t *register_end_p = register_p + register_end;
while (register_p < register_end_p)
{
if (ecma_is_value_object (*register_p))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*register_p));
}
register_p++;
}
if (executable_object_p->frame_ctx.context_depth > 0)
{
ecma_value_t *context_end_p = register_p;
register_p += executable_object_p->frame_ctx.context_depth;
ecma_value_t *context_top_p = register_p;
do
{
if (VM_CONTEXT_IS_VARIABLE_LENGTH (VM_GET_CONTEXT_TYPE (context_top_p[-1])))
{
ecma_value_t *last_item_p = context_top_p - VM_GET_CONTEXT_END (context_top_p[-1]);
JERRY_ASSERT (last_item_p >= context_end_p);
context_top_p--;
do
{
--context_top_p;
if (ecma_is_value_object (*context_top_p))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*context_top_p));
}
} while (context_top_p > last_item_p);
continue;
}
uint32_t offsets = vm_get_context_value_offsets (context_top_p);
while (VM_CONTEXT_HAS_NEXT_OFFSET (offsets))
{
int32_t offset = VM_CONTEXT_GET_NEXT_OFFSET (offsets);
if (ecma_is_value_object (context_top_p[offset]))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (context_top_p[offset]));
}
offsets >>= VM_CONTEXT_OFFSET_SHIFT;
}
JERRY_ASSERT (context_top_p >= context_end_p + offsets);
context_top_p -= offsets;
} while (context_top_p > context_end_p);
}
register_end_p = executable_object_p->frame_ctx.stack_top_p;
while (register_p < register_end_p)
{
if (ecma_is_value_object (*register_p))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (*register_p));
}
register_p++;
}
if (ecma_is_value_object (executable_object_p->iterator))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (executable_object_p->iterator));
}
} /* ecma_gc_mark_executable_object */
#if JERRY_BUILTIN_PROXY
/**
* Mark the objects referenced by a proxy object
*/
static void
ecma_gc_mark_proxy_object (ecma_object_t *object_p) /**< proxy object */
{
JERRY_ASSERT (ECMA_OBJECT_IS_PROXY (object_p));
ecma_proxy_object_t *proxy_p = (ecma_proxy_object_t *) object_p;
if (!ecma_is_value_null (proxy_p->target))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (proxy_p->target));
}
if (!ecma_is_value_null (proxy_p->handler))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (proxy_p->handler));
}
} /* ecma_gc_mark_proxy_object */
#endif /* JERRY_BUILTIN_PROXY */
/**
* Mark objects as visited starting from specified object as root
*/
static void
ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
{
JERRY_ASSERT (object_p != NULL);
JERRY_ASSERT (ecma_gc_is_object_visited (object_p));
if (ecma_is_lexical_environment (object_p))
{
jmem_cpointer_t outer_lex_env_cp = object_p->u2.outer_reference_cp;
if (outer_lex_env_cp != JMEM_CP_NULL)
{
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, outer_lex_env_cp));
}
switch (ecma_get_lex_env_type (object_p))
{
case ECMA_LEXICAL_ENVIRONMENT_CLASS:
{
#if JERRY_MODULE_SYSTEM
if (object_p->type_flags_refs & ECMA_OBJECT_FLAG_LEXICAL_ENV_HAS_DATA)
{
if (ECMA_LEX_ENV_CLASS_IS_MODULE (object_p))
{
ecma_gc_mark_properties (object_p, true);
}
ecma_gc_set_object_visited (((ecma_lexical_environment_class_t *) object_p)->object_p);
return;
}
#endif /* JERRY_MODULE_SYSTEM */
/* FALLTHRU */
}
case ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND:
{
ecma_object_t *binding_object_p = ecma_get_lex_env_binding_object (object_p);
ecma_gc_set_object_visited (binding_object_p);
return;
}
default:
{
break;
}
}
}
else
{
/**
* Have the object's prototype here so the object could set it to JMEM_CP_NULL
* if the prototype should be ignored (like in case of PROXY).
*/
jmem_cpointer_t proto_cp = object_p->u2.prototype_cp;
switch (ecma_get_object_type (object_p))
{
case ECMA_OBJECT_TYPE_BUILT_IN_GENERAL:
{
ecma_extended_object_t *extended_object_p = (ecma_extended_object_t *) object_p;
if (extended_object_p->u.built_in.id == ECMA_BUILTIN_ID_GLOBAL)
{
ecma_gc_mark_global_object ((ecma_global_object_t *) object_p);
}
#if JERRY_BUILTIN_REALMS
ecma_value_t realm_value = extended_object_p->u.built_in.realm_value;
ecma_gc_set_object_visited (ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, realm_value));
#endif /* JERRY_BUILTIN_REALMS */
break;
}
case ECMA_OBJECT_TYPE_BUILT_IN_CLASS:
{
#if JERRY_BUILTIN_REALMS
ecma_value_t realm_value = ((ecma_extended_built_in_object_t *) object_p)->built_in.realm_value;
ecma_gc_set_object_visited (ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, realm_value));
#endif /* JERRY_BUILTIN_REALMS */
/* FALLTHRU */
}
case ECMA_OBJECT_TYPE_CLASS:
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
switch (ext_object_p->u.cls.type)
{
case ECMA_OBJECT_CLASS_ARGUMENTS:
{
ecma_gc_mark_arguments_object (ext_object_p);
break;
}
#if JERRY_PARSER
case ECMA_OBJECT_CLASS_SCRIPT:
{
const ecma_compiled_code_t *compiled_code_p;
compiled_code_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t, ext_object_p->u.cls.u3.value);
JERRY_ASSERT (!(compiled_code_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION));
ecma_gc_mark_compiled_code (((cbc_uint8_arguments_t *) compiled_code_p)->script_value);
break;
}
#endif /* JERRY_PARSER */
#if JERRY_BUILTIN_TYPEDARRAY
case ECMA_OBJECT_CLASS_TYPEDARRAY:
{
ecma_gc_set_object_visited (ecma_typedarray_get_arraybuffer (object_p));
break;
}
#endif /* JERRY_BUILTIN_TYPEDARRAY */
#if JERRY_MODULE_SYSTEM
case ECMA_OBJECT_CLASS_MODULE_NAMESPACE:
{
JERRY_ASSERT (proto_cp == JMEM_CP_NULL);
ecma_gc_set_object_visited (ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, ext_object_p->u.cls.u3.value));
ecma_gc_mark_properties (object_p, true);
return;
}
#endif /* JERRY_MODULE_SYSTEM */
#if JERRY_MODULE_SYSTEM
case ECMA_OBJECT_CLASS_MODULE:
{
ecma_module_t *module_p = ((ecma_module_t *) ext_object_p);
if (module_p->scope_p != NULL)
{
ecma_gc_set_object_visited (((ecma_module_t *) ext_object_p)->scope_p);
}
if (module_p->namespace_object_p != NULL)
{
ecma_gc_set_object_visited (((ecma_module_t *) ext_object_p)->namespace_object_p);
}
if (!(module_p->header.u.cls.u2.module_flags & ECMA_MODULE_IS_NATIVE)
&& module_p->u.compiled_code_p != NULL)
{
const ecma_compiled_code_t *compiled_code_p = module_p->u.compiled_code_p;
JERRY_ASSERT (!(compiled_code_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION));
ecma_gc_mark_compiled_code (((cbc_uint8_arguments_t *) compiled_code_p)->script_value);
}
ecma_module_node_t *node_p = module_p->imports_p;
while (node_p != NULL)
{
if (ecma_is_value_object (node_p->u.path_or_module))
{
ecma_gc_set_object_visited (ecma_get_object_from_value (node_p->u.path_or_module));
}
node_p = node_p->next_p;
}
break;
}
#endif /* JERRY_MODULE_SYSTEM */
#if JERRY_BUILTIN_DATAVIEW
case ECMA_OBJECT_CLASS_DATAVIEW:
{
ecma_dataview_object_t *dataview_p = (ecma_dataview_object_t *) object_p;
ecma_gc_set_object_visited (dataview_p->buffer_p);
break;
}
#endif /* JERRY_BUILTIN_DATAVIEW */
#if JERRY_BUILTIN_CONTAINER
case ECMA_OBJECT_CLASS_CONTAINER:
{
if (ext_object_p->u.cls.u2.container_id == LIT_MAGIC_STRING_MAP_UL)
{
ecma_gc_mark_map_object (object_p);
break;
}
if (ext_object_p->u.cls.u2.container_id == LIT_MAGIC_STRING_WEAKMAP_UL)
{
ecma_gc_mark_weakmap_object (object_p);
break;
}
if (ext_object_p->u.cls.u2.container_id == LIT_MAGIC_STRING_SET_UL)
{
ecma_gc_mark_set_object (object_p);
break;
}
JERRY_ASSERT (ext_object_p->u.cls.u2.container_id == LIT_MAGIC_STRING_WEAKSET_UL);
break;
}
#endif /* JERRY_BUILTIN_CONTAINER */
case ECMA_OBJECT_CLASS_GENERATOR:
case ECMA_OBJECT_CLASS_ASYNC_GENERATOR:
{
ecma_gc_mark_executable_object (object_p);
break;