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
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.
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.
31 #include "allocation.h"
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.
46 INLINE(explicit Handle(T** location)) { location_ = location; }
47 INLINE(explicit Handle(T* obj));
48 INLINE(Handle(T* obj, Isolate* isolate));
50 INLINE(Handle()) : location_(NULL) {}
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) {
58 a = b; // Fake assignment to enforce type checks.
61 location_ = reinterpret_cast<T**>(handle.location());
64 INLINE(T* operator ->() const) { return operator*(); }
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;
71 // Provides the C++ dereference operator.
72 INLINE(T* operator*() const);
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);
81 template <class S> static Handle<T> cast(Handle<S> that) {
83 return Handle<T>(reinterpret_cast<T**>(that.location()));
86 static Handle<T> null() { return Handle<T>(); }
87 bool is_null() const { return location_ == NULL; }
89 // Closes the given scope, but lets this handle escape. See
90 // implementation in api.h.
91 inline Handle<T> EscapeFrom(v8::HandleScope* scope);
98 // A stack-allocated class that governs a number of local handles.
99 // After a handle scope has been created, all local handles will be
100 // allocated within that handle scope until either the handle scope is
101 // deleted or another handle scope is created. If there is already a
102 // handle scope and a new one is created, all allocations will take
103 // place in the new handle scope until it is deleted. After that,
104 // new handles will again be allocated in the original handle scope.
106 // After the handle scope of a local handle has been deleted the
107 // garbage collector will no longer track the object stored in the
108 // handle and may deallocate it. The behavior of accessing a handle
109 // for which the handle scope has been deleted is undefined.
112 inline HandleScope();
113 explicit inline HandleScope(Isolate* isolate);
115 inline ~HandleScope();
117 // Counts the number of allocated handles.
118 static int NumberOfHandles();
120 // Creates a new handle with the given value.
121 template <typename T>
122 static inline T** CreateHandle(T* value, Isolate* isolate);
124 // Deallocates any extensions used by the current scope.
125 static void DeleteExtensions(Isolate* isolate);
127 static Address current_next_address();
128 static Address current_limit_address();
129 static Address current_level_address();
131 // Closes the HandleScope (invalidating all handles
132 // created in the scope of the HandleScope) and returns
133 // a Handle backed by the parent scope holding the
134 // value of the argument handle.
135 template <typename T>
136 Handle<T> CloseAndEscape(Handle<T> handle_value);
138 Isolate* isolate() { return isolate_; }
141 // Prevent heap allocation or illegal handle scopes.
142 HandleScope(const HandleScope&);
143 void operator=(const HandleScope&);
144 void* operator new(size_t size);
145 void operator delete(void* size_t);
147 inline void CloseScope();
151 Object** prev_limit_;
153 // Extend the handle scope making room for more handles.
154 static internal::Object** Extend();
156 // Zaps the handles in the half-open interval [start, end).
157 static void ZapRange(internal::Object** start, internal::Object** end);
159 friend class v8::HandleScope;
160 friend class v8::ImplementationUtilities;
164 // ----------------------------------------------------------------------------
165 // Handle operations.
166 // They might invoke garbage collection. The result is an handle to
167 // an object of expected type, or the handle is an error if running out
168 // of space or encountering an internal error.
170 // Flattens a string.
171 void FlattenString(Handle<String> str);
173 // Flattens a string and returns the underlying external or sequential
175 Handle<String> FlattenGetString(Handle<String> str);
177 int Utf8Length(Handle<String> str);
179 Handle<Object> SetProperty(Handle<Object> object,
181 Handle<Object> value,
182 PropertyAttributes attributes,
183 StrictModeFlag strict_mode);
185 Handle<Object> ForceSetProperty(Handle<JSObject> object,
187 Handle<Object> value,
188 PropertyAttributes attributes);
190 Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
193 Handle<Object> GetProperty(Handle<JSReceiver> obj,
196 Handle<Object> GetProperty(Handle<Object> obj,
199 Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
200 Handle<JSObject> holder,
202 PropertyAttributes* attributes);
204 Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value);
206 Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);
208 Handle<JSObject> Copy(Handle<JSObject> obj);
210 Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info);
212 Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
213 Handle<JSArray> array);
215 // Get the JS object corresponding to the given script; create it
217 Handle<JSValue> GetScriptWrapper(Handle<Script> script);
219 // Script line number computations.
220 void InitScriptLineEnds(Handle<Script> script);
221 // For string calculates an array of line end positions. If the string
222 // does not end with a new line character, this character may optionally be
224 Handle<FixedArray> CalculateLineEnds(Handle<String> string,
225 bool with_imaginary_last_new_line);
226 int GetScriptLineNumber(Handle<Script> script, int code_position);
227 // The safe version does not make heap allocations but may work much slower.
228 int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
229 int GetScriptColumnNumber(Handle<Script> script, int code_position);
231 // Computes the enumerable keys from interceptors. Used for debug mirrors and
232 // by GetKeysInFixedArrayFor below.
233 v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
234 Handle<JSObject> object);
235 v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
236 Handle<JSObject> object);
238 enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS };
240 // Computes the enumerable keys for a JSObject. Used for implementing
241 // "for (n in object) { }".
242 Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
243 KeyCollectionType type,
245 Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw);
246 Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
249 // Computes the union of keys and return the result.
250 // Used for implementing "for (n in object) { }"
251 Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
252 Handle<FixedArray> second);
254 Handle<String> SubString(Handle<String> str,
257 PretenureFlag pretenure = NOT_TENURED);
259 // Sets the expected number of properties for the function's instances.
260 void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
262 // Sets the prototype property for a function instance.
263 void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value);
265 // Sets the expected number of properties based on estimate from compiler.
266 void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
270 Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
271 Handle<JSFunction> constructor,
272 Handle<JSGlobalProxy> global);
274 Handle<Object> SetPrototype(Handle<JSFunction> function,
275 Handle<Object> prototype);
277 Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table,
280 Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table,
283 Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
285 Handle<Object> value);
287 class NoHandleAllocation BASE_EMBEDDED {
290 NoHandleAllocation() {}
291 ~NoHandleAllocation() {}
293 inline NoHandleAllocation();
294 inline ~NoHandleAllocation();
300 } } // namespace v8::internal
302 #endif // V8_HANDLES_H_