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/allocation.h"
9 #include "src/layout-descriptor.h"
10 #include "src/objects-body-descriptors.h"
11 #include "src/objects.h"
12 #include "src/objects/hash-table.h"
13 #include "src/objects/ordered-hash-table.h"
14 #include "src/objects/string.h"
15 #include "src/visitors.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 class BigInt;
21 class BytecodeArray;
22 class DataHandler;
23 class JSArrayBuffer;
24 class JSRegExp;
25 class JSWeakCollection;
26 class UncompiledDataWithoutPreParsedScope;
27 class UncompiledDataWithPreParsedScope;
28 
29 #define TYPED_VISITOR_ID_LIST(V)         \
30   V(AllocationSite)                      \
31   V(BigInt)                              \
32   V(ByteArray)                           \
33   V(BytecodeArray)                       \
34   V(Cell)                                \
35   V(Code)                                \
36   V(CodeDataContainer)                   \
37   V(ConsString)                          \
38   V(DataHandler)                         \
39   V(EphemeronHashTable)                  \
40   V(FeedbackCell)                        \
41   V(FeedbackVector)                      \
42   V(FixedArray)                          \
43   V(FixedDoubleArray)                    \
44   V(FixedFloat64Array)                   \
45   V(FixedTypedArrayBase)                 \
46   V(JSArrayBuffer)                       \
47   V(JSFunction)                          \
48   V(JSObject)                            \
49   V(JSWeakCollection)                    \
50   V(Map)                                 \
51   V(Oddball)                             \
52   V(PreParsedScopeData)                  \
53   V(PropertyArray)                       \
54   V(PropertyCell)                        \
55   V(PrototypeInfo)                       \
56   V(SeqOneByteString)                    \
57   V(SeqTwoByteString)                    \
58   V(SharedFunctionInfo)                  \
59   V(SlicedString)                        \
60   V(SmallOrderedHashMap)                 \
61   V(SmallOrderedHashSet)                 \
62   V(Symbol)                              \
63   V(ThinString)                          \
64   V(TransitionArray)                     \
65   V(UncompiledDataWithoutPreParsedScope) \
66   V(UncompiledDataWithPreParsedScope)    \
67   V(WasmInstanceObject)
68 
69 // The base class for visitors that need to dispatch on object type. The default
70 // behavior of all visit functions is to iterate body of the given object using
71 // the BodyDescriptor of the object.
72 //
73 // The visit functions return the size of the object cast to ResultType.
74 //
75 // This class is intended to be used in the following way:
76 //
77 //   class SomeVisitor : public HeapVisitor<ResultType, SomeVisitor> {
78 //     ...
79 //   }
80 template <typename ResultType, typename ConcreteVisitor>
81 class HeapVisitor : public ObjectVisitor {
82  public:
83   V8_INLINE ResultType Visit(HeapObject* object);
84   V8_INLINE ResultType Visit(Map* map, HeapObject* object);
85 
86  protected:
87   // A guard predicate for visiting the object.
88   // If it returns false then the default implementations of the Visit*
89   // functions bailout from iterating the object pointers.
ShouldVisit(HeapObject * object)90   V8_INLINE bool ShouldVisit(HeapObject* object) { return true; }
91   // Guard predicate for visiting the objects map pointer separately.
ShouldVisitMapPointer()92   V8_INLINE bool ShouldVisitMapPointer() { return true; }
93   // A callback for visiting the map pointer in the object header.
94   V8_INLINE void VisitMapPointer(HeapObject* host, HeapObject** map);
95   // If this predicate returns false, then the heap visitor will fail
96   // in default Visit implemention for subclasses of JSObject.
AllowDefaultJSObjectVisit()97   V8_INLINE bool AllowDefaultJSObjectVisit() { return true; }
98 
99 #define VISIT(type) V8_INLINE ResultType Visit##type(Map* map, type* object);
100   TYPED_VISITOR_ID_LIST(VISIT)
101 #undef VISIT
102   V8_INLINE ResultType VisitShortcutCandidate(Map* map, ConsString* object);
103   V8_INLINE ResultType VisitNativeContext(Map* map, Context* object);
104   V8_INLINE ResultType VisitDataObject(Map* map, HeapObject* object);
105   V8_INLINE ResultType VisitJSObjectFast(Map* map, JSObject* object);
106   V8_INLINE ResultType VisitJSApiObject(Map* map, JSObject* object);
107   V8_INLINE ResultType VisitStruct(Map* map, HeapObject* object);
108   V8_INLINE ResultType VisitFreeSpace(Map* map, FreeSpace* object);
109   V8_INLINE ResultType VisitWeakArray(Map* map, HeapObject* object);
110 
111   template <typename T>
112   static V8_INLINE T* Cast(HeapObject* object);
113 };
114 
115 template <typename ConcreteVisitor>
116 class NewSpaceVisitor : public HeapVisitor<int, ConcreteVisitor> {
117  public:
ShouldVisitMapPointer()118   V8_INLINE bool ShouldVisitMapPointer() { return false; }
119 
120   // Special cases for young generation.
121 
122   V8_INLINE int VisitJSFunction(Map* map, JSFunction* object);
123   V8_INLINE int VisitNativeContext(Map* map, Context* object);
124   V8_INLINE int VisitJSApiObject(Map* map, JSObject* object);
125 
VisitBytecodeArray(Map * map,BytecodeArray * object)126   int VisitBytecodeArray(Map* map, BytecodeArray* object) {
127     UNREACHABLE();
128     return 0;
129   }
130 
VisitSharedFunctionInfo(Map * map,SharedFunctionInfo * object)131   int VisitSharedFunctionInfo(Map* map, SharedFunctionInfo* object) {
132     UNREACHABLE();
133     return 0;
134   }
135 };
136 
137 class WeakObjectRetainer;
138 
139 // A weak list is single linked list where each element has a weak pointer to
140 // the next element. Given the head of the list, this function removes dead
141 // elements from the list and if requested records slots for next-element
142 // pointers. The template parameter T is a WeakListVisitor that defines how to
143 // access the next-element pointers.
144 template <class T>
145 Object* VisitWeakList(Heap* heap, Object* list, WeakObjectRetainer* retainer);
146 }  // namespace internal
147 }  // namespace v8
148 
149 #endif  // V8_HEAP_OBJECTS_VISITING_H_
150