blob: 430f410a6eaf61c4a71e80d3227278ee5c594490 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 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
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000065// Setup for Linux shared library export. There is no need to distinguish
66// between building or using the V8 shared library, but we should not
67// export symbols when we are building a static library.
68#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000070#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000071#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000072#endif // defined(__GNUC__) && (__GNUC__ >= 4)
73
74#endif // _WIN32
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000077 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 */
79namespace v8 {
80
81class Context;
82class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000083class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084class Value;
85class Utils;
86class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000087class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088class Object;
89class Array;
90class Int32;
91class Uint32;
92class External;
93class Primitive;
94class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000095class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096class Integer;
97class Function;
98class Date;
99class ImplementationUtilities;
100class Signature;
101template <class T> class Handle;
102template <class T> class Local;
103template <class T> class Persistent;
104class FunctionTemplate;
105class ObjectTemplate;
106class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000107class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000108class StackTrace;
109class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000110class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000112namespace internal {
113
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000114class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000116class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117class HeapObject;
118class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000119}
120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000122// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
124
125/**
126 * A weak reference callback function.
127 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000128 * This callback should either explicitly invoke Dispose on |object| if
129 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
130 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 * \param object the weak global object to be reclaimed by the garbage collector
132 * \param parameter the value passed in when making the weak global object
133 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000134typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135 void* parameter);
136
137
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000138// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000140#define TYPE_CHECK(T, S) \
141 while (false) { \
142 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 }
144
145/**
146 * An object reference managed by the v8 garbage collector.
147 *
148 * All objects returned from v8 have to be tracked by the garbage
149 * collector so that it knows that the objects are still alive. Also,
150 * because the garbage collector may move objects, it is unsafe to
151 * point directly to an object. Instead, all objects are stored in
152 * handles which are known by the garbage collector and updated
153 * whenever an object moves. Handles should always be passed by value
154 * (except in cases like out-parameters) and they should never be
155 * allocated on the heap.
156 *
157 * There are two types of handles: local and persistent handles.
158 * Local handles are light-weight and transient and typically used in
159 * local operations. They are managed by HandleScopes. Persistent
160 * handles can be used when storing objects across several independent
161 * operations and have to be explicitly deallocated when they're no
162 * longer used.
163 *
164 * It is safe to extract the object stored in the handle by
165 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000166 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 * behind the scenes and the same rules apply to these values as to
168 * their handles.
169 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000170template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 /**
173 * Creates an empty handle.
174 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000175 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176
177 /**
178 * Creates a new handle for the specified value.
179 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000180 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181
182 /**
183 * Creates a handle for the contents of the specified handle. This
184 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000185 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000187 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 * between compatible handles, for instance assigning a
189 * Handle<String> to a variable declared as Handle<Value>, is legal
190 * because String is a subclass of Value.
191 */
192 template <class S> inline Handle(Handle<S> that)
193 : val_(reinterpret_cast<T*>(*that)) {
194 /**
195 * This check fails when trying to convert between incompatible
196 * handles. For example, converting from a Handle<String> to a
197 * Handle<Number>.
198 */
199 TYPE_CHECK(T, S);
200 }
201
202 /**
203 * Returns true if the handle is empty.
204 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000205 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 /**
208 * Sets the handle to be empty. IsEmpty() will then return true.
209 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000210 inline void Clear() { val_ = 0; }
211
212 inline T* operator->() const { return val_; }
213
214 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
216 /**
217 * Checks whether two handles are the same.
218 * Returns true if both are empty, or if the objects
219 * to which they refer are identical.
220 * The handles' references are not checked.
221 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000222 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000223 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
224 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 if (a == 0) return b == 0;
226 if (b == 0) return false;
227 return *a == *b;
228 }
229
230 /**
231 * Checks whether two handles are different.
232 * Returns true if only one of the handles is empty, or if
233 * the objects to which they refer are different.
234 * The handles' references are not checked.
235 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000236 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 return !operator==(that);
238 }
239
240 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000241#ifdef V8_ENABLE_CHECKS
242 // If we're going to perform the type check then we have to check
243 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000245#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 return Handle<T>(T::Cast(*that));
247 }
248
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000249 template <class S> inline Handle<S> As() {
250 return Handle<S>::Cast(*this);
251 }
252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 private:
254 T* val_;
255};
256
257
258/**
259 * A light-weight stack-allocated object handle. All operations
260 * that return objects from within v8 return them in local handles. They
261 * are created within HandleScopes, and all local handles allocated within a
262 * handle scope are destroyed when the handle scope is destroyed. Hence it
263 * is not necessary to explicitly deallocate local handles.
264 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000265template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000267 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 template <class S> inline Local(Local<S> that)
269 : Handle<T>(reinterpret_cast<T*>(*that)) {
270 /**
271 * This check fails when trying to convert between incompatible
272 * handles. For example, converting from a Handle<String> to a
273 * Handle<Number>.
274 */
275 TYPE_CHECK(T, S);
276 }
277 template <class S> inline Local(S* that) : Handle<T>(that) { }
278 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000279#ifdef V8_ENABLE_CHECKS
280 // If we're going to perform the type check then we have to check
281 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000283#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 return Local<T>(T::Cast(*that));
285 }
286
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000287 template <class S> inline Local<S> As() {
288 return Local<S>::Cast(*this);
289 }
290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 /** Create a local handle for the content of another handle.
292 * The referee is kept alive by the local handle even when
293 * the original handle is destroyed/disposed.
294 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000295 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296};
297
298
299/**
300 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000301 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 * allocated, a Persistent handle remains valid until it is explicitly
303 * disposed.
304 *
305 * A persistent handle contains a reference to a storage cell within
306 * the v8 engine which holds an object value and which is updated by
307 * the garbage collector whenever the object is moved. A new storage
308 * cell can be created using Persistent::New and existing handles can
309 * be disposed using Persistent::Dispose. Since persistent handles
310 * are passed by value you may have many persistent handle objects
311 * that point to the same storage cell. For instance, if you pass a
312 * persistent handle as an argument to a function you will not get two
313 * different storage cells but rather two references to the same
314 * storage cell.
315 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000316template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318 /**
319 * Creates an empty persistent handle that doesn't point to any
320 * storage cell.
321 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000322 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323
324 /**
325 * Creates a persistent handle for the same storage cell as the
326 * specified handle. This constructor allows you to pass persistent
327 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000328 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000330 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 * between compatible persistent handles, for instance assigning a
332 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000333 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 */
335 template <class S> inline Persistent(Persistent<S> that)
336 : Handle<T>(reinterpret_cast<T*>(*that)) {
337 /**
338 * This check fails when trying to convert between incompatible
339 * handles. For example, converting from a Handle<String> to a
340 * Handle<Number>.
341 */
342 TYPE_CHECK(T, S);
343 }
344
345 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
346
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000347 /**
348 * "Casts" a plain handle which is known to be a persistent handle
349 * to a persistent handle.
350 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 template <class S> explicit inline Persistent(Handle<S> that)
352 : Handle<T>(*that) { }
353
354 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000355#ifdef V8_ENABLE_CHECKS
356 // If we're going to perform the type check then we have to check
357 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000359#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 return Persistent<T>(T::Cast(*that));
361 }
362
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000363 template <class S> inline Persistent<S> As() {
364 return Persistent<S>::Cast(*this);
365 }
366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000368 * Creates a new persistent handle for an existing local or
369 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000371 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372
373 /**
374 * Releases the storage cell referenced by this persistent handle.
375 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000376 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 * cell remain and IsEmpty will still return false.
378 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000379 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380
381 /**
382 * Make the reference to this object weak. When only weak handles
383 * refer to the object, the garbage collector will perform a
384 * callback to the given V8::WeakReferenceCallback function, passing
385 * it the object reference and the given parameters.
386 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000387 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388
389 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000390 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391
392 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000393 * Marks the reference to this object independent. Garbage collector
394 * is free to ignore any object groups containing this object.
395 * Weak callback for an independent handle should not
396 * assume that it will be preceded by a global GC prologue callback
397 * or followed by a global GC epilogue callback.
398 */
399 inline void MarkIndependent();
400
401 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 *Checks if the handle holds the only reference to an object.
403 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000404 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
406 /**
407 * Returns true if the handle's reference is weak.
408 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000409 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000411 /**
412 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
413 * interface description in v8-profiler.h for details.
414 */
415 inline void SetWrapperClassId(uint16_t class_id);
416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 private:
418 friend class ImplementationUtilities;
419 friend class ObjectTemplate;
420};
421
422
v8.team.kasperl727e9952008-09-02 14:56:44 +0000423 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424 * A stack-allocated class that governs a number of local handles.
425 * After a handle scope has been created, all local handles will be
426 * allocated within that handle scope until either the handle scope is
427 * deleted or another handle scope is created. If there is already a
428 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000429 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 * new handles will again be allocated in the original handle scope.
431 *
432 * After the handle scope of a local handle has been deleted the
433 * garbage collector will no longer track the object stored in the
434 * handle and may deallocate it. The behavior of accessing a handle
435 * for which the handle scope has been deleted is undefined.
436 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000437class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000439 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000441 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442
443 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 * Closes the handle scope and returns the value as a handle in the
445 * previous scope, which is the new current scope after the call.
446 */
447 template <class T> Local<T> Close(Handle<T> value);
448
449 /**
450 * Counts the number of allocated handles.
451 */
452 static int NumberOfHandles();
453
454 /**
455 * Creates a new handle with the given value.
456 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000457 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 // Faster version, uses HeapObject to obtain the current Isolate.
459 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460
461 private:
462 // Make it impossible to create heap-allocated or illegal handle
463 // scopes by disallowing certain operations.
464 HandleScope(const HandleScope&);
465 void operator=(const HandleScope&);
466 void* operator new(size_t size);
467 void operator delete(void*, size_t);
468
ager@chromium.org3811b432009-10-28 14:53:37 +0000469 // This Data class is accessible internally as HandleScopeData through a
470 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000471 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000473 internal::Object** next;
474 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000475 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000478 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 }
480 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000481
lrn@chromium.org303ada72010-10-27 09:33:13 +0000482 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000485 internal::Object** prev_next_;
486 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000488 // Allow for the active closing of HandleScopes which allows to pass a handle
489 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000491 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 friend class ImplementationUtilities;
494};
495
496
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000497// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498
499
500/**
501 * The superclass of values and API object templates.
502 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000503class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 private:
505 Data();
506};
507
508
509/**
510 * Pre-compilation data that can be associated with a script. This
511 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000512 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000513 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000515class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 public:
517 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000518
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000519 /**
520 * Pre-compiles the specified script (context-independent).
521 *
522 * \param input Pointer to UTF-8 script source code.
523 * \param length Length of UTF-8 script source code.
524 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000527 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000528 * Pre-compiles the specified script (context-independent).
529 *
530 * NOTE: Pre-compilation using this method cannot happen on another thread
531 * without using Lockers.
532 *
533 * \param source Script source code.
534 */
535 static ScriptData* PreCompile(Handle<String> source);
536
537 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000538 * Load previous pre-compilation data.
539 *
540 * \param data Pointer to data returned by a call to Data() of a previous
541 * ScriptData. Ownership is not transferred.
542 * \param length Length of data.
543 */
544 static ScriptData* New(const char* data, int length);
545
546 /**
547 * Returns the length of Data().
548 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000550
551 /**
552 * Returns a serialized representation of this ScriptData that can later be
553 * passed to New(). NOTE: Serialized data is platform-dependent.
554 */
555 virtual const char* Data() = 0;
556
557 /**
558 * Returns true if the source code could not be parsed.
559 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000560 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561};
562
563
564/**
565 * The origin, within a file, of a script.
566 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000567class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000569 inline ScriptOrigin(
570 Handle<Value> resource_name,
571 Handle<Integer> resource_line_offset = Handle<Integer>(),
572 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 : resource_name_(resource_name),
574 resource_line_offset_(resource_line_offset),
575 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000576 inline Handle<Value> ResourceName() const;
577 inline Handle<Integer> ResourceLineOffset() const;
578 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000580 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 Handle<Integer> resource_line_offset_;
582 Handle<Integer> resource_column_offset_;
583};
584
585
586/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000587 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000589class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000592 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000593 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000594 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000595 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 * when New() returns
597 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
598 * using pre_data speeds compilation if it's done multiple times.
599 * Owned by caller, no references are kept when New() returns.
600 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000601 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 * available to compile event handlers.
603 * \return Compiled script object (context independent; when run it
604 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000606 static Local<Script> New(Handle<String> source,
607 ScriptOrigin* origin = NULL,
608 ScriptData* pre_data = NULL,
609 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
mads.s.agercbaa0602008-08-14 13:41:48 +0000611 /**
612 * Compiles the specified script using the specified file name
613 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000614 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000615 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000616 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000617 * as the script's origin.
618 * \return Compiled script object (context independent; when run it
619 * will use the currently entered context).
620 */
621 static Local<Script> New(Handle<String> source,
622 Handle<Value> file_name);
623
624 /**
625 * Compiles the specified script (bound to current context).
626 *
627 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000628 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000629 * when Compile() returns
630 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
631 * using pre_data speeds compilation if it's done multiple times.
632 * Owned by caller, no references are kept when Compile() returns.
633 * \param script_data Arbitrary data associated with script. Using
634 * this has same effect as calling SetData(), but makes data available
635 * earlier (i.e. to compile event handlers).
636 * \return Compiled script object, bound to the context that was active
637 * when this function was called. When run it will always use this
638 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000639 */
640 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000641 ScriptOrigin* origin = NULL,
642 ScriptData* pre_data = NULL,
643 Handle<String> script_data = Handle<String>());
644
645 /**
646 * Compiles the specified script using the specified file name
647 * object (typically a string) as the script's origin.
648 *
649 * \param source Script source code.
650 * \param file_name File name to use as script's origin
651 * \param script_data Arbitrary data associated with script. Using
652 * this has same effect as calling SetData(), but makes data available
653 * earlier (i.e. to compile event handlers).
654 * \return Compiled script object, bound to the context that was active
655 * when this function was called. When run it will always use this
656 * context.
657 */
658 static Local<Script> Compile(Handle<String> source,
659 Handle<Value> file_name,
660 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000661
v8.team.kasperl727e9952008-09-02 14:56:44 +0000662 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000663 * Runs the script returning the resulting value. If the script is
664 * context independent (created using ::New) it will be run in the
665 * currently entered context. If it is context specific (created
666 * using ::Compile) it will be run in the context in which it was
667 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000668 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000670
671 /**
672 * Returns the script id value.
673 */
674 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000675
676 /**
677 * Associate an additional data object with the script. This is mainly used
678 * with the debugger as this data object is only available through the
679 * debugger API.
680 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000681 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682};
683
684
685/**
686 * An error message.
687 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000688class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000690 Local<String> Get() const;
691 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000693 /**
694 * Returns the resource name for the script from where the function causing
695 * the error originates.
696 */
ager@chromium.org32912102009-01-16 10:38:43 +0000697 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000699 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000700 * Returns the resource data for the script from where the function causing
701 * the error originates.
702 */
703 Handle<Value> GetScriptData() const;
704
705 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000706 * Exception stack trace. By default stack traces are not captured for
707 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
708 * to change this option.
709 */
710 Handle<StackTrace> GetStackTrace() const;
711
712 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000713 * Returns the number, 1-based, of the line where the error occurred.
714 */
ager@chromium.org32912102009-01-16 10:38:43 +0000715 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000717 /**
718 * Returns the index within the script of the first character where
719 * the error occurred.
720 */
ager@chromium.org32912102009-01-16 10:38:43 +0000721 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000722
723 /**
724 * Returns the index within the script of the last character where
725 * the error occurred.
726 */
ager@chromium.org32912102009-01-16 10:38:43 +0000727 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000728
729 /**
730 * Returns the index within the line of the first character where
731 * the error occurred.
732 */
ager@chromium.org32912102009-01-16 10:38:43 +0000733 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000734
735 /**
736 * Returns the index within the line of the last character where
737 * the error occurred.
738 */
ager@chromium.org32912102009-01-16 10:38:43 +0000739 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 // TODO(1245381): Print to a string instead of on a FILE.
742 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000743
744 static const int kNoLineNumberInfo = 0;
745 static const int kNoColumnInfo = 0;
746};
747
748
749/**
750 * Representation of a JavaScript stack trace. The information collected is a
751 * snapshot of the execution stack and the information remains valid after
752 * execution continues.
753 */
754class V8EXPORT StackTrace {
755 public:
756 /**
757 * Flags that determine what information is placed captured for each
758 * StackFrame when grabbing the current stack trace.
759 */
760 enum StackTraceOptions {
761 kLineNumber = 1,
762 kColumnOffset = 1 << 1 | kLineNumber,
763 kScriptName = 1 << 2,
764 kFunctionName = 1 << 3,
765 kIsEval = 1 << 4,
766 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000767 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000768 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000769 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000770 };
771
772 /**
773 * Returns a StackFrame at a particular index.
774 */
775 Local<StackFrame> GetFrame(uint32_t index) const;
776
777 /**
778 * Returns the number of StackFrames.
779 */
780 int GetFrameCount() const;
781
782 /**
783 * Returns StackTrace as a v8::Array that contains StackFrame objects.
784 */
785 Local<Array> AsArray();
786
787 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000788 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000789 *
790 * \param frame_limit The maximum number of stack frames we want to capture.
791 * \param options Enumerates the set of things we will capture for each
792 * StackFrame.
793 */
794 static Local<StackTrace> CurrentStackTrace(
795 int frame_limit,
796 StackTraceOptions options = kOverview);
797};
798
799
800/**
801 * A single JavaScript stack frame.
802 */
803class V8EXPORT StackFrame {
804 public:
805 /**
806 * Returns the number, 1-based, of the line for the associate function call.
807 * This method will return Message::kNoLineNumberInfo if it is unable to
808 * retrieve the line number, or if kLineNumber was not passed as an option
809 * when capturing the StackTrace.
810 */
811 int GetLineNumber() const;
812
813 /**
814 * Returns the 1-based column offset on the line for the associated function
815 * call.
816 * This method will return Message::kNoColumnInfo if it is unable to retrieve
817 * the column number, or if kColumnOffset was not passed as an option when
818 * capturing the StackTrace.
819 */
820 int GetColumn() const;
821
822 /**
823 * Returns the name of the resource that contains the script for the
824 * function for this StackFrame.
825 */
826 Local<String> GetScriptName() const;
827
828 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000829 * Returns the name of the resource that contains the script for the
830 * function for this StackFrame or sourceURL value if the script name
831 * is undefined and its source ends with //@ sourceURL=... string.
832 */
833 Local<String> GetScriptNameOrSourceURL() const;
834
835 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000836 * Returns the name of the function associated with this stack frame.
837 */
838 Local<String> GetFunctionName() const;
839
840 /**
841 * Returns whether or not the associated function is compiled via a call to
842 * eval().
843 */
844 bool IsEval() const;
845
846 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000847 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000848 * constructor via "new".
849 */
850 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851};
852
853
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000854// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
856
857/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000858 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000860class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862 /**
863 * Returns true if this value is the undefined value. See ECMA-262
864 * 4.3.10.
865 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000866 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 /**
869 * Returns true if this value is the null value. See ECMA-262
870 * 4.3.11.
871 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000872 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
874 /**
875 * Returns true if this value is true.
876 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000877 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879 /**
880 * Returns true if this value is false.
881 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000882 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 /**
885 * Returns true if this value is an instance of the String type.
886 * See ECMA-262 8.4.
887 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000888 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
890 /**
891 * Returns true if this value is a function.
892 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000893 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894
895 /**
896 * Returns true if this value is an array.
897 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000898 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
v8.team.kasperl727e9952008-09-02 14:56:44 +0000900 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 * Returns true if this value is an object.
902 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000903 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
v8.team.kasperl727e9952008-09-02 14:56:44 +0000905 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 * Returns true if this value is boolean.
907 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000908 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000909
v8.team.kasperl727e9952008-09-02 14:56:44 +0000910 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 * Returns true if this value is a number.
912 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000913 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914
v8.team.kasperl727e9952008-09-02 14:56:44 +0000915 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 * Returns true if this value is external.
917 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000918 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919
v8.team.kasperl727e9952008-09-02 14:56:44 +0000920 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 * Returns true if this value is a 32-bit signed integer.
922 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000923 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000925 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000926 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000927 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000928 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000929
930 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000931 * Returns true if this value is a Date.
932 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000933 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000934
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000935 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000936 * Returns true if this value is a Boolean object.
937 */
938 V8EXPORT bool IsBooleanObject() const;
939
940 /**
941 * Returns true if this value is a Number object.
942 */
943 V8EXPORT bool IsNumberObject() const;
944
945 /**
946 * Returns true if this value is a String object.
947 */
948 V8EXPORT bool IsStringObject() const;
949
950 /**
951 * Returns true if this value is a NativeError.
952 */
953 V8EXPORT bool IsNativeError() const;
954
955 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000956 * Returns true if this value is a RegExp.
957 */
958 V8EXPORT bool IsRegExp() const;
959
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000960 V8EXPORT Local<Boolean> ToBoolean() const;
961 V8EXPORT Local<Number> ToNumber() const;
962 V8EXPORT Local<String> ToString() const;
963 V8EXPORT Local<String> ToDetailString() const;
964 V8EXPORT Local<Object> ToObject() const;
965 V8EXPORT Local<Integer> ToInteger() const;
966 V8EXPORT Local<Uint32> ToUint32() const;
967 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968
969 /**
970 * Attempts to convert a string to an array index.
971 * Returns an empty handle if the conversion fails.
972 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000973 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000975 V8EXPORT bool BooleanValue() const;
976 V8EXPORT double NumberValue() const;
977 V8EXPORT int64_t IntegerValue() const;
978 V8EXPORT uint32_t Uint32Value() const;
979 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
981 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000982 V8EXPORT bool Equals(Handle<Value> that) const;
983 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000984
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000985 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000986 inline bool QuickIsUndefined() const;
987 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000988 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000989 V8EXPORT bool FullIsUndefined() const;
990 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000991 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992};
993
994
995/**
996 * The superclass of primitive values. See ECMA-262 4.3.2.
997 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000998class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
1000
1001/**
1002 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1003 * or false value.
1004 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001005class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001007 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 static inline Handle<Boolean> New(bool value);
1009};
1010
1011
1012/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001013 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001015class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001017 /**
1018 * Returns the number of characters in this string.
1019 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001020 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021
v8.team.kasperl727e9952008-09-02 14:56:44 +00001022 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001023 * Returns the number of bytes in the UTF-8 encoded
1024 * representation of this string.
1025 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001026 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001027
1028 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001029 * A fast conservative check for non-ASCII characters. May
1030 * return true even for ASCII strings, but if it returns
1031 * false you can be sure that all characters are in the range
1032 * 0-127.
1033 */
1034 V8EXPORT bool MayContainNonAscii() const;
1035
1036 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001037 * Write the contents of the string to an external buffer.
1038 * If no arguments are given, expects the buffer to be large
1039 * enough to hold the entire string and NULL terminator. Copies
1040 * the contents of the string and the NULL terminator into the
1041 * buffer.
1042 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001043 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1044 * before the end of the buffer.
1045 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001046 * Copies up to length characters into the output buffer.
1047 * Only null-terminates if there is enough space in the buffer.
1048 *
1049 * \param buffer The buffer into which the string will be copied.
1050 * \param start The starting position within the string at which
1051 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001052 * \param length The number of characters to copy from the string. For
1053 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001054 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001055 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001056 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001057 * \return The number of characters copied to the buffer excluding the null
1058 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001059 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001060 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001061 enum WriteOptions {
1062 NO_OPTIONS = 0,
1063 HINT_MANY_WRITES_EXPECTED = 1,
1064 NO_NULL_TERMINATION = 2
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001065 };
1066
lrn@chromium.org34e60782011-09-15 07:25:40 +00001067 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001068 V8EXPORT int Write(uint16_t* buffer,
1069 int start = 0,
1070 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001071 int options = NO_OPTIONS) const;
1072 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001073 V8EXPORT int WriteAscii(char* buffer,
1074 int start = 0,
1075 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001076 int options = NO_OPTIONS) const;
1077 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001078 V8EXPORT int WriteUtf8(char* buffer,
1079 int length = -1,
1080 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001081 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082
v8.team.kasperl727e9952008-09-02 14:56:44 +00001083 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001084 * A zero length string.
1085 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001086 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001087
1088 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001089 * Returns true if the string is external
1090 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001091 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092
v8.team.kasperl727e9952008-09-02 14:56:44 +00001093 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001094 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001095 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001096 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001097
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001098 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001099 public:
1100 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001101
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001102 protected:
1103 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001104
1105 /**
1106 * Internally V8 will call this Dispose method when the external string
1107 * resource is no longer needed. The default implementation will use the
1108 * delete operator. This method can be overridden in subclasses to
1109 * control how allocated external string resources are disposed.
1110 */
1111 virtual void Dispose() { delete this; }
1112
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001113 private:
1114 // Disallow copying and assigning.
1115 ExternalStringResourceBase(const ExternalStringResourceBase&);
1116 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001117
1118 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001119 };
1120
v8.team.kasperl727e9952008-09-02 14:56:44 +00001121 /**
1122 * An ExternalStringResource is a wrapper around a two-byte string
1123 * buffer that resides outside V8's heap. Implement an
1124 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001125 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001126 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001127 class V8EXPORT ExternalStringResource
1128 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 public:
1130 /**
1131 * Override the destructor to manage the life cycle of the underlying
1132 * buffer.
1133 */
1134 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001135
1136 /**
1137 * The string data from the underlying buffer.
1138 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001140
1141 /**
1142 * The length of the string. That is, the number of two-byte characters.
1143 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 protected:
1147 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 };
1149
1150 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001151 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001152 * string buffer that resides outside V8's heap. Implement an
1153 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001154 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001155 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001156 * UTF-8, which would require special treatment internally in the
1157 * engine and, in the case of UTF-8, do not allow efficient indexing.
1158 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001159 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001161 class V8EXPORT ExternalAsciiStringResource
1162 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 public:
1164 /**
1165 * Override the destructor to manage the life cycle of the underlying
1166 * buffer.
1167 */
1168 virtual ~ExternalAsciiStringResource() {}
1169 /** The string data from the underlying buffer.*/
1170 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001171 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 virtual size_t length() const = 0;
1173 protected:
1174 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 };
1176
1177 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001178 * Get the ExternalStringResource for an external string. Returns
1179 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001181 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182
1183 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001184 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001185 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001187 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1188 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001190 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191
1192 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001193 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001195 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196 * be careful to supply the length parameter.
1197 * If it is not given, the function calls
1198 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001199 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001201 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202
lrn@chromium.org34e60782011-09-15 07:25:40 +00001203 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001204 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205
1206 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001207 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
v8.team.kasperl727e9952008-09-02 14:56:44 +00001209 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001210 * Creates a new string by concatenating the left and the right strings
1211 * passed in as parameters.
1212 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001213 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001214 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001215
1216 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001217 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001218 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001219 * resource will be disposed by calling its Dispose method. The caller of
1220 * this function should not otherwise delete or modify the resource. Neither
1221 * should the underlying buffer be deallocated or modified except through the
1222 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001223 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001224 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001225
ager@chromium.org6f10e412009-02-13 10:11:16 +00001226 /**
1227 * Associate an external string resource with this string by transforming it
1228 * in place so that existing references to this string in the JavaScript heap
1229 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001230 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001231 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001232 * The string is not modified if the operation fails. See NewExternal for
1233 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001234 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001235 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236
v8.team.kasperl727e9952008-09-02 14:56:44 +00001237 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001238 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001239 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001240 * resource will be disposed by calling its Dispose method. The caller of
1241 * this function should not otherwise delete or modify the resource. Neither
1242 * should the underlying buffer be deallocated or modified except through the
1243 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001244 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001245 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001246
ager@chromium.org6f10e412009-02-13 10:11:16 +00001247 /**
1248 * Associate an external string resource with this string by transforming it
1249 * in place so that existing references to this string in the JavaScript heap
1250 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001251 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001252 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001253 * The string is not modified if the operation fails. See NewExternal for
1254 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001255 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001256 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001258 /**
1259 * Returns true if this string can be made external.
1260 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001261 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001262
lrn@chromium.org34e60782011-09-15 07:25:40 +00001263 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001264 V8EXPORT static Local<String> NewUndetectable(const char* data,
1265 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266
lrn@chromium.org34e60782011-09-15 07:25:40 +00001267 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001268 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1269 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270
1271 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001272 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001273 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001274 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001275 * then the length() method returns 0 and the * operator returns
1276 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001277 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001278 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001279 public:
1280 explicit Utf8Value(Handle<v8::Value> obj);
1281 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001282 char* operator*() { return str_; }
1283 const char* operator*() const { return str_; }
1284 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001285 private:
1286 char* str_;
1287 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001288
1289 // Disallow copying and assigning.
1290 Utf8Value(const Utf8Value&);
1291 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001292 };
1293
1294 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001295 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001297 * If conversion to a string fails (eg. due to an exception in the toString()
1298 * method of the object) then the length() method returns 0 and the * operator
1299 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001301 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 public:
1303 explicit AsciiValue(Handle<v8::Value> obj);
1304 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001305 char* operator*() { return str_; }
1306 const char* operator*() const { return str_; }
1307 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 private:
1309 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001310 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001311
1312 // Disallow copying and assigning.
1313 AsciiValue(const AsciiValue&);
1314 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 };
1316
1317 /**
1318 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001319 * If conversion to a string fails (eg. due to an exception in the toString()
1320 * method of the object) then the length() method returns 0 and the * operator
1321 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001323 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 public:
1325 explicit Value(Handle<v8::Value> obj);
1326 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001327 uint16_t* operator*() { return str_; }
1328 const uint16_t* operator*() const { return str_; }
1329 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 private:
1331 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001332 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001333
1334 // Disallow copying and assigning.
1335 Value(const Value&);
1336 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001338
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001339 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001340 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1341 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342};
1343
1344
1345/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001346 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001348class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001350 V8EXPORT double Value() const;
1351 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001352 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001354 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001355 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356};
1357
1358
1359/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001360 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001362class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001364 V8EXPORT static Local<Integer> New(int32_t value);
1365 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1366 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001367 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001369 V8EXPORT Integer();
1370 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371};
1372
1373
1374/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001375 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001377class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001379 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001381 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382};
1383
1384
1385/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001386 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001388class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001390 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001392 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393};
1394
1395
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396enum PropertyAttribute {
1397 None = 0,
1398 ReadOnly = 1 << 0,
1399 DontEnum = 1 << 1,
1400 DontDelete = 1 << 2
1401};
1402
ager@chromium.org3811b432009-10-28 14:53:37 +00001403enum ExternalArrayType {
1404 kExternalByteArray = 1,
1405 kExternalUnsignedByteArray,
1406 kExternalShortArray,
1407 kExternalUnsignedShortArray,
1408 kExternalIntArray,
1409 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001410 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001411 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001412 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001413};
1414
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001416 * Accessor[Getter|Setter] are used as callback functions when
1417 * setting|getting a particular property. See Object and ObjectTemplate's
1418 * method SetAccessor.
1419 */
1420typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1421 const AccessorInfo& info);
1422
1423
1424typedef void (*AccessorSetter)(Local<String> property,
1425 Local<Value> value,
1426 const AccessorInfo& info);
1427
1428
1429/**
1430 * Access control specifications.
1431 *
1432 * Some accessors should be accessible across contexts. These
1433 * accessors have an explicit access control parameter which specifies
1434 * the kind of cross-context access that should be allowed.
1435 *
1436 * Additionally, for security, accessors can prohibit overwriting by
1437 * accessors defined in JavaScript. For objects that have such
1438 * accessors either locally or in their prototype chain it is not
1439 * possible to overwrite the accessor by using __defineGetter__ or
1440 * __defineSetter__ from JavaScript code.
1441 */
1442enum AccessControl {
1443 DEFAULT = 0,
1444 ALL_CAN_READ = 1,
1445 ALL_CAN_WRITE = 1 << 1,
1446 PROHIBITS_OVERWRITING = 1 << 2
1447};
1448
1449
1450/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001451 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001453class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001455 V8EXPORT bool Set(Handle<Value> key,
1456 Handle<Value> value,
1457 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001458
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001459 V8EXPORT bool Set(uint32_t index,
1460 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001461
ager@chromium.orge2902be2009-06-08 12:21:35 +00001462 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001463 // overriding accessors or read-only properties.
1464 //
1465 // Note that if the object has an interceptor the property will be set
1466 // locally, but since the interceptor takes precedence the local property
1467 // will only be returned if the interceptor doesn't return a value.
1468 //
1469 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001470 V8EXPORT bool ForceSet(Handle<Value> key,
1471 Handle<Value> value,
1472 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001473
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001474 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001476 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001477
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001478 /**
1479 * Gets the property attributes of a property which can be None or
1480 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1481 * None when the property doesn't exist.
1482 */
1483 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1484
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 // TODO(1245389): Replace the type-specific versions of these
1486 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001487 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001488
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001489 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001490
1491 // Delete a property on this object bypassing interceptors and
1492 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001493 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001494
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001495 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001496
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001499 V8EXPORT bool SetAccessor(Handle<String> name,
1500 AccessorGetter getter,
1501 AccessorSetter setter = 0,
1502 Handle<Value> data = Handle<Value>(),
1503 AccessControl settings = DEFAULT,
1504 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001507 * Returns an array containing the names of the enumerable properties
1508 * of this object, including properties from prototype objects. The
1509 * array returned by this method contains the same values as would
1510 * be enumerated by a for-in statement over this object.
1511 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001512 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001513
1514 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001515 * This function has the same functionality as GetPropertyNames but
1516 * the returned array doesn't contain the names of properties from
1517 * prototype objects.
1518 */
1519 V8EXPORT Local<Array> GetOwnPropertyNames();
1520
1521 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522 * Get the prototype object. This does not skip objects marked to
1523 * be skipped by __proto__ and it does not consult the security
1524 * handler.
1525 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001526 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527
1528 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001529 * Set the prototype object. This does not skip objects marked to
1530 * be skipped by __proto__ and it does not consult the security
1531 * handler.
1532 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001533 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001534
1535 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001536 * Finds an instance of the given function template in the prototype
1537 * chain.
1538 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1540 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001541
1542 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 * Call builtin Object.prototype.toString on this object.
1544 * This is different from Value::ToString() that may call
1545 * user-defined toString function. This one does not.
1546 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001547 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001549 /**
1550 * Returns the name of the function invoked as a constructor for this object.
1551 */
1552 V8EXPORT Local<String> GetConstructorName();
1553
kasper.lund212ac232008-07-16 07:07:30 +00001554 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001555 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001556 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001557 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001558 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001559 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001561 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001562 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001563
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001564 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001565 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001568 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001569 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1570 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1571 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572
1573 /**
1574 * If result.IsEmpty() no real property was located in the prototype chain.
1575 * This means interceptors in the prototype chain are not called.
1576 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001577 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1578 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001579
1580 /**
1581 * If result.IsEmpty() no real property was located on the object or
1582 * in the prototype chain.
1583 * This means interceptors in the prototype chain are not called.
1584 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001585 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586
1587 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001588 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589
kasper.lund212ac232008-07-16 07:07:30 +00001590 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001591 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001593 /**
1594 * Turns on access check on the object if the object is an instance of
1595 * a template that has access check callbacks. If an object has no
1596 * access check info, the object cannot be accessed by anyone.
1597 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001598 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001599
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001600 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001601 * Returns the identity hash for this object. The current implementation
1602 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001603 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001604 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001605 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001606 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001607 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001608
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001609 /**
1610 * Access hidden properties on JavaScript objects. These properties are
1611 * hidden from the executing JavaScript and only accessible through the V8
1612 * C++ API. Hidden properties introduced by V8 internally (for example the
1613 * identity hash) are prefixed with "v8::".
1614 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001615 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1616 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1617 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001618
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001619 /**
1620 * Returns true if this is an instance of an api function (one
1621 * created from a function created from a function template) and has
1622 * been modified since it was created. Note that this method is
1623 * conservative and may return true for objects that haven't actually
1624 * been modified.
1625 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001626 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001627
1628 /**
1629 * Clone this object with a fast but shallow copy. Values will point
1630 * to the same values as the original object.
1631 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001632 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001634 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001635 * Returns the context in which the object was created.
1636 */
1637 V8EXPORT Local<Context> CreationContext();
1638
1639 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001640 * Set the backing store of the indexed properties to be managed by the
1641 * embedding layer. Access to the indexed properties will follow the rules
1642 * spelled out in CanvasPixelArray.
1643 * Note: The embedding program still owns the data and needs to ensure that
1644 * the backing store is preserved while V8 has a reference.
1645 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001646 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001647 V8EXPORT bool HasIndexedPropertiesInPixelData();
1648 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1649 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001650
ager@chromium.org3811b432009-10-28 14:53:37 +00001651 /**
1652 * Set the backing store of the indexed properties to be managed by the
1653 * embedding layer. Access to the indexed properties will follow the rules
1654 * spelled out for the CanvasArray subtypes in the WebGL specification.
1655 * Note: The embedding program still owns the data and needs to ensure that
1656 * the backing store is preserved while V8 has a reference.
1657 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001658 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1659 void* data,
1660 ExternalArrayType array_type,
1661 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001662 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1663 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1664 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1665 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001666
lrn@chromium.org1c092762011-05-09 09:42:16 +00001667 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001668 * Checks whether a callback is set by the
1669 * ObjectTemplate::SetCallAsFunctionHandler method.
1670 * When an Object is callable this method returns true.
1671 */
1672 V8EXPORT bool IsCallable();
1673
1674 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001675 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001676 * ObjectTemplate::SetCallAsFunctionHandler method.
1677 */
1678 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1679 int argc,
1680 Handle<Value> argv[]);
1681
1682 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001683 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001684 * ObjectTemplate::SetCallAsFunctionHandler method.
1685 * Note: This method behaves like the Function::NewInstance method.
1686 */
1687 V8EXPORT Local<Value> CallAsConstructor(int argc,
1688 Handle<Value> argv[]);
1689
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001690 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001691 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001694 V8EXPORT Object();
1695 V8EXPORT static void CheckCast(Value* obj);
1696 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1697 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001698
1699 /**
1700 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001701 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001702 */
1703 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704};
1705
1706
1707/**
1708 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1709 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001710class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001712 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713
ager@chromium.org3e875802009-06-29 08:26:34 +00001714 /**
1715 * Clones an element at index |index|. Returns an empty
1716 * handle if cloning fails (for any reason).
1717 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001718 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001719
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001720 /**
1721 * Creates a JavaScript array with the given length. If the length
1722 * is negative the returned array will have length 0.
1723 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001724 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001725
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001726 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001728 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001729 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730};
1731
1732
1733/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001734 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001736class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001738 V8EXPORT Local<Object> NewInstance() const;
1739 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1740 V8EXPORT Local<Value> Call(Handle<Object> recv,
1741 int argc,
1742 Handle<Value> argv[]);
1743 V8EXPORT void SetName(Handle<String> name);
1744 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001745
1746 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001747 * Name inferred from variable or property assignment of this function.
1748 * Used to facilitate debugging and profiling of JavaScript code written
1749 * in an OO style, where many functions are anonymous but are assigned
1750 * to object properties.
1751 */
1752 V8EXPORT Handle<Value> GetInferredName() const;
1753
1754 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001755 * Returns zero based line number of function body and
1756 * kLineOffsetNotFound if no information available.
1757 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001758 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001759 /**
1760 * Returns zero based column number of function body and
1761 * kLineOffsetNotFound if no information available.
1762 */
1763 V8EXPORT int GetScriptColumnNumber() const;
1764 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001765 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001766 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001767 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001770 V8EXPORT Function();
1771 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772};
1773
1774
1775/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001776 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1777 */
1778class Date : public Object {
1779 public:
1780 V8EXPORT static Local<Value> New(double time);
1781
1782 /**
1783 * A specialization of Value::NumberValue that is more efficient
1784 * because we know the structure of this object.
1785 */
1786 V8EXPORT double NumberValue() const;
1787
1788 static inline Date* Cast(v8::Value* obj);
1789
1790 /**
1791 * Notification that the embedder has changed the time zone,
1792 * daylight savings time, or other date / time configuration
1793 * parameters. V8 keeps a cache of various values used for
1794 * date / time computation. This notification will reset
1795 * those cached values for the current context so that date /
1796 * time configuration changes would be reflected in the Date
1797 * object.
1798 *
1799 * This API should not be called more than needed as it will
1800 * negatively impact the performance of date operations.
1801 */
1802 V8EXPORT static void DateTimeConfigurationChangeNotification();
1803
1804 private:
1805 V8EXPORT static void CheckCast(v8::Value* obj);
1806};
1807
1808
1809/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001810 * A Number object (ECMA-262, 4.3.21).
1811 */
1812class NumberObject : public Object {
1813 public:
1814 V8EXPORT static Local<Value> New(double value);
1815
1816 /**
1817 * Returns the Number held by the object.
1818 */
1819 V8EXPORT double NumberValue() const;
1820
1821 static inline NumberObject* Cast(v8::Value* obj);
1822
1823 private:
1824 V8EXPORT static void CheckCast(v8::Value* obj);
1825};
1826
1827
1828/**
1829 * A Boolean object (ECMA-262, 4.3.15).
1830 */
1831class BooleanObject : public Object {
1832 public:
1833 V8EXPORT static Local<Value> New(bool value);
1834
1835 /**
1836 * Returns the Boolean held by the object.
1837 */
1838 V8EXPORT bool BooleanValue() const;
1839
1840 static inline BooleanObject* Cast(v8::Value* obj);
1841
1842 private:
1843 V8EXPORT static void CheckCast(v8::Value* obj);
1844};
1845
1846
1847/**
1848 * A String object (ECMA-262, 4.3.18).
1849 */
1850class StringObject : public Object {
1851 public:
1852 V8EXPORT static Local<Value> New(Handle<String> value);
1853
1854 /**
1855 * Returns the String held by the object.
1856 */
1857 V8EXPORT Local<String> StringValue() const;
1858
1859 static inline StringObject* Cast(v8::Value* obj);
1860
1861 private:
1862 V8EXPORT static void CheckCast(v8::Value* obj);
1863};
1864
1865
1866/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001867 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1868 */
1869class RegExp : public Object {
1870 public:
1871 /**
1872 * Regular expression flag bits. They can be or'ed to enable a set
1873 * of flags.
1874 */
1875 enum Flags {
1876 kNone = 0,
1877 kGlobal = 1,
1878 kIgnoreCase = 2,
1879 kMultiline = 4
1880 };
1881
1882 /**
1883 * Creates a regular expression from the given pattern string and
1884 * the flags bit field. May throw a JavaScript exception as
1885 * described in ECMA-262, 15.10.4.1.
1886 *
1887 * For example,
1888 * RegExp::New(v8::String::New("foo"),
1889 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1890 * is equivalent to evaluating "/foo/gm".
1891 */
1892 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1893 Flags flags);
1894
1895 /**
1896 * Returns the value of the source property: a string representing
1897 * the regular expression.
1898 */
1899 V8EXPORT Local<String> GetSource() const;
1900
1901 /**
1902 * Returns the flags bit field.
1903 */
1904 V8EXPORT Flags GetFlags() const;
1905
1906 static inline RegExp* Cast(v8::Value* obj);
1907
1908 private:
1909 V8EXPORT static void CheckCast(v8::Value* obj);
1910};
1911
1912
1913/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001914 * A JavaScript value that wraps a C++ void*. This type of value is
1915 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001917 *
1918 * The Wrap function V8 will return the most optimal Value object wrapping the
1919 * C++ void*. The type of the value is not guaranteed to be an External object
1920 * and no assumptions about its type should be made. To access the wrapped
1921 * value Unwrap should be used, all other operations on that object will lead
1922 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001924class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001926 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001927 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001928
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001929 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001930 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001931 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001933 V8EXPORT External();
1934 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001935 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001936 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937};
1938
1939
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001940// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941
1942
1943/**
1944 * The superclass of object and function templates.
1945 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001946class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 public:
1948 /** Adds a property to each instance created by this template.*/
1949 void Set(Handle<String> name, Handle<Data> value,
1950 PropertyAttribute attributes = None);
1951 inline void Set(const char* name, Handle<Data> value);
1952 private:
1953 Template();
1954
1955 friend class ObjectTemplate;
1956 friend class FunctionTemplate;
1957};
1958
1959
1960/**
1961 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001962 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 * including the receiver, the number and values of arguments, and
1964 * the holder of the function.
1965 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001966class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 public:
1968 inline int Length() const;
1969 inline Local<Value> operator[](int i) const;
1970 inline Local<Function> Callee() const;
1971 inline Local<Object> This() const;
1972 inline Local<Object> Holder() const;
1973 inline bool IsConstructCall() const;
1974 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001975 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00001976
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001978 static const int kIsolateIndex = 0;
1979 static const int kDataIndex = -1;
1980 static const int kCalleeIndex = -2;
1981 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001982
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001984 inline Arguments(internal::Object** implicit_args,
1985 internal::Object** values,
1986 int length,
1987 bool is_construct_call);
1988 internal::Object** implicit_args_;
1989 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001991 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992};
1993
1994
1995/**
1996 * The information passed to an accessor callback about the context
1997 * of the property access.
1998 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001999class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002001 inline AccessorInfo(internal::Object** args)
2002 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002003 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 inline Local<Value> Data() const;
2005 inline Local<Object> This() const;
2006 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002007
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002009 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010};
2011
2012
2013typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 * NamedProperty[Getter|Setter] are used as interceptors on object.
2017 * See ObjectTemplate::SetNamedPropertyHandler.
2018 */
2019typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2020 const AccessorInfo& info);
2021
2022
2023/**
2024 * Returns the value if the setter intercepts the request.
2025 * Otherwise, returns an empty handle.
2026 */
2027typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2028 Local<Value> value,
2029 const AccessorInfo& info);
2030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031/**
2032 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002033 * The result is an integer encoding property attributes (like v8::None,
2034 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002036typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2037 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038
2039
2040/**
2041 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002042 * The return value is true if the property could be deleted and false
2043 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 */
2045typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2046 const AccessorInfo& info);
2047
2048/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002049 * Returns an array containing the names of the properties the named
2050 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 */
2052typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2053
v8.team.kasperl727e9952008-09-02 14:56:44 +00002054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002056 * Returns the value of the property if the getter intercepts the
2057 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 */
2059typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2060 const AccessorInfo& info);
2061
2062
2063/**
2064 * Returns the value if the setter intercepts the request.
2065 * Otherwise, returns an empty handle.
2066 */
2067typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2068 Local<Value> value,
2069 const AccessorInfo& info);
2070
2071
2072/**
2073 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002074 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002076typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2077 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078
2079/**
2080 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002081 * The return value is true if the property could be deleted and false
2082 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 */
2084typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2085 const AccessorInfo& info);
2086
v8.team.kasperl727e9952008-09-02 14:56:44 +00002087/**
2088 * Returns an array containing the indices of the properties the
2089 * indexed property getter intercepts.
2090 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2092
2093
2094/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002095 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 */
2097enum AccessType {
2098 ACCESS_GET,
2099 ACCESS_SET,
2100 ACCESS_HAS,
2101 ACCESS_DELETE,
2102 ACCESS_KEYS
2103};
2104
v8.team.kasperl727e9952008-09-02 14:56:44 +00002105
2106/**
2107 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002108 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002109 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002110typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 Local<Value> key,
2112 AccessType type,
2113 Local<Value> data);
2114
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115
2116/**
2117 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002118 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002119 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002120typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 uint32_t index,
2122 AccessType type,
2123 Local<Value> data);
2124
2125
2126/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002127 * A FunctionTemplate is used to create functions at runtime. There
2128 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002129 * context. The lifetime of the created function is equal to the
2130 * lifetime of the context. So in case the embedder needs to create
2131 * temporary functions that can be collected using Scripts is
2132 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 *
2134 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002135 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002137 * A FunctionTemplate has a corresponding instance template which is
2138 * used to create object instances when the function is used as a
2139 * constructor. Properties added to the instance template are added to
2140 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141 *
2142 * A FunctionTemplate can have a prototype template. The prototype template
2143 * is used to create the prototype object of the function.
2144 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002145 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002147 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2149 * t->Set("func_property", v8::Number::New(1));
2150 *
2151 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2152 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2153 * proto_t->Set("proto_const", v8::Number::New(2));
2154 *
2155 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2156 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2157 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2158 * instance_t->Set("instance_property", Number::New(3));
2159 *
2160 * v8::Local<v8::Function> function = t->GetFunction();
2161 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002162 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 *
2164 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002165 * and "instance" for the instance object created above. The function
2166 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002168 * \code
2169 * func_property in function == true;
2170 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * function.prototype.proto_method() invokes 'InvokeCallback'
2173 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 * instance instanceof function == true;
2176 * instance.instance_accessor calls 'InstanceAccessorCallback'
2177 * instance.instance_property == 3;
2178 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002180 * A FunctionTemplate can inherit from another one by calling the
2181 * FunctionTemplate::Inherit method. The following graph illustrates
2182 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * \code
2185 * FunctionTemplate Parent -> Parent() . prototype -> { }
2186 * ^ ^
2187 * | Inherit(Parent) | .__proto__
2188 * | |
2189 * FunctionTemplate Child -> Child() . prototype -> { }
2190 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002192 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2193 * object of the Child() function has __proto__ pointing to the
2194 * Parent() function's prototype object. An instance of the Child
2195 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197 * Let Parent be the FunctionTemplate initialized in the previous
2198 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002200 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 * Local<FunctionTemplate> parent = t;
2202 * Local<FunctionTemplate> child = FunctionTemplate::New();
2203 * child->Inherit(parent);
2204 *
2205 * Local<Function> child_function = child->GetFunction();
2206 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002207 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002209 * The Child function and Child instance will have the following
2210 * properties:
2211 *
2212 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002214 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002215 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002216 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002218class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 public:
2220 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002221 static Local<FunctionTemplate> New(
2222 InvocationCallback callback = 0,
2223 Handle<Value> data = Handle<Value>(),
2224 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 /** Returns the unique function instance in the current execution context.*/
2226 Local<Function> GetFunction();
2227
v8.team.kasperl727e9952008-09-02 14:56:44 +00002228 /**
2229 * Set the call-handler callback for a FunctionTemplate. This
2230 * callback is called whenever the function created from this
2231 * FunctionTemplate is called.
2232 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 void SetCallHandler(InvocationCallback callback,
2234 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235
v8.team.kasperl727e9952008-09-02 14:56:44 +00002236 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 Local<ObjectTemplate> InstanceTemplate();
2238
2239 /** Causes the function template to inherit from a parent function template.*/
2240 void Inherit(Handle<FunctionTemplate> parent);
2241
2242 /**
2243 * A PrototypeTemplate is the template used to create the prototype object
2244 * of the function created by this template.
2245 */
2246 Local<ObjectTemplate> PrototypeTemplate();
2247
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248
2249 /**
2250 * Set the class name of the FunctionTemplate. This is used for
2251 * printing objects created with the function created from the
2252 * FunctionTemplate as its constructor.
2253 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 void SetClassName(Handle<String> name);
2255
2256 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002257 * Determines whether the __proto__ accessor ignores instances of
2258 * the function template. If instances of the function template are
2259 * ignored, __proto__ skips all instances and instead returns the
2260 * next object in the prototype chain.
2261 *
2262 * Call with a value of true to make the __proto__ accessor ignore
2263 * instances of the function template. Call with a value of false
2264 * to make the __proto__ accessor not ignore instances of the
2265 * function template. By default, instances of a function template
2266 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 */
2268 void SetHiddenPrototype(bool value);
2269
2270 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002271 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2272 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002273 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002274 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002275
2276 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002277 * Returns true if the given object is an instance of this function
2278 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 */
2280 bool HasInstance(Handle<Value> object);
2281
2282 private:
2283 FunctionTemplate();
2284 void AddInstancePropertyAccessor(Handle<String> name,
2285 AccessorGetter getter,
2286 AccessorSetter setter,
2287 Handle<Value> data,
2288 AccessControl settings,
2289 PropertyAttribute attributes);
2290 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2291 NamedPropertySetter setter,
2292 NamedPropertyQuery query,
2293 NamedPropertyDeleter remover,
2294 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002295 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2297 IndexedPropertySetter setter,
2298 IndexedPropertyQuery query,
2299 IndexedPropertyDeleter remover,
2300 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002301 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2303 Handle<Value> data);
2304
2305 friend class Context;
2306 friend class ObjectTemplate;
2307};
2308
2309
2310/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002311 * An ObjectTemplate is used to create objects at runtime.
2312 *
2313 * Properties added to an ObjectTemplate are added to each object
2314 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002316class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002318 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 /** Creates a new instance of this template.*/
2322 Local<Object> NewInstance();
2323
2324 /**
2325 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002326 *
2327 * Whenever the property with the given name is accessed on objects
2328 * created from this ObjectTemplate the getter and setter callbacks
2329 * are called instead of getting and setting the property directly
2330 * on the JavaScript object.
2331 *
2332 * \param name The name of the property for which an accessor is added.
2333 * \param getter The callback to invoke when getting the property.
2334 * \param setter The callback to invoke when setting the property.
2335 * \param data A piece of data that will be passed to the getter and setter
2336 * callbacks whenever they are invoked.
2337 * \param settings Access control settings for the accessor. This is a bit
2338 * field consisting of one of more of
2339 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2340 * The default is to not allow cross-context access.
2341 * ALL_CAN_READ means that all cross-context reads are allowed.
2342 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2343 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2344 * cross-context access.
2345 * \param attribute The attributes of the property for which an accessor
2346 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 */
2348 void SetAccessor(Handle<String> name,
2349 AccessorGetter getter,
2350 AccessorSetter setter = 0,
2351 Handle<Value> data = Handle<Value>(),
2352 AccessControl settings = DEFAULT,
2353 PropertyAttribute attribute = None);
2354
2355 /**
2356 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002357 *
2358 * Whenever a named property is accessed on objects created from
2359 * this object template, the provided callback is invoked instead of
2360 * accessing the property directly on the JavaScript object.
2361 *
2362 * \param getter The callback to invoke when getting a property.
2363 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002364 * \param query The callback to invoke to check if a property is present,
2365 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002366 * \param deleter The callback to invoke when deleting a property.
2367 * \param enumerator The callback to invoke to enumerate all the named
2368 * properties of an object.
2369 * \param data A piece of data that will be passed to the callbacks
2370 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371 */
2372 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2373 NamedPropertySetter setter = 0,
2374 NamedPropertyQuery query = 0,
2375 NamedPropertyDeleter deleter = 0,
2376 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002377 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378
2379 /**
2380 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002381 *
2382 * Whenever an indexed property is accessed on objects created from
2383 * this object template, the provided callback is invoked instead of
2384 * accessing the property directly on the JavaScript object.
2385 *
2386 * \param getter The callback to invoke when getting a property.
2387 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002388 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002389 * \param deleter The callback to invoke when deleting a property.
2390 * \param enumerator The callback to invoke to enumerate all the indexed
2391 * properties of an object.
2392 * \param data A piece of data that will be passed to the callbacks
2393 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394 */
2395 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2396 IndexedPropertySetter setter = 0,
2397 IndexedPropertyQuery query = 0,
2398 IndexedPropertyDeleter deleter = 0,
2399 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002400 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002401
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 /**
2403 * Sets the callback to be used when calling instances created from
2404 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002405 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 * function.
2407 */
2408 void SetCallAsFunctionHandler(InvocationCallback callback,
2409 Handle<Value> data = Handle<Value>());
2410
v8.team.kasperl727e9952008-09-02 14:56:44 +00002411 /**
2412 * Mark object instances of the template as undetectable.
2413 *
2414 * In many ways, undetectable objects behave as though they are not
2415 * there. They behave like 'undefined' in conditionals and when
2416 * printed. However, properties can be accessed and called as on
2417 * normal objects.
2418 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 void MarkAsUndetectable();
2420
v8.team.kasperl727e9952008-09-02 14:56:44 +00002421 /**
2422 * Sets access check callbacks on the object template.
2423 *
2424 * When accessing properties on instances of this object template,
2425 * the access check callback will be called to determine whether or
2426 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002427 * The last parameter specifies whether access checks are turned
2428 * on by default on instances. If access checks are off by default,
2429 * they can be turned on on individual instances by calling
2430 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002431 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2433 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002434 Handle<Value> data = Handle<Value>(),
2435 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436
kasper.lund212ac232008-07-16 07:07:30 +00002437 /**
2438 * Gets the number of internal fields for objects generated from
2439 * this template.
2440 */
2441 int InternalFieldCount();
2442
2443 /**
2444 * Sets the number of internal fields for objects generated from
2445 * this template.
2446 */
2447 void SetInternalFieldCount(int value);
2448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 private:
2450 ObjectTemplate();
2451 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2452 friend class FunctionTemplate;
2453};
2454
2455
2456/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002457 * A Signature specifies which receivers and arguments a function can
2458 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002460class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 public:
2462 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2463 Handle<FunctionTemplate>(),
2464 int argc = 0,
2465 Handle<FunctionTemplate> argv[] = 0);
2466 private:
2467 Signature();
2468};
2469
2470
2471/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002472 * A utility for determining the type of objects based on the template
2473 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002475class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 public:
2477 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2478 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2479 int match(Handle<Value> value);
2480 private:
2481 TypeSwitch();
2482};
2483
2484
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002485// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002487class V8EXPORT ExternalAsciiStringResourceImpl
2488 : public String::ExternalAsciiStringResource {
2489 public:
2490 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2491 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2492 : data_(data), length_(length) {}
2493 const char* data() const { return data_; }
2494 size_t length() const { return length_; }
2495
2496 private:
2497 const char* data_;
2498 size_t length_;
2499};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500
2501/**
2502 * Ignore
2503 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002504class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002506 // Note that the strings passed into this constructor must live as long
2507 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 Extension(const char* name,
2509 const char* source = 0,
2510 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002511 const char** deps = 0,
2512 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002513 virtual ~Extension() { }
2514 virtual v8::Handle<v8::FunctionTemplate>
2515 GetNativeFunction(v8::Handle<v8::String> name) {
2516 return v8::Handle<v8::FunctionTemplate>();
2517 }
2518
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002519 const char* name() const { return name_; }
2520 size_t source_length() const { return source_length_; }
2521 const String::ExternalAsciiStringResource* source() const {
2522 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523 int dependency_count() { return dep_count_; }
2524 const char** dependencies() { return deps_; }
2525 void set_auto_enable(bool value) { auto_enable_ = value; }
2526 bool auto_enable() { return auto_enable_; }
2527
2528 private:
2529 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002530 size_t source_length_; // expected to initialize before source_
2531 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532 int dep_count_;
2533 const char** deps_;
2534 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002535
2536 // Disallow copying and assigning.
2537 Extension(const Extension&);
2538 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539};
2540
2541
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002542void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543
2544
2545/**
2546 * Ignore
2547 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002548class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549 public:
2550 inline DeclareExtension(Extension* extension) {
2551 RegisterExtension(extension);
2552 }
2553};
2554
2555
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002556// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557
2558
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002559Handle<Primitive> V8EXPORT Undefined();
2560Handle<Primitive> V8EXPORT Null();
2561Handle<Boolean> V8EXPORT True();
2562Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563
2564
2565/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002566 * A set of constraints that specifies the limits of the runtime's memory use.
2567 * You must set the heap size before initializing the VM - the size cannot be
2568 * adjusted after the VM is initialized.
2569 *
2570 * If you are using threads then you should hold the V8::Locker lock while
2571 * setting the stack limit and you must set a non-default stack limit separately
2572 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002574class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 public:
2576 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002577 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002578 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002579 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002581 int max_executable_size() { return max_executable_size_; }
2582 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002583 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002584 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002585 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2586 private:
2587 int max_young_space_size_;
2588 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002589 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 uint32_t* stack_limit_;
2591};
2592
2593
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002594bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595
2596
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002597// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598
2599
2600typedef void (*FatalErrorCallback)(const char* location, const char* message);
2601
2602
2603typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2604
2605
2606/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002607 * Schedules an exception to be thrown when returning to JavaScript. When an
2608 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002610 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002612Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613
2614/**
2615 * Create new error objects by calling the corresponding error object
2616 * constructor with the message.
2617 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002618class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 public:
2620 static Local<Value> RangeError(Handle<String> message);
2621 static Local<Value> ReferenceError(Handle<String> message);
2622 static Local<Value> SyntaxError(Handle<String> message);
2623 static Local<Value> TypeError(Handle<String> message);
2624 static Local<Value> Error(Handle<String> message);
2625};
2626
2627
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002628// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002630typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002632typedef void* (*CreateHistogramCallback)(const char* name,
2633 int min,
2634 int max,
2635 size_t buckets);
2636
2637typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2638
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002639// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002640 enum ObjectSpace {
2641 kObjectSpaceNewSpace = 1 << 0,
2642 kObjectSpaceOldPointerSpace = 1 << 1,
2643 kObjectSpaceOldDataSpace = 1 << 2,
2644 kObjectSpaceCodeSpace = 1 << 3,
2645 kObjectSpaceMapSpace = 1 << 4,
2646 kObjectSpaceLoSpace = 1 << 5,
2647
2648 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2649 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2650 kObjectSpaceLoSpace
2651 };
2652
2653 enum AllocationAction {
2654 kAllocationActionAllocate = 1 << 0,
2655 kAllocationActionFree = 1 << 1,
2656 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2657 };
2658
2659typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2660 AllocationAction action,
2661 int size);
2662
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002663// --- Leave Script Callback ---
2664typedef void (*CallCompletedCallback)();
2665
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002666// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2668 AccessType type,
2669 Local<Value> data);
2670
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002671// --- AllowCodeGenerationFromStrings callbacks ---
2672
2673/**
2674 * Callback to check if code generation from strings is allowed. See
2675 * Context::AllowCodeGenerationFromStrings.
2676 */
2677typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2678
2679// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680
2681/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002682 * Applications can register callback functions which will be called
2683 * before and after a garbage collection. Allocations are not
2684 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002685 * objects (set or delete properties for example) since it is possible
2686 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002688enum GCType {
2689 kGCTypeScavenge = 1 << 0,
2690 kGCTypeMarkSweepCompact = 1 << 1,
2691 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2692};
2693
2694enum GCCallbackFlags {
2695 kNoGCCallbackFlags = 0,
2696 kGCCallbackFlagCompacted = 1 << 0
2697};
2698
2699typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2700typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702typedef void (*GCCallback)();
2703
2704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002706 * Collection of V8 heap information.
2707 *
2708 * Instances of this class can be passed to v8::V8::HeapStatistics to
2709 * get heap statistics from V8.
2710 */
2711class V8EXPORT HeapStatistics {
2712 public:
2713 HeapStatistics();
2714 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002715 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002716 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002717 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002718
2719 private:
2720 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002721 void set_total_heap_size_executable(size_t size) {
2722 total_heap_size_executable_ = size;
2723 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002724 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002725 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002726
2727 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002728 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002729 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002730 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002731
2732 friend class V8;
2733};
2734
2735
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002736class RetainedObjectInfo;
2737
ager@chromium.org3811b432009-10-28 14:53:37 +00002738/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002739 * Isolate represents an isolated instance of the V8 engine. V8
2740 * isolates have completely separate states. Objects from one isolate
2741 * must not be used in other isolates. When V8 is initialized a
2742 * default isolate is implicitly created and entered. The embedder
2743 * can create additional isolates and use them in parallel in multiple
2744 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002745 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002746 */
2747class V8EXPORT Isolate {
2748 public:
2749 /**
2750 * Stack-allocated class which sets the isolate for all operations
2751 * executed within a local scope.
2752 */
2753 class V8EXPORT Scope {
2754 public:
2755 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2756 isolate->Enter();
2757 }
2758
2759 ~Scope() { isolate_->Exit(); }
2760
2761 private:
2762 Isolate* const isolate_;
2763
2764 // Prevent copying of Scope objects.
2765 Scope(const Scope&);
2766 Scope& operator=(const Scope&);
2767 };
2768
2769 /**
2770 * Creates a new isolate. Does not change the currently entered
2771 * isolate.
2772 *
2773 * When an isolate is no longer used its resources should be freed
2774 * by calling Dispose(). Using the delete operator is not allowed.
2775 */
2776 static Isolate* New();
2777
2778 /**
2779 * Returns the entered isolate for the current thread or NULL in
2780 * case there is no current isolate.
2781 */
2782 static Isolate* GetCurrent();
2783
2784 /**
2785 * Methods below this point require holding a lock (using Locker) in
2786 * a multi-threaded environment.
2787 */
2788
2789 /**
2790 * Sets this isolate as the entered one for the current thread.
2791 * Saves the previously entered one (if any), so that it can be
2792 * restored when exiting. Re-entering an isolate is allowed.
2793 */
2794 void Enter();
2795
2796 /**
2797 * Exits this isolate by restoring the previously entered one in the
2798 * current thread. The isolate may still stay the same, if it was
2799 * entered more than once.
2800 *
2801 * Requires: this == Isolate::GetCurrent().
2802 */
2803 void Exit();
2804
2805 /**
2806 * Disposes the isolate. The isolate must not be entered by any
2807 * thread to be disposable.
2808 */
2809 void Dispose();
2810
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002811 /**
2812 * Associate embedder-specific data with the isolate
2813 */
2814 void SetData(void* data);
2815
2816 /**
2817 * Retrive embedder-specific data from the isolate.
2818 * Returns NULL if SetData has never been called.
2819 */
2820 void* GetData();
2821
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002822 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002823 Isolate();
2824 Isolate(const Isolate&);
2825 ~Isolate();
2826 Isolate& operator=(const Isolate&);
2827 void* operator new(size_t size);
2828 void operator delete(void*, size_t);
2829};
2830
2831
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002832class StartupData {
2833 public:
2834 enum CompressionAlgorithm {
2835 kUncompressed,
2836 kBZip2
2837 };
2838
2839 const char* data;
2840 int compressed_size;
2841 int raw_size;
2842};
2843
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002844
2845/**
2846 * A helper class for driving V8 startup data decompression. It is based on
2847 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2848 * for an embedder to use this class, instead, API functions can be used
2849 * directly.
2850 *
2851 * For an example of the class usage, see the "shell.cc" sample application.
2852 */
2853class V8EXPORT StartupDataDecompressor { // NOLINT
2854 public:
2855 StartupDataDecompressor();
2856 virtual ~StartupDataDecompressor();
2857 int Decompress();
2858
2859 protected:
2860 virtual int DecompressData(char* raw_data,
2861 int* raw_data_size,
2862 const char* compressed_data,
2863 int compressed_data_size) = 0;
2864
2865 private:
2866 char** raw_data;
2867};
2868
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002869
2870/**
2871 * EntropySource is used as a callback function when v8 needs a source
2872 * of entropy.
2873 */
2874typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2875
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002876
2877/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002878 * ReturnAddressLocationResolver is used as a callback function when v8 is
2879 * resolving the location of a return address on the stack. Profilers that
2880 * change the return address on the stack can use this to resolve the stack
2881 * location to whereever the profiler stashed the original return address.
2882 * When invoked, return_addr_location will point to a location on stack where
2883 * a machine return address resides, this function should return either the
2884 * same pointer, or a pointer to the profiler's copy of the original return
2885 * address.
2886 */
2887typedef uintptr_t (*ReturnAddressLocationResolver)(
2888 uintptr_t return_addr_location);
2889
2890
2891/**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002892 * Interface for iterating though all external resources in the heap.
2893 */
2894class V8EXPORT ExternalResourceVisitor { // NOLINT
2895 public:
2896 virtual ~ExternalResourceVisitor() {}
2897 virtual void VisitExternalString(Handle<String> string) {}
2898};
2899
2900
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002901/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 * Container class for static utility functions.
2903 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002904class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002906 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002907 static void SetFatalErrorHandler(FatalErrorCallback that);
2908
v8.team.kasperl727e9952008-09-02 14:56:44 +00002909 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002910 * Set the callback to invoke to check if code generation from
2911 * strings should be allowed.
2912 */
2913 static void SetAllowCodeGenerationFromStringsCallback(
2914 AllowCodeGenerationFromStringsCallback that);
2915
2916 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002917 * Ignore out-of-memory exceptions.
2918 *
2919 * V8 running out of memory is treated as a fatal error by default.
2920 * This means that the fatal error handler is called and that V8 is
2921 * terminated.
2922 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002923 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002924 * out-of-memory situation as a fatal error. This way, the contexts
2925 * that did not cause the out of memory problem might be able to
2926 * continue execution.
2927 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928 static void IgnoreOutOfMemoryException();
2929
v8.team.kasperl727e9952008-09-02 14:56:44 +00002930 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002931 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002932 * fatal errors such as out-of-memory situations.
2933 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002934 static bool IsDead();
2935
2936 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002937 * The following 4 functions are to be used when V8 is built with
2938 * the 'compress_startup_data' flag enabled. In this case, the
2939 * embedder must decompress startup data prior to initializing V8.
2940 *
2941 * This is how interaction with V8 should look like:
2942 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2943 * v8::StartupData* compressed_data =
2944 * new v8::StartupData[compressed_data_count];
2945 * v8::V8::GetCompressedStartupData(compressed_data);
2946 * ... decompress data (compressed_data can be updated in-place) ...
2947 * v8::V8::SetDecompressedStartupData(compressed_data);
2948 * ... now V8 can be initialized
2949 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002950 *
2951 * A helper class StartupDataDecompressor is provided. It implements
2952 * the protocol of the interaction described above, and can be used in
2953 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002954 */
2955 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2956 static int GetCompressedStartupDataCount();
2957 static void GetCompressedStartupData(StartupData* compressed_data);
2958 static void SetDecompressedStartupData(StartupData* decompressed_data);
2959
2960 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002961 * Adds a message listener.
2962 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002963 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002964 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965 */
2966 static bool AddMessageListener(MessageCallback that,
2967 Handle<Value> data = Handle<Value>());
2968
2969 /**
2970 * Remove all message listeners from the specified callback function.
2971 */
2972 static void RemoveMessageListeners(MessageCallback that);
2973
2974 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002975 * Tells V8 to capture current stack trace when uncaught exception occurs
2976 * and report it to the message listeners. The option is off by default.
2977 */
2978 static void SetCaptureStackTraceForUncaughtExceptions(
2979 bool capture,
2980 int frame_limit = 10,
2981 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2982
2983 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002984 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985 */
2986 static void SetFlagsFromString(const char* str, int length);
2987
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002988 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002989 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002990 */
2991 static void SetFlagsFromCommandLine(int* argc,
2992 char** argv,
2993 bool remove_flags);
2994
kasper.lund7276f142008-07-30 08:49:36 +00002995 /** Get the version string. */
2996 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997
2998 /**
2999 * Enables the host application to provide a mechanism for recording
3000 * statistics counters.
3001 */
3002 static void SetCounterFunction(CounterLookupCallback);
3003
3004 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003005 * Enables the host application to provide a mechanism for recording
3006 * histograms. The CreateHistogram function returns a
3007 * histogram which will later be passed to the AddHistogramSample
3008 * function.
3009 */
3010 static void SetCreateHistogramFunction(CreateHistogramCallback);
3011 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3012
3013 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003014 * Enables the computation of a sliding window of states. The sliding
3015 * window information is recorded in statistics counters.
3016 */
3017 static void EnableSlidingStateWindow();
3018
3019 /** Callback function for reporting failed access checks.*/
3020 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3021
3022 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003023 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003024 * garbage collection. Allocations are not allowed in the
3025 * callback function, you therefore cannot manipulate objects (set
3026 * or delete properties for example) since it is possible such
3027 * operations will result in the allocation of objects. It is possible
3028 * to specify the GCType filter for your callback. But it is not possible to
3029 * register the same callback function two times with different
3030 * GCType filters.
3031 */
3032 static void AddGCPrologueCallback(
3033 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3034
3035 /**
3036 * This function removes callback which was installed by
3037 * AddGCPrologueCallback function.
3038 */
3039 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3040
3041 /**
3042 * The function is deprecated. Please use AddGCPrologueCallback instead.
3043 * Enables the host application to receive a notification before a
3044 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003045 * callback function, you therefore cannot manipulate objects (set
3046 * or delete properties for example) since it is possible such
3047 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003048 */
3049 static void SetGlobalGCPrologueCallback(GCCallback);
3050
3051 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003052 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003053 * garbage collection. Allocations are not allowed in the
3054 * callback function, you therefore cannot manipulate objects (set
3055 * or delete properties for example) since it is possible such
3056 * operations will result in the allocation of objects. It is possible
3057 * to specify the GCType filter for your callback. But it is not possible to
3058 * register the same callback function two times with different
3059 * GCType filters.
3060 */
3061 static void AddGCEpilogueCallback(
3062 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3063
3064 /**
3065 * This function removes callback which was installed by
3066 * AddGCEpilogueCallback function.
3067 */
3068 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3069
3070 /**
3071 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3072 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003073 * major garbage collection. Allocations are not allowed in the
3074 * callback function, you therefore cannot manipulate objects (set
3075 * or delete properties for example) since it is possible such
3076 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077 */
3078 static void SetGlobalGCEpilogueCallback(GCCallback);
3079
3080 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003081 * Enables the host application to provide a mechanism to be notified
3082 * and perform custom logging when V8 Allocates Executable Memory.
3083 */
3084 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3085 ObjectSpace space,
3086 AllocationAction action);
3087
3088 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003089 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003090 */
3091 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3092
3093 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003094 * Adds a callback to notify the host application when a script finished
3095 * running. If a script re-enters the runtime during executing, the
3096 * CallCompletedCallback is only invoked when the outer-most script
3097 * execution ends. Executing scripts inside the callback do not trigger
3098 * further callbacks.
3099 */
3100 static void AddCallCompletedCallback(CallCompletedCallback callback);
3101
3102 /**
3103 * Removes callback that was installed by AddCallCompletedCallback.
3104 */
3105 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3106
3107 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003108 * Allows the host application to group objects together. If one
3109 * object in the group is alive, all objects in the group are alive.
3110 * After each garbage collection, object groups are removed. It is
3111 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003112 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00003113 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003114 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003116 static void AddObjectGroup(Persistent<Value>* objects,
3117 size_t length,
3118 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003119
3120 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003121 * Allows the host application to declare implicit references between
3122 * the objects: if |parent| is alive, all |children| are alive too.
3123 * After each garbage collection, all implicit references
3124 * are removed. It is intended to be used in the before-garbage-collection
3125 * callback function.
3126 */
3127 static void AddImplicitReferences(Persistent<Object> parent,
3128 Persistent<Value>* children,
3129 size_t length);
3130
3131 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003132 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003133 * initialize from scratch. This function is called implicitly if
3134 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135 */
3136 static bool Initialize();
3137
kasper.lund7276f142008-07-30 08:49:36 +00003138 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003139 * Allows the host application to provide a callback which can be used
3140 * as a source of entropy for random number generators.
3141 */
3142 static void SetEntropySource(EntropySource source);
3143
3144 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003145 * Allows the host application to provide a callback that allows v8 to
3146 * cooperate with a profiler that rewrites return addresses on stack.
3147 */
3148 static void SetReturnAddressLocationResolver(
3149 ReturnAddressLocationResolver return_address_resolver);
3150
3151 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003152 * Adjusts the amount of registered external memory. Used to give
3153 * V8 an indication of the amount of externally allocated memory
3154 * that is kept alive by JavaScript objects. V8 uses this to decide
3155 * when to perform global garbage collections. Registering
3156 * externally allocated memory will trigger global garbage
3157 * collections more often than otherwise in an attempt to garbage
3158 * collect the JavaScript objects keeping the externally allocated
3159 * memory alive.
3160 *
3161 * \param change_in_bytes the change in externally allocated memory
3162 * that is kept alive by JavaScript objects.
3163 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003164 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003165 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3166 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003167
iposva@chromium.org245aa852009-02-10 00:49:54 +00003168 /**
3169 * Suspends recording of tick samples in the profiler.
3170 * When the V8 profiling mode is enabled (usually via command line
3171 * switches) this function suspends recording of tick samples.
3172 * Profiling ticks are discarded until ResumeProfiler() is called.
3173 *
3174 * See also the --prof and --prof_auto command line switches to
3175 * enable V8 profiling.
3176 */
3177 static void PauseProfiler();
3178
3179 /**
3180 * Resumes recording of tick samples in the profiler.
3181 * See also PauseProfiler().
3182 */
3183 static void ResumeProfiler();
3184
ager@chromium.org41826e72009-03-30 13:30:57 +00003185 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003186 * Return whether profiler is currently paused.
3187 */
3188 static bool IsProfilerPaused();
3189
3190 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003191 * Retrieve the V8 thread id of the calling thread.
3192 *
3193 * The thread id for a thread should only be retrieved after the V8
3194 * lock has been acquired with a Locker object with that thread.
3195 */
3196 static int GetCurrentThreadId();
3197
3198 /**
3199 * Forcefully terminate execution of a JavaScript thread. This can
3200 * be used to terminate long-running scripts.
3201 *
3202 * TerminateExecution should only be called when then V8 lock has
3203 * been acquired with a Locker object. Therefore, in order to be
3204 * able to terminate long-running threads, preemption must be
3205 * enabled to allow the user of TerminateExecution to acquire the
3206 * lock.
3207 *
3208 * The termination is achieved by throwing an exception that is
3209 * uncatchable by JavaScript exception handlers. Termination
3210 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003211 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003212 * exception handler that catches an exception should check if that
3213 * exception is a termination exception and immediately return if
3214 * that is the case. Returning immediately in that case will
3215 * continue the propagation of the termination exception if needed.
3216 *
3217 * The thread id passed to TerminateExecution must have been
3218 * obtained by calling GetCurrentThreadId on the thread in question.
3219 *
3220 * \param thread_id The thread id of the thread to terminate.
3221 */
3222 static void TerminateExecution(int thread_id);
3223
3224 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003225 * Forcefully terminate the current thread of JavaScript execution
3226 * in the given isolate. If no isolate is provided, the default
3227 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003228 *
3229 * This method can be used by any thread even if that thread has not
3230 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003231 *
3232 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003233 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003234 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003235
3236 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003237 * Is V8 terminating JavaScript execution.
3238 *
3239 * Returns true if JavaScript execution is currently terminating
3240 * because of a call to TerminateExecution. In that case there are
3241 * still JavaScript frames on the stack and the termination
3242 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003243 *
3244 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003245 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003246 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003247
3248 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003249 * Releases any resources used by v8 and stops any utility threads
3250 * that may be running. Note that disposing v8 is permanent, it
3251 * cannot be reinitialized.
3252 *
3253 * It should generally not be necessary to dispose v8 before exiting
3254 * a process, this should happen automatically. It is only necessary
3255 * to use if the process needs the resources taken up by v8.
3256 */
3257 static bool Dispose();
3258
ager@chromium.org3811b432009-10-28 14:53:37 +00003259 /**
3260 * Get statistics about the heap memory usage.
3261 */
3262 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003263
3264 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003265 * Iterates through all external resources referenced from current isolate
3266 * heap. This method is not expected to be used except for debugging purposes
3267 * and may be quite slow.
3268 */
3269 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3270
3271 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003272 * Optional notification that the embedder is idle.
3273 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003274 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003275 * Returns true if the embedder should stop calling IdleNotification
3276 * until real work has been done. This indicates that V8 has done
3277 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003278 *
3279 * The hint argument specifies the amount of work to be done in the function
3280 * on scale from 1 to 1000. There is no guarantee that the actual work will
3281 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003282 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003283 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003284
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003285 /**
3286 * Optional notification that the system is running low on memory.
3287 * V8 uses these notifications to attempt to free memory.
3288 */
3289 static void LowMemoryNotification();
3290
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003291 /**
3292 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003293 * these notifications to guide the GC heuristic. Returns the number
3294 * of context disposals - including this one - since the last time
3295 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003296 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003297 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003298
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 private:
3300 V8();
3301
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003302 static internal::Object** GlobalizeReference(internal::Object** handle);
3303 static void DisposeGlobal(internal::Object** global_handle);
3304 static void MakeWeak(internal::Object** global_handle,
3305 void* data,
3306 WeakReferenceCallback);
3307 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003308 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003309 static bool IsGlobalNearDeath(internal::Object** global_handle);
3310 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003311 static void SetWrapperClassId(internal::Object** global_handle,
3312 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313
3314 template <class T> friend class Handle;
3315 template <class T> friend class Local;
3316 template <class T> friend class Persistent;
3317 friend class Context;
3318};
3319
3320
3321/**
3322 * An external exception handler.
3323 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003324class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003325 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 /**
3327 * Creates a new try/catch block and registers it with v8.
3328 */
3329 TryCatch();
3330
3331 /**
3332 * Unregisters and deletes this try/catch block.
3333 */
3334 ~TryCatch();
3335
3336 /**
3337 * Returns true if an exception has been caught by this try/catch block.
3338 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003339 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340
3341 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003342 * For certain types of exceptions, it makes no sense to continue
3343 * execution.
3344 *
3345 * Currently, the only type of exception that can be caught by a
3346 * TryCatch handler and for which it does not make sense to continue
3347 * is termination exception. Such exceptions are thrown when the
3348 * TerminateExecution methods are called to terminate a long-running
3349 * script.
3350 *
3351 * If CanContinue returns false, the correct action is to perform
3352 * any C++ cleanup needed and then return.
3353 */
3354 bool CanContinue() const;
3355
3356 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003357 * Throws the exception caught by this TryCatch in a way that avoids
3358 * it being caught again by this same TryCatch. As with ThrowException
3359 * it is illegal to execute any JavaScript operations after calling
3360 * ReThrow; the caller must return immediately to where the exception
3361 * is caught.
3362 */
3363 Handle<Value> ReThrow();
3364
3365 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366 * Returns the exception caught by this try/catch block. If no exception has
3367 * been caught an empty handle is returned.
3368 *
3369 * The returned handle is valid until this TryCatch block has been destroyed.
3370 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003371 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372
3373 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003374 * Returns the .stack property of the thrown object. If no .stack
3375 * property is present an empty handle is returned.
3376 */
3377 Local<Value> StackTrace() const;
3378
3379 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003380 * Returns the message associated with this exception. If there is
3381 * no message associated an empty handle is returned.
3382 *
3383 * The returned handle is valid until this TryCatch block has been
3384 * destroyed.
3385 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003386 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003387
3388 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003389 * Clears any exceptions that may have been caught by this try/catch block.
3390 * After this method has been called, HasCaught() will return false.
3391 *
3392 * It is not necessary to clear a try/catch block before using it again; if
3393 * another exception is thrown the previously caught exception will just be
3394 * overwritten. However, it is often a good idea since it makes it easier
3395 * to determine which operation threw a given exception.
3396 */
3397 void Reset();
3398
v8.team.kasperl727e9952008-09-02 14:56:44 +00003399 /**
3400 * Set verbosity of the external exception handler.
3401 *
3402 * By default, exceptions that are caught by an external exception
3403 * handler are not reported. Call SetVerbose with true on an
3404 * external exception handler to have exceptions caught by the
3405 * handler reported as if they were not caught.
3406 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003407 void SetVerbose(bool value);
3408
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003409 /**
3410 * Set whether or not this TryCatch should capture a Message object
3411 * which holds source information about where the exception
3412 * occurred. True by default.
3413 */
3414 void SetCaptureMessage(bool value);
3415
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003416 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003417 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003418 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003420 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003421 bool is_verbose_ : 1;
3422 bool can_continue_ : 1;
3423 bool capture_message_ : 1;
3424 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003425
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003426 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427};
3428
3429
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003430// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431
3432
3433/**
3434 * Ignore
3435 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003436class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437 public:
3438 ExtensionConfiguration(int name_count, const char* names[])
3439 : name_count_(name_count), names_(names) { }
3440 private:
3441 friend class ImplementationUtilities;
3442 int name_count_;
3443 const char** names_;
3444};
3445
3446
3447/**
3448 * A sandboxed execution context with its own set of built-in objects
3449 * and functions.
3450 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003451class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003452 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003453 /**
3454 * Returns the global proxy object or global object itself for
3455 * detached contexts.
3456 *
3457 * Global proxy object is a thin wrapper whose prototype points to
3458 * actual context's global object with the properties like Object, etc.
3459 * This is done that way for security reasons (for more details see
3460 * https://wiki.mozilla.org/Gecko:SplitWindow).
3461 *
3462 * Please note that changes to global proxy object prototype most probably
3463 * would break VM---v8 expects only global object as a prototype of
3464 * global proxy object.
3465 *
3466 * If DetachGlobal() has been invoked, Global() would return actual global
3467 * object until global is reattached with ReattachGlobal().
3468 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003469 Local<Object> Global();
3470
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003471 /**
3472 * Detaches the global object from its context before
3473 * the global object can be reused to create a new context.
3474 */
3475 void DetachGlobal();
3476
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003477 /**
3478 * Reattaches a global object to a context. This can be used to
3479 * restore the connection between a global object and a context
3480 * after DetachGlobal has been called.
3481 *
3482 * \param global_object The global object to reattach to the
3483 * context. For this to work, the global object must be the global
3484 * object that was associated with this context before a call to
3485 * DetachGlobal.
3486 */
3487 void ReattachGlobal(Handle<Object> global_object);
3488
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003489 /** Creates a new context.
3490 *
3491 * Returns a persistent handle to the newly allocated context. This
3492 * persistent handle has to be disposed when the context is no
3493 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003494 *
3495 * \param extensions An optional extension configuration containing
3496 * the extensions to be installed in the newly created context.
3497 *
3498 * \param global_template An optional object template from which the
3499 * global object for the newly created context will be created.
3500 *
3501 * \param global_object An optional global object to be reused for
3502 * the newly created context. This global object must have been
3503 * created by a previous call to Context::New with the same global
3504 * template. The state of the global object will be completely reset
3505 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003506 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003507 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003508 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003509 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3510 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003511
kasper.lund44510672008-07-25 07:37:58 +00003512 /** Returns the last entered context. */
3513 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514
kasper.lund44510672008-07-25 07:37:58 +00003515 /** Returns the context that is on the top of the stack. */
3516 static Local<Context> GetCurrent();
3517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003519 * Returns the context of the calling JavaScript code. That is the
3520 * context of the top-most JavaScript frame. If there are no
3521 * JavaScript frames an empty handle is returned.
3522 */
3523 static Local<Context> GetCalling();
3524
3525 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526 * Sets the security token for the context. To access an object in
3527 * another context, the security tokens must match.
3528 */
3529 void SetSecurityToken(Handle<Value> token);
3530
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003531 /** Restores the security token to the default value. */
3532 void UseDefaultSecurityToken();
3533
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003534 /** Returns the security token of this context.*/
3535 Handle<Value> GetSecurityToken();
3536
v8.team.kasperl727e9952008-09-02 14:56:44 +00003537 /**
3538 * Enter this context. After entering a context, all code compiled
3539 * and run is compiled and run in this context. If another context
3540 * is already entered, this old context is saved so it can be
3541 * restored when the new context is exited.
3542 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003543 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003544
3545 /**
3546 * Exit this context. Exiting the current context restores the
3547 * context that was in place when entering the current context.
3548 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549 void Exit();
3550
v8.team.kasperl727e9952008-09-02 14:56:44 +00003551 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003552 bool HasOutOfMemoryException();
3553
v8.team.kasperl727e9952008-09-02 14:56:44 +00003554 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555 static bool InContext();
3556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003557 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003558 * Associate an additional data object with the context. This is mainly used
3559 * with the debugger to provide additional information on the context through
3560 * the debugger API.
3561 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003562 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003563 Local<Value> GetData();
3564
3565 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003566 * Control whether code generation from strings is allowed. Calling
3567 * this method with false will disable 'eval' and the 'Function'
3568 * constructor for code running in this context. If 'eval' or the
3569 * 'Function' constructor are used an exception will be thrown.
3570 *
3571 * If code generation from strings is not allowed the
3572 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3573 * set before blocking the call to 'eval' or the 'Function'
3574 * constructor. If that callback returns true, the call will be
3575 * allowed, otherwise an exception will be thrown. If no callback is
3576 * set an exception will be thrown.
3577 */
3578 void AllowCodeGenerationFromStrings(bool allow);
3579
3580 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003581 * Returns true if code generation from strings is allowed for the context.
3582 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3583 */
3584 bool IsCodeGenerationFromStringsAllowed();
3585
3586 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 * Stack-allocated class which sets the execution context for all
3588 * operations executed within a local scope.
3589 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003590 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003592 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 context_->Enter();
3594 }
3595 inline ~Scope() { context_->Exit(); }
3596 private:
3597 Handle<Context> context_;
3598 };
3599
3600 private:
3601 friend class Value;
3602 friend class Script;
3603 friend class Object;
3604 friend class Function;
3605};
3606
3607
3608/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003609 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003610 * is allowed to use any given V8 isolate. See Isolate class
3611 * comments. The definition of 'using V8 isolate' includes
3612 * accessing handles or holding onto object pointers obtained
3613 * from V8 handles while in the particular V8 isolate. It is up
3614 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003615 * constraint is not violated. In addition to any other synchronization
3616 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3617 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003619 * v8::Locker is a scoped lock object. While it's
3620 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003621 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3622 * locked by at most one thread at any time. In other words, the scope of a
3623 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003624 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003625 * Sample usage:
3626* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627 * ...
3628 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003629 * v8::Locker locker(isolate);
3630 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003631 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003632 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003633 * ...
3634 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003635 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003636 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003637 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003638 * by destroying the v8::Locker object as above or by constructing a
3639 * v8::Unlocker object:
3640 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003641 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003642 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003643 * isolate->Exit();
3644 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003645 * ...
3646 * // Code not using V8 goes here while V8 can run in another thread.
3647 * ...
3648 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003649 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003650 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003651 *
3652 * The Unlocker object is intended for use in a long-running callback
3653 * from V8, where you want to release the V8 lock for other threads to
3654 * use.
3655 *
3656 * The v8::Locker is a recursive lock. That is, you can lock more than
3657 * once in a given thread. This can be useful if you have code that can
3658 * be called either from code that holds the lock or from code that does
3659 * not. The Unlocker is not recursive so you can not have several
3660 * Unlockers on the stack at once, and you can not use an Unlocker in a
3661 * thread that is not inside a Locker's scope.
3662 *
3663 * An unlocker will unlock several lockers if it has to and reinstate
3664 * the correct depth of locking on its destruction. eg.:
3665 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003666 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003667 * // V8 not locked.
3668 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003669 * v8::Locker locker(isolate);
3670 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671 * // V8 locked.
3672 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003673 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003674 * // V8 still locked (2 levels).
3675 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003676 * isolate->Exit();
3677 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003678 * // V8 not locked.
3679 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003680 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681 * // V8 locked again (2 levels).
3682 * }
3683 * // V8 still locked (1 level).
3684 * }
3685 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003686 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003687 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003688 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003689 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003690class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003692 /**
3693 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3694 */
3695 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003697 private:
3698 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699};
3700
3701
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003702class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003704 /**
3705 * Initialize Locker for a given Isolate. NULL means default isolate.
3706 */
3707 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003708 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003709
3710 /**
3711 * Start preemption.
3712 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003713 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003714 * that will switch between multiple threads that are in contention
3715 * for the V8 lock.
3716 */
3717 static void StartPreemption(int every_n_ms);
3718
3719 /**
3720 * Stop preemption.
3721 */
3722 static void StopPreemption();
3723
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003724 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003725 * Returns whether or not the locker for a given isolate, or default isolate
3726 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003727 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003728 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003729
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003730 /**
3731 * Returns whether v8::Locker is being used by this V8 instance.
3732 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003733 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003734
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003735 private:
3736 bool has_lock_;
3737 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003738 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003739
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003740 static bool active_;
3741
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003742 // Disallow copying and assigning.
3743 Locker(const Locker&);
3744 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003745};
3746
3747
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003748/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003749 * A struct for exporting HeapStats data from V8, using "push" model.
3750 */
3751struct HeapStatsUpdate;
3752
3753
3754/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003755 * An interface for exporting data from V8, using "push" model.
3756 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003757class V8EXPORT OutputStream { // NOLINT
3758 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003759 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003760 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003761 };
3762 enum WriteResult {
3763 kContinue = 0,
3764 kAbort = 1
3765 };
3766 virtual ~OutputStream() {}
3767 /** Notify about the end of stream. */
3768 virtual void EndOfStream() = 0;
3769 /** Get preferred output chunk size. Called only once. */
3770 virtual int GetChunkSize() { return 1024; }
3771 /** Get preferred output encoding. Called only once. */
3772 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3773 /**
3774 * Writes the next chunk of snapshot data into the stream. Writing
3775 * can be stopped by returning kAbort as function result. EndOfStream
3776 * will not be called in case writing was aborted.
3777 */
3778 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003779 /**
3780 * Writes the next chunk of heap stats data into the stream. Writing
3781 * can be stopped by returning kAbort as function result. EndOfStream
3782 * will not be called in case writing was aborted.
3783 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003784 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00003785 return kAbort;
3786 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003787};
3788
3789
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003790/**
3791 * An interface for reporting progress and controlling long-running
3792 * activities.
3793 */
3794class V8EXPORT ActivityControl { // NOLINT
3795 public:
3796 enum ControlOption {
3797 kContinue = 0,
3798 kAbort = 1
3799 };
3800 virtual ~ActivityControl() {}
3801 /**
3802 * Notify about current progress. The activity can be stopped by
3803 * returning kAbort as the callback result.
3804 */
3805 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3806};
3807
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003809// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003810
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003811
3812namespace internal {
3813
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00003814const int kApiPointerSize = sizeof(void*); // NOLINT
3815const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003816
3817// Tag information for HeapObject.
3818const int kHeapObjectTag = 1;
3819const int kHeapObjectTagSize = 2;
3820const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3821
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003822// Tag information for Smi.
3823const int kSmiTag = 0;
3824const int kSmiTagSize = 1;
3825const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3826
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003827template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003828
3829// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003830template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003831 static const int kSmiShiftSize = 0;
3832 static const int kSmiValueSize = 31;
3833 static inline int SmiToInt(internal::Object* value) {
3834 int shift_bits = kSmiTagSize + kSmiShiftSize;
3835 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3836 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3837 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003838
3839 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3840 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003841 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003842 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003843};
3844
3845// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003846template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003847 static const int kSmiShiftSize = 31;
3848 static const int kSmiValueSize = 32;
3849 static inline int SmiToInt(internal::Object* value) {
3850 int shift_bits = kSmiTagSize + kSmiShiftSize;
3851 // Shift down and throw away top 32 bits.
3852 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3853 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003854
3855 // To maximize the range of pointers that can be encoded
3856 // in the available 32 bits, we require them to be 8 bytes aligned.
3857 // This gives 2 ^ (32 + 3) = 32G address space covered.
3858 // It might be not enough to cover stack allocated objects on some platforms.
3859 static const int kPointerAlignment = 3;
3860
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003861 static const uintptr_t kEncodablePointerMask =
3862 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003863
3864 static const int kPointerToSmiShift =
3865 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003866};
3867
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003868typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3869const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3870const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003871const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003872 PlatformSmiTagging::kEncodablePointerMask;
3873const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003874
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003875template <size_t ptr_size> struct InternalConstants;
3876
3877// Internal constants for 32-bit systems.
3878template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003879 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003880};
3881
3882// Internal constants for 64-bit systems.
3883template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003884 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003885};
3886
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003887/**
3888 * This class exports constants and functionality from within v8 that
3889 * is necessary to implement inline functions in the v8 api. Don't
3890 * depend on functions and constants defined here.
3891 */
3892class Internals {
3893 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003894 // These values match non-compiler-dependent values defined within
3895 // the implementation of v8.
3896 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003897 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003898 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003899 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003900
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003901 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003902 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003903 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003904 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003905 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003906
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003907 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003908 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003909 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003910 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003911
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003912 static const int kUndefinedOddballKind = 5;
3913 static const int kNullOddballKind = 3;
3914
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003915 static inline bool HasHeapObjectTag(internal::Object* value) {
3916 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3917 kHeapObjectTag);
3918 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003919
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003920 static inline bool HasSmiTag(internal::Object* value) {
3921 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3922 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003923
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003924 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003925 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003926 }
3927
3928 static inline int GetInstanceType(internal::Object* obj) {
3929 typedef internal::Object O;
3930 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3931 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3932 }
3933
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00003934 static inline int GetOddballKind(internal::Object* obj) {
3935 typedef internal::Object O;
3936 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
3937 }
3938
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003939 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003940 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003941 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3942 }
3943
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003944 static inline void* GetExternalPointer(internal::Object* obj) {
3945 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003946 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003947 } else if (GetInstanceType(obj) == kForeignType) {
3948 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003949 } else {
3950 return NULL;
3951 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003952 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003953
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003954 static inline bool IsExternalTwoByteString(int instance_type) {
3955 int representation = (instance_type & kFullStringRepresentationMask);
3956 return representation == kExternalTwoByteRepresentationTag;
3957 }
3958
3959 template <typename T>
3960 static inline T ReadField(Object* ptr, int offset) {
3961 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3962 return *reinterpret_cast<T*>(addr);
3963 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003964
3965 static inline bool CanCastToHeapObject(void* o) { return false; }
3966 static inline bool CanCastToHeapObject(Context* o) { return true; }
3967 static inline bool CanCastToHeapObject(String* o) { return true; }
3968 static inline bool CanCastToHeapObject(Object* o) { return true; }
3969 static inline bool CanCastToHeapObject(Message* o) { return true; }
3970 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3971 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003972};
3973
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003974} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003975
3976
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003978Local<T>::Local() : Handle<T>() { }
3979
3980
3981template <class T>
3982Local<T> Local<T>::New(Handle<T> that) {
3983 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003984 T* that_ptr = *that;
3985 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3986 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3987 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3988 reinterpret_cast<internal::HeapObject*>(*p))));
3989 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3991}
3992
3993
3994template <class T>
3995Persistent<T> Persistent<T>::New(Handle<T> that) {
3996 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003997 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003998 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3999}
4000
4001
4002template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004003bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004004 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004005 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006}
4007
4008
4009template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004010bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004012 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004013}
4014
4015
4016template <class T>
4017void Persistent<T>::Dispose() {
4018 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004019 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020}
4021
4022
4023template <class T>
4024Persistent<T>::Persistent() : Handle<T>() { }
4025
4026template <class T>
4027void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004028 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4029 parameters,
4030 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031}
4032
4033template <class T>
4034void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004035 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036}
4037
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004038template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004039void Persistent<T>::MarkIndependent() {
4040 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4041}
4042
4043template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004044void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4045 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4046}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004047
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004048Arguments::Arguments(internal::Object** implicit_args,
4049 internal::Object** values, int length,
4050 bool is_construct_call)
4051 : implicit_args_(implicit_args),
4052 values_(values),
4053 length_(length),
4054 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004055
4056
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004057Local<Value> Arguments::operator[](int i) const {
4058 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4059 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4060}
4061
4062
4063Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004064 return Local<Function>(reinterpret_cast<Function*>(
4065 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004066}
4067
4068
4069Local<Object> Arguments::This() const {
4070 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4071}
4072
4073
4074Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004075 return Local<Object>(reinterpret_cast<Object*>(
4076 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077}
4078
4079
4080Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004081 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082}
4083
4084
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004085Isolate* Arguments::GetIsolate() const {
4086 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4087}
4088
4089
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090bool Arguments::IsConstructCall() const {
4091 return is_construct_call_;
4092}
4093
4094
4095int Arguments::Length() const {
4096 return length_;
4097}
4098
4099
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004100template <class T>
4101Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004102 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4103 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004104 return Local<T>(reinterpret_cast<T*>(after));
4105}
4106
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004107Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 return resource_name_;
4109}
4110
4111
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004112Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004113 return resource_line_offset_;
4114}
4115
4116
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004117Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004118 return resource_column_offset_;
4119}
4120
4121
4122Handle<Boolean> Boolean::New(bool value) {
4123 return value ? True() : False();
4124}
4125
4126
4127void Template::Set(const char* name, v8::Handle<Data> value) {
4128 Set(v8::String::New(name), value);
4129}
4130
4131
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004132Local<Value> Object::GetInternalField(int index) {
4133#ifndef V8_ENABLE_CHECKS
4134 Local<Value> quick_result = UncheckedGetInternalField(index);
4135 if (!quick_result.IsEmpty()) return quick_result;
4136#endif
4137 return CheckedGetInternalField(index);
4138}
4139
4140
4141Local<Value> Object::UncheckedGetInternalField(int index) {
4142 typedef internal::Object O;
4143 typedef internal::Internals I;
4144 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004145 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004146 // If the object is a plain JSObject, which is the common case,
4147 // we know where to find the internal fields and can return the
4148 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004149 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004150 O* value = I::ReadField<O*>(obj, offset);
4151 O** result = HandleScope::CreateHandle(value);
4152 return Local<Value>(reinterpret_cast<Value*>(result));
4153 } else {
4154 return Local<Value>();
4155 }
4156}
4157
4158
4159void* External::Unwrap(Handle<v8::Value> obj) {
4160#ifdef V8_ENABLE_CHECKS
4161 return FullUnwrap(obj);
4162#else
4163 return QuickUnwrap(obj);
4164#endif
4165}
4166
4167
4168void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
4169 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004170 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004171 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004172}
4173
4174
4175void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004176 typedef internal::Object O;
4177 typedef internal::Internals I;
4178
4179 O* obj = *reinterpret_cast<O**>(this);
4180
4181 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4182 // If the object is a plain JSObject, which is the common case,
4183 // we know where to find the internal fields and can return the
4184 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004185 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004186 O* value = I::ReadField<O*>(obj, offset);
4187 return I::GetExternalPointer(value);
4188 }
4189
4190 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004191}
4192
4193
4194String* String::Cast(v8::Value* value) {
4195#ifdef V8_ENABLE_CHECKS
4196 CheckCast(value);
4197#endif
4198 return static_cast<String*>(value);
4199}
4200
4201
4202String::ExternalStringResource* String::GetExternalStringResource() const {
4203 typedef internal::Object O;
4204 typedef internal::Internals I;
4205 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004206 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004207 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004208 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4209 result = reinterpret_cast<String::ExternalStringResource*>(value);
4210 } else {
4211 result = NULL;
4212 }
4213#ifdef V8_ENABLE_CHECKS
4214 VerifyExternalStringResource(result);
4215#endif
4216 return result;
4217}
4218
4219
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004220bool Value::IsUndefined() const {
4221#ifdef V8_ENABLE_CHECKS
4222 return FullIsUndefined();
4223#else
4224 return QuickIsUndefined();
4225#endif
4226}
4227
4228bool Value::QuickIsUndefined() const {
4229 typedef internal::Object O;
4230 typedef internal::Internals I;
4231 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4232 if (!I::HasHeapObjectTag(obj)) return false;
4233 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4234 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4235}
4236
4237
4238bool Value::IsNull() const {
4239#ifdef V8_ENABLE_CHECKS
4240 return FullIsNull();
4241#else
4242 return QuickIsNull();
4243#endif
4244}
4245
4246bool Value::QuickIsNull() const {
4247 typedef internal::Object O;
4248 typedef internal::Internals I;
4249 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4250 if (!I::HasHeapObjectTag(obj)) return false;
4251 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4252 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4253}
4254
4255
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004256bool Value::IsString() const {
4257#ifdef V8_ENABLE_CHECKS
4258 return FullIsString();
4259#else
4260 return QuickIsString();
4261#endif
4262}
4263
4264bool Value::QuickIsString() const {
4265 typedef internal::Object O;
4266 typedef internal::Internals I;
4267 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4268 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004269 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004270}
4271
4272
4273Number* Number::Cast(v8::Value* value) {
4274#ifdef V8_ENABLE_CHECKS
4275 CheckCast(value);
4276#endif
4277 return static_cast<Number*>(value);
4278}
4279
4280
4281Integer* Integer::Cast(v8::Value* value) {
4282#ifdef V8_ENABLE_CHECKS
4283 CheckCast(value);
4284#endif
4285 return static_cast<Integer*>(value);
4286}
4287
4288
4289Date* Date::Cast(v8::Value* value) {
4290#ifdef V8_ENABLE_CHECKS
4291 CheckCast(value);
4292#endif
4293 return static_cast<Date*>(value);
4294}
4295
4296
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004297StringObject* StringObject::Cast(v8::Value* value) {
4298#ifdef V8_ENABLE_CHECKS
4299 CheckCast(value);
4300#endif
4301 return static_cast<StringObject*>(value);
4302}
4303
4304
4305NumberObject* NumberObject::Cast(v8::Value* value) {
4306#ifdef V8_ENABLE_CHECKS
4307 CheckCast(value);
4308#endif
4309 return static_cast<NumberObject*>(value);
4310}
4311
4312
4313BooleanObject* BooleanObject::Cast(v8::Value* value) {
4314#ifdef V8_ENABLE_CHECKS
4315 CheckCast(value);
4316#endif
4317 return static_cast<BooleanObject*>(value);
4318}
4319
4320
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004321RegExp* RegExp::Cast(v8::Value* value) {
4322#ifdef V8_ENABLE_CHECKS
4323 CheckCast(value);
4324#endif
4325 return static_cast<RegExp*>(value);
4326}
4327
4328
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004329Object* Object::Cast(v8::Value* value) {
4330#ifdef V8_ENABLE_CHECKS
4331 CheckCast(value);
4332#endif
4333 return static_cast<Object*>(value);
4334}
4335
4336
4337Array* Array::Cast(v8::Value* value) {
4338#ifdef V8_ENABLE_CHECKS
4339 CheckCast(value);
4340#endif
4341 return static_cast<Array*>(value);
4342}
4343
4344
4345Function* Function::Cast(v8::Value* value) {
4346#ifdef V8_ENABLE_CHECKS
4347 CheckCast(value);
4348#endif
4349 return static_cast<Function*>(value);
4350}
4351
4352
4353External* External::Cast(v8::Value* value) {
4354#ifdef V8_ENABLE_CHECKS
4355 CheckCast(value);
4356#endif
4357 return static_cast<External*>(value);
4358}
4359
4360
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004361Isolate* AccessorInfo::GetIsolate() const {
4362 return *reinterpret_cast<Isolate**>(&args_[-3]);
4363}
4364
4365
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004366Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004367 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004368}
4369
4370
4371Local<Object> AccessorInfo::This() const {
4372 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4373}
4374
4375
4376Local<Object> AccessorInfo::Holder() const {
4377 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4378}
4379
4380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004381/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004382 * \example shell.cc
4383 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004384 * command-line and executes them.
4385 */
4386
4387
4388/**
4389 * \example process.cc
4390 */
4391
4392
4393} // namespace v8
4394
4395
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004396#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004397#undef TYPE_CHECK
4398
4399
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004400#endif // V8_H_