V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
handles.cc
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 #include "src/handles.h"
6 
7 #include "src/address-map.h"
8 #include "src/api.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"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // Handles should be trivially copyable so that they can be efficiently passed
19 // by value. If they are not trivially copyable, they cannot be passed in
20 // registers.
21 ASSERT_TRIVIALLY_COPYABLE(HandleBase);
22 ASSERT_TRIVIALLY_COPYABLE(Handle<Object>);
23 ASSERT_TRIVIALLY_COPYABLE(MaybeHandle<Object>);
24 
25 #ifdef DEBUG
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);
31  Isolate* isolate;
32  if (!Isolate::FromWritableHeapObject(heap_object, &isolate)) return true;
33  RootIndex root_index;
34  if (isolate->roots_table().IsRootHandleLocation(location_, &root_index) &&
35  RootsTable::IsImmortalImmovable(root_index)) {
36  return true;
37  }
38  if (!AllowHandleDereference::IsAllowed()) return false;
39  if (mode == INCLUDE_DEFERRED_CHECK &&
40  !AllowDeferredHandleDereference::IsAllowed()) {
41  // Accessing cells, maps and internalized strings is safe.
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_);
46  }
47  return true;
48 }
49 #endif
50 
51 
52 int HandleScope::NumberOfHandles(Isolate* isolate) {
53  HandleScopeImplementer* impl = isolate->handle_scope_implementer();
54  int n = static_cast<int>(impl->blocks()->size());
55  if (n == 0) return 0;
56  return ((n - 1) * kHandleBlockSize) +
57  static_cast<int>(
58  (isolate->handle_scope_data()->next - impl->blocks()->back()));
59 }
60 
61 Address* HandleScope::Extend(Isolate* isolate) {
62  HandleScopeData* current = isolate->handle_scope_data();
63 
64  Address* result = current->next;
65 
66  DCHECK(result == current->limit);
67  // Make sure there's at least one scope on the stack and that the
68  // top of the scope stack isn't a barrier.
69  if (!Utils::ApiCheck(current->level != current->sealed_level,
70  "v8::HandleScope::CreateHandle()",
71  "Cannot create a handle without a HandleScope")) {
72  return nullptr;
73  }
74  HandleScopeImplementer* impl = isolate->handle_scope_implementer();
75  // If there's more room in the last block, we use that. This is used
76  // for fast creation of scopes after scope barriers.
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);
82  }
83  }
84 
85  // If we still haven't found a slot for the handle, we extend the
86  // current handle scope by allocating a new handle block.
87  if (result == current->limit) {
88  // If there's a spare block, use it for growing the current scope.
89  result = impl->GetSpareOrNewBlock();
90  // Add the extension to the global list of blocks, but count the
91  // extension as part of the current scope.
92  impl->blocks()->push_back(result);
93  current->limit = &result[kHandleBlockSize];
94  }
95 
96  return result;
97 }
98 
99 
100 void HandleScope::DeleteExtensions(Isolate* isolate) {
101  HandleScopeData* current = isolate->handle_scope_data();
102  isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
103 }
104 
105 
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);
111  }
112 }
113 #endif
114 
115 
116 Address HandleScope::current_level_address(Isolate* isolate) {
117  return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
118 }
119 
120 
121 Address HandleScope::current_next_address(Isolate* isolate) {
122  return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
123 }
124 
125 
126 Address HandleScope::current_limit_address(Isolate* isolate) {
127  return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
128 }
129 
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;
139 }
140 
141 
142 CanonicalHandleScope::~CanonicalHandleScope() {
143  delete root_index_map_;
144  delete identity_map_;
145  isolate_->handle_scope_data()->canonical_scope = prev_canonical_scope_;
146 }
147 
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_) {
151  // We are in an inner handle scope. Do not canonicalize since we will leave
152  // this handle scope while still being in the canonical scope.
153  return HandleScope::CreateHandle(isolate_, object);
154  }
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();
159  }
160  }
161  Address** entry = identity_map_->Get(reinterpret_cast<Object*>(object));
162  if (*entry == nullptr) {
163  // Allocate new handle location.
164  *entry = HandleScope::CreateHandle(isolate_, object);
165  }
166  return *entry;
167 }
168 
169 
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];
176  // Check that at least one HandleScope with at least one Handle in it exists,
177  // see the class description.
178  DCHECK(!impl_->blocks()->empty());
179  // Check that we are not in a SealedHandleScope.
180  DCHECK(data->limit == &impl_->blocks()->back()[kHandleBlockSize]);
181  impl_->blocks()->push_back(new_next);
182 
183 #ifdef DEBUG
184  prev_level_ = data->level;
185 #endif
186  data->level++;
187  prev_limit_ = data->limit;
188  prev_next_ = data->next;
189  data->next = new_next;
190  data->limit = new_limit;
191 }
192 
193 
194 DeferredHandleScope::~DeferredHandleScope() {
195  impl_->isolate()->handle_scope_data()->level--;
196  DCHECK(handles_detached_);
197  DCHECK(impl_->isolate()->handle_scope_data()->level == prev_level_);
198 }
199 
200 
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_;
206 #ifdef DEBUG
207  handles_detached_ = true;
208 #endif
209  return deferred;
210 }
211 
212 } // namespace internal
213 } // namespace v8
Definition: libplatform.h:13