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.h
Go to the documentation of this file.
1 // Copyright 2011 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 #ifndef V8_HANDLES_H_
29 #define V8_HANDLES_H_
30 
31 #include "allocation.h"
32 #include "objects.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 // ----------------------------------------------------------------------------
38 // A Handle provides a reference to an object that survives relocation by
39 // the garbage collector.
40 // Handles are only valid within a HandleScope.
41 // When a handle is created for an object a cell is allocated in the heap.
42 
43 template<typename T>
44 class Handle {
45  public:
46  INLINE(explicit Handle(T** location)) { location_ = location; }
47  INLINE(explicit Handle(T* obj));
48  INLINE(Handle(T* obj, Isolate* isolate));
49 
50  INLINE(Handle()) : location_(NULL) {}
51 
52  // Constructor for handling automatic up casting.
53  // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected.
54  template <class S> Handle(Handle<S> handle) {
55 #ifdef DEBUG
56  T* a = NULL;
57  S* b = NULL;
58  a = b; // Fake assignment to enforce type checks.
59  USE(a);
60 #endif
61  location_ = reinterpret_cast<T**>(handle.location_);
62  }
63 
64  INLINE(T* operator->() const) { return operator*(); }
65 
66  // Check if this handle refers to the exact same object as the other handle.
67  INLINE(bool is_identical_to(const Handle<T> other) const);
68 
69  // Provides the C++ dereference operator.
70  INLINE(T* operator*() const);
71 
72  // Returns the address to where the raw pointer is stored.
73  INLINE(T** location() const);
74 
75  template <class S> static Handle<T> cast(Handle<S> that) {
76  T::cast(*reinterpret_cast<T**>(that.location_));
77  return Handle<T>(reinterpret_cast<T**>(that.location_));
78  }
79 
80  static Handle<T> null() { return Handle<T>(); }
81  bool is_null() const { return location_ == NULL; }
82 
83  // Closes the given scope, but lets this handle escape. See
84  // implementation in api.h.
86 
87 #ifdef DEBUG
88  enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK };
89 
90  bool IsDereferenceAllowed(DereferenceCheckMode mode) const;
91 #endif // DEBUG
92 
93  private:
94  T** location_;
95 
96  // Handles of different classes are allowed to access each other's location_.
97  template<class S> friend class Handle;
98 };
99 
100 
101 // Convenience wrapper.
102 template<class T>
103 inline Handle<T> handle(T* t, Isolate* isolate) {
104  return Handle<T>(t, isolate);
105 }
106 
107 
108 // Convenience wrapper.
109 template<class T>
110 inline Handle<T> handle(T* t) {
111  return Handle<T>(t, t->GetIsolate());
112 }
113 
114 
115 class DeferredHandles;
116 class HandleScopeImplementer;
117 
118 
119 // A stack-allocated class that governs a number of local handles.
120 // After a handle scope has been created, all local handles will be
121 // allocated within that handle scope until either the handle scope is
122 // deleted or another handle scope is created. If there is already a
123 // handle scope and a new one is created, all allocations will take
124 // place in the new handle scope until it is deleted. After that,
125 // new handles will again be allocated in the original handle scope.
126 //
127 // After the handle scope of a local handle has been deleted the
128 // garbage collector will no longer track the object stored in the
129 // handle and may deallocate it. The behavior of accessing a handle
130 // for which the handle scope has been deleted is undefined.
131 class HandleScope {
132  public:
133  explicit inline HandleScope(Isolate* isolate);
134 
135  inline ~HandleScope();
136 
137  // Counts the number of allocated handles.
138  static int NumberOfHandles(Isolate* isolate);
139 
140  // Creates a new handle with the given value.
141  template <typename T>
142  static inline T** CreateHandle(Isolate* isolate, T* value);
143 
144  // Deallocates any extensions used by the current scope.
145  static void DeleteExtensions(Isolate* isolate);
146 
147  static Address current_next_address(Isolate* isolate);
148  static Address current_limit_address(Isolate* isolate);
149  static Address current_level_address(Isolate* isolate);
150 
151  // Closes the HandleScope (invalidating all handles
152  // created in the scope of the HandleScope) and returns
153  // a Handle backed by the parent scope holding the
154  // value of the argument handle.
155  template <typename T>
156  Handle<T> CloseAndEscape(Handle<T> handle_value);
157 
158  Isolate* isolate() { return isolate_; }
159 
160  private:
161  // Prevent heap allocation or illegal handle scopes.
162  HandleScope(const HandleScope&);
163  void operator=(const HandleScope&);
164  void* operator new(size_t size);
165  void operator delete(void* size_t);
166 
167  Isolate* isolate_;
168  Object** prev_next_;
169  Object** prev_limit_;
170 
171  // Close the handle scope resetting limits to a previous state.
172  static inline void CloseScope(Isolate* isolate,
173  Object** prev_next,
174  Object** prev_limit);
175 
176  // Extend the handle scope making room for more handles.
177  static internal::Object** Extend(Isolate* isolate);
178 
179 #ifdef ENABLE_HANDLE_ZAPPING
180  // Zaps the handles in the half-open interval [start, end).
181  static void ZapRange(Object** start, Object** end);
182 #endif
183 
184  friend class v8::HandleScope;
187  friend class v8::internal::Isolate;
188 };
189 
190 
191 class DeferredHandles;
192 
193 
195  public:
196  explicit DeferredHandleScope(Isolate* isolate);
197  // The DeferredHandles object returned stores the Handles created
198  // since the creation of this DeferredHandleScope. The Handles are
199  // alive as long as the DeferredHandles object is alive.
202 
203  private:
204  Object** prev_limit_;
205  Object** prev_next_;
206  HandleScopeImplementer* impl_;
207 
208 #ifdef DEBUG
209  bool handles_detached_;
210  int prev_level_;
211 #endif
212 
214 };
215 
216 
217 // ----------------------------------------------------------------------------
218 // Handle operations.
219 // They might invoke garbage collection. The result is an handle to
220 // an object of expected type, or the handle is an error if running out
221 // of space or encountering an internal error.
222 
223 // Flattens a string.
224 void FlattenString(Handle<String> str);
225 
226 // Flattens a string and returns the underlying external or sequential
227 // string.
229 
231  Handle<Object> key,
232  Handle<Object> value,
233  PropertyAttributes attributes);
234 
236 
238 
240 
242 
245  Handle<Object> key);
246 
248  uint32_t index);
249 
251  Handle<JSArray> array);
252 
253 // Get the JS object corresponding to the given script; create it
254 // if none exists.
256 
257 // Script line number computations. Note that the line number is zero-based.
259 // For string calculates an array of line end positions. If the string
260 // does not end with a new line character, this character may optionally be
261 // imagined.
263  bool with_imaginary_last_new_line);
264 int GetScriptLineNumber(Handle<Script> script, int code_position);
265 // The safe version does not make heap allocations but may work much slower.
266 int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
267 int GetScriptColumnNumber(Handle<Script> script, int code_position);
269 
270 // Computes the enumerable keys from interceptors. Used for debug mirrors and
271 // by GetKeysInFixedArrayFor below.
273  Handle<JSObject> object);
275  Handle<JSObject> object);
276 
278 
279 // Computes the enumerable keys for a JSObject. Used for implementing
280 // "for (n in object) { }".
282  KeyCollectionType type,
283  bool* threw);
284 Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw);
287  bool cache_result);
288 
289 // Computes the union of keys and return the result.
290 // Used for implementing "for (n in object) { }"
292  Handle<FixedArray> second);
293 
295  Handle<JSFunction> constructor,
296  Handle<JSGlobalProxy> global);
297 
298 void AddWeakObjectToCodeDependency(Heap* heap,
299  Handle<Object> object,
301 
302 // Seal off the current HandleScope so that new handles can only be created
303 // if a new HandleScope is entered.
304 class SealHandleScope BASE_EMBEDDED {
305  public:
306 #ifndef DEBUG
307  explicit SealHandleScope(Isolate* isolate) {}
309 #else
310  explicit inline SealHandleScope(Isolate* isolate);
311  inline ~SealHandleScope();
312  private:
313  Isolate* isolate_;
314  Object** limit_;
315  int level_;
316 #endif
317 };
318 
322  int level;
323 
324  void Initialize() {
325  next = limit = NULL;
326  level = 0;
327  }
328 };
329 
330 } } // namespace v8::internal
331 
332 #endif // V8_HANDLES_H_
byte * Address
Definition: globals.h:186
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
void FlattenString(Handle< String > string)
Definition: handles.cc:151
Handle< T > EscapeFrom(v8::EscapableHandleScope *scope)
Definition: api.h:318
Handle(Handle< S > handle)
Definition: handles.h:54
Handle< FixedArray > AddKeysFromJSArray(Handle< FixedArray > content, Handle< JSArray > array)
Definition: handles.cc:127
v8::Handle< v8::Array > GetKeysForIndexedInterceptor(Handle< JSReceiver > receiver, Handle< JSObject > object)
Definition: handles.cc:452
Handle< FixedArray > CalculateLineEnds(Handle< String > src, bool with_last_line)
Definition: handles.cc:328
static Handle< T > cast(Handle< S > that)
Definition: handles.h:75
internal::Object ** limit
Definition: handles.h:321
INLINE(explicit Handle(T **location))
Definition: handles.h:46
Handle< Object > DeleteProperty(Handle< JSObject > object, Handle< Object > key)
Definition: handles.cc:170
Handle< FixedArray > UnionOfKeys(Handle< FixedArray > first, Handle< FixedArray > second)
Definition: handles.cc:134
void InitScriptLineEnds(Handle< Script > script)
Definition: handles.cc:278
Handle< JSArray > GetKeysFor(Handle< JSReceiver > object, bool *threw)
Definition: handles.cc:606
friend class Handle
Definition: handles.h:97
int GetScriptColumnNumber(Handle< Script > script, int code_pos)
Definition: handles.cc:389
Handle< Object > GetProperty(Handle< JSReceiver > obj, const char *name)
Definition: handles.cc:196
PropertyAttributes
Handle< String > FlattenGetString(Handle< String > string)
Definition: handles.cc:156
Handle< Object > ForceDeleteProperty(Handle< JSObject > object, Handle< Object > key)
Definition: handles.cc:179
Handle< Object > HasProperty(Handle< JSReceiver > obj, Handle< Object > key)
Definition: handles.cc:189
DeferredHandleScope(Isolate *isolate)
Definition: handles.cc:726
Handle< FixedArray > ReduceFixedArrayTo(Handle< FixedArray > array, int length)
Definition: handles.cc:615
DeferredHandles * Detach()
Definition: handles.cc:753
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
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 size
Definition: flags.cc:211
HandleScope(Isolate *isolate)
Definition: handles-inl.h:112
static int NumberOfHandles(Isolate *isolate)
Definition: handles.cc:48
INLINE(T *operator->() const)
Definition: handles.h:64
SealHandleScope(Isolate *isolate)
Definition: handles.h:307
Handle< JSValue > GetScriptWrapper(Handle< Script > script)
Definition: handles.cc:240
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 code(assertions) for debugging") DEFINE_bool(code_comments
int GetScriptLineNumberSafe(Handle< Script > script, int code_pos)
Definition: handles.cc:403
Handle< FixedArray > GetKeysInFixedArrayFor(Handle< JSReceiver > object, KeyCollectionType type, bool *threw)
Definition: handles.cc:505
#define BASE_EMBEDDED
Definition: allocation.h:68
internal::Object ** next
Definition: handles.h:320
static Address current_limit_address(Isolate *isolate)
Definition: handles.cc:122
#define T(name, string, precedence)
Definition: token.cc:48
void AddWeakObjectToCodeDependency(Heap *heap, Handle< Object > object, Handle< Code > code)
Definition: handles.cc:765
int GetScriptLineNumber(Handle< Script > script, int code_pos)
Definition: handles.cc:363
v8::Handle< v8::Array > GetKeysForNamedInterceptor(Handle< JSReceiver > receiver, Handle< JSObject > object)
Definition: handles.cc:429
bool is_null() const
Definition: handles.h:81
Handle< T > handle(T *t, Isolate *isolate)
Definition: handles.h:103
Handle< String > LookupSingleCharacterStringFromCode(Isolate *isolate, uint32_t index)
Definition: handles.cc:212
INLINE(Handle())
Definition: handles.h:50
Handle< Object > GetScriptNameOrSourceURL(Handle< Script > script)
Definition: handles.cc:474
Handle< JSGlobalProxy > ReinitializeJSGlobalProxy(Handle< JSFunction > constructor, Handle< JSGlobalProxy > global)
Definition: handles.cc:141
static Handle< T > null()
Definition: handles.h:80
void USE(T)
Definition: globals.h:341
HeapObject * obj
Handle< Object > ForceSetProperty(Handle< JSObject > object, Handle< Object > key, Handle< Object > value, PropertyAttributes attributes)
Definition: handles.cc:161
Handle< FixedArray > GetEnumPropertyKeys(Handle< JSObject > object, bool cache_result)
Definition: handles.cc:626
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 expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in name
Definition: flags.cc:505
static Address current_next_address(Isolate *isolate)
Definition: handles.cc:117
static Address current_level_address(Isolate *isolate)
Definition: handles.cc:112
Handle< T > CloseAndEscape(Handle< T > handle_value)
Definition: handles-inl.h:146
static void DeleteExtensions(Isolate *isolate)
Definition: handles.cc:96