ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | // |
| 28 | |
| 29 | #ifndef V8_HANDLES_INL_H_ |
| 30 | #define V8_HANDLES_INL_H_ |
| 31 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 32 | #include "api.h" |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 33 | #include "apiutils.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "handles.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 35 | #include "isolate.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 37 | namespace v8 { |
| 38 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 40 | template<typename T> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | Handle<T>::Handle(T* obj) { |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 42 | ASSERT(!obj->IsFailure()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 43 | location_ = HandleScope::CreateHandle(obj->GetIsolate(), obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | template<typename T> |
| 48 | Handle<T>::Handle(T* obj, Isolate* isolate) { |
| 49 | ASSERT(!obj->IsFailure()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 50 | location_ = HandleScope::CreateHandle(isolate, obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 54 | template <typename T> |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 55 | inline bool Handle<T>::is_identical_to(const Handle<T> other) const { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 56 | ASSERT(location_ == NULL || !(*location_)->IsFailure()); |
| 57 | if (location_ == other.location_) return true; |
| 58 | if (location_ == NULL || other.location_ == NULL) return false; |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 59 | // Dereferencing deferred handles to check object equality is safe. |
| 60 | SLOW_ASSERT(IsDereferenceAllowed(true) && other.IsDereferenceAllowed(true)); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 61 | return *location_ == *other.location_; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | template <typename T> |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | inline T* Handle<T>::operator*() const { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 67 | ASSERT(location_ != NULL && !(*location_)->IsFailure()); |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 68 | SLOW_ASSERT(IsDereferenceAllowed(false)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 69 | return *BitCast<T**>(location_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | } |
| 71 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 72 | template <typename T> |
| 73 | inline T** Handle<T>::location() const { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 74 | ASSERT(location_ == NULL || !(*location_)->IsFailure()); |
| 75 | SLOW_ASSERT(location_ == NULL || IsDereferenceAllowed(false)); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 76 | return location_; |
| 77 | } |
| 78 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 79 | #ifdef DEBUG |
| 80 | template <typename T> |
| 81 | bool Handle<T>::IsDereferenceAllowed(bool allow_deferred) const { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 82 | ASSERT(location_ != NULL); |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 83 | Object* object = *BitCast<T**>(location_); |
| 84 | if (object->IsSmi()) return true; |
| 85 | HeapObject* heap_object = HeapObject::cast(object); |
| 86 | Isolate* isolate = heap_object->GetIsolate(); |
| 87 | Object** handle = reinterpret_cast<Object**>(location_); |
| 88 | Object** roots_array_start = isolate->heap()->roots_array_start(); |
| 89 | if (roots_array_start <= handle && |
| 90 | handle < roots_array_start + Heap::kStrongRootListLength) { |
| 91 | return true; |
| 92 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 93 | if (isolate->optimizing_compiler_thread()->IsOptimizerThread() && |
| 94 | !Heap::RelocationLock::IsLockedByOptimizerThread(isolate->heap())) { |
| 95 | return false; |
| 96 | } |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 97 | switch (isolate->HandleDereferenceGuardState()) { |
| 98 | case HandleDereferenceGuard::ALLOW: |
| 99 | return true; |
| 100 | case HandleDereferenceGuard::DISALLOW: |
| 101 | return false; |
| 102 | case HandleDereferenceGuard::DISALLOW_DEFERRED: |
| 103 | // Accessing maps and internalized strings is safe. |
| 104 | if (heap_object->IsMap()) return true; |
| 105 | if (heap_object->IsInternalizedString()) return true; |
| 106 | return allow_deferred || !isolate->IsDeferredHandle(handle); |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 114 | HandleScope::HandleScope(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 115 | v8::ImplementationUtilities::HandleScopeData* current = |
| 116 | isolate->handle_scope_data(); |
| 117 | isolate_ = isolate; |
| 118 | prev_next_ = current->next; |
| 119 | prev_limit_ = current->limit; |
| 120 | current->level++; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | HandleScope::~HandleScope() { |
| 125 | CloseScope(); |
| 126 | } |
| 127 | |
| 128 | void HandleScope::CloseScope() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 129 | v8::ImplementationUtilities::HandleScopeData* current = |
| 130 | isolate_->handle_scope_data(); |
| 131 | current->next = prev_next_; |
| 132 | current->level--; |
| 133 | if (current->limit != prev_limit_) { |
| 134 | current->limit = prev_limit_; |
| 135 | DeleteExtensions(isolate_); |
| 136 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 137 | #ifdef ENABLE_EXTRA_CHECKS |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 138 | ZapRange(prev_next_, prev_limit_); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | |
| 143 | template <typename T> |
| 144 | Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { |
| 145 | T* value = *handle_value; |
| 146 | // Throw away all handles in the current scope. |
| 147 | CloseScope(); |
| 148 | v8::ImplementationUtilities::HandleScopeData* current = |
| 149 | isolate_->handle_scope_data(); |
| 150 | // Allocate one handle in the parent scope. |
| 151 | ASSERT(current->level > 0); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 152 | Handle<T> result(CreateHandle<T>(isolate_, value)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 153 | // Reinitialize the current scope (so that it's ready |
| 154 | // to be used or closed again). |
| 155 | prev_next_ = current->next; |
| 156 | prev_limit_ = current->limit; |
| 157 | current->level++; |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | template <typename T> |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 163 | T** HandleScope::CreateHandle(Isolate* isolate, T* value) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 164 | v8::ImplementationUtilities::HandleScopeData* current = |
| 165 | isolate->handle_scope_data(); |
| 166 | |
| 167 | internal::Object** cur = current->next; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 168 | if (cur == current->limit) cur = Extend(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 169 | // Update the current next field, set the value in the created |
| 170 | // handle, and return the result. |
| 171 | ASSERT(cur < current->limit); |
| 172 | current->next = cur + 1; |
| 173 | |
| 174 | T** result = reinterpret_cast<T**>(cur); |
| 175 | *result = value; |
| 176 | return result; |
| 177 | } |
| 178 | |
| 179 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | #ifdef DEBUG |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 181 | inline NoHandleAllocation::NoHandleAllocation(Isolate* isolate) |
| 182 | : isolate_(isolate) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 183 | v8::ImplementationUtilities::HandleScopeData* current = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 184 | isolate_->handle_scope_data(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 185 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 186 | active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread(); |
| 187 | if (active_) { |
| 188 | // Shrink the current handle scope to make it impossible to do |
| 189 | // handle allocations without an explicit handle scope. |
| 190 | current->limit = current->next; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 191 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 192 | level_ = current->level; |
| 193 | current->level = 0; |
| 194 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | inline NoHandleAllocation::~NoHandleAllocation() { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 199 | if (active_) { |
| 200 | // Restore state in current handle scope to re-enable handle |
| 201 | // allocations. |
| 202 | v8::ImplementationUtilities::HandleScopeData* data = |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 203 | isolate_->handle_scope_data(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 204 | ASSERT_EQ(0, data->level); |
| 205 | data->level = level_; |
| 206 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | } |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 208 | |
| 209 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 210 | HandleDereferenceGuard::HandleDereferenceGuard(Isolate* isolate, State state) |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 211 | : isolate_(isolate) { |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 212 | old_state_ = isolate_->HandleDereferenceGuardState(); |
| 213 | isolate_->SetHandleDereferenceGuardState(state); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 217 | HandleDereferenceGuard::~HandleDereferenceGuard() { |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 218 | isolate_->SetHandleDereferenceGuardState(old_state_); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 219 | } |
| 220 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | #endif |
| 222 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | } } // namespace v8::internal |
| 224 | |
| 225 | #endif // V8_HANDLES_INL_H_ |