Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #ifndef V8_HANDLES_INL_H_ |
| 7 | #define V8_HANDLES_INL_H_ |
| 8 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/api.h" |
| 10 | #include "src/handles.h" |
| 11 | #include "src/heap/heap.h" |
| 12 | #include "src/isolate.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 17 | template<typename T> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | Handle<T>::Handle(T* obj) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | location_ = HandleScope::CreateHandle(obj->GetIsolate(), obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | |
| 23 | template<typename T> |
| 24 | Handle<T>::Handle(T* obj, Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 25 | location_ = HandleScope::CreateHandle(isolate, obj); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | template <typename T> |
| 30 | inline bool Handle<T>::is_identical_to(const Handle<T> o) const { |
| 31 | // Dereferencing deferred handles to check object equality is safe. |
| 32 | SLOW_DCHECK( |
| 33 | (location_ == NULL || IsDereferenceAllowed(NO_DEFERRED_CHECK)) && |
| 34 | (o.location_ == NULL || o.IsDereferenceAllowed(NO_DEFERRED_CHECK))); |
| 35 | if (location_ == o.location_) return true; |
| 36 | if (location_ == NULL || o.location_ == NULL) return false; |
| 37 | return *location_ == *o.location_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 41 | template <typename T> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | inline T* Handle<T>::operator*() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | SLOW_DCHECK(IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK)); |
| 44 | return *bit_cast<T**>(location_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | template <typename T> |
| 48 | inline T** Handle<T>::location() const { |
| 49 | SLOW_DCHECK(location_ == NULL || |
| 50 | IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK)); |
| 51 | return location_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 54 | #ifdef DEBUG |
| 55 | template <typename T> |
| 56 | bool Handle<T>::IsDereferenceAllowed(DereferenceCheckMode mode) const { |
| 57 | DCHECK(location_ != NULL); |
| 58 | Object* object = *bit_cast<T**>(location_); |
| 59 | if (object->IsSmi()) return true; |
| 60 | HeapObject* heap_object = HeapObject::cast(object); |
| 61 | Heap* heap = heap_object->GetHeap(); |
| 62 | Object** handle = reinterpret_cast<Object**>(location_); |
| 63 | Object** roots_array_start = heap->roots_array_start(); |
| 64 | if (roots_array_start <= handle && |
| 65 | handle < roots_array_start + Heap::kStrongRootListLength && |
| 66 | heap->RootCanBeTreatedAsConstant( |
| 67 | static_cast<Heap::RootListIndex>(handle - roots_array_start))) { |
| 68 | return true; |
| 69 | } |
| 70 | if (!AllowHandleDereference::IsAllowed()) return false; |
| 71 | if (mode == INCLUDE_DEFERRED_CHECK && |
| 72 | !AllowDeferredHandleDereference::IsAllowed()) { |
| 73 | // Accessing cells, maps and internalized strings is safe. |
| 74 | if (heap_object->IsCell()) return true; |
| 75 | if (heap_object->IsMap()) return true; |
| 76 | if (heap_object->IsInternalizedString()) return true; |
| 77 | return !heap->isolate()->IsDeferredHandle(handle); |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 84 | |
| 85 | HandleScope::HandleScope(Isolate* isolate) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | HandleScopeData* current = isolate->handle_scope_data(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 87 | isolate_ = isolate; |
| 88 | prev_next_ = current->next; |
| 89 | prev_limit_ = current->limit; |
| 90 | current->level++; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | HandleScope::~HandleScope() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 95 | CloseScope(isolate_, prev_next_, prev_limit_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | |
| 99 | void HandleScope::CloseScope(Isolate* isolate, |
| 100 | Object** prev_next, |
| 101 | Object** prev_limit) { |
| 102 | HandleScopeData* current = isolate->handle_scope_data(); |
| 103 | |
| 104 | std::swap(current->next, prev_next); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 105 | current->level--; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | if (current->limit != prev_limit) { |
| 107 | current->limit = prev_limit; |
| 108 | DeleteExtensions(isolate); |
| 109 | #ifdef ENABLE_HANDLE_ZAPPING |
| 110 | ZapRange(current->next, prev_limit); |
| 111 | } else { |
| 112 | ZapRange(current->next, prev_next); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 113 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 114 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | template <typename T> |
| 119 | Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 120 | HandleScopeData* current = isolate_->handle_scope_data(); |
| 121 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | T* value = *handle_value; |
| 123 | // Throw away all handles in the current scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 124 | CloseScope(isolate_, prev_next_, prev_limit_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 125 | // Allocate one handle in the parent scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 126 | DCHECK(current->level > 0); |
| 127 | Handle<T> result(CreateHandle<T>(isolate_, value)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 128 | // Reinitialize the current scope (so that it's ready |
| 129 | // to be used or closed again). |
| 130 | prev_next_ = current->next; |
| 131 | prev_limit_ = current->limit; |
| 132 | current->level++; |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | template <typename T> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 138 | T** HandleScope::CreateHandle(Isolate* isolate, T* value) { |
| 139 | DCHECK(AllowHandleAllocation::IsAllowed()); |
| 140 | HandleScopeData* current = isolate->handle_scope_data(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 141 | |
| 142 | internal::Object** cur = current->next; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | if (cur == current->limit) cur = Extend(isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 144 | // Update the current next field, set the value in the created |
| 145 | // handle, and return the result. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | DCHECK(cur < current->limit); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | current->next = cur + 1; |
| 148 | |
| 149 | T** result = reinterpret_cast<T**>(cur); |
| 150 | *result = value; |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 156 | inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) { |
| 157 | // Make sure the current thread is allowed to create handles to begin with. |
| 158 | CHECK(AllowHandleAllocation::IsAllowed()); |
| 159 | HandleScopeData* current = isolate_->handle_scope_data(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | // Shrink the current handle scope to make it impossible to do |
| 161 | // handle allocations without an explicit handle scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 162 | limit_ = current->limit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | current->limit = current->next; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 164 | level_ = current->level; |
| 165 | current->level = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 169 | inline SealHandleScope::~SealHandleScope() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 170 | // Restore state in current handle scope to re-enable handle |
| 171 | // allocations. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 172 | HandleScopeData* current = isolate_->handle_scope_data(); |
| 173 | DCHECK_EQ(0, current->level); |
| 174 | current->level = level_; |
| 175 | DCHECK_EQ(current->next, current->limit); |
| 176 | current->limit = limit_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | |
| 181 | } } // namespace v8::internal |
| 182 | |
| 183 | #endif // V8_HANDLES_INL_H_ |