| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #define V8_HANDLES_INL_H_ | 30 #define V8_HANDLES_INL_H_ |
| 31 | 31 |
| 32 #include "api.h" | 32 #include "api.h" |
| 33 #include "apiutils.h" | 33 #include "apiutils.h" |
| 34 #include "handles.h" | 34 #include "handles.h" |
| 35 #include "isolate.h" | 35 #include "isolate.h" |
| 36 | 36 |
| 37 namespace v8 { | 37 namespace v8 { |
| 38 namespace internal { | 38 namespace internal { |
| 39 | 39 |
| 40 inline Isolate* GetIsolateForHandle(Object* obj) { | |
| 41 return Isolate::Current(); | |
| 42 } | |
| 43 | |
| 44 inline Isolate* GetIsolateForHandle(HeapObject* obj) { | |
| 45 return obj->GetIsolate(); | |
| 46 } | |
| 47 | |
| 48 template<typename T> | 40 template<typename T> |
| 49 Handle<T>::Handle(T* obj) { | 41 Handle<T>::Handle(T* obj) { |
| 50 ASSERT(!obj->IsFailure()); | 42 ASSERT(!obj->IsFailure()); |
| 51 location_ = HandleScope::CreateHandle(obj, GetIsolateForHandle(obj)); | 43 location_ = HandleScope::CreateHandle(obj->GetIsolate(), obj); |
| 52 } | 44 } |
| 53 | 45 |
| 54 | 46 |
| 55 template<typename T> | 47 template<typename T> |
| 56 Handle<T>::Handle(T* obj, Isolate* isolate) { | 48 Handle<T>::Handle(T* obj, Isolate* isolate) { |
| 57 ASSERT(!obj->IsFailure()); | 49 ASSERT(!obj->IsFailure()); |
| 58 location_ = HandleScope::CreateHandle(obj, isolate); | 50 location_ = HandleScope::CreateHandle(isolate, obj); |
| 59 } | 51 } |
| 60 | 52 |
| 61 | 53 |
| 62 template <typename T> | 54 template <typename T> |
| 63 inline T* Handle<T>::operator*() const { | 55 inline T* Handle<T>::operator*() const { |
| 64 ASSERT(location_ != NULL); | 56 ASSERT(location_ != NULL); |
| 65 ASSERT(reinterpret_cast<Address>(*location_) != kHandleZapValue); | 57 ASSERT(reinterpret_cast<Address>(*location_) != kHandleZapValue); |
| 66 SLOW_ASSERT(ISOLATE->allow_handle_deref()); | 58 SLOW_ASSERT(ISOLATE->allow_handle_deref()); |
| 67 return *BitCast<T**>(location_); | 59 return *BitCast<T**>(location_); |
| 68 } | 60 } |
| 69 | 61 |
| 70 template <typename T> | 62 template <typename T> |
| 71 inline T** Handle<T>::location() const { | 63 inline T** Handle<T>::location() const { |
| 72 ASSERT(location_ == NULL || | 64 ASSERT(location_ == NULL || |
| 73 reinterpret_cast<Address>(*location_) != kZapValue); | 65 reinterpret_cast<Address>(*location_) != kZapValue); |
| 74 SLOW_ASSERT(ISOLATE->allow_handle_deref()); | 66 SLOW_ASSERT(ISOLATE->allow_handle_deref()); |
| 75 return location_; | 67 return location_; |
| 76 } | 68 } |
| 77 | 69 |
| 78 | 70 |
| 79 HandleScope::HandleScope(Isolate* isolate) { | 71 HandleScope::HandleScope(Isolate* isolate) { |
| 80 ASSERT(isolate == Isolate::Current()); | |
| 81 v8::ImplementationUtilities::HandleScopeData* current = | 72 v8::ImplementationUtilities::HandleScopeData* current = |
| 82 isolate->handle_scope_data(); | 73 isolate->handle_scope_data(); |
| 83 isolate_ = isolate; | 74 isolate_ = isolate; |
| 84 prev_next_ = current->next; | 75 prev_next_ = current->next; |
| 85 prev_limit_ = current->limit; | 76 prev_limit_ = current->limit; |
| 86 current->level++; | 77 current->level++; |
| 87 } | 78 } |
| 88 | 79 |
| 89 | 80 |
| 90 HandleScope::~HandleScope() { | 81 HandleScope::~HandleScope() { |
| 91 CloseScope(); | 82 CloseScope(); |
| 92 } | 83 } |
| 93 | 84 |
| 94 void HandleScope::CloseScope() { | 85 void HandleScope::CloseScope() { |
| 95 ASSERT(isolate_ == Isolate::Current()); | |
| 96 v8::ImplementationUtilities::HandleScopeData* current = | 86 v8::ImplementationUtilities::HandleScopeData* current = |
| 97 isolate_->handle_scope_data(); | 87 isolate_->handle_scope_data(); |
| 98 current->next = prev_next_; | 88 current->next = prev_next_; |
| 99 current->level--; | 89 current->level--; |
| 100 if (current->limit != prev_limit_) { | 90 if (current->limit != prev_limit_) { |
| 101 current->limit = prev_limit_; | 91 current->limit = prev_limit_; |
| 102 DeleteExtensions(isolate_); | 92 DeleteExtensions(isolate_); |
| 103 } | 93 } |
| 104 #ifdef DEBUG | 94 #ifdef DEBUG |
| 105 ZapRange(prev_next_, prev_limit_); | 95 ZapRange(prev_next_, prev_limit_); |
| 106 #endif | 96 #endif |
| 107 } | 97 } |
| 108 | 98 |
| 109 | 99 |
| 110 template <typename T> | 100 template <typename T> |
| 111 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { | 101 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { |
| 112 T* value = *handle_value; | 102 T* value = *handle_value; |
| 113 // Throw away all handles in the current scope. | 103 // Throw away all handles in the current scope. |
| 114 CloseScope(); | 104 CloseScope(); |
| 115 v8::ImplementationUtilities::HandleScopeData* current = | 105 v8::ImplementationUtilities::HandleScopeData* current = |
| 116 isolate_->handle_scope_data(); | 106 isolate_->handle_scope_data(); |
| 117 // Allocate one handle in the parent scope. | 107 // Allocate one handle in the parent scope. |
| 118 ASSERT(current->level > 0); | 108 ASSERT(current->level > 0); |
| 119 Handle<T> result(CreateHandle<T>(value, isolate_)); | 109 Handle<T> result(CreateHandle<T>(isolate_, value)); |
| 120 // Reinitialize the current scope (so that it's ready | 110 // Reinitialize the current scope (so that it's ready |
| 121 // to be used or closed again). | 111 // to be used or closed again). |
| 122 prev_next_ = current->next; | 112 prev_next_ = current->next; |
| 123 prev_limit_ = current->limit; | 113 prev_limit_ = current->limit; |
| 124 current->level++; | 114 current->level++; |
| 125 return result; | 115 return result; |
| 126 } | 116 } |
| 127 | 117 |
| 128 | 118 |
| 129 template <typename T> | 119 template <typename T> |
| 130 T** HandleScope::CreateHandle(T* value, Isolate* isolate) { | 120 T** HandleScope::CreateHandle(Isolate* isolate, T* value) { |
| 131 ASSERT(isolate == Isolate::Current()); | |
| 132 v8::ImplementationUtilities::HandleScopeData* current = | 121 v8::ImplementationUtilities::HandleScopeData* current = |
| 133 isolate->handle_scope_data(); | 122 isolate->handle_scope_data(); |
| 134 | 123 |
| 135 internal::Object** cur = current->next; | 124 internal::Object** cur = current->next; |
| 136 if (cur == current->limit) cur = Extend(); | 125 if (cur == current->limit) cur = Extend(isolate); |
| 137 // Update the current next field, set the value in the created | 126 // Update the current next field, set the value in the created |
| 138 // handle, and return the result. | 127 // handle, and return the result. |
| 139 ASSERT(cur < current->limit); | 128 ASSERT(cur < current->limit); |
| 140 current->next = cur + 1; | 129 current->next = cur + 1; |
| 141 | 130 |
| 142 T** result = reinterpret_cast<T**>(cur); | 131 T** result = reinterpret_cast<T**>(cur); |
| 143 *result = value; | 132 *result = value; |
| 144 return result; | 133 return result; |
| 145 } | 134 } |
| 146 | 135 |
| 147 | 136 |
| 148 #ifdef DEBUG | 137 #ifdef DEBUG |
| 149 inline NoHandleAllocation::NoHandleAllocation() { | 138 inline NoHandleAllocation::NoHandleAllocation(Isolate* isolate) |
| 150 Isolate* isolate = Isolate::Current(); | 139 : isolate_(isolate) { |
| 151 v8::ImplementationUtilities::HandleScopeData* current = | 140 v8::ImplementationUtilities::HandleScopeData* current = |
| 152 isolate->handle_scope_data(); | 141 isolate_->handle_scope_data(); |
| 153 | 142 |
| 154 active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread(); | 143 active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread(); |
| 155 if (active_) { | 144 if (active_) { |
| 156 // Shrink the current handle scope to make it impossible to do | 145 // Shrink the current handle scope to make it impossible to do |
| 157 // handle allocations without an explicit handle scope. | 146 // handle allocations without an explicit handle scope. |
| 158 current->limit = current->next; | 147 current->limit = current->next; |
| 159 | 148 |
| 160 level_ = current->level; | 149 level_ = current->level; |
| 161 current->level = 0; | 150 current->level = 0; |
| 162 } | 151 } |
| 163 } | 152 } |
| 164 | 153 |
| 165 | 154 |
| 166 inline NoHandleAllocation::~NoHandleAllocation() { | 155 inline NoHandleAllocation::~NoHandleAllocation() { |
| 167 if (active_) { | 156 if (active_) { |
| 168 // Restore state in current handle scope to re-enable handle | 157 // Restore state in current handle scope to re-enable handle |
| 169 // allocations. | 158 // allocations. |
| 170 v8::ImplementationUtilities::HandleScopeData* data = | 159 v8::ImplementationUtilities::HandleScopeData* data = |
| 171 Isolate::Current()->handle_scope_data(); | 160 isolate_->handle_scope_data(); |
| 172 ASSERT_EQ(0, data->level); | 161 ASSERT_EQ(0, data->level); |
| 173 data->level = level_; | 162 data->level = level_; |
| 174 } | 163 } |
| 175 } | 164 } |
| 176 | 165 |
| 177 | 166 |
| 178 NoHandleDereference::NoHandleDereference() { | 167 NoHandleDereference::NoHandleDereference(Isolate* isolate) |
| 168 : isolate_(isolate) { |
| 179 // The guard is set on a per-isolate basis, so it affects all threads. | 169 // The guard is set on a per-isolate basis, so it affects all threads. |
| 180 // That's why we can only use it when running without parallel recompilation. | 170 // That's why we can only use it when running without parallel recompilation. |
| 181 if (FLAG_parallel_recompilation) return; | 171 if (FLAG_parallel_recompilation) return; |
| 182 Isolate* isolate = Isolate::Current(); | |
| 183 old_state_ = isolate->allow_handle_deref(); | 172 old_state_ = isolate->allow_handle_deref(); |
| 184 isolate->set_allow_handle_deref(false); | 173 isolate_->set_allow_handle_deref(false); |
| 185 } | 174 } |
| 186 | 175 |
| 187 | 176 |
| 188 NoHandleDereference::~NoHandleDereference() { | 177 NoHandleDereference::~NoHandleDereference() { |
| 189 if (FLAG_parallel_recompilation) return; | 178 if (FLAG_parallel_recompilation) return; |
| 190 Isolate::Current()->set_allow_handle_deref(old_state_); | 179 isolate_->set_allow_handle_deref(old_state_); |
| 191 } | 180 } |
| 192 | 181 |
| 193 | 182 |
| 194 AllowHandleDereference::AllowHandleDereference() { | 183 AllowHandleDereference::AllowHandleDereference(Isolate* isolate) |
| 184 : isolate_(isolate) { |
| 195 // The guard is set on a per-isolate basis, so it affects all threads. | 185 // The guard is set on a per-isolate basis, so it affects all threads. |
| 196 // That's why we can only use it when running without parallel recompilation. | 186 // That's why we can only use it when running without parallel recompilation. |
| 197 if (FLAG_parallel_recompilation) return; | 187 if (FLAG_parallel_recompilation) return; |
| 198 Isolate* isolate = Isolate::Current(); | |
| 199 old_state_ = isolate->allow_handle_deref(); | 188 old_state_ = isolate->allow_handle_deref(); |
| 200 isolate->set_allow_handle_deref(true); | 189 isolate_->set_allow_handle_deref(true); |
| 201 } | 190 } |
| 202 | 191 |
| 203 | 192 |
| 204 AllowHandleDereference::~AllowHandleDereference() { | 193 AllowHandleDereference::~AllowHandleDereference() { |
| 205 if (FLAG_parallel_recompilation) return; | 194 if (FLAG_parallel_recompilation) return; |
| 206 Isolate::Current()->set_allow_handle_deref(old_state_); | 195 isolate_->set_allow_handle_deref(old_state_); |
| 207 } | 196 } |
| 208 #endif | 197 #endif |
| 209 | 198 |
| 210 | 199 |
| 211 } } // namespace v8::internal | 200 } } // namespace v8::internal |
| 212 | 201 |
| 213 #endif // V8_HANDLES_INL_H_ | 202 #endif // V8_HANDLES_INL_H_ |
| OLD | NEW |