V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
js-weak-refs.h
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 
8 #include "src/objects/js-objects.h"
9 #include "src/objects/microtask.h"
10 
11 // Has to be the last include (doesn't have include guards):
12 #include "src/objects/object-macros.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class JSWeakCell;
18 
19 // WeakFactory object from the JS Weak Refs spec proposal:
20 // https://github.com/tc39/proposal-weakrefs
21 class JSWeakFactory : public JSObject {
22  public:
23  DECL_PRINTER(JSWeakFactory)
24  DECL_VERIFIER(JSWeakFactory)
25  DECL_CAST(JSWeakFactory)
26 
27  DECL_ACCESSORS2(native_context, Context)
28  DECL_ACCESSORS(cleanup, Object)
29  DECL_ACCESSORS(active_cells, Object)
30  DECL_ACCESSORS(cleared_cells, Object)
31 
32  // For storing a list of JSWeakFactory objects in NativeContext.
33  DECL_ACCESSORS(next, Object)
34 
35  DECL_INT_ACCESSORS(flags)
36 
37  // Adds a newly constructed JSWeakCell object into this JSWeakFactory.
38  inline void AddWeakCell(JSWeakCell* weak_cell);
39 
40  // Returns true if the cleared_cells list is non-empty.
41  inline bool NeedsCleanup() const;
42 
43  inline bool scheduled_for_cleanup() const;
44  inline void set_scheduled_for_cleanup(bool scheduled_for_cleanup);
45 
46  // Get and remove the first cleared JSWeakCell from the cleared_cells
47  // list. (Assumes there is one.)
48  inline JSWeakCell* PopClearedCell(Isolate* isolate);
49 
50  // Constructs an iterator for the WeakCells in the cleared_cells list and
51  // calls the user's cleanup function.
52  static void Cleanup(Handle<JSWeakFactory> weak_factory, Isolate* isolate);
53 
54  static const int kNativeContextOffset = JSObject::kHeaderSize;
55  static const int kCleanupOffset = kNativeContextOffset + kPointerSize;
56  static const int kActiveCellsOffset = kCleanupOffset + kPointerSize;
57  static const int kClearedCellsOffset = kActiveCellsOffset + kPointerSize;
58  static const int kNextOffset = kClearedCellsOffset + kPointerSize;
59  static const int kFlagsOffset = kNextOffset + kPointerSize;
60  static const int kSize = kFlagsOffset + kPointerSize;
61 
62  // Bitfields in flags.
63  class ScheduledForCleanupField : public BitField<bool, 0, 1> {};
64 
65  private:
66  DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakFactory);
67 };
68 
69 // WeakCell object from the JS Weak Refs spec proposal.
70 class JSWeakCell : public JSObject {
71  public:
72  DECL_PRINTER(JSWeakCell)
73  DECL_VERIFIER(JSWeakCell)
74  DECL_CAST(JSWeakCell)
75 
76  DECL_ACCESSORS(factory, Object)
77  DECL_ACCESSORS(target, Object)
78  DECL_ACCESSORS(holdings, Object)
79 
80  // For storing doubly linked lists of JSWeakCells in JSWeakFactory.
81  DECL_ACCESSORS(prev, Object)
82  DECL_ACCESSORS(next, Object)
83 
84  static const int kFactoryOffset = JSObject::kHeaderSize;
85  static const int kTargetOffset = kFactoryOffset + kPointerSize;
86  static const int kHoldingsOffset = kTargetOffset + kPointerSize;
87  static const int kPrevOffset = kHoldingsOffset + kPointerSize;
88  static const int kNextOffset = kPrevOffset + kPointerSize;
89  static const int kSize = kNextOffset + kPointerSize;
90 
91  class BodyDescriptor;
92 
93  // Nullify is called during GC and it modifies the pointers in JSWeakCell and
94  // JSWeakFactory. Thus we need to tell the GC about the modified slots via the
95  // gc_notify_updated_slot function. The normal write barrier is not enough,
96  // since it's disabled before GC.
97  inline void Nullify(
98  Isolate* isolate,
99  std::function<void(HeapObject* object, ObjectSlot slot, Object* target)>
100  gc_notify_updated_slot);
101 
102  inline void Clear(Isolate* isolate);
103 
104  private:
105  DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakCell);
106 };
107 
108 class JSWeakRef : public JSWeakCell {
109  public:
110  DECL_CAST(JSWeakRef)
111  private:
112  DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakRef);
113 };
114 
116  public:
117  DECL_ACCESSORS(factory, JSWeakFactory)
118 
119  DECL_CAST(WeakFactoryCleanupJobTask)
120  DECL_VERIFIER(WeakFactoryCleanupJobTask)
121  DECL_PRINTER(WeakFactoryCleanupJobTask)
122 
123  static const int kFactoryOffset = Microtask::kHeaderSize;
124  static const int kSize = kFactoryOffset + kPointerSize;
125 
126  private:
127  DISALLOW_IMPLICIT_CONSTRUCTORS(WeakFactoryCleanupJobTask);
128 };
129 
131  public:
132  DECL_PRINTER(JSWeakFactoryCleanupIterator)
133  DECL_VERIFIER(JSWeakFactoryCleanupIterator)
135 
136  DECL_ACCESSORS(factory, JSWeakFactory)
137 
138  static const int kFactoryOffset = JSObject::kHeaderSize;
139  static const int kSize = kFactoryOffset + kPointerSize;
140 
141  private:
142  DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakFactoryCleanupIterator);
143 };
144 
145 } // namespace internal
146 } // namespace v8
147 
148 #include "src/objects/object-macros-undef.h"
149 
150 #endif // V8_OBJECTS_JS_WEAK_REFS_H_
Definition: libplatform.h:13