blob: 69170ff203fd17ba7f7ba65117bdf20d56e81a57 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
ager@chromium.orgddb913d2009-01-27 10:01:48 +000031#include "apiutils.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36// ----------------------------------------------------------------------------
37// A Handle provides a reference to an object that survives relocation by
38// the garbage collector.
ager@chromium.orgddb913d2009-01-27 10:01:48 +000039// Handles are only valid within a HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040// When a handle is created for an object a cell is allocated in the heap.
41
42template<class T>
43class Handle {
44 public:
ager@chromium.orgac091b72010-05-05 07:34:42 +000045 INLINE(explicit Handle(T** location)) { location_ = location; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000062 INLINE(T* operator ->() const) { return operator*(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
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>(); }
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000085 bool is_null() { return location_ == NULL; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086
87 // Closes the given scope, but lets this handle escape. See
88 // implementation in api.h.
ager@chromium.orgddb913d2009-01-27 10:01:48 +000089 inline Handle<T> EscapeFrom(v8::HandleScope* scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090
91 private:
92 T** location_;
93};
94
95
ager@chromium.orgddb913d2009-01-27 10:01:48 +000096// 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:
110 HandleScope() : previous_(current_) {
111 current_.extensions = 0;
112 }
113
114 ~HandleScope() {
115 Leave(&previous_);
116 }
117
118 // Counts the number of allocated handles.
119 static int NumberOfHandles();
120
121 // Creates a new handle with the given value.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000122 template <typename T>
123 static inline T** CreateHandle(T* value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000124 internal::Object** cur = current_.next;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000125 if (cur == current_.limit) cur = Extend();
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000126 // Update the current next field, set the value in the created
127 // handle, and return the result.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000128 ASSERT(cur < current_.limit);
129 current_.next = cur + 1;
130
131 T** result = reinterpret_cast<T**>(cur);
132 *result = value;
133 return result;
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000134 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000135
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000136 // Deallocates any extensions used by the current scope.
137 static void DeleteExtensions();
138
139 static Address current_extensions_address();
140 static Address current_next_address();
141 static Address current_limit_address();
142
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000143 private:
144 // Prevent heap allocation or illegal handle scopes.
145 HandleScope(const HandleScope&);
146 void operator=(const HandleScope&);
147 void* operator new(size_t size);
148 void operator delete(void* size_t);
149
150 static v8::ImplementationUtilities::HandleScopeData current_;
151 const v8::ImplementationUtilities::HandleScopeData previous_;
152
153 // Pushes a fresh handle scope to be used when allocating new handles.
154 static void Enter(
155 v8::ImplementationUtilities::HandleScopeData* previous) {
156 *previous = current_;
157 current_.extensions = 0;
158 }
159
160 // Re-establishes the previous scope state. Should be called only
161 // once, and only for the current scope.
162 static void Leave(
163 const v8::ImplementationUtilities::HandleScopeData* previous) {
164 if (current_.extensions > 0) {
165 DeleteExtensions();
166 }
167 current_ = *previous;
168#ifdef DEBUG
169 ZapRange(current_.next, current_.limit);
170#endif
171 }
172
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000173 // Extend the handle scope making room for more handles.
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000174 static internal::Object** Extend();
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000175
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000176 // Zaps the handles in the half-open interval [start, end).
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000177 static void ZapRange(internal::Object** start, internal::Object** end);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000178
179 friend class v8::HandleScope;
180 friend class v8::ImplementationUtilities;
181};
182
183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184// ----------------------------------------------------------------------------
185// Handle operations.
186// They might invoke garbage collection. The result is an handle to
187// an object of expected type, or the handle is an error if running out
ager@chromium.org32912102009-01-16 10:38:43 +0000188// of space or encountering an internal error.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189
ager@chromium.org32912102009-01-16 10:38:43 +0000190void NormalizeProperties(Handle<JSObject> object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000191 PropertyNormalizationMode mode,
192 int expected_additional_properties);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193void NormalizeElements(Handle<JSObject> object);
194void TransformToFastProperties(Handle<JSObject> object,
195 int unused_property_fields);
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000196
197// Flattens a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198void FlattenString(Handle<String> str);
199
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000200// Flattens a string and returns the underlying external or sequential
201// string.
202Handle<String> FlattenGetString(Handle<String> str);
203
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204Handle<Object> SetProperty(Handle<JSObject> object,
205 Handle<String> key,
206 Handle<Object> value,
207 PropertyAttributes attributes);
208
209Handle<Object> SetProperty(Handle<Object> object,
210 Handle<Object> key,
211 Handle<Object> value,
212 PropertyAttributes attributes);
213
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000214Handle<Object> ForceSetProperty(Handle<JSObject> object,
215 Handle<Object> key,
216 Handle<Object> value,
217 PropertyAttributes attributes);
218
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000219Handle<Object> SetNormalizedProperty(Handle<JSObject> object,
220 Handle<String> key,
221 Handle<Object> value,
222 PropertyDetails details);
223
ager@chromium.orge2902be2009-06-08 12:21:35 +0000224Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
225 Handle<Object> key);
226
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000227Handle<Object> IgnoreAttributesAndSetLocalProperty(Handle<JSObject> object,
228 Handle<String> key,
229 Handle<Object> value,
230 PropertyAttributes attributes);
231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
233 Handle<String> key,
234 Handle<Object> value,
235 PropertyAttributes attributes);
236
237Handle<Object> SetElement(Handle<JSObject> object,
238 uint32_t index,
239 Handle<Object> value);
240
241Handle<Object> GetProperty(Handle<JSObject> obj,
242 const char* name);
243
244Handle<Object> GetProperty(Handle<Object> obj,
245 Handle<Object> key);
246
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000247Handle<Object> GetElement(Handle<Object> obj,
248 uint32_t index);
249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
251 Handle<JSObject> holder,
252 Handle<String> name,
253 PropertyAttributes* attributes);
254
255Handle<Object> GetPrototype(Handle<Object> obj);
256
ager@chromium.org5c838252010-02-19 08:53:10 +0000257Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value);
258
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000259// Return the object's hidden properties object. If the object has no hidden
260// properties and create_if_needed is true, then a new hidden property object
261// will be allocated. Otherwise the Heap::undefined_value is returned.
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000262Handle<Object> GetHiddenProperties(Handle<JSObject> obj, bool create_if_needed);
263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264Handle<Object> DeleteElement(Handle<JSObject> obj, uint32_t index);
265Handle<Object> DeleteProperty(Handle<JSObject> obj, Handle<String> prop);
266
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000267Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);
268
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000269Handle<JSObject> Copy(Handle<JSObject> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000271Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info);
272
iposva@chromium.org245aa852009-02-10 00:49:54 +0000273Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
274 Handle<JSArray> array);
275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276// Get the JS object corresponding to the given script; create it
277// if none exists.
278Handle<JSValue> GetScriptWrapper(Handle<Script> script);
279
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000280// Script line number computations.
281void InitScriptLineEnds(Handle<Script> script);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000282// For string calculates an array of line end positions. If the string
283// does not end with a new line character, this character may optionally be
284// imagined.
285Handle<FixedArray> CalculateLineEnds(Handle<String> string,
286 bool with_imaginary_last_new_line);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000287int GetScriptLineNumber(Handle<Script> script, int code_position);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000288// The safe version does not make heap allocations but may work much slower.
289int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291// Computes the enumerable keys from interceptors. Used for debug mirrors and
292// by GetKeysInFixedArrayFor below.
293v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
294 Handle<JSObject> object);
295v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
296 Handle<JSObject> object);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000297
298enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS };
299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300// Computes the enumerable keys for a JSObject. Used for implementing
301// "for (n in object) { }".
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000302Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object,
303 KeyCollectionType type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304Handle<JSArray> GetKeysFor(Handle<JSObject> object);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000305Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
306 bool cache_result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307
308// Computes the union of keys and return the result.
309// Used for implementing "for (n in object) { }"
310Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
311 Handle<FixedArray> second);
312
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000313Handle<String> SubString(Handle<String> str,
314 int start,
315 int end,
316 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317
318
319// Sets the expected number of properties for the function's instances.
320void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
321
322// Sets the prototype property for a function instance.
323void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value);
324
325// Sets the expected number of properties based on estimate from compiler.
326void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
327 int estimate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328
329
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000330Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 Handle<JSFunction> constructor,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000332 Handle<JSGlobalProxy> global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333
334Handle<Object> SetPrototype(Handle<JSFunction> function,
335 Handle<Object> prototype);
336
337
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000338// Does lazy compilation of the given function. Returns true on success and
339// false if the compilation resulted in a stack overflow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION };
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000341
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000342bool EnsureCompiled(Handle<SharedFunctionInfo> shared,
343 ClearExceptionFlag flag);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000344
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000345bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
346 ClearExceptionFlag flag);
347
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000348bool CompileLazy(Handle<JSFunction> function, ClearExceptionFlag flag);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000349
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000350bool CompileLazyInLoop(Handle<JSFunction> function, ClearExceptionFlag flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352class NoHandleAllocation BASE_EMBEDDED {
353 public:
354#ifndef DEBUG
355 NoHandleAllocation() {}
356 ~NoHandleAllocation() {}
357#else
358 inline NoHandleAllocation();
359 inline ~NoHandleAllocation();
360 private:
361 int extensions_;
362#endif
363};
364
365
366// ----------------------------------------------------------------------------
367
368
369// Stack allocated wrapper call for optimizing adding multiple
370// properties to an object.
371class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED {
372 public:
373 OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000374 int expected_property_count,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 bool condition = true);
376 ~OptimizedObjectForAddingMultipleProperties();
377 private:
378 bool has_been_transformed_; // Tells whether the object has been transformed.
379 int unused_property_fields_; // Captures the unused number of field.
380 Handle<JSObject> object_; // The object being optimized.
381};
382
383
384} } // namespace v8::internal
385
386#endif // V8_HANDLES_H_