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 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | |
5 | 4 |
6 #ifndef V8_HANDLES_INL_H_ | 5 #ifndef V8_HANDLES_INL_H_ |
7 #define V8_HANDLES_INL_H_ | 6 #define V8_HANDLES_INL_H_ |
8 | 7 |
9 #include "src/api.h" | 8 #include "src/api.h" |
10 #include "src/handles.h" | 9 #include "src/handles.h" |
11 #include "src/heap/heap.h" | 10 #include "src/heap/heap.h" |
12 #include "src/isolate.h" | 11 #include "src/isolate.h" |
13 | 12 |
14 namespace v8 { | 13 namespace v8 { |
15 namespace internal { | 14 namespace internal { |
16 | 15 |
17 template<typename T> | 16 HandleBase::HandleBase(Object* object, Isolate* isolate) |
18 Handle<T>::Handle(T* obj) { | 17 : location_(HandleScope::CreateHandle(isolate, object)) {} |
19 location_ = HandleScope::CreateHandle(obj->GetIsolate(), obj); | |
20 } | |
21 | |
22 | |
23 template<typename T> | |
24 Handle<T>::Handle(T* obj, Isolate* isolate) { | |
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_; | |
38 } | |
39 | |
40 | |
41 template <typename T> | |
42 inline T* Handle<T>::operator*() const { | |
43 SLOW_DCHECK(IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK)); | |
44 return *bit_cast<T**>(location_); | |
45 } | |
46 | |
47 template <typename T> | |
48 inline T** Handle<T>::location() const { | |
49 SLOW_DCHECK(location_ == NULL || | |
50 IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK)); | |
51 return location_; | |
52 } | |
53 | |
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 | 18 |
84 | 19 |
85 HandleScope::HandleScope(Isolate* isolate) { | 20 HandleScope::HandleScope(Isolate* isolate) { |
86 HandleScopeData* current = isolate->handle_scope_data(); | 21 HandleScopeData* current = isolate->handle_scope_data(); |
87 isolate_ = isolate; | 22 isolate_ = isolate; |
88 prev_next_ = current->next; | 23 prev_next_ = current->next; |
89 prev_limit_ = current->limit; | 24 prev_limit_ = current->limit; |
90 current->level++; | 25 current->level++; |
91 } | 26 } |
92 | 27 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 64 |
130 template <typename T> | 65 template <typename T> |
131 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { | 66 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { |
132 HandleScopeData* current = isolate_->handle_scope_data(); | 67 HandleScopeData* current = isolate_->handle_scope_data(); |
133 | 68 |
134 T* value = *handle_value; | 69 T* value = *handle_value; |
135 // Throw away all handles in the current scope. | 70 // Throw away all handles in the current scope. |
136 CloseScope(isolate_, prev_next_, prev_limit_); | 71 CloseScope(isolate_, prev_next_, prev_limit_); |
137 // Allocate one handle in the parent scope. | 72 // Allocate one handle in the parent scope. |
138 DCHECK(current->level > 0); | 73 DCHECK(current->level > 0); |
139 Handle<T> result(CreateHandle<T>(isolate_, value)); | 74 Handle<T> result(value, isolate_); |
140 // Reinitialize the current scope (so that it's ready | 75 // Reinitialize the current scope (so that it's ready |
141 // to be used or closed again). | 76 // to be used or closed again). |
142 prev_next_ = current->next; | 77 prev_next_ = current->next; |
143 prev_limit_ = current->limit; | 78 prev_limit_ = current->limit; |
144 current->level++; | 79 current->level++; |
145 return result; | 80 return result; |
146 } | 81 } |
147 | 82 |
148 | 83 |
149 template <typename T> | 84 template <typename T> |
150 T** HandleScope::CreateHandle(Isolate* isolate, T* value) { | 85 T** HandleScope::CreateHandle(Isolate* isolate, T* value) { |
151 DCHECK(AllowHandleAllocation::IsAllowed()); | 86 DCHECK(AllowHandleAllocation::IsAllowed()); |
152 HandleScopeData* current = isolate->handle_scope_data(); | 87 HandleScopeData* current = isolate->handle_scope_data(); |
153 | 88 |
154 internal::Object** cur = current->next; | 89 Object** cur = current->next; |
155 if (cur == current->limit) cur = Extend(isolate); | 90 if (cur == current->limit) cur = Extend(isolate); |
156 // Update the current next field, set the value in the created | 91 // Update the current next field, set the value in the created |
157 // handle, and return the result. | 92 // handle, and return the result. |
158 DCHECK(cur < current->limit); | 93 DCHECK(cur < current->limit); |
159 current->next = cur + 1; | 94 current->next = cur + 1; |
160 | 95 |
161 T** result = reinterpret_cast<T**>(cur); | 96 T** result = reinterpret_cast<T**>(cur); |
162 *result = value; | 97 *result = value; |
163 return result; | 98 return result; |
164 } | 99 } |
(...skipping 18 matching lines...) Expand all Loading... |
183 // allocations. | 118 // allocations. |
184 HandleScopeData* current = isolate_->handle_scope_data(); | 119 HandleScopeData* current = isolate_->handle_scope_data(); |
185 DCHECK_EQ(0, current->level); | 120 DCHECK_EQ(0, current->level); |
186 current->level = level_; | 121 current->level = level_; |
187 DCHECK_EQ(current->next, current->limit); | 122 DCHECK_EQ(current->next, current->limit); |
188 current->limit = limit_; | 123 current->limit = limit_; |
189 } | 124 } |
190 | 125 |
191 #endif | 126 #endif |
192 | 127 |
193 } } // namespace v8::internal | 128 } // namespace internal |
| 129 } // namespace v8 |
194 | 130 |
195 #endif // V8_HANDLES_INL_H_ | 131 #endif // V8_HANDLES_INL_H_ |
OLD | NEW |