blob: 960696b5fb81f705f2128b5586fef44ca0c8e7fb [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "apiutils.h"
33
34namespace v8 {
35namespace 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
Ben Murdoche0cee9b2011-05-25 10:26:03 +010043template<typename T>
Steve Blocka7e24c12009-10-30 11:49:00 +000044class Handle {
45 public:
Steve Block6ded16b2010-05-10 14:33:55 +010046 INLINE(explicit Handle(T** location)) { location_ = location; }
Steve Blocka7e24c12009-10-30 11:49:00 +000047 INLINE(explicit Handle(T* obj));
Steve Block44f0eee2011-05-26 01:26:41 +010048 INLINE(Handle(T* obj, Isolate* isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +000049
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>(); }
Steve Block44f0eee2011-05-26 01:26:41 +010087 bool is_null() const { return location_ == NULL; }
Steve Blocka7e24c12009-10-30 11:49:00 +000088
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// 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.
105//
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.
110class HandleScope {
111 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100112 inline HandleScope();
113 explicit inline HandleScope(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000114
Steve Block44f0eee2011-05-26 01:26:41 +0100115 inline ~HandleScope();
Steve Blocka7e24c12009-10-30 11:49:00 +0000116
117 // Counts the number of allocated handles.
118 static int NumberOfHandles();
119
120 // Creates a new handle with the given value.
121 template <typename T>
Steve Block44f0eee2011-05-26 01:26:41 +0100122 static inline T** CreateHandle(T* value, Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
Steve Blockd0582a62009-12-15 09:54:21 +0000124 // Deallocates any extensions used by the current scope.
Steve Block44f0eee2011-05-26 01:26:41 +0100125 static void DeleteExtensions(Isolate* isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000126
Steve Blockd0582a62009-12-15 09:54:21 +0000127 static Address current_next_address();
128 static Address current_limit_address();
John Reck59135872010-11-02 12:39:01 -0700129 static Address current_level_address();
Steve Blockd0582a62009-12-15 09:54:21 +0000130
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100131 // 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>
Steve Block44f0eee2011-05-26 01:26:41 +0100136 Handle<T> CloseAndEscape(Handle<T> handle_value);
137
138 Isolate* isolate() { return isolate_; }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100139
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 private:
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);
146
Steve Block44f0eee2011-05-26 01:26:41 +0100147 inline void CloseScope();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100148
Steve Block44f0eee2011-05-26 01:26:41 +0100149 Isolate* isolate_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100150 Object** prev_next_;
151 Object** prev_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000152
153 // Extend the handle scope making room for more handles.
154 static internal::Object** Extend();
155
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 // Zaps the handles in the half-open interval [start, end).
157 static void ZapRange(internal::Object** start, internal::Object** end);
158
159 friend class v8::HandleScope;
160 friend class v8::ImplementationUtilities;
161};
162
163
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.
169
Steve Block8defd9f2010-07-08 12:39:36 +0100170// Flattens a string.
Steve Blocka7e24c12009-10-30 11:49:00 +0000171void FlattenString(Handle<String> str);
172
Steve Block8defd9f2010-07-08 12:39:36 +0100173// Flattens a string and returns the underlying external or sequential
174// string.
175Handle<String> FlattenGetString(Handle<String> str);
176
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100177int Utf8Length(Handle<String> str);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100178
Steve Blocka7e24c12009-10-30 11:49:00 +0000179Handle<Object> SetProperty(Handle<Object> object,
180 Handle<Object> key,
181 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100182 PropertyAttributes attributes,
183 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185Handle<Object> ForceSetProperty(Handle<JSObject> object,
186 Handle<Object> key,
187 Handle<Object> value,
188 PropertyAttributes attributes);
189
190Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
191 Handle<Object> key);
192
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000193Handle<Object> GetProperty(Handle<JSReceiver> obj,
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 const char* name);
195
196Handle<Object> GetProperty(Handle<Object> obj,
197 Handle<Object> key);
198
199Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
200 Handle<JSObject> holder,
201 Handle<String> name,
202 PropertyAttributes* attributes);
203
Andrei Popescu402d9372010-02-26 13:31:12 +0000204Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value);
205
Steve Blocka7e24c12009-10-30 11:49:00 +0000206Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index);
207
208Handle<JSObject> Copy(Handle<JSObject> obj);
209
Leon Clarkef7060e22010-06-03 12:02:55 +0100210Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info);
211
Steve Blocka7e24c12009-10-30 11:49:00 +0000212Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
213 Handle<JSArray> array);
214
215// Get the JS object corresponding to the given script; create it
216// if none exists.
217Handle<JSValue> GetScriptWrapper(Handle<Script> script);
218
219// Script line number computations.
220void InitScriptLineEnds(Handle<Script> script);
Steve Block6ded16b2010-05-10 14:33:55 +0100221// 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
223// imagined.
224Handle<FixedArray> CalculateLineEnds(Handle<String> string,
225 bool with_imaginary_last_new_line);
Steve Blocka7e24c12009-10-30 11:49:00 +0000226int GetScriptLineNumber(Handle<Script> script, int code_position);
Steve Block6ded16b2010-05-10 14:33:55 +0100227// The safe version does not make heap allocations but may work much slower.
228int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100229int GetScriptColumnNumber(Handle<Script> script, int code_position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000230
231// Computes the enumerable keys from interceptors. Used for debug mirrors and
232// by GetKeysInFixedArrayFor below.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100233v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 Handle<JSObject> object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 Handle<JSObject> object);
237
238enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS };
239
240// Computes the enumerable keys for a JSObject. Used for implementing
241// "for (n in object) { }".
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100242Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
243 KeyCollectionType type,
244 bool* threw);
245Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw);
Steve Blockd0582a62009-12-15 09:54:21 +0000246Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
247 bool cache_result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000248
249// Computes the union of keys and return the result.
250// Used for implementing "for (n in object) { }"
251Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
252 Handle<FixedArray> second);
253
Steve Block6ded16b2010-05-10 14:33:55 +0100254Handle<String> SubString(Handle<String> str,
255 int start,
256 int end,
257 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +0000258
Steve Blocka7e24c12009-10-30 11:49:00 +0000259// Sets the expected number of properties for the function's instances.
260void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
261
262// Sets the prototype property for a function instance.
263void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value);
264
265// Sets the expected number of properties based on estimate from compiler.
266void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
267 int estimate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000268
269
270Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
271 Handle<JSFunction> constructor,
272 Handle<JSGlobalProxy> global);
273
274Handle<Object> SetPrototype(Handle<JSFunction> function,
275 Handle<Object> prototype);
276
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100277Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table,
278 Handle<Object> key);
279
280Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table,
281 Handle<Object> key);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000282
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000283Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100284 Handle<Object> key,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000285 Handle<Object> value);
286
Steve Blocka7e24c12009-10-30 11:49:00 +0000287class NoHandleAllocation BASE_EMBEDDED {
288 public:
289#ifndef DEBUG
290 NoHandleAllocation() {}
291 ~NoHandleAllocation() {}
292#else
293 inline NoHandleAllocation();
294 inline ~NoHandleAllocation();
295 private:
John Reck59135872010-11-02 12:39:01 -0700296 int level_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000297#endif
298};
299
Steve Blocka7e24c12009-10-30 11:49:00 +0000300} } // namespace v8::internal
301
302#endif // V8_HANDLES_H_