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