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 | 4 |
5 #ifndef V8_HANDLES_INL_H_ | 5 #ifndef V8_HANDLES_INL_H_ |
6 #define V8_HANDLES_INL_H_ | 6 #define V8_HANDLES_INL_H_ |
7 | 7 |
8 #include "src/api.h" | 8 #include "src/api.h" |
9 #include "src/handles.h" | 9 #include "src/handles.h" |
10 #include "src/heap/heap.h" | 10 #include "src/heap/heap.h" |
11 #include "src/isolate.h" | 11 #include "src/isolate.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
16 HandleBase::HandleBase(Object* object, Isolate* isolate) | 16 HandleBase::HandleBase(Object* object, Isolate* isolate) |
17 : location_(HandleScope::CreateHandle(isolate, object)) {} | 17 : location_(HandleScope::GetHandle(isolate, object)) {} |
| 18 |
| 19 |
| 20 template <typename T> |
| 21 // Allocate a new handle for the object, do not canonicalize. |
| 22 Handle<T> Handle<T>::New(T* object, Isolate* isolate) { |
| 23 return Handle( |
| 24 reinterpret_cast<T**>(HandleScope::CreateHandle(isolate, object))); |
| 25 } |
18 | 26 |
19 | 27 |
20 HandleScope::HandleScope(Isolate* isolate) { | 28 HandleScope::HandleScope(Isolate* isolate) { |
21 HandleScopeData* current = isolate->handle_scope_data(); | 29 HandleScopeData* data = isolate->handle_scope_data(); |
22 isolate_ = isolate; | 30 isolate_ = isolate; |
23 prev_next_ = current->next; | 31 prev_next_ = data->next; |
24 prev_limit_ = current->limit; | 32 prev_limit_ = data->limit; |
25 current->level++; | 33 data->level++; |
26 } | 34 } |
27 | 35 |
28 | 36 |
29 template <typename T> | 37 template <typename T> |
30 inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) { | 38 inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) { |
31 return os << Brief(*handle); | 39 return os << Brief(*handle); |
32 } | 40 } |
33 | 41 |
34 | 42 |
35 HandleScope::~HandleScope() { | 43 HandleScope::~HandleScope() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 77 |
70 | 78 |
71 template <typename T> | 79 template <typename T> |
72 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { | 80 Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) { |
73 HandleScopeData* current = isolate_->handle_scope_data(); | 81 HandleScopeData* current = isolate_->handle_scope_data(); |
74 | 82 |
75 T* value = *handle_value; | 83 T* value = *handle_value; |
76 // Throw away all handles in the current scope. | 84 // Throw away all handles in the current scope. |
77 CloseScope(isolate_, prev_next_, prev_limit_); | 85 CloseScope(isolate_, prev_next_, prev_limit_); |
78 // Allocate one handle in the parent scope. | 86 // Allocate one handle in the parent scope. |
79 DCHECK(current->level > 0); | 87 DCHECK(current->level > current->sealed_level); |
80 Handle<T> result(value, isolate_); | 88 Handle<T> result(value, isolate_); |
81 // Reinitialize the current scope (so that it's ready | 89 // Reinitialize the current scope (so that it's ready |
82 // to be used or closed again). | 90 // to be used or closed again). |
83 prev_next_ = current->next; | 91 prev_next_ = current->next; |
84 prev_limit_ = current->limit; | 92 prev_limit_ = current->limit; |
85 current->level++; | 93 current->level++; |
86 return result; | 94 return result; |
87 } | 95 } |
88 | 96 |
89 | 97 |
90 template <typename T> | 98 Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) { |
91 T** HandleScope::CreateHandle(Isolate* isolate, T* value) { | |
92 DCHECK(AllowHandleAllocation::IsAllowed()); | 99 DCHECK(AllowHandleAllocation::IsAllowed()); |
93 HandleScopeData* current = isolate->handle_scope_data(); | 100 HandleScopeData* data = isolate->handle_scope_data(); |
94 | 101 |
95 Object** cur = current->next; | 102 Object** result = data->next; |
96 if (cur == current->limit) cur = Extend(isolate); | 103 if (result == data->limit) result = Extend(isolate); |
97 // Update the current next field, set the value in the created | 104 // Update the current next field, set the value in the created |
98 // handle, and return the result. | 105 // handle, and return the result. |
99 DCHECK(cur < current->limit); | 106 DCHECK(result < data->limit); |
100 current->next = cur + 1; | 107 data->next = result + 1; |
101 | 108 |
102 T** result = reinterpret_cast<T**>(cur); | |
103 *result = value; | 109 *result = value; |
104 return result; | 110 return result; |
105 } | 111 } |
106 | 112 |
107 | 113 |
| 114 Object** HandleScope::GetHandle(Isolate* isolate, Object* value) { |
| 115 DCHECK(AllowHandleAllocation::IsAllowed()); |
| 116 HandleScopeData* data = isolate->handle_scope_data(); |
| 117 CanonicalHandleScope* canonical = data->canonical_scope; |
| 118 return canonical ? canonical->Lookup(value) : CreateHandle(isolate, value); |
| 119 } |
| 120 |
| 121 |
108 #ifdef DEBUG | 122 #ifdef DEBUG |
109 inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) { | 123 inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) { |
110 // Make sure the current thread is allowed to create handles to begin with. | 124 // Make sure the current thread is allowed to create handles to begin with. |
111 CHECK(AllowHandleAllocation::IsAllowed()); | 125 CHECK(AllowHandleAllocation::IsAllowed()); |
112 HandleScopeData* current = isolate_->handle_scope_data(); | 126 HandleScopeData* current = isolate_->handle_scope_data(); |
113 // Shrink the current handle scope to make it impossible to do | 127 // Shrink the current handle scope to make it impossible to do |
114 // handle allocations without an explicit handle scope. | 128 // handle allocations without an explicit handle scope. |
115 limit_ = current->limit; | 129 prev_limit_ = current->limit; |
116 current->limit = current->next; | 130 current->limit = current->next; |
117 level_ = current->level; | 131 prev_sealed_level_ = current->sealed_level; |
118 current->level = 0; | 132 current->sealed_level = current->level; |
119 } | 133 } |
120 | 134 |
121 | 135 |
122 inline SealHandleScope::~SealHandleScope() { | 136 inline SealHandleScope::~SealHandleScope() { |
123 // Restore state in current handle scope to re-enable handle | 137 // Restore state in current handle scope to re-enable handle |
124 // allocations. | 138 // allocations. |
125 HandleScopeData* current = isolate_->handle_scope_data(); | 139 HandleScopeData* current = isolate_->handle_scope_data(); |
126 DCHECK_EQ(0, current->level); | |
127 current->level = level_; | |
128 DCHECK_EQ(current->next, current->limit); | 140 DCHECK_EQ(current->next, current->limit); |
129 current->limit = limit_; | 141 current->limit = prev_limit_; |
| 142 DCHECK_EQ(current->level, current->sealed_level); |
| 143 current->sealed_level = prev_sealed_level_; |
130 } | 144 } |
131 | 145 |
132 #endif | 146 #endif |
133 | 147 |
134 } // namespace internal | 148 } // namespace internal |
135 } // namespace v8 | 149 } // namespace v8 |
136 | 150 |
137 #endif // V8_HANDLES_INL_H_ | 151 #endif // V8_HANDLES_INL_H_ |
OLD | NEW |