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
|
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/execution/isolate.h"
#include "src/execution/microtask-queue.h"
#include "src/handles/handles-inl.h"
#include "src/heap/factory-inl.h"
#include "src/heap/heap-inl.h"
#include "src/objects/js-objects.h"
#include "src/objects/js-weak-refs-inl.h"
#include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h"
namespace v8 {
namespace internal {
namespace {
Handle<JSFinalizationRegistry> ConstructJSFinalizationRegistry(
Isolate* isolate) {
Factory* factory = isolate->factory();
Handle<String> finalization_registry_name =
factory->NewStringFromStaticChars("FinalizationRegistry");
Handle<Object> global =
handle(isolate->native_context()->global_object(), isolate);
Handle<JSFunction> finalization_registry_fun = Handle<JSFunction>::cast(
Object::GetProperty(isolate, global, finalization_registry_name)
.ToHandleChecked());
auto finalization_registry = Handle<JSFinalizationRegistry>::cast(
JSObject::New(finalization_registry_fun, finalization_registry_fun,
Handle<AllocationSite>::null())
.ToHandleChecked());
#ifdef VERIFY_HEAP
finalization_registry->JSFinalizationRegistryVerify(isolate);
#endif // VERIFY_HEAP
return finalization_registry;
}
Handle<JSWeakRef> ConstructJSWeakRef(Handle<JSReceiver> target,
Isolate* isolate) {
Factory* factory = isolate->factory();
Handle<String> weak_ref_name = factory->WeakRef_string();
Handle<Object> global =
handle(isolate->native_context()->global_object(), isolate);
Handle<JSFunction> weak_ref_fun = Handle<JSFunction>::cast(
Object::GetProperty(isolate, global, weak_ref_name).ToHandleChecked());
auto weak_ref = Handle<JSWeakRef>::cast(
JSObject::New(weak_ref_fun, weak_ref_fun, Handle<AllocationSite>::null())
.ToHandleChecked());
weak_ref->set_target(*target);
#ifdef VERIFY_HEAP
weak_ref->JSWeakRefVerify(isolate);
#endif // VERIFY_HEAP
return weak_ref;
}
Handle<JSObject> CreateKey(const char* key_prop_value, Isolate* isolate) {
Factory* factory = isolate->factory();
Handle<String> key_string = factory->NewStringFromStaticChars("key_string");
Handle<JSObject> key =
isolate->factory()->NewJSObject(isolate->object_function());
JSObject::AddProperty(isolate, key, key_string,
factory->NewStringFromAsciiChecked(key_prop_value),
NONE);
return key;
}
Handle<WeakCell> FinalizationRegistryRegister(
Handle<JSFinalizationRegistry> finalization_registry,
Handle<JSObject> target, Handle<Object> holdings, Handle<Object> key,
Isolate* isolate) {
JSFinalizationRegistry::Register(finalization_registry, target, holdings, key,
isolate);
CHECK(finalization_registry->active_cells().IsWeakCell());
Handle<WeakCell> weak_cell =
handle(WeakCell::cast(finalization_registry->active_cells()), isolate);
#ifdef VERIFY_HEAP
weak_cell->WeakCellVerify(isolate);
#endif // VERIFY_HEAP
return weak_cell;
}
Handle<WeakCell> FinalizationRegistryRegister(
Handle<JSFinalizationRegistry> finalization_registry,
Handle<JSObject> target, Isolate* isolate) {
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
return FinalizationRegistryRegister(finalization_registry, target, undefined,
undefined, isolate);
}
void NullifyWeakCell(Handle<WeakCell> weak_cell, Isolate* isolate) {
auto empty_func = [](HeapObject object, ObjectSlot slot, Object target) {};
weak_cell->Nullify(isolate, empty_func);
#ifdef VERIFY_HEAP
weak_cell->WeakCellVerify(isolate);
#endif // VERIFY_HEAP
}
Object PopClearedCellHoldings(
Handle<JSFinalizationRegistry> finalization_registry, Isolate* isolate) {
// PopClearedCell is implemented in Torque. Reproduce that implementation here
// for testing.
Handle<WeakCell> weak_cell =
handle(WeakCell::cast(finalization_registry->cleared_cells()), isolate);
DCHECK(weak_cell->prev().IsUndefined(isolate));
finalization_registry->set_cleared_cells(weak_cell->next());
weak_cell->set_next(ReadOnlyRoots(isolate).undefined_value());
if (finalization_registry->cleared_cells().IsWeakCell()) {
WeakCell cleared_cells_head =
WeakCell::cast(finalization_registry->cleared_cells());
DCHECK_EQ(cleared_cells_head.prev(), *weak_cell);
cleared_cells_head.set_prev(ReadOnlyRoots(isolate).undefined_value());
} else {
DCHECK(finalization_registry->cleared_cells().IsUndefined(isolate));
}
if (!weak_cell->unregister_token().IsUndefined(isolate)) {
JSFinalizationRegistry::RemoveCellFromUnregisterTokenMap(
isolate, finalization_registry->ptr(), weak_cell->ptr());
}
return weak_cell->holdings();
}
// Usage: VerifyWeakCellChain(isolate, list_head, n, cell1, cell2, ..., celln);
// verifies that list_head == cell1 and cell1, cell2, ..., celln. form a list.
void VerifyWeakCellChain(Isolate* isolate, Object list_head, int n_args, ...) {
CHECK_GE(n_args, 0);
va_list args;
va_start(args, n_args);
if (n_args == 0) {
// Verify empty list
CHECK(list_head.IsUndefined(isolate));
} else {
WeakCell current = WeakCell::cast(Object(va_arg(args, Address)));
CHECK_EQ(current, list_head);
CHECK(current.prev().IsUndefined(isolate));
for (int i = 1; i < n_args; i++) {
WeakCell next = WeakCell::cast(Object(va_arg(args, Address)));
CHECK_EQ(current.next(), next);
CHECK_EQ(next.prev(), current);
current = next;
}
CHECK(current.next().IsUndefined(isolate));
}
va_end(args);
}
// Like VerifyWeakCellChain but verifies the chain created with key_list_prev
// and key_list_next instead of prev and next.
void VerifyWeakCellKeyChain(Isolate* isolate, SimpleNumberDictionary key_map,
Object unregister_token, int n_args, ...) {
CHECK_GE(n_args, 0);
va_list args;
va_start(args, n_args);
Object hash = unregister_token.GetHash();
InternalIndex entry = InternalIndex::NotFound();
if (!hash.IsUndefined(isolate)) {
uint32_t key = Smi::ToInt(hash);
entry = key_map.FindEntry(isolate, key);
}
if (n_args == 0) {
// Verify empty list
CHECK(entry.is_not_found());
} else {
CHECK(entry.is_found());
WeakCell current = WeakCell::cast(Object(va_arg(args, Address)));
Object list_head = key_map.ValueAt(entry);
CHECK_EQ(current, list_head);
CHECK(current.key_list_prev().IsUndefined(isolate));
for (int i = 1; i < n_args; i++) {
WeakCell next = WeakCell::cast(Object(va_arg(args, Address)));
CHECK_EQ(current.key_list_next(), next);
CHECK_EQ(next.key_list_prev(), current);
current = next;
}
CHECK(current.key_list_next().IsUndefined(isolate));
}
va_end(args);
}
Handle<JSWeakRef> MakeWeakRefAndKeepDuringJob(Isolate* isolate) {
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(js_object, isolate);
isolate->heap()->KeepDuringJob(js_object);
return inner_scope.CloseAndEscape(inner_weak_ref);
}
} // namespace
TEST(TestRegister) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
// Register a weak reference and verify internal data structures.
Handle<WeakCell> weak_cell1 =
FinalizationRegistryRegister(finalization_registry, js_object, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 1,
*weak_cell1);
CHECK(weak_cell1->key_list_prev().IsUndefined(isolate));
CHECK(weak_cell1->key_list_next().IsUndefined(isolate));
CHECK(finalization_registry->cleared_cells().IsUndefined(isolate));
// No key was used during registration, key-based map stays uninitialized.
CHECK(finalization_registry->key_map().IsUndefined(isolate));
// Register another weak reference and verify internal data structures.
Handle<WeakCell> weak_cell2 =
FinalizationRegistryRegister(finalization_registry, js_object, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 2,
*weak_cell2, *weak_cell1);
CHECK(weak_cell2->key_list_prev().IsUndefined(isolate));
CHECK(weak_cell2->key_list_next().IsUndefined(isolate));
CHECK(finalization_registry->cleared_cells().IsUndefined(isolate));
CHECK(finalization_registry->key_map().IsUndefined(isolate));
}
TEST(TestRegisterWithKey) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<JSObject> token2 = CreateKey("token2", isolate);
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
// Register a weak reference with a key and verify internal data structures.
Handle<WeakCell> weak_cell1 = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 1, *weak_cell1);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 0);
}
// Register another weak reference with a different key and verify internal
// data structures.
Handle<WeakCell> weak_cell2 = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 1, *weak_cell1);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 1, *weak_cell2);
}
// Register another weak reference with token1 and verify internal data
// structures.
Handle<WeakCell> weak_cell3 = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell3,
*weak_cell1);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 1, *weak_cell2);
}
}
TEST(TestWeakCellNullify1) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<WeakCell> weak_cell1 =
FinalizationRegistryRegister(finalization_registry, js_object, isolate);
Handle<WeakCell> weak_cell2 =
FinalizationRegistryRegister(finalization_registry, js_object, isolate);
// Nullify the first WeakCell and verify internal data structures.
NullifyWeakCell(weak_cell1, isolate);
CHECK_EQ(finalization_registry->active_cells(), *weak_cell2);
CHECK(weak_cell2->prev().IsUndefined(isolate));
CHECK(weak_cell2->next().IsUndefined(isolate));
CHECK_EQ(finalization_registry->cleared_cells(), *weak_cell1);
CHECK(weak_cell1->prev().IsUndefined(isolate));
CHECK(weak_cell1->next().IsUndefined(isolate));
// Nullify the second WeakCell and verify internal data structures.
NullifyWeakCell(weak_cell2, isolate);
CHECK(finalization_registry->active_cells().IsUndefined(isolate));
CHECK_EQ(finalization_registry->cleared_cells(), *weak_cell2);
CHECK_EQ(weak_cell2->next(), *weak_cell1);
CHECK(weak_cell2->prev().IsUndefined(isolate));
CHECK_EQ(weak_cell1->prev(), *weak_cell2);
CHECK(weak_cell1->next().IsUndefined(isolate));
}
TEST(TestWeakCellNullify2) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<WeakCell> weak_cell1 =
FinalizationRegistryRegister(finalization_registry, js_object, isolate);
Handle<WeakCell> weak_cell2 =
FinalizationRegistryRegister(finalization_registry, js_object, isolate);
// Like TestWeakCellNullify1 but nullify the WeakCells in opposite order.
NullifyWeakCell(weak_cell2, isolate);
CHECK_EQ(finalization_registry->active_cells(), *weak_cell1);
CHECK(weak_cell1->prev().IsUndefined(isolate));
CHECK(weak_cell1->next().IsUndefined(isolate));
CHECK_EQ(finalization_registry->cleared_cells(), *weak_cell2);
CHECK(weak_cell2->prev().IsUndefined(isolate));
CHECK(weak_cell2->next().IsUndefined(isolate));
NullifyWeakCell(weak_cell1, isolate);
CHECK(finalization_registry->active_cells().IsUndefined(isolate));
CHECK_EQ(finalization_registry->cleared_cells(), *weak_cell1);
CHECK_EQ(weak_cell1->next(), *weak_cell2);
CHECK(weak_cell1->prev().IsUndefined(isolate));
CHECK_EQ(weak_cell2->prev(), *weak_cell1);
CHECK(weak_cell2->next().IsUndefined(isolate));
}
TEST(TestJSFinalizationRegistryPopClearedCellHoldings1) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
Factory* factory = isolate->factory();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
Handle<Object> holdings1 = factory->NewStringFromAsciiChecked("holdings1");
Handle<WeakCell> weak_cell1 = FinalizationRegistryRegister(
finalization_registry, js_object, holdings1, undefined, isolate);
Handle<Object> holdings2 = factory->NewStringFromAsciiChecked("holdings2");
Handle<WeakCell> weak_cell2 = FinalizationRegistryRegister(
finalization_registry, js_object, holdings2, undefined, isolate);
Handle<Object> holdings3 = factory->NewStringFromAsciiChecked("holdings3");
Handle<WeakCell> weak_cell3 = FinalizationRegistryRegister(
finalization_registry, js_object, holdings3, undefined, isolate);
NullifyWeakCell(weak_cell2, isolate);
NullifyWeakCell(weak_cell3, isolate);
CHECK(finalization_registry->NeedsCleanup());
Object cleared1 = PopClearedCellHoldings(finalization_registry, isolate);
CHECK_EQ(cleared1, *holdings3);
CHECK(weak_cell3->prev().IsUndefined(isolate));
CHECK(weak_cell3->next().IsUndefined(isolate));
CHECK(finalization_registry->NeedsCleanup());
Object cleared2 = PopClearedCellHoldings(finalization_registry, isolate);
CHECK_EQ(cleared2, *holdings2);
CHECK(weak_cell2->prev().IsUndefined(isolate));
CHECK(weak_cell2->next().IsUndefined(isolate));
CHECK(!finalization_registry->NeedsCleanup());
NullifyWeakCell(weak_cell1, isolate);
CHECK(finalization_registry->NeedsCleanup());
Object cleared3 = PopClearedCellHoldings(finalization_registry, isolate);
CHECK_EQ(cleared3, *holdings1);
CHECK(weak_cell1->prev().IsUndefined(isolate));
CHECK(weak_cell1->next().IsUndefined(isolate));
CHECK(!finalization_registry->NeedsCleanup());
CHECK(finalization_registry->active_cells().IsUndefined(isolate));
CHECK(finalization_registry->cleared_cells().IsUndefined(isolate));
}
TEST(TestJSFinalizationRegistryPopClearedCellHoldings2) {
// Test that when all WeakCells for a key are popped, the key is removed from
// the key map.
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
Factory* factory = isolate->factory();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<Object> holdings1 = factory->NewStringFromAsciiChecked("holdings1");
Handle<WeakCell> weak_cell1 = FinalizationRegistryRegister(
finalization_registry, js_object, holdings1, token1, isolate);
Handle<Object> holdings2 = factory->NewStringFromAsciiChecked("holdings2");
Handle<WeakCell> weak_cell2 = FinalizationRegistryRegister(
finalization_registry, js_object, holdings2, token1, isolate);
NullifyWeakCell(weak_cell1, isolate);
NullifyWeakCell(weak_cell2, isolate);
// Nullifying doesn't affect the key chains (just moves WeakCells from
// active_cells to cleared_cells).
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell2,
*weak_cell1);
}
Object cleared1 = PopClearedCellHoldings(finalization_registry, isolate);
CHECK_EQ(cleared1, *holdings2);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 1, *weak_cell1);
}
Object cleared2 = PopClearedCellHoldings(finalization_registry, isolate);
CHECK_EQ(cleared2, *holdings1);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 0);
}
}
TEST(TestUnregisterActiveCells) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<JSObject> token2 = CreateKey("token2", isolate);
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
Handle<WeakCell> weak_cell1a = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
Handle<WeakCell> weak_cell1b = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
Handle<WeakCell> weak_cell2a = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
Handle<WeakCell> weak_cell2b = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 4,
*weak_cell2b, *weak_cell2a, *weak_cell1b, *weak_cell1a);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell1b,
*weak_cell1a);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 2, *weak_cell2b,
*weak_cell2a);
}
JSFinalizationRegistry::Unregister(finalization_registry, token1, isolate);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 0);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 2, *weak_cell2b,
*weak_cell2a);
}
// Both weak_cell1a and weak_cell1b removed from active_cells.
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 2,
*weak_cell2b, *weak_cell2a);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
}
TEST(TestUnregisterActiveAndClearedCells) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<JSObject> token2 = CreateKey("token2", isolate);
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
Handle<WeakCell> weak_cell1a = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
Handle<WeakCell> weak_cell1b = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
Handle<WeakCell> weak_cell2a = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
Handle<WeakCell> weak_cell2b = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
NullifyWeakCell(weak_cell2a, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 3,
*weak_cell2b, *weak_cell1b, *weak_cell1a);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 1,
*weak_cell2a);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell1b,
*weak_cell1a);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 2, *weak_cell2b,
*weak_cell2a);
}
JSFinalizationRegistry::Unregister(finalization_registry, token2, isolate);
// Both weak_cell2a and weak_cell2b removed.
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 2,
*weak_cell1b, *weak_cell1a);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell1b,
*weak_cell1a);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 0);
}
}
TEST(TestWeakCellUnregisterTwice) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
Handle<WeakCell> weak_cell1 = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 1,
*weak_cell1);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 1, *weak_cell1);
}
JSFinalizationRegistry::Unregister(finalization_registry, token1, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 0);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 0);
}
JSFinalizationRegistry::Unregister(finalization_registry, token1, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 0);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 0);
}
}
TEST(TestWeakCellUnregisterPopped) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
Factory* factory = isolate->factory();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<Object> holdings1 = factory->NewStringFromAsciiChecked("holdings1");
Handle<WeakCell> weak_cell1 = FinalizationRegistryRegister(
finalization_registry, js_object, holdings1, token1, isolate);
NullifyWeakCell(weak_cell1, isolate);
CHECK(finalization_registry->NeedsCleanup());
Object cleared1 = PopClearedCellHoldings(finalization_registry, isolate);
CHECK_EQ(cleared1, *holdings1);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 0);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 0);
}
JSFinalizationRegistry::Unregister(finalization_registry, token1, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 0);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 0);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 0);
}
}
TEST(TestWeakCellUnregisterNonexistentKey) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> token1 = CreateKey("token1", isolate);
JSFinalizationRegistry::Unregister(finalization_registry, token1, isolate);
}
TEST(TestJSWeakRef) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref;
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
// This doesn't add the target into the KeepDuringJob set.
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(js_object, isolate);
CcTest::CollectAllGarbage();
CHECK(!inner_weak_ref->target().IsUndefined(isolate));
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
CHECK(!weak_ref->target().IsUndefined(isolate));
CcTest::CollectAllGarbage();
CHECK(weak_ref->target().IsUndefined(isolate));
}
TEST(TestJSWeakRefIncrementalMarking) {
FLAG_harmony_weak_refs = true;
if (!FLAG_incremental_marking) {
return;
}
ManualGCScope manual_gc_scope;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
Heap* heap = isolate->heap();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref;
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
// This doesn't add the target into the KeepDuringJob set.
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(js_object, isolate);
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(!inner_weak_ref->target().IsUndefined(isolate));
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
CHECK(!weak_ref->target().IsUndefined(isolate));
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(weak_ref->target().IsUndefined(isolate));
}
TEST(TestJSWeakRefKeepDuringJob) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref = MakeWeakRefAndKeepDuringJob(isolate);
CHECK(!weak_ref->target().IsUndefined(isolate));
CcTest::CollectAllGarbage();
CHECK(!weak_ref->target().IsUndefined(isolate));
// Clears the KeepDuringJob set.
context->GetIsolate()->ClearKeptObjects();
CcTest::CollectAllGarbage();
CHECK(weak_ref->target().IsUndefined(isolate));
weak_ref = MakeWeakRefAndKeepDuringJob(isolate);
CHECK(!weak_ref->target().IsUndefined(isolate));
CcTest::CollectAllGarbage();
CHECK(!weak_ref->target().IsUndefined(isolate));
// ClearKeptObjects should be called by PerformMicrotasksCheckpoint.
CcTest::isolate()->PerformMicrotaskCheckpoint();
CcTest::CollectAllGarbage();
CHECK(weak_ref->target().IsUndefined(isolate));
weak_ref = MakeWeakRefAndKeepDuringJob(isolate);
CHECK(!weak_ref->target().IsUndefined(isolate));
CcTest::CollectAllGarbage();
CHECK(!weak_ref->target().IsUndefined(isolate));
// ClearKeptObjects should be called by MicrotasksScope::PerformCheckpoint.
v8::MicrotasksScope::PerformCheckpoint(CcTest::isolate());
CcTest::CollectAllGarbage();
CHECK(weak_ref->target().IsUndefined(isolate));
}
TEST(TestJSWeakRefKeepDuringJobIncrementalMarking) {
FLAG_harmony_weak_refs = true;
if (!FLAG_incremental_marking) {
return;
}
ManualGCScope manual_gc_scope;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
Heap* heap = isolate->heap();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref = MakeWeakRefAndKeepDuringJob(isolate);
CHECK(!weak_ref->target().IsUndefined(isolate));
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(!weak_ref->target().IsUndefined(isolate));
// Clears the KeepDuringJob set.
context->GetIsolate()->ClearKeptObjects();
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(weak_ref->target().IsUndefined(isolate));
}
TEST(TestRemoveUnregisterToken) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSFinalizationRegistry> finalization_registry =
ConstructJSFinalizationRegistry(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSObject> token1 = CreateKey("token1", isolate);
Handle<JSObject> token2 = CreateKey("token2", isolate);
Handle<Object> undefined =
handle(ReadOnlyRoots(isolate).undefined_value(), isolate);
Handle<WeakCell> weak_cell1a = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
Handle<WeakCell> weak_cell1b = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token1, isolate);
Handle<WeakCell> weak_cell2a = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
Handle<WeakCell> weak_cell2b = FinalizationRegistryRegister(
finalization_registry, js_object, undefined, token2, isolate);
NullifyWeakCell(weak_cell2a, isolate);
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 3,
*weak_cell2b, *weak_cell1b, *weak_cell1a);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 1,
*weak_cell2a);
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell1b,
*weak_cell1a);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 2, *weak_cell2b,
*weak_cell2a);
}
finalization_registry->RemoveUnregisterToken(
JSReceiver::cast(*token2), isolate,
[undefined](WeakCell matched_cell) {
matched_cell.set_unregister_token(*undefined);
},
[](HeapObject, ObjectSlot, Object) {});
// Both weak_cell2a and weak_cell2b remain on the weak cell chains.
VerifyWeakCellChain(isolate, finalization_registry->active_cells(), 3,
*weak_cell2b, *weak_cell1b, *weak_cell1a);
VerifyWeakCellChain(isolate, finalization_registry->cleared_cells(), 1,
*weak_cell2a);
// But both weak_cell2a and weak_cell2b are removed from the key chain.
{
SimpleNumberDictionary key_map =
SimpleNumberDictionary::cast(finalization_registry->key_map());
VerifyWeakCellKeyChain(isolate, key_map, *token1, 2, *weak_cell1b,
*weak_cell1a);
VerifyWeakCellKeyChain(isolate, key_map, *token2, 0);
}
}
TEST(JSWeakRefScavengedInWorklist) {
FLAG_harmony_weak_refs = true;
if (!FLAG_incremental_marking) {
return;
}
ManualGCScope manual_gc_scope;
CcTest::InitializeVM();
Isolate* isolate = CcTest::i_isolate();
Heap* heap = isolate->heap();
{
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref;
// Make a WeakRef that points to a target, both of which become unreachable.
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(js_object, isolate);
CHECK(Heap::InYoungGeneration(*js_object));
CHECK(Heap::InYoungGeneration(*inner_weak_ref));
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
// Do marking. This puts the WeakRef above into the js_weak_refs worklist
// since its target isn't marked.
CHECK(
heap->mark_compact_collector()->weak_objects()->js_weak_refs.IsEmpty());
heap::SimulateIncrementalMarking(heap, true);
CHECK(!heap->mark_compact_collector()
->weak_objects()
->js_weak_refs.IsEmpty());
}
// Now collect both weak_ref and its target. The worklist should be empty.
CcTest::CollectGarbage(NEW_SPACE);
CHECK(heap->mark_compact_collector()->weak_objects()->js_weak_refs.IsEmpty());
// The mark-compactor shouldn't see zapped WeakRefs in the worklist.
CcTest::CollectAllGarbage();
}
TEST(JSWeakRefTenuredInWorklist) {
FLAG_harmony_weak_refs = true;
if (!FLAG_incremental_marking) {
return;
}
ManualGCScope manual_gc_scope;
CcTest::InitializeVM();
Isolate* isolate = CcTest::i_isolate();
Heap* heap = isolate->heap();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref;
// Make a WeakRef that points to a target. The target becomes unreachable.
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(js_object, isolate);
CHECK(Heap::InYoungGeneration(*js_object));
CHECK(Heap::InYoungGeneration(*inner_weak_ref));
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
JSWeakRef old_weak_ref_location = *weak_ref;
// Do marking. This puts the WeakRef above into the js_weak_refs worklist
// since its target isn't marked.
CHECK(heap->mark_compact_collector()->weak_objects()->js_weak_refs.IsEmpty());
heap::SimulateIncrementalMarking(heap, true);
CHECK(
!heap->mark_compact_collector()->weak_objects()->js_weak_refs.IsEmpty());
// Now collect weak_ref's target. We still have a Handle to weak_ref, so it is
// moved and remains on the worklist.
CcTest::CollectGarbage(NEW_SPACE);
JSWeakRef new_weak_ref_location = *weak_ref;
CHECK_NE(old_weak_ref_location, new_weak_ref_location);
CHECK(
!heap->mark_compact_collector()->weak_objects()->js_weak_refs.IsEmpty());
// The mark-compactor should see the moved WeakRef in the worklist.
CcTest::CollectAllGarbage();
CHECK(heap->mark_compact_collector()->weak_objects()->js_weak_refs.IsEmpty());
CHECK(weak_ref->target().IsUndefined(isolate));
}
} // namespace internal
} // namespace v8
|