blob: 6d75c77a177a97e97f3c2ed20b5d1005f7338e96 [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
31namespace v8 { namespace internal {
32
33// ----------------------------------------------------------------------------
34// A Handle provides a reference to an object that survives relocation by
35// the garbage collector.
36// Handles are only valid withing a HandleScope.
37// When a handle is created for an object a cell is allocated in the heap.
38
39template<class T>
40class Handle {
41 public:
42 INLINE(Handle(T** location)) { location_ = location; }
43 INLINE(explicit Handle(T* obj));
44
45 INLINE(Handle()) : location_(NULL) {}
46
47 // Constructor for handling automatic up casting.
48 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected.
49 template <class S> Handle(Handle<S> handle) {
50#ifdef DEBUG
51 T* a = NULL;
52 S* b = NULL;
53 a = b; // Fake assignment to enforce type checks.
54 USE(a);
55#endif
56 location_ = reinterpret_cast<T**>(handle.location());
57 }
58
59 INLINE(T* operator ->() const) { return operator*(); }
60
61 // Check if this handle refers to the exact same object as the other handle.
62 bool is_identical_to(const Handle<T> other) const {
63 return operator*() == *other;
64 }
65
66 // Provides the C++ dereference operator.
67 INLINE(T* operator*() const);
68
69 // Returns the address to where the raw pointer is stored.
70 T** location() const {
71 ASSERT(location_ == NULL ||
72 reinterpret_cast<Address>(*location_) != kZapValue);
73 return location_;
74 }
75
76 template <class S> static Handle<T> cast(Handle<S> that) {
77 T::cast(*that);
78 return Handle<T>(reinterpret_cast<T**>(that.location()));
79 }
80
81 static Handle<T> null() { return Handle<T>(); }
82 bool is_null() {return location_ == NULL; }
83
84 // Closes the given scope, but lets this handle escape. See
85 // implementation in api.h.
86 inline Handle<T> EscapeFrom(HandleScope* scope);
87
88 private:
89 T** location_;
90};
91
92
93// ----------------------------------------------------------------------------
94// Handle operations.
95// They might invoke garbage collection. The result is an handle to
96// an object of expected type, or the handle is an error if running out
97// of space or encounting an internal error.
98
99void NormalizeProperties(Handle<JSObject> object);
100void NormalizeElements(Handle<JSObject> object);
101void TransformToFastProperties(Handle<JSObject> object,
102 int unused_property_fields);
103void FlattenString(Handle<String> str);
104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105Handle<Object> SetProperty(Handle<JSObject> object,
106 Handle<String> key,
107 Handle<Object> value,
108 PropertyAttributes attributes);
109
110Handle<Object> SetProperty(Handle<Object> object,
111 Handle<Object> key,
112 Handle<Object> value,
113 PropertyAttributes attributes);
114
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000115Handle<Object> IgnoreAttributesAndSetLocalProperty(Handle<JSObject> object,
116 Handle<String> key,
117 Handle<Object> value,
118 PropertyAttributes attributes);
119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
121 Handle<String> key,
122 Handle<Object> value,
123 PropertyAttributes attributes);
124
125Handle<Object> SetElement(Handle<JSObject> object,
126 uint32_t index,
127 Handle<Object> value);
128
129Handle<Object> GetProperty(Handle<JSObject> obj,
130 const char* name);
131
132Handle<Object> GetProperty(Handle<Object> obj,
133 Handle<Object> key);
134
135Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
136 Handle<JSObject> holder,
137 Handle<String> name,
138 PropertyAttributes* attributes);
139
140Handle<Object> GetPrototype(Handle<Object> obj);
141
142Handle<Object> DeleteElement(Handle<JSObject> obj, uint32_t index);
143Handle<Object> DeleteProperty(Handle<JSObject> obj, Handle<String> prop);
144
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000145Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);
146
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000147Handle<JSObject> Copy(Handle<JSObject> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149// Get the JS object corresponding to the given script; create it
150// if none exists.
151Handle<JSValue> GetScriptWrapper(Handle<Script> script);
152
153// Computes the enumerable keys from interceptors. Used for debug mirrors and
154// by GetKeysInFixedArrayFor below.
155v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
156 Handle<JSObject> object);
157v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
158 Handle<JSObject> object);
159// Computes the enumerable keys for a JSObject. Used for implementing
160// "for (n in object) { }".
161Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object);
162Handle<JSArray> GetKeysFor(Handle<JSObject> object);
163Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object);
164
165// Computes the union of keys and return the result.
166// Used for implementing "for (n in object) { }"
167Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
168 Handle<FixedArray> second);
169
170Handle<String> SubString(Handle<String> str, int start, int end);
171
172
173// Sets the expected number of properties for the function's instances.
174void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
175
176// Sets the prototype property for a function instance.
177void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value);
178
179// Sets the expected number of properties based on estimate from compiler.
180void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
181 int estimate);
182void SetExpectedNofPropertiesFromEstimate(Handle<JSFunction> func,
183 int estimate);
184
185
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000186Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 Handle<JSFunction> constructor,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000188 Handle<JSGlobalProxy> global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189
190Handle<Object> SetPrototype(Handle<JSFunction> function,
191 Handle<Object> prototype);
192
193
194// Do lazy compilation of the given function. Returns true on success
195// and false if the compilation resulted in a stack overflow.
196enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION };
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000197
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000199 ClearExceptionFlag flag,
200 int loop_nesting);
201
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202bool CompileLazy(Handle<JSFunction> function, ClearExceptionFlag flag);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000203bool CompileLazyInLoop(Handle<JSFunction> function, ClearExceptionFlag flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
205// These deal with lazily loaded properties.
206void SetupLazy(Handle<JSFunction> fun,
207 int index,
208 Handle<Context> compile_context,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000209 Handle<Context> function_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210void LoadLazy(Handle<JSFunction> fun, bool* pending_exception);
211
212class NoHandleAllocation BASE_EMBEDDED {
213 public:
214#ifndef DEBUG
215 NoHandleAllocation() {}
216 ~NoHandleAllocation() {}
217#else
218 inline NoHandleAllocation();
219 inline ~NoHandleAllocation();
220 private:
221 int extensions_;
222#endif
223};
224
225
226// ----------------------------------------------------------------------------
227
228
229// Stack allocated wrapper call for optimizing adding multiple
230// properties to an object.
231class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED {
232 public:
233 OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object,
234 bool condition = true);
235 ~OptimizedObjectForAddingMultipleProperties();
236 private:
237 bool has_been_transformed_; // Tells whether the object has been transformed.
238 int unused_property_fields_; // Captures the unused number of field.
239 Handle<JSObject> object_; // The object being optimized.
240};
241
242
243} } // namespace v8::internal
244
245#endif // V8_HANDLES_H_