5 #ifndef V8_HANDLES_INL_H_ 6 #define V8_HANDLES_INL_H_ 8 #include "src/handles.h" 9 #include "src/isolate.h" 15 HandleBase::HandleBase(Address
object, Isolate* isolate)
16 : location_(HandleScope::GetHandle(isolate, object)) {}
20 template <
typename T1,
typename>
21 Handle<T> Handle<T>::New(T*
object, Isolate* isolate) {
22 return Handle(reinterpret_cast<T**>(
23 HandleScope::CreateHandle(isolate, reinterpret_cast<Address>(
object))));
26 template <
typename T1,
typename>
27 Handle<T> Handle<T>::New(T
object, Isolate* isolate) {
28 return Handle(HandleScope::CreateHandle(isolate,
object.ptr()));
33 const Handle<T> Handle<T>::cast(Handle<S> that) {
34 T::cast(*reinterpret_cast<Object**>(that.location()));
35 return Handle<T>(that.location_);
38 HandleScope::HandleScope(Isolate* isolate) {
39 HandleScopeData* data = isolate->handle_scope_data();
41 prev_next_ = data->next;
42 prev_limit_ = data->limit;
47 template <
typename T1,
typename>
48 Handle<T>::Handle(T*
object, Isolate* isolate)
49 : HandleBase(reinterpret_cast<Address>(object), isolate) {}
52 template <
typename T1,
typename>
53 Handle<T>::Handle(T
object, Isolate* isolate)
54 : HandleBase(object.ptr(), isolate) {}
56 template <
typename T,
typename =
typename std::enable_if<
57 std::is_base_of<Object, T>::value>::type>
58 V8_INLINE Handle<T> handle(T*
object, Isolate* isolate) {
59 return Handle<T>(object, isolate);
62 template <
typename T,
typename =
typename std::enable_if<
63 std::is_base_of<ObjectPtr, T>::value>::type>
64 V8_INLINE Handle<T> handle(T
object, Isolate* isolate) {
65 return Handle<T>(object, isolate);
69 inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) {
70 return os << Brief(*handle);
73 HandleScope::~HandleScope() {
75 if (FLAG_check_handle_count) {
76 int before = NumberOfHandles(isolate_);
77 CloseScope(isolate_, prev_next_, prev_limit_);
78 int after = NumberOfHandles(isolate_);
79 DCHECK_LT(after - before, kCheckHandleThreshold);
80 DCHECK_LT(before, kCheckHandleThreshold);
83 CloseScope(isolate_, prev_next_, prev_limit_);
89 void HandleScope::CloseScope(Isolate* isolate, Address* prev_next,
90 Address* prev_limit) {
91 HandleScopeData* current = isolate->handle_scope_data();
93 std::swap(current->next, prev_next);
95 Address* limit = prev_next;
96 if (current->limit != prev_limit) {
97 current->limit = prev_limit;
99 DeleteExtensions(isolate);
101 #ifdef ENABLE_HANDLE_ZAPPING 102 ZapRange(current->next, limit);
104 MSAN_ALLOCATED_UNINITIALIZED_MEMORY(
106 static_cast<size_t>(reinterpret_cast<Address>(limit) -
107 reinterpret_cast<Address>(current->next)));
110 template <
typename T>
111 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) {
112 HandleScopeData* current = isolate_->handle_scope_data();
115 typename std::conditional<std::is_base_of<Object, T>::value, T*, T>::type
118 ValueType value = *handle_value;
120 CloseScope(isolate_, prev_next_, prev_limit_);
122 DCHECK(current->level > current->sealed_level);
123 Handle<T> result(value, isolate_);
126 prev_next_ = current->next;
127 prev_limit_ = current->limit;
132 Address* HandleScope::CreateHandle(Isolate* isolate, Address value) {
133 DCHECK(AllowHandleAllocation::IsAllowed());
134 HandleScopeData* data = isolate->handle_scope_data();
135 Address* result = data->next;
136 if (result == data->limit) {
137 result = Extend(isolate);
141 DCHECK_LT(reinterpret_cast<Address>(result),
142 reinterpret_cast<Address>(data->limit));
143 data->next =
reinterpret_cast<Address*
>(
reinterpret_cast<Address
>(result) +
149 Address* HandleScope::GetHandle(Isolate* isolate, Address value) {
150 DCHECK(AllowHandleAllocation::IsAllowed());
151 HandleScopeData* data = isolate->handle_scope_data();
152 CanonicalHandleScope* canonical = data->canonical_scope;
153 return canonical ? canonical->Lookup(value) : CreateHandle(isolate, value);
158 inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) {
160 DCHECK(AllowHandleAllocation::IsAllowed());
161 HandleScopeData* current = isolate_->handle_scope_data();
164 prev_limit_ = current->limit;
165 current->limit = current->next;
166 prev_sealed_level_ = current->sealed_level;
167 current->sealed_level = current->level;
171 inline SealHandleScope::~SealHandleScope() {
174 HandleScopeData* current = isolate_->handle_scope_data();
175 DCHECK_EQ(current->next, current->limit);
176 current->limit = prev_limit_;
177 DCHECK_EQ(current->level, current->sealed_level);
178 current->sealed_level = prev_sealed_level_;
186 #endif // V8_HANDLES_INL_H_