5 #include "src/handles.h" 7 #include "src/address-map.h" 9 #include "src/base/logging.h" 10 #include "src/identity-map.h" 11 #include "src/maybe-handles.h" 12 #include "src/objects-inl.h" 13 #include "src/roots-inl.h" 21 ASSERT_TRIVIALLY_COPYABLE(HandleBase);
22 ASSERT_TRIVIALLY_COPYABLE(Handle<Object>);
23 ASSERT_TRIVIALLY_COPYABLE(MaybeHandle<Object>);
26 bool HandleBase::IsDereferenceAllowed(DereferenceCheckMode mode)
const {
27 DCHECK_NOT_NULL(location_);
28 Object*
object =
reinterpret_cast<Object*
>(*location_);
29 if (object->IsSmi())
return true;
30 HeapObject* heap_object = HeapObject::cast(
object);
32 if (!Isolate::FromWritableHeapObject(heap_object, &isolate))
return true;
34 if (isolate->roots_table().IsRootHandleLocation(location_, &root_index) &&
35 RootsTable::IsImmortalImmovable(root_index)) {
38 if (!AllowHandleDereference::IsAllowed())
return false;
39 if (mode == INCLUDE_DEFERRED_CHECK &&
40 !AllowDeferredHandleDereference::IsAllowed()) {
42 if (heap_object->IsCell())
return true;
43 if (heap_object->IsMap())
return true;
44 if (heap_object->IsInternalizedString())
return true;
45 return !isolate->IsDeferredHandle(location_);
52 int HandleScope::NumberOfHandles(Isolate* isolate) {
53 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
54 int n =
static_cast<int>(impl->blocks()->size());
56 return ((n - 1) * kHandleBlockSize) +
58 (isolate->handle_scope_data()->next - impl->blocks()->back()));
61 Address* HandleScope::Extend(Isolate* isolate) {
62 HandleScopeData* current = isolate->handle_scope_data();
64 Address* result = current->next;
66 DCHECK(result == current->limit);
69 if (!Utils::ApiCheck(current->level != current->sealed_level,
70 "v8::HandleScope::CreateHandle()",
71 "Cannot create a handle without a HandleScope")) {
74 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
77 if (!impl->blocks()->empty()) {
78 Address* limit = &impl->blocks()->back()[kHandleBlockSize];
79 if (current->limit != limit) {
80 current->limit = limit;
81 DCHECK_LT(limit - current->next, kHandleBlockSize);
87 if (result == current->limit) {
89 result = impl->GetSpareOrNewBlock();
92 impl->blocks()->push_back(result);
93 current->limit = &result[kHandleBlockSize];
100 void HandleScope::DeleteExtensions(Isolate* isolate) {
101 HandleScopeData* current = isolate->handle_scope_data();
102 isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
106 #ifdef ENABLE_HANDLE_ZAPPING 107 void HandleScope::ZapRange(Address* start, Address* end) {
108 DCHECK_LE(end - start, kHandleBlockSize);
109 for (Address* p = start; p != end; p++) {
110 *p =
static_cast<Address
>(kHandleZapValue);
116 Address HandleScope::current_level_address(Isolate* isolate) {
117 return reinterpret_cast<Address
>(&isolate->handle_scope_data()->level);
121 Address HandleScope::current_next_address(Isolate* isolate) {
122 return reinterpret_cast<Address
>(&isolate->handle_scope_data()->next);
126 Address HandleScope::current_limit_address(Isolate* isolate) {
127 return reinterpret_cast<Address
>(&isolate->handle_scope_data()->limit);
130 CanonicalHandleScope::CanonicalHandleScope(Isolate* isolate)
131 : isolate_(isolate), zone_(isolate->allocator(), ZONE_NAME) {
132 HandleScopeData* handle_scope_data = isolate_->handle_scope_data();
133 prev_canonical_scope_ = handle_scope_data->canonical_scope;
134 handle_scope_data->canonical_scope =
this;
135 root_index_map_ =
new RootIndexMap(isolate);
136 identity_map_ =
new IdentityMap<Address*, ZoneAllocationPolicy>(
137 isolate->heap(), ZoneAllocationPolicy(&zone_));
138 canonical_level_ = handle_scope_data->level;
142 CanonicalHandleScope::~CanonicalHandleScope() {
143 delete root_index_map_;
144 delete identity_map_;
145 isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_;
148 Address* CanonicalHandleScope::Lookup(Address
object) {
149 DCHECK_LE(canonical_level_, isolate_->handle_scope_data()->level);
150 if (isolate_->handle_scope_data()->level != canonical_level_) {
153 return HandleScope::CreateHandle(isolate_,
object);
155 if (Internals::HasHeapObjectTag(
object)) {
156 RootIndex root_index;
157 if (root_index_map_->Lookup(
object, &root_index)) {
158 return isolate_->root_handle(root_index).location();
161 Address** entry = identity_map_->Get(reinterpret_cast<Object*>(
object));
162 if (*entry ==
nullptr) {
164 *entry = HandleScope::CreateHandle(isolate_,
object);
170 DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
171 : impl_(isolate->handle_scope_implementer()) {
172 impl_->BeginDeferredScope();
173 HandleScopeData* data = impl_->isolate()->handle_scope_data();
174 Address* new_next = impl_->GetSpareOrNewBlock();
175 Address* new_limit = &new_next[kHandleBlockSize];
178 DCHECK(!impl_->blocks()->empty());
180 DCHECK(data->limit == &impl_->blocks()->back()[kHandleBlockSize]);
181 impl_->blocks()->push_back(new_next);
184 prev_level_ = data->level;
187 prev_limit_ = data->limit;
188 prev_next_ = data->next;
189 data->next = new_next;
190 data->limit = new_limit;
194 DeferredHandleScope::~DeferredHandleScope() {
195 impl_->isolate()->handle_scope_data()->level--;
196 DCHECK(handles_detached_);
197 DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_);
201 DeferredHandles* DeferredHandleScope::Detach() {
202 DeferredHandles* deferred = impl_->Detach(prev_limit_);
203 HandleScopeData* data = impl_->isolate()->handle_scope_data();
204 data->next = prev_next_;
205 data->limit = prev_limit_;
207 handles_detached_ =
true;