blob: bb51968815196232bc0104b8d4816eae7664260c [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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#ifndef V8_HANDLES_H_
29#define V8_HANDLES_H_
30
31#include "apiutils.h"
32
33namespace v8 {
34namespace internal {
35
36// ----------------------------------------------------------------------------
37// A Handle provides a reference to an object that survives relocation by
38// the garbage collector.
39// Handles are only valid within a HandleScope.
40// When a handle is created for an object a cell is allocated in the heap.
41
Ben Murdoche0cee9b2011-05-25 10:26:03 +010042template<typename T>
Steve Blocka7e24c12009-10-30 11:49:00 +000043class Handle {
44 public:
Steve Block6ded16b2010-05-10 14:33:55 +010045 INLINE(explicit Handle(T** location)) { location_ = location; }
Steve Blocka7e24c12009-10-30 11:49:00 +000046 INLINE(explicit Handle(T* obj));
47
48 INLINE(Handle()) : location_(NULL) {}
49
50 // Constructor for handling automatic up casting.
51 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected.
52 template <class S> Handle(Handle<S> handle) {
53#ifdef DEBUG
54 T* a = NULL;
55 S* b = NULL;
56 a = b; // Fake assignment to enforce type checks.
57 USE(a);
58#endif
59 location_ = reinterpret_cast<T**>(handle.location());
60 }
61
62 INLINE(T* operator ->() const) { return operator*(); }
63
64 // Check if this handle refers to the exact same object as the other handle.
65 bool is_identical_to(const Handle<T> other) const {
66 return operator*() == *other;
67 }
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 T** location() const {
74 ASSERT(location_ == NULL ||
75 reinterpret_cast<Address>(*location_) != kZapValue);
76 return location_;
77 }
78
79 template <class S> static Handle<T> cast(Handle<S> that) {
80 T::cast(*that);
81 return Handle<T>(reinterpret_cast<T**>(that.location()));
82 }
83
84 static Handle<T> null() { return Handle<T>(); }
85 bool is_null() { return location_ == NULL; }
86
87 // Closes the given scope, but lets this handle escape. See
88 // implementation in api.h.
89 inline Handle<T> EscapeFrom(v8::HandleScope* scope);
90
91 private:
92 T** location_;
93};
94
95
96// A stack-allocated class that governs a number of local handles.
97// After a handle scope has been created, all local handles will be
98// allocated within that handle scope until either the handle scope is
99// deleted or another handle scope is created. If there is already a
100// handle scope and a new one is created, all allocations will take
101// place in the new handle scope until it is deleted. After that,
102// new handles will again be allocated in the original handle scope.
103//
104// After the handle scope of a local handle has been deleted the
105// garbage collector will no longer track the object stored in the
106// handle and may deallocate it. The behavior of accessing a handle
107// for which the handle scope has been deleted is undefined.
108class HandleScope {
109 public:
John Reck59135872010-11-02 12:39:01 -0700110 HandleScope() : prev_next_(current_.next), prev_limit_(current_.limit) {
111 current_.level++;
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 }
113
114 ~HandleScope() {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100115 CloseScope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 }
117
118 // Counts the number of allocated handles.
119 static int NumberOfHandles();
120
121 // Creates a new handle with the given value.
122 template <typename T>
123 static inline T** CreateHandle(T* value) {
124 internal::Object** cur = current_.next;
125 if (cur == current_.limit) cur = Extend();
126 // Update the current next field, set the value in the created
127 // handle, and return the result.
128 ASSERT(cur < current_.limit);
129 current_.next = cur + 1;
130
131 T** result = reinterpret_cast<T**>(cur);
132 *result = value;
133 return result;
134 }
135
Steve Blockd0582a62009-12-15 09:54:21 +0000136 // Deallocates any extensions used by the current scope.
137 static void DeleteExtensions();
138
Steve Blockd0582a62009-12-15 09:54:21 +0000139 static Address current_next_address();
140 static Address current_limit_address();
John Reck59135872010-11-02 12:39:01 -0700141 static Address current_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000142
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100143 // Closes the HandleScope (invalidating all handles
144 // created in the scope of the HandleScope) and returns
145 // a Handle backed by the parent scope holding the
146 // value of the argument handle.
147 template <typename T>
148 Handle<T> CloseAndEscape(Handle<T> handle_value) {
149 T* value = *handle_value;
150 // Throw away all handles in the current scope.
151 CloseScope();
152 // Allocate one handle in the parent scope.
153 ASSERT(current_.level > 0);
154 Handle<T> result(CreateHandle<T>(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
Steve Blocka7e24c12009-10-30 11:49:00 +0000163 private:
164 // Prevent heap allocation or illegal handle scopes.
165 HandleScope(const HandleScope&);
166 void operator=(const HandleScope&);
167 void* operator new(size_t size);
168 void operator delete(void* size_t);
169
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100170 inline void CloseScope() {
171 current_.next = prev_next_;
172 current_.level--;
173 if (current_.limit != prev_limit_) {
174 current_.limit = prev_limit_;
175 DeleteExtensions();
176 }
177#ifdef DEBUG
178 ZapRange(prev_next_, prev_limit_);
179#endif
180 }
181
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 static v8::ImplementationUtilities::HandleScopeData current_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100183 // Holds values on entry. The prev_next_ value is never NULL
184 // on_entry, but is set to NULL when this scope is closed.
185 Object** prev_next_;
186 Object** prev_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000187
188 // Extend the handle scope making room for more handles.
189 static internal::Object** Extend();
190
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 // Zaps the handles in the half-open interval [start, end).
192 static void ZapRange(internal::Object** start, internal::Object** end);
193
194 friend class v8::HandleScope;
195 friend class v8::ImplementationUtilities;
196};
197
198
199// ----------------------------------------------------------------------------
200// Handle operations.
201// They might invoke garbage collection. The result is an handle to
202// an object of expected type, or the handle is an error if running out
203// of space or encountering an internal error.
204
205void NormalizeProperties(Handle<JSObject> object,
206 PropertyNormalizationMode mode,
207 int expected_additional_properties);
208void NormalizeElements(Handle<JSObject> object);
209void TransformToFastProperties(Handle<JSObject> object,
210 int unused_property_fields);
John Reck59135872010-11-02 12:39:01 -0700211void NumberDictionarySet(Handle<NumberDictionary> dictionary,
212 uint32_t index,
213 Handle<Object> value,
214 PropertyDetails details);
Steve Block8defd9f2010-07-08 12:39:36 +0100215
216// Flattens a string.
Steve Blocka7e24c12009-10-30 11:49:00 +0000217void FlattenString(Handle<String> str);
218
Steve Block8defd9f2010-07-08 12:39:36 +0100219// Flattens a string and returns the underlying external or sequential
220// string.
221Handle<String> FlattenGetString(Handle<String> str);
222
Steve Blocka7e24c12009-10-30 11:49:00 +0000223Handle<Object> SetProperty(Handle<JSObject> object,
224 Handle<String> key,
225 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100226 PropertyAttributes attributes,
227 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000228
229Handle<Object> SetProperty(Handle<Object> object,
230 Handle<Object> key,
231 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100232 PropertyAttributes attributes,
233 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000234
235Handle<Object> ForceSetProperty(Handle<JSObject> object,
236 Handle<Object> key,
237 Handle<Object> value,
238 PropertyAttributes attributes);
239
Andrei Popescu31002712010-02-23 13:46:05 +0000240Handle<Object> SetNormalizedProperty(Handle<JSObject> object,
241 Handle<String> key,
242 Handle<Object> value,
243 PropertyDetails details);
244
Steve Blocka7e24c12009-10-30 11:49:00 +0000245Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
246 Handle<Object> key);
247
Ben Murdoch086aeea2011-05-13 15:57:08 +0100248Handle<Object> SetLocalPropertyIgnoreAttributes(
249 Handle<JSObject> object,
250 Handle<String> key,
251 Handle<Object> value,
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 PropertyAttributes attributes);
253
Steve Block1e0659c2011-05-24 12:43:12 +0100254// Used to set local properties on the object we totally control
255// and which therefore has no accessors and alikes.
256void SetLocalPropertyNoThrow(Handle<JSObject> object,
257 Handle<String> key,
258 Handle<Object> value,
259 PropertyAttributes attributes = NONE);
260
Steve Blocka7e24c12009-10-30 11:49:00 +0000261Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
262 Handle<String> key,
263 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100264 PropertyAttributes attributes,
265 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000266
267Handle<Object> SetElement(Handle<JSObject> object,
268 uint32_t index,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100269 Handle<Object> value,
270 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000271
Ben Murdoch086aeea2011-05-13 15:57:08 +0100272Handle<Object> SetOwnElement(Handle<JSObject> object,
273 uint32_t index,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100274 Handle<Object> value,
275 StrictModeFlag strict_mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100276
Steve Blocka7e24c12009-10-30 11:49:00 +0000277Handle<Object> GetProperty(Handle<JSObject> obj,
278 const char* name);
279
280Handle<Object> GetProperty(Handle<Object> obj,
281 Handle<Object> key);
282
Steve Block6ded16b2010-05-10 14:33:55 +0100283Handle<Object> GetElement(Handle<Object> obj,
284 uint32_t index);
285
Steve Blocka7e24c12009-10-30 11:49:00 +0000286Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
287 Handle<JSObject> holder,
288 Handle<String> name,
289 PropertyAttributes* attributes);
290
291Handle<Object> GetPrototype(Handle<Object> obj);
292
Andrei Popescu402d9372010-02-26 13:31:12 +0000293Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value);
294
Steve Blocka7e24c12009-10-30 11:49:00 +0000295// Return the object's hidden properties object. If the object has no hidden
296// properties and create_if_needed is true, then a new hidden property object
297// will be allocated. Otherwise the Heap::undefined_value is returned.
298Handle<Object> GetHiddenProperties(Handle<JSObject> obj, bool create_if_needed);
299
300Handle<Object> DeleteElement(Handle<JSObject> obj, uint32_t index);
301Handle<Object> DeleteProperty(Handle<JSObject> obj, Handle<String> prop);
302
303Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);
304
305Handle<JSObject> Copy(Handle<JSObject> obj);
306
Leon Clarkef7060e22010-06-03 12:02:55 +0100307Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info);
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
310 Handle<JSArray> array);
311
312// Get the JS object corresponding to the given script; create it
313// if none exists.
314Handle<JSValue> GetScriptWrapper(Handle<Script> script);
315
316// Script line number computations.
317void InitScriptLineEnds(Handle<Script> script);
Steve Block6ded16b2010-05-10 14:33:55 +0100318// For string calculates an array of line end positions. If the string
319// does not end with a new line character, this character may optionally be
320// imagined.
321Handle<FixedArray> CalculateLineEnds(Handle<String> string,
322 bool with_imaginary_last_new_line);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323int GetScriptLineNumber(Handle<Script> script, int code_position);
Steve Block6ded16b2010-05-10 14:33:55 +0100324// The safe version does not make heap allocations but may work much slower.
325int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000326
327// Computes the enumerable keys from interceptors. Used for debug mirrors and
328// by GetKeysInFixedArrayFor below.
329v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
330 Handle<JSObject> object);
331v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
332 Handle<JSObject> object);
333
334enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS };
335
336// Computes the enumerable keys for a JSObject. Used for implementing
337// "for (n in object) { }".
338Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object,
339 KeyCollectionType type);
340Handle<JSArray> GetKeysFor(Handle<JSObject> object);
Steve Blockd0582a62009-12-15 09:54:21 +0000341Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
342 bool cache_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000343
344// Computes the union of keys and return the result.
345// Used for implementing "for (n in object) { }"
346Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
347 Handle<FixedArray> second);
348
Steve Block6ded16b2010-05-10 14:33:55 +0100349Handle<String> SubString(Handle<String> str,
350 int start,
351 int end,
352 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000353
354
355// Sets the expected number of properties for the function's instances.
356void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
357
358// Sets the prototype property for a function instance.
359void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value);
360
361// Sets the expected number of properties based on estimate from compiler.
362void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
363 int estimate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364
365
366Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
367 Handle<JSFunction> constructor,
368 Handle<JSGlobalProxy> global);
369
370Handle<Object> SetPrototype(Handle<JSFunction> function,
371 Handle<Object> prototype);
372
373
Steve Blockd0582a62009-12-15 09:54:21 +0000374// Does lazy compilation of the given function. Returns true on success and
375// false if the compilation resulted in a stack overflow.
Steve Blocka7e24c12009-10-30 11:49:00 +0000376enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION };
377
Leon Clarke4515c472010-02-03 11:58:03 +0000378bool EnsureCompiled(Handle<SharedFunctionInfo> shared,
379 ClearExceptionFlag flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000380
Leon Clarke4515c472010-02-03 11:58:03 +0000381bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
382 ClearExceptionFlag flag);
383
Ben Murdochf87a2032010-10-22 12:50:53 +0100384bool CompileLazy(Handle<JSFunction> function, ClearExceptionFlag flag);
Leon Clarke4515c472010-02-03 11:58:03 +0000385
Ben Murdochf87a2032010-10-22 12:50:53 +0100386bool CompileLazyInLoop(Handle<JSFunction> function, ClearExceptionFlag flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000387
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100388bool CompileOptimized(Handle<JSFunction> function,
389 int osr_ast_id,
390 ClearExceptionFlag flag);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100391
Steve Blocka7e24c12009-10-30 11:49:00 +0000392class NoHandleAllocation BASE_EMBEDDED {
393 public:
394#ifndef DEBUG
395 NoHandleAllocation() {}
396 ~NoHandleAllocation() {}
397#else
398 inline NoHandleAllocation();
399 inline ~NoHandleAllocation();
400 private:
John Reck59135872010-11-02 12:39:01 -0700401 int level_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000402#endif
403};
404
405
406// ----------------------------------------------------------------------------
407
408
409// Stack allocated wrapper call for optimizing adding multiple
410// properties to an object.
411class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED {
412 public:
413 OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object,
414 int expected_property_count,
415 bool condition = true);
416 ~OptimizedObjectForAddingMultipleProperties();
417 private:
418 bool has_been_transformed_; // Tells whether the object has been transformed.
419 int unused_property_fields_; // Captures the unused number of field.
420 Handle<JSObject> object_; // The object being optimized.
421};
422
423
424} } // namespace v8::internal
425
426#endif // V8_HANDLES_H_