1 // Copyright 2012 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_HEAP_OBJECTS_VISITING_H_
6 #define V8_HEAP_OBJECTS_VISITING_H_
7
8 #include "src/objects/fixed-array.h"
9 #include "src/objects/map.h"
10 #include "src/objects/objects.h"
11 #include "src/objects/visitors.h"
12
13 namespace v8 {
14 namespace internal {
15
16 #define TYPED_VISITOR_ID_LIST(V) \
17 V(AllocationSite) \
18 V(BigInt) \
19 V(ByteArray) \
20 V(BytecodeArray) \
21 V(Cell) \
22 V(Code) \
23 V(CodeDataContainer) \
24 V(CoverageInfo) \
25 V(DataHandler) \
26 V(EmbedderDataArray) \
27 V(EphemeronHashTable) \
28 V(FeedbackCell) \
29 V(FeedbackMetadata) \
30 V(FixedDoubleArray) \
31 V(JSArrayBuffer) \
32 V(JSDataView) \
33 V(JSExternalObject) \
34 V(JSFinalizationRegistry) \
35 V(JSFunction) \
36 V(JSObject) \
37 V(JSTypedArray) \
38 V(WeakCell) \
39 V(JSWeakCollection) \
40 V(JSWeakRef) \
41 V(Map) \
42 V(NativeContext) \
43 V(Oddball) \
44 V(PreparseData) \
45 V(PromiseOnStack) \
46 V(PropertyArray) \
47 V(PropertyCell) \
48 V(PrototypeInfo) \
49 V(SmallOrderedHashMap) \
50 V(SmallOrderedHashSet) \
51 V(SmallOrderedNameDictionary) \
52 V(SourceTextModule) \
53 V(SwissNameDictionary) \
54 V(Symbol) \
55 V(SyntheticModule) \
56 V(TransitionArray) \
57 IF_WASM(V, WasmApiFunctionRef) \
58 IF_WASM(V, WasmArray) \
59 IF_WASM(V, WasmCapiFunctionData) \
60 IF_WASM(V, WasmExportedFunctionData) \
61 IF_WASM(V, WasmFunctionData) \
62 IF_WASM(V, WasmIndirectFunctionTable) \
63 IF_WASM(V, WasmInstanceObject) \
64 IF_WASM(V, WasmInternalFunction) \
65 IF_WASM(V, WasmJSFunctionData) \
66 IF_WASM(V, WasmStruct) \
67 IF_WASM(V, WasmSuspenderObject) \
68 IF_WASM(V, WasmOnFulfilledData) \
69 IF_WASM(V, WasmTypeInfo)
70
71 #define FORWARD_DECLARE(TypeName) class TypeName;
72 TYPED_VISITOR_ID_LIST(FORWARD_DECLARE)
TORQUE_VISITOR_ID_LIST(FORWARD_DECLARE)73 TORQUE_VISITOR_ID_LIST(FORWARD_DECLARE)
74 #undef FORWARD_DECLARE
75
76 // The base class for visitors that need to dispatch on object type. The default
77 // behavior of all visit functions is to iterate body of the given object using
78 // the BodyDescriptor of the object.
79 //
80 // The visit functions return the size of the object cast to ResultType.
81 //
82 // This class is intended to be used in the following way:
83 //
84 // class SomeVisitor : public HeapVisitor<ResultType, SomeVisitor> {
85 // ...
86 // }
87 template <typename ResultType, typename ConcreteVisitor>
88 class HeapVisitor : public ObjectVisitorWithCageBases {
89 public:
90 inline HeapVisitor(PtrComprCageBase cage_base,
91 PtrComprCageBase code_cage_base);
92 inline explicit HeapVisitor(Isolate* isolate);
93 inline explicit HeapVisitor(Heap* heap);
94
95 V8_INLINE ResultType Visit(HeapObject object);
96 V8_INLINE ResultType Visit(Map map, HeapObject object);
97 // A callback for visiting the map pointer in the object header.
98 V8_INLINE void VisitMapPointer(HeapObject host);
99
100 protected:
101 // A guard predicate for visiting the object.
102 // If it returns false then the default implementations of the Visit*
103 // functions bailout from iterating the object pointers.
104 V8_INLINE bool ShouldVisit(HeapObject object) { return true; }
105 // Guard predicate for visiting the objects map pointer separately.
106 V8_INLINE bool ShouldVisitMapPointer() { return true; }
107 // If this predicate returns false, then the heap visitor will fail
108 // in default Visit implemention for subclasses of JSObject.
109 V8_INLINE bool AllowDefaultJSObjectVisit() { return true; }
110
111 #define VISIT(TypeName) \
112 V8_INLINE ResultType Visit##TypeName(Map map, TypeName object);
113 TYPED_VISITOR_ID_LIST(VISIT)
114 TORQUE_VISITOR_ID_LIST(VISIT)
115 #undef VISIT
116 V8_INLINE ResultType VisitShortcutCandidate(Map map, ConsString object);
117 V8_INLINE ResultType VisitDataObject(Map map, HeapObject object);
118 V8_INLINE ResultType VisitJSObjectFast(Map map, JSObject object);
119 V8_INLINE ResultType VisitJSApiObject(Map map, JSObject object);
120 V8_INLINE ResultType VisitStruct(Map map, HeapObject object);
121 V8_INLINE ResultType VisitFreeSpace(Map map, FreeSpace object);
122
123 template <typename T>
124 static V8_INLINE T Cast(HeapObject object);
125 };
126
127 template <typename ConcreteVisitor>
128 class NewSpaceVisitor : public HeapVisitor<int, ConcreteVisitor> {
129 public:
130 V8_INLINE NewSpaceVisitor(Isolate* isolate);
131
ShouldVisitMapPointer()132 V8_INLINE bool ShouldVisitMapPointer() { return false; }
133
134 // Special cases for young generation.
135
136 V8_INLINE int VisitNativeContext(Map map, NativeContext object);
137 V8_INLINE int VisitJSApiObject(Map map, JSObject object);
138
VisitBytecodeArray(Map map,BytecodeArray object)139 int VisitBytecodeArray(Map map, BytecodeArray object) {
140 UNREACHABLE();
141 return 0;
142 }
143
144 int VisitSharedFunctionInfo(Map map, SharedFunctionInfo object);
145 int VisitWeakCell(Map map, WeakCell weak_cell);
146 };
147
148 class WeakObjectRetainer;
149
150 // A weak list is single linked list where each element has a weak pointer to
151 // the next element. Given the head of the list, this function removes dead
152 // elements from the list and if requested records slots for next-element
153 // pointers. The template parameter T is a WeakListVisitor that defines how to
154 // access the next-element pointers.
155 template <class T>
156 Object VisitWeakList(Heap* heap, Object list, WeakObjectRetainer* retainer);
157 } // namespace internal
158 } // namespace v8
159
160 #endif // V8_HEAP_OBJECTS_VISITING_H_
161