v8  3.14.5(node0.10.28)
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 "apiutils.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  bool is_identical_to(const Handle<T> other) const {
68  return operator*() == *other;
69  }
70 
71  // Provides the C++ dereference operator.
72  INLINE(T* operator*() const);
73 
74  // Returns the address to where the raw pointer is stored.
75  T** location() const {
76  ASSERT(location_ == NULL ||
77  reinterpret_cast<Address>(*location_) != kZapValue);
78  return location_;
79  }
80 
81  template <class S> static Handle<T> cast(Handle<S> that) {
82  T::cast(*that);
83  return Handle<T>(reinterpret_cast<T**>(that.location()));
84  }
85 
86  static Handle<T> null() { return Handle<T>(); }
87  bool is_null() const { return location_ == NULL; }
88 
89  // Closes the given scope, but lets this handle escape. See
90  // implementation in api.h.
91  inline Handle<T> EscapeFrom(v8::HandleScope* scope);
92 
93  private:
94  T** location_;
95 };
96 
97 
98 class DeferredHandles;
99 class HandleScopeImplementer;
100 
101 
102 // A stack-allocated class that governs a number of local handles.
103 // After a handle scope has been created, all local handles will be
104 // allocated within that handle scope until either the handle scope is
105 // deleted or another handle scope is created. If there is already a
106 // handle scope and a new one is created, all allocations will take
107 // place in the new handle scope until it is deleted. After that,
108 // new handles will again be allocated in the original handle scope.
109 //
110 // After the handle scope of a local handle has been deleted the
111 // garbage collector will no longer track the object stored in the
112 // handle and may deallocate it. The behavior of accessing a handle
113 // for which the handle scope has been deleted is undefined.
114 class HandleScope {
115  public:
116  inline HandleScope();
117  explicit inline HandleScope(Isolate* isolate);
118 
119  inline ~HandleScope();
120 
121  // Counts the number of allocated handles.
122  static int NumberOfHandles();
123 
124  // Creates a new handle with the given value.
125  template <typename T>
126  static inline T** CreateHandle(T* value, Isolate* isolate);
127 
128  // Deallocates any extensions used by the current scope.
129  static void DeleteExtensions(Isolate* isolate);
130 
131  static Address current_next_address();
134 
135  // Closes the HandleScope (invalidating all handles
136  // created in the scope of the HandleScope) and returns
137  // a Handle backed by the parent scope holding the
138  // value of the argument handle.
139  template <typename T>
140  Handle<T> CloseAndEscape(Handle<T> handle_value);
141 
142  Isolate* isolate() { return isolate_; }
143 
144  private:
145  // Prevent heap allocation or illegal handle scopes.
146  HandleScope(const HandleScope&);
147  void operator=(const HandleScope&);
148  void* operator new(size_t size);
149  void operator delete(void* size_t);
150 
151  inline void CloseScope();
152 
153  Isolate* isolate_;
154  Object** prev_next_;
155  Object** prev_limit_;
156 
157  // Extend the handle scope making room for more handles.
158  static internal::Object** Extend();
159 
160  // Zaps the handles in the half-open interval [start, end).
161  static void ZapRange(internal::Object** start, internal::Object** end);
162 
164  friend class v8::HandleScope;
167  friend class v8::internal::Isolate;
168 };
169 
170 
171 class DeferredHandles;
172 
173 
175  public:
176  explicit DeferredHandleScope(Isolate* isolate);
177  // The DeferredHandles object returned stores the Handles created
178  // since the creation of this DeferredHandleScope. The Handles are
179  // alive as long as the DeferredHandles object is alive.
182 
183  private:
184  Object** prev_limit_;
185  Object** prev_next_;
186  HandleScopeImplementer* impl_;
187 
188 #ifdef DEBUG
189  bool handles_detached_;
190  int prev_level_;
191 #endif
192 
194 };
195 
196 
197 // ----------------------------------------------------------------------------
198 // Handle operations.
199 // They might invoke garbage collection. The result is an handle to
200 // an object of expected type, or the handle is an error if running out
201 // of space or encountering an internal error.
202 
203 // Flattens a string.
204 void FlattenString(Handle<String> str);
205 
206 // Flattens a string and returns the underlying external or sequential
207 // string.
209 
210 int Utf8Length(Handle<String> str);
211 
213  Handle<Object> key,
214  Handle<Object> value,
215  PropertyAttributes attributes,
216  StrictModeFlag strict_mode);
217 
219  Handle<Object> key,
220  Handle<Object> value,
221  PropertyAttributes attributes);
222 
224  Handle<Object> key);
225 
227  const char* name);
228 
230  Handle<Object> key);
231 
233  Handle<JSObject> holder,
234  Handle<String> name,
235  PropertyAttributes* attributes);
236 
238 
240 
242 
244 
246  Handle<JSArray> array);
247 
248 // Get the JS object corresponding to the given script; create it
249 // if none exists.
251 
252 // Script line number computations. Note that the line number is zero-based.
254 // For string calculates an array of line end positions. If the string
255 // does not end with a new line character, this character may optionally be
256 // imagined.
258  bool with_imaginary_last_new_line);
259 int GetScriptLineNumber(Handle<Script> script, int code_position);
260 // The safe version does not make heap allocations but may work much slower.
261 int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
262 int GetScriptColumnNumber(Handle<Script> script, int code_position);
263 
264 // Computes the enumerable keys from interceptors. Used for debug mirrors and
265 // by GetKeysInFixedArrayFor below.
267  Handle<JSObject> object);
269  Handle<JSObject> object);
270 
272 
273 // Computes the enumerable keys for a JSObject. Used for implementing
274 // "for (n in object) { }".
276  KeyCollectionType type,
277  bool* threw);
278 Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw);
281  bool cache_result);
282 
283 // Computes the union of keys and return the result.
284 // Used for implementing "for (n in object) { }"
286  Handle<FixedArray> second);
287 
289  int start,
290  int end,
291  PretenureFlag pretenure = NOT_TENURED);
292 
293 // Sets the expected number of properties for the function's instances.
294 void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
295 
296 // Sets the prototype property for a function instance.
298 
299 // Sets the expected number of properties based on estimate from compiler.
301  int estimate);
302 
303 
305  Handle<JSFunction> constructor,
306  Handle<JSGlobalProxy> global);
307 
309  Handle<Object> prototype);
310 
312  Handle<Object> key);
313 
315  Handle<Object> key);
316 
318  Handle<Object> key,
319  Handle<Object> value);
320 
321 class NoHandleAllocation BASE_EMBEDDED {
322  public:
323 #ifndef DEBUG
326 #else
327  inline NoHandleAllocation();
328  inline ~NoHandleAllocation();
329  private:
330  int level_;
331  bool active_;
332 #endif
333 };
334 
335 } } // namespace v8::internal
336 
337 #endif // V8_HANDLES_H_
byte * Address
Definition: globals.h:157
int Utf8Length(Handle< String > str)
Definition: handles.cc:1010
void FlattenString(Handle< String > string)
Definition: handles.cc:211
static int NumberOfHandles()
Definition: handles.cc:48
static Address current_level_address()
Definition: handles.cc:115
Handle(Handle< S > handle)
Definition: handles.h:54
Handle< FixedArray > AddKeysFromJSArray(Handle< FixedArray > content, Handle< JSArray > array)
Definition: handles.cc:133
v8::Handle< v8::Array > GetKeysForIndexedInterceptor(Handle< JSReceiver > receiver, Handle< JSObject > object)
Definition: handles.cc:572
Handle< FixedArray > CalculateLineEnds(Handle< String > src, bool with_last_line)
Definition: handles.cc:444
static Handle< T > cast(Handle< S > that)
Definition: handles.h:81
bool is_identical_to(const Handle< T > other) const
Definition: handles.h:67
INLINE(explicit Handle(T **location))
Definition: handles.h:46
Handle< Object > SetAccessor(Handle< JSObject > obj, Handle< AccessorInfo > info)
Definition: handles.cc:342
void SetPrototypeProperty(Handle< JSFunction > func, Handle< JSObject > value)
Definition: handles.cc:176
static Address current_next_address()
Definition: handles.cc:121
Handle< FixedArray > UnionOfKeys(Handle< FixedArray > first, Handle< FixedArray > second)
Definition: handles.cc:140
void InitScriptLineEnds(Handle< Script > script)
Definition: handles.cc:394
#define ASSERT(condition)
Definition: checks.h:270
Handle< JSArray > GetKeysFor(Handle< JSReceiver > object, bool *threw)
Definition: handles.cc:699
Handle< Object > GetPropertyWithInterceptor(Handle< JSObject > receiver, Handle< JSObject > holder, Handle< String > name, PropertyAttributes *attributes)
Definition: handles.cc:298
int GetScriptColumnNumber(Handle< Script > script, int code_pos)
Definition: handles.cc:504
Handle< Object > GetProperty(Handle< JSReceiver > obj, const char *name)
Definition: handles.cc:282
PropertyAttributes
Handle< String > FlattenGetString(Handle< String > string)
Definition: handles.cc:216
Handle< Object > ForceDeleteProperty(Handle< JSObject > object, Handle< Object > key)
Definition: handles.cc:259
static Address current_limit_address()
Definition: handles.cc:127
Handle< String > SubString(Handle< String > str, int start, int end, PretenureFlag pretenure)
Definition: handles.cc:326
DeferredHandleScope(Isolate *isolate)
Definition: handles.cc:1025
T ** location() const
Definition: handles.h:75
Handle< FixedArray > ReduceFixedArrayTo(Handle< FixedArray > array, int length)
Definition: handles.cc:708
DeferredHandles * Detach()
Definition: handles.cc:1054
Handle< Object > LookupSingleCharacterStringFromCode(uint32_t index)
Definition: handles.cc:318
void SetExpectedNofProperties(Handle< JSFunction > func, int nof)
Definition: handles.cc:157
Handle< ObjectHashSet > ObjectHashSetAdd(Handle< ObjectHashSet > table, Handle< Object > key)
Definition: handles.cc:841
Handle< Object > SetProperty(Handle< Object > object, Handle< Object > key, Handle< Object > value, PropertyAttributes attributes, StrictModeFlag strict_mode)
Definition: handles.cc:232
INLINE(T *operator->() const)
Definition: handles.h:64
const Address kZapValue
Definition: v8globals.h:80
Handle< JSValue > GetScriptWrapper(Handle< Script > script)
Definition: handles.cc:366
int GetScriptLineNumberSafe(Handle< Script > script, int code_pos)
Definition: handles.cc:517
Handle< FixedArray > GetKeysInFixedArrayFor(Handle< JSReceiver > object, KeyCollectionType type, bool *threw)
Definition: handles.cc:606
#define BASE_EMBEDDED
Definition: allocation.h:68
Handle< ObjectHashTable > PutIntoObjectHashTable(Handle< ObjectHashTable > table, Handle< Object > key, Handle< Object > value)
Definition: handles.cc:857
#define T(name, string, precedence)
Definition: token.cc:48
int GetScriptLineNumber(Handle< Script > script, int code_pos)
Definition: handles.cc:479
v8::Handle< v8::Array > GetKeysForNamedInterceptor(Handle< JSReceiver > receiver, Handle< JSObject > object)
Definition: handles.cc:547
bool is_null() const
Definition: handles.h:87
INLINE(Handle())
Definition: handles.h:50
Handle< T > EscapeFrom(v8::HandleScope *scope)
Definition: api.h:246
Handle< JSGlobalProxy > ReinitializeJSGlobalProxy(Handle< JSFunction > constructor, Handle< JSGlobalProxy > global)
Definition: handles.cc:147
static Handle< T > null()
Definition: handles.h:86
void USE(T)
Definition: globals.h:289
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit 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 SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available 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 MIPS FPU instructions if NULL
Definition: flags.cc:301
Handle< Object > ForceSetProperty(Handle< JSObject > object, Handle< Object > key, Handle< Object > value, PropertyAttributes attributes)
Definition: handles.cc:246
Handle< ObjectHashSet > ObjectHashSetRemove(Handle< ObjectHashSet > table, Handle< Object > key)
Definition: handles.cc:849
void SetExpectedNofPropertiesFromEstimate(Handle< SharedFunctionInfo > shared, int estimate)
Definition: handles.cc:201
Handle< Object > SetPrototype(Handle< JSFunction > function, Handle< Object > prototype)
Definition: handles.cc:221
Handle< FixedArray > GetEnumPropertyKeys(Handle< JSObject > object, bool cache_result)
Definition: handles.cc:719
Handle< T > CloseAndEscape(Handle< T > handle_value)
Definition: handles-inl.h:113
static void DeleteExtensions(Isolate *isolate)
Definition: handles.cc:99
static T ** CreateHandle(T *value, Isolate *isolate)
Definition: handles-inl.h:132
Handle< JSObject > Copy(Handle< JSObject > obj)
Definition: handles.cc:335