// Copyright 2012 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. #ifndef V8_HEAP_OBJECTS_VISITING_INL_H_ #define V8_HEAP_OBJECTS_VISITING_INL_H_ #include "src/heap/embedder-tracing.h" #include "src/heap/mark-compact.h" #include "src/heap/objects-visiting.h" #include "src/objects/arguments.h" #include "src/objects/data-handler-inl.h" #include "src/objects/free-space-inl.h" #include "src/objects/js-weak-refs-inl.h" #include "src/objects/module-inl.h" #include "src/objects/objects-body-descriptors-inl.h" #include "src/objects/objects-inl.h" #include "src/objects/oddball.h" #include "src/objects/ordered-hash-table.h" #include "src/objects/synthetic-module-inl.h" #include "src/objects/torque-defined-classes.h" #include "src/objects/visitors.h" #if V8_ENABLE_WEBASSEMBLY #include "src/wasm/wasm-objects.h" #endif // V8_ENABLE_WEBASSEMBLY namespace v8 { namespace internal { template HeapVisitor::HeapVisitor( PtrComprCageBase cage_base, PtrComprCageBase code_cage_base) : ObjectVisitorWithCageBases(cage_base, code_cage_base) {} template HeapVisitor::HeapVisitor(Isolate* isolate) : ObjectVisitorWithCageBases(isolate) {} template HeapVisitor::HeapVisitor(Heap* heap) : ObjectVisitorWithCageBases(heap) {} template template T HeapVisitor::Cast(HeapObject object) { return T::cast(object); } template ResultType HeapVisitor::Visit(HeapObject object) { return Visit(object.map(cage_base()), object); } template ResultType HeapVisitor::Visit(Map map, HeapObject object) { ConcreteVisitor* visitor = static_cast(this); switch (map.visitor_id()) { #define CASE(TypeName) \ case kVisit##TypeName: \ return visitor->Visit##TypeName( \ map, ConcreteVisitor::template Cast(object)); TYPED_VISITOR_ID_LIST(CASE) TORQUE_VISITOR_ID_LIST(CASE) #undef CASE case kVisitShortcutCandidate: return visitor->VisitShortcutCandidate( map, ConcreteVisitor::template Cast(object)); case kVisitDataObject: return visitor->VisitDataObject(map, object); case kVisitJSObjectFast: return visitor->VisitJSObjectFast( map, ConcreteVisitor::template Cast(object)); case kVisitJSApiObject: return visitor->VisitJSApiObject( map, ConcreteVisitor::template Cast(object)); case kVisitStruct: return visitor->VisitStruct(map, object); case kVisitFreeSpace: return visitor->VisitFreeSpace(map, FreeSpace::cast(object)); case kDataOnlyVisitorIdCount: case kVisitorIdCount: UNREACHABLE(); } UNREACHABLE(); // Make the compiler happy. return ResultType(); } template void HeapVisitor::VisitMapPointer( HeapObject host) { DCHECK(!host.map_word(kRelaxedLoad).IsForwardingAddress()); if (!static_cast(this)->ShouldVisitMapPointer()) return; static_cast(this)->VisitMapPointer(host); } #define VISIT(TypeName) \ template \ ResultType HeapVisitor::Visit##TypeName( \ Map map, TypeName object) { \ ConcreteVisitor* visitor = static_cast(this); \ if (!visitor->ShouldVisit(object)) return ResultType(); \ if (!visitor->AllowDefaultJSObjectVisit()) { \ DCHECK_WITH_MSG(!map.IsJSObjectMap(), \ "Implement custom visitor for new JSObject subclass in " \ "concurrent marker"); \ } \ int size = TypeName::BodyDescriptor::SizeOf(map, object); \ if (visitor->ShouldVisitMapPointer()) { \ visitor->VisitMapPointer(object); \ } \ TypeName::BodyDescriptor::IterateBody(map, object, size, visitor); \ return static_cast(size); \ } TYPED_VISITOR_ID_LIST(VISIT) TORQUE_VISITOR_ID_LIST(VISIT) #undef VISIT template ResultType HeapVisitor::VisitShortcutCandidate( Map map, ConsString object) { return static_cast(this)->VisitConsString(map, object); } template ResultType HeapVisitor::VisitDataObject( Map map, HeapObject object) { ConcreteVisitor* visitor = static_cast(this); if (!visitor->ShouldVisit(object)) return ResultType(); int size = map.instance_size(); if (visitor->ShouldVisitMapPointer()) { visitor->VisitMapPointer(object); } #ifdef V8_SANDBOXED_EXTERNAL_POINTERS // The following types have external pointers, which must be visited. // TODO(v8:10391) Consider adding custom visitor IDs for these. if (object.IsExternalOneByteString()) { ExternalOneByteString::BodyDescriptor::IterateBody(map, object, size, visitor); } else if (object.IsExternalTwoByteString()) { ExternalTwoByteString::BodyDescriptor::IterateBody(map, object, size, visitor); } else if (object.IsForeign()) { Foreign::BodyDescriptor::IterateBody(map, object, size, visitor); } #endif // V8_SANDBOXED_EXTERNAL_POINTERS return static_cast(size); } template ResultType HeapVisitor::VisitJSObjectFast( Map map, JSObject object) { ConcreteVisitor* visitor = static_cast(this); if (!visitor->ShouldVisit(object)) return ResultType(); int size = JSObject::FastBodyDescriptor::SizeOf(map, object); if (visitor->ShouldVisitMapPointer()) { visitor->VisitMapPointer(object); } JSObject::FastBodyDescriptor::IterateBody(map, object, size, visitor); return static_cast(size); } template ResultType HeapVisitor::VisitJSApiObject( Map map, JSObject object) { ConcreteVisitor* visitor = static_cast(this); if (!visitor->ShouldVisit(object)) return ResultType(); int size = JSObject::BodyDescriptor::SizeOf(map, object); if (visitor->ShouldVisitMapPointer()) { visitor->VisitMapPointer(object); } JSObject::BodyDescriptor::IterateBody(map, object, size, visitor); return static_cast(size); } template ResultType HeapVisitor::VisitStruct( Map map, HeapObject object) { ConcreteVisitor* visitor = static_cast(this); if (!visitor->ShouldVisit(object)) return ResultType(); int size = map.instance_size(); if (visitor->ShouldVisitMapPointer()) { visitor->VisitMapPointer(object); } StructBodyDescriptor::IterateBody(map, object, size, visitor); return static_cast(size); } template ResultType HeapVisitor::VisitFreeSpace( Map map, FreeSpace object) { ConcreteVisitor* visitor = static_cast(this); if (!visitor->ShouldVisit(object)) return ResultType(); if (visitor->ShouldVisitMapPointer()) { visitor->VisitMapPointer(object); } return static_cast(object.size(kRelaxedLoad)); } template NewSpaceVisitor::NewSpaceVisitor(Isolate* isolate) : HeapVisitor(isolate) {} template int NewSpaceVisitor::VisitNativeContext(Map map, NativeContext object) { ConcreteVisitor* visitor = static_cast(this); int size = NativeContext::BodyDescriptor::SizeOf(map, object); NativeContext::BodyDescriptor::IterateBody(map, object, size, visitor); return size; } template int NewSpaceVisitor::VisitJSApiObject(Map map, JSObject object) { ConcreteVisitor* visitor = static_cast(this); return visitor->VisitJSObject(map, object); } template int NewSpaceVisitor::VisitSharedFunctionInfo( Map map, SharedFunctionInfo object) { UNREACHABLE(); return 0; } template int NewSpaceVisitor::VisitWeakCell(Map map, WeakCell weak_cell) { UNREACHABLE(); return 0; } } // namespace internal } // namespace v8 #endif // V8_HEAP_OBJECTS_VISITING_INL_H_