v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
handles-inl.h
Go to the documentation of this file.
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
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 
32 #include "api.h"
33 #include "handles.h"
34 #include "heap.h"
35 #include "isolate.h"
36 
37 namespace v8 {
38 namespace internal {
39 
40 template<typename T>
42  ASSERT(!obj->IsFailure());
43  location_ = HandleScope::CreateHandle(obj->GetIsolate(), obj);
44 }
45 
46 
47 template<typename T>
49  ASSERT(!obj->IsFailure());
50  location_ = HandleScope::CreateHandle(isolate, obj);
51 }
52 
53 
54 template <typename T>
55 inline bool Handle<T>::is_identical_to(const Handle<T> other) const {
56  ASSERT(location_ == NULL || !(*location_)->IsFailure());
57  if (location_ == other.location_) return true;
58  if (location_ == NULL || other.location_ == NULL) return false;
59  // Dereferencing deferred handles to check object equality is safe.
60  SLOW_ASSERT(IsDereferenceAllowed(NO_DEFERRED_CHECK) &&
61  other.IsDereferenceAllowed(NO_DEFERRED_CHECK));
62  return *location_ == *other.location_;
63 }
64 
65 
66 template <typename T>
67 inline T* Handle<T>::operator*() const {
68  ASSERT(location_ != NULL && !(*location_)->IsFailure());
69  SLOW_ASSERT(IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK));
70  return *BitCast<T**>(location_);
71 }
72 
73 template <typename T>
74 inline T** Handle<T>::location() const {
75  ASSERT(location_ == NULL || !(*location_)->IsFailure());
76  SLOW_ASSERT(location_ == NULL ||
77  IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK));
78  return location_;
79 }
80 
81 #ifdef DEBUG
82 template <typename T>
83 bool Handle<T>::IsDereferenceAllowed(DereferenceCheckMode mode) const {
84  ASSERT(location_ != NULL);
85  Object* object = *BitCast<T**>(location_);
86  if (object->IsSmi()) return true;
87  HeapObject* heap_object = HeapObject::cast(object);
88  Heap* heap = heap_object->GetHeap();
89  Object** handle = reinterpret_cast<Object**>(location_);
90  Object** roots_array_start = heap->roots_array_start();
91  if (roots_array_start <= handle &&
92  handle < roots_array_start + Heap::kStrongRootListLength &&
93  heap->RootCanBeTreatedAsConstant(
94  static_cast<Heap::RootListIndex>(handle - roots_array_start))) {
95  return true;
96  }
97  if (!AllowHandleDereference::IsAllowed()) return false;
98  if (mode == INCLUDE_DEFERRED_CHECK &&
99  !AllowDeferredHandleDereference::IsAllowed()) {
100  // Accessing cells, maps and internalized strings is safe.
101  if (heap_object->IsCell()) return true;
102  if (heap_object->IsMap()) return true;
103  if (heap_object->IsInternalizedString()) return true;
104  return !heap->isolate()->IsDeferredHandle(handle);
105  }
106  return true;
107 }
108 #endif
109 
110 
111 
113  HandleScopeData* current = isolate->handle_scope_data();
114  isolate_ = isolate;
115  prev_next_ = current->next;
116  prev_limit_ = current->limit;
117  current->level++;
118 }
119 
120 
122  CloseScope(isolate_, prev_next_, prev_limit_);
123 }
124 
125 
126 void HandleScope::CloseScope(Isolate* isolate,
127  Object** prev_next,
128  Object** prev_limit) {
129  HandleScopeData* current = isolate->handle_scope_data();
130 
131  std::swap(current->next, prev_next);
132  current->level--;
133  if (current->limit != prev_limit) {
134  current->limit = prev_limit;
135  DeleteExtensions(isolate);
136 #ifdef ENABLE_HANDLE_ZAPPING
137  ZapRange(current->next, prev_limit);
138  } else {
139  ZapRange(current->next, prev_next);
140 #endif
141  }
142 }
143 
144 
145 template <typename T>
147  HandleScopeData* current = isolate_->handle_scope_data();
148 
149  T* value = *handle_value;
150  // Throw away all handles in the current scope.
151  CloseScope(isolate_, prev_next_, prev_limit_);
152  // Allocate one handle in the parent scope.
153  ASSERT(current->level > 0);
154  Handle<T> result(CreateHandle<T>(isolate_, value));
155  // Reinitialize the current scope (so that it's ready
156  // to be used or closed again).
157  prev_next_ = current->next;
158  prev_limit_ = current->limit;
159  current->level++;
160  return result;
161 }
162 
163 
164 template <typename T>
165 T** HandleScope::CreateHandle(Isolate* isolate, T* value) {
166  ASSERT(AllowHandleAllocation::IsAllowed());
167  HandleScopeData* current = isolate->handle_scope_data();
168 
169  internal::Object** cur = current->next;
170  if (cur == current->limit) cur = Extend(isolate);
171  // Update the current next field, set the value in the created
172  // handle, and return the result.
173  ASSERT(cur < current->limit);
174  current->next = cur + 1;
175 
176  T** result = reinterpret_cast<T**>(cur);
177  *result = value;
178  return result;
179 }
180 
181 
182 #ifdef DEBUG
183 inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) {
184  // Make sure the current thread is allowed to create handles to begin with.
185  CHECK(AllowHandleAllocation::IsAllowed());
186  HandleScopeData* current = isolate_->handle_scope_data();
187  // Shrink the current handle scope to make it impossible to do
188  // handle allocations without an explicit handle scope.
189  limit_ = current->limit;
190  current->limit = current->next;
191  level_ = current->level;
192  current->level = 0;
193 }
194 
195 
196 inline SealHandleScope::~SealHandleScope() {
197  // Restore state in current handle scope to re-enable handle
198  // allocations.
199  HandleScopeData* current = isolate_->handle_scope_data();
200  ASSERT_EQ(0, current->level);
201  current->level = level_;
202  ASSERT_EQ(current->next, current->limit);
203  current->limit = limit_;
204 }
205 
206 #endif
207 
208 } } // namespace v8::internal
209 
210 #endif // V8_HANDLES_INL_H_
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
static T ** CreateHandle(Isolate *isolate, T *value)
Definition: handles-inl.h:165
#define SLOW_ASSERT(condition)
Definition: checks.h:306
static HeapObject * cast(Object *obj)
internal::Object ** limit
Definition: handles.h:321
kSerializedDataOffset Object
Definition: objects-inl.h:5016
HandleScopeData * handle_scope_data()
Definition: isolate.h:900
#define ASSERT(condition)
Definition: checks.h:329
friend class Handle
Definition: handles.h:97
#define CHECK(condition)
Definition: checks.h:75
V8_INLINE T * operator*() const
Definition: v8.h:257
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long mode(MIPS only)") DEFINE_string(expose_natives_as
HandleScope(Isolate *isolate)
Definition: handles-inl.h:112
Definition: v8.h:123
internal::Object ** next
Definition: handles.h:320
#define T(name, string, precedence)
Definition: token.cc:48
Handle< T > handle(T *t, Isolate *isolate)
Definition: handles.h:103
#define ASSERT_EQ(v1, v2)
Definition: checks.h:330
HeapObject * obj
Handle< T > CloseAndEscape(Handle< T > handle_value)
Definition: handles-inl.h:146
static void DeleteExtensions(Isolate *isolate)
Definition: handles.cc:96