blob: c1400ed841c4f8d842ba4731ca0c27b6d7533658 [file] [log] [blame]
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001// Copyright 2011 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
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
ager@chromium.orgddb913d2009-01-27 10:01:48 +000032#include "apiutils.h"
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +000033#include "objects.h"
ager@chromium.orgddb913d2009-01-27 10:01:48 +000034
kasperl@chromium.org71affb52009-05-26 05:44:31 +000035namespace v8 {
36namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
38// ----------------------------------------------------------------------------
39// A Handle provides a reference to an object that survives relocation by
40// the garbage collector.
ager@chromium.orgddb913d2009-01-27 10:01:48 +000041// Handles are only valid within a HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042// When a handle is created for an object a cell is allocated in the heap.
43
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +000044template<typename T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045class Handle {
46 public:
ager@chromium.orgac091b72010-05-05 07:34:42 +000047 INLINE(explicit Handle(T** location)) { location_ = location; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048 INLINE(explicit Handle(T* obj));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 INLINE(Handle(T* obj, Isolate* isolate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
51 INLINE(Handle()) : location_(NULL) {}
52
53 // Constructor for handling automatic up casting.
54 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected.
55 template <class S> Handle(Handle<S> handle) {
56#ifdef DEBUG
57 T* a = NULL;
58 S* b = NULL;
59 a = b; // Fake assignment to enforce type checks.
60 USE(a);
61#endif
yangguo@chromium.org003650e2013-01-24 16:31:08 +000062 location_ = reinterpret_cast<T**>(handle.location_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063 }
64
ulan@chromium.orgdfe53072013-06-06 14:14:51 +000065 INLINE(T* operator->() const) { return operator*(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066
67 // Check if this handle refers to the exact same object as the other handle.
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000068 INLINE(bool is_identical_to(const Handle<T> other) const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069
70 // Provides the C++ dereference operator.
71 INLINE(T* operator*() const);
72
73 // Returns the address to where the raw pointer is stored.
yangguo@chromium.org003650e2013-01-24 16:31:08 +000074 INLINE(T** location() const);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
76 template <class S> static Handle<T> cast(Handle<S> that) {
ulan@chromium.org32d7dba2013-04-24 10:59:06 +000077 T::cast(*reinterpret_cast<T**>(that.location_));
78 return Handle<T>(reinterpret_cast<T**>(that.location_));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079 }
80
81 static Handle<T> null() { return Handle<T>(); }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 bool is_null() const { return location_ == NULL; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083
84 // Closes the given scope, but lets this handle escape. See
85 // implementation in api.h.
ager@chromium.orgddb913d2009-01-27 10:01:48 +000086 inline Handle<T> EscapeFrom(v8::HandleScope* scope);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087
ulan@chromium.org32d7dba2013-04-24 10:59:06 +000088#ifdef DEBUG
ulan@chromium.orgdfe53072013-06-06 14:14:51 +000089 enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK };
90
91 bool IsDereferenceAllowed(DereferenceCheckMode mode) const;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +000092#endif // DEBUG
93
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 private:
95 T** location_;
yangguo@chromium.org003650e2013-01-24 16:31:08 +000096
97 // Handles of different classes are allowed to access each other's location_.
98 template<class S> friend class Handle;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099};
100
101
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000102// Convenience wrapper.
103template<class T>
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000104inline Handle<T> handle(T* t, Isolate* isolate) {
105 return Handle<T>(t, isolate);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000106}
107
108
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000109// Convenience wrapper.
110template<class T>
111inline Handle<T> handle(T* t) {
112 return Handle<T>(t, t->GetIsolate());
113}
114
115
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000116class DeferredHandles;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000117class HandleScopeImplementer;
118
119
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000120// A stack-allocated class that governs a number of local handles.
121// After a handle scope has been created, all local handles will be
122// allocated within that handle scope until either the handle scope is
123// deleted or another handle scope is created. If there is already a
124// handle scope and a new one is created, all allocations will take
125// place in the new handle scope until it is deleted. After that,
126// new handles will again be allocated in the original handle scope.
127//
128// After the handle scope of a local handle has been deleted the
129// garbage collector will no longer track the object stored in the
130// handle and may deallocate it. The behavior of accessing a handle
131// for which the handle scope has been deleted is undefined.
132class HandleScope {
133 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000134 explicit inline HandleScope(Isolate* isolate);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000135
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000136 inline ~HandleScope();
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000137
138 // Counts the number of allocated handles.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000139 static int NumberOfHandles(Isolate* isolate);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000140
141 // Creates a new handle with the given value.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000142 template <typename T>
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000143 static inline T** CreateHandle(Isolate* isolate, T* value);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000144
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000145 // Deallocates any extensions used by the current scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 static void DeleteExtensions(Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000147
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000148 static Address current_next_address(Isolate* isolate);
149 static Address current_limit_address(Isolate* isolate);
150 static Address current_level_address(Isolate* isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000151
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000152 // Closes the HandleScope (invalidating all handles
153 // created in the scope of the HandleScope) and returns
154 // a Handle backed by the parent scope holding the
155 // value of the argument handle.
156 template <typename T>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000157 Handle<T> CloseAndEscape(Handle<T> handle_value);
158
159 Isolate* isolate() { return isolate_; }
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000160
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000161 private:
162 // Prevent heap allocation or illegal handle scopes.
163 HandleScope(const HandleScope&);
164 void operator=(const HandleScope&);
165 void* operator new(size_t size);
166 void operator delete(void* size_t);
167
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168 Isolate* isolate_;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000169 Object** prev_next_;
170 Object** prev_limit_;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000171
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000172 // Close the handle scope resetting limits to a previous state.
173 static inline void CloseScope(Isolate* isolate,
174 Object** prev_next,
175 Object** prev_limit);
176
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000177 // Extend the handle scope making room for more handles.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000178 static internal::Object** Extend(Isolate* isolate);
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000179
jkummerow@chromium.orgc3669762013-09-30 13:42:25 +0000180#ifdef ENABLE_HANDLE_ZAPPING
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000181 // Zaps the handles in the half-open interval [start, end).
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000182 static void ZapRange(Object** start, Object** end);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000183#endif
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000184
185 friend class v8::HandleScope;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000186 friend class v8::internal::DeferredHandles;
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000187 friend class v8::internal::HandleScopeImplementer;
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000188 friend class v8::internal::Isolate;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000189};
190
191
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000192class DeferredHandles;
193
194
195class DeferredHandleScope {
196 public:
197 explicit DeferredHandleScope(Isolate* isolate);
198 // The DeferredHandles object returned stores the Handles created
199 // since the creation of this DeferredHandleScope. The Handles are
200 // alive as long as the DeferredHandles object is alive.
201 DeferredHandles* Detach();
202 ~DeferredHandleScope();
203
204 private:
205 Object** prev_limit_;
206 Object** prev_next_;
207 HandleScopeImplementer* impl_;
208
209#ifdef DEBUG
210 bool handles_detached_;
211 int prev_level_;
212#endif
213
214 friend class HandleScopeImplementer;
215};
216
217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218// ----------------------------------------------------------------------------
219// Handle operations.
220// They might invoke garbage collection. The result is an handle to
221// an object of expected type, or the handle is an error if running out
ager@chromium.org32912102009-01-16 10:38:43 +0000222// of space or encountering an internal error.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000224// Flattens a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225void FlattenString(Handle<String> str);
226
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000227// Flattens a string and returns the underlying external or sequential
228// string.
229Handle<String> FlattenGetString(Handle<String> str);
230
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000231Handle<Object> SetProperty(Isolate* isolate,
232 Handle<Object> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 Handle<Object> key,
234 Handle<Object> value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000235 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000236 StrictModeFlag strict_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000238Handle<Object> ForceSetProperty(Handle<JSObject> object,
239 Handle<Object> key,
240 Handle<Object> value,
241 PropertyAttributes attributes);
242
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000243Handle<Object> DeleteProperty(Handle<JSObject> object, Handle<Object> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000244
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000245Handle<Object> ForceDeleteProperty(Handle<JSObject> object, Handle<Object> key);
246
247Handle<Object> HasProperty(Handle<JSReceiver> obj, Handle<Object> key);
248
249Handle<Object> GetProperty(Handle<JSReceiver> obj, const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000251Handle<Object> GetProperty(Isolate* isolate,
252 Handle<Object> obj,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 Handle<Object> key);
254
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000255Handle<Object> LookupSingleCharacterStringFromCode(Isolate* isolate,
256 uint32_t index);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000257
iposva@chromium.org245aa852009-02-10 00:49:54 +0000258Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>,
259 Handle<JSArray> array);
260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261// Get the JS object corresponding to the given script; create it
262// if none exists.
263Handle<JSValue> GetScriptWrapper(Handle<Script> script);
264
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000265// Script line number computations. Note that the line number is zero-based.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000266void InitScriptLineEnds(Handle<Script> script);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000267// For string calculates an array of line end positions. If the string
268// does not end with a new line character, this character may optionally be
269// imagined.
270Handle<FixedArray> CalculateLineEnds(Handle<String> string,
271 bool with_imaginary_last_new_line);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000272int GetScriptLineNumber(Handle<Script> script, int code_position);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000273// The safe version does not make heap allocations but may work much slower.
274int GetScriptLineNumberSafe(Handle<Script> script, int code_position);
danno@chromium.orgc612e022011-11-10 11:38:15 +0000275int GetScriptColumnNumber(Handle<Script> script, int code_position);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000276Handle<Object> GetScriptNameOrSourceURL(Handle<Script> script);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278// Computes the enumerable keys from interceptors. Used for debug mirrors and
279// by GetKeysInFixedArrayFor below.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000280v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 Handle<JSObject> object);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000282v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 Handle<JSObject> object);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000284
285enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS };
286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287// Computes the enumerable keys for a JSObject. Used for implementing
288// "for (n in object) { }".
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000289Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
290 KeyCollectionType type,
291 bool* threw);
292Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw);
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000293Handle<FixedArray> ReduceFixedArrayTo(Handle<FixedArray> array, int length);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000294Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
295 bool cache_result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296
297// Computes the union of keys and return the result.
298// Used for implementing "for (n in object) { }"
299Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
300 Handle<FixedArray> second);
301
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302// Sets the expected number of properties for the function's instances.
303void SetExpectedNofProperties(Handle<JSFunction> func, int nof);
304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305// Sets the expected number of properties based on estimate from compiler.
306void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
307 int estimate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308
309
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000310Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 Handle<JSFunction> constructor,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000312 Handle<JSGlobalProxy> global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000314Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table,
315 Handle<Object> key);
316
317Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table,
318 Handle<Object> key);
319
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000320Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000321 Handle<Object> key,
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000322 Handle<Object> value);
323
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +0000324void AddWeakObjectToCodeDependency(Heap* heap,
325 Handle<Object> object,
326 Handle<Code> code);
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000327
328// Seal off the current HandleScope so that new handles can only be created
329// if a new HandleScope is entered.
330class SealHandleScope BASE_EMBEDDED {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 public:
332#ifndef DEBUG
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000333 explicit SealHandleScope(Isolate* isolate) {}
334 ~SealHandleScope() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335#else
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000336 explicit inline SealHandleScope(Isolate* isolate);
337 inline ~SealHandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 private:
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000339 Isolate* isolate_;
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000340 Object** limit_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000341 int level_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342#endif
343};
344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345} } // namespace v8::internal
346
347#endif // V8_HANDLES_H_