v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
js-weak-refs.h
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_JS_WEAK_REFS_H_
6#define V8_OBJECTS_JS_WEAK_REFS_H_
7
9#include "torque-generated/bit-fields.h"
10
11// Has to be the last include (doesn't have include guards):
13
14namespace v8 {
15namespace internal {
16
17class NativeContext;
18class WeakCell;
19
20#include "torque-generated/src/objects/js-weak-refs-tq.inc"
21
22// FinalizationRegistry object from the JS Weak Refs spec proposal:
23// https://github.com/tc39/proposal-weakrefs
25 : public TorqueGeneratedJSFinalizationRegistry<JSFinalizationRegistry,
26 JSObject> {
27 public:
30
31 DECL_BOOLEAN_ACCESSORS(scheduled_for_cleanup)
32
33 class BodyDescriptor;
34
36 DirectHandle<JSFinalizationRegistry> finalization_registry,
37 DirectHandle<WeakCell> weak_cell, Isolate* isolate);
38 inline static bool Unregister(
39 DirectHandle<JSFinalizationRegistry> finalization_registry,
40 DirectHandle<HeapObject> unregister_token, Isolate* isolate);
41
42 // RemoveUnregisterToken is called from both Unregister and during GC. Since
43 // it modifies slots in key_map and WeakCells and the normal write barrier is
44 // disabled during GC, we need to tell the GC about the modified slots via the
45 // gc_notify_updated_slot function.
50 template <typename GCNotifyUpdatedSlotCallback>
51 inline bool RemoveUnregisterToken(
52 Tagged<HeapObject> unregister_token, Isolate* isolate,
53 RemoveUnregisterTokenMode removal_mode,
54 GCNotifyUpdatedSlotCallback gc_notify_updated_slot);
55
56 // Returns true if the cleared_cells list is non-empty.
57 inline bool NeedsCleanup() const;
58
60 Isolate* isolate, bool* key_map_may_need_shrink);
61
62 static void ShrinkKeyMap(
63 Isolate* isolate,
64 DirectHandle<JSFinalizationRegistry> finalization_registry);
65
66 // Pop cleared cells and call their finalizers.
67 static Maybe<bool> Cleanup(
68 Isolate* isolate,
69 DirectHandle<JSFinalizationRegistry> finalization_registry);
70
71 // Remove the already-popped weak_cell from its unregister token linked list,
72 // as well as removing the entry from the key map if it is the only WeakCell
73 // with its unregister token. This method cannot GC and does not shrink the
74 // key map. Asserts that weak_cell has a non-undefined unregister token.
76 Isolate* isolate, Tagged<WeakCell> weak_cell);
77
78 // Bitfields in flags.
79 DEFINE_TORQUE_GENERATED_FINALIZATION_REGISTRY_FLAGS()
80
82};
83
84// Internal object for storing weak references in JSFinalizationRegistry.
85class WeakCell : public TorqueGeneratedWeakCell<WeakCell, HeapObject> {
86 public:
88
89 class BodyDescriptor;
90
91 // Provide relaxed load access to target field.
92 inline Tagged<HeapObject> relaxed_target() const;
93
94 // Provide relaxed load access to the unregister token field.
95 inline Tagged<HeapObject> relaxed_unregister_token() const;
96
97 // Nullify is called during GC and it modifies the pointers in WeakCell and
98 // JSFinalizationRegistry. Thus we need to tell the GC about the modified
99 // slots via the gc_notify_updated_slot function. The normal write barrier is
100 // not enough, since it's disabled before GC.
101 template <typename GCNotifyUpdatedSlotCallback>
102 inline void Nullify(Isolate* isolate,
103 GCNotifyUpdatedSlotCallback gc_notify_updated_slot);
104
105 inline void RemoveFromFinalizationRegistryCells(Isolate* isolate);
106
108};
109
110class JSWeakRef : public TorqueGeneratedJSWeakRef<JSWeakRef, JSObject> {
111 public:
114
115 class BodyDescriptor;
116
118};
119
120} // namespace internal
121} // namespace v8
122
124
125#endif // V8_OBJECTS_JS_WEAK_REFS_H_
static bool Unregister(DirectHandle< JSFinalizationRegistry > finalization_registry, DirectHandle< HeapObject > unregister_token, Isolate *isolate)
static void ShrinkKeyMap(Isolate *isolate, DirectHandle< JSFinalizationRegistry > finalization_registry)
V8_EXPORT_PRIVATE void RemoveCellFromUnregisterTokenMap(Isolate *isolate, Tagged< WeakCell > weak_cell)
static void RegisterWeakCellWithUnregisterToken(DirectHandle< JSFinalizationRegistry > finalization_registry, DirectHandle< WeakCell > weak_cell, Isolate *isolate)
V8_EXPORT_PRIVATE Tagged< WeakCell > PopClearedCell(Isolate *isolate, bool *key_map_may_need_shrink)
bool RemoveUnregisterToken(Tagged< HeapObject > unregister_token, Isolate *isolate, RemoveUnregisterTokenMode removal_mode, GCNotifyUpdatedSlotCallback gc_notify_updated_slot)
static Maybe< bool > Cleanup(Isolate *isolate, DirectHandle< JSFinalizationRegistry > finalization_registry)
#define DECL_BOOLEAN_ACCESSORS(name)
#define DECL_PRINTER(Name)
#define TQ_OBJECT_CONSTRUCTORS(Type)
#define EXPORT_DECL_VERIFIER(Name)
#define V8_EXPORT_PRIVATE
Definition macros.h:460