blob: c3bccdc2413769e90b7542c7e63d896a7f9ae0e0 [file] [log] [blame]
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
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;
83class Value;
84class Utils;
85class Number;
86class Object;
87class Array;
88class Int32;
89class Uint32;
90class External;
91class Primitive;
92class Boolean;
93class Integer;
94class Function;
95class Date;
96class ImplementationUtilities;
97class Signature;
98template <class T> class Handle;
99template <class T> class Local;
100template <class T> class Persistent;
101class FunctionTemplate;
102class ObjectTemplate;
103class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000104class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000105class StackTrace;
106class StackFrame;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000108namespace internal {
109
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000110class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000111class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000112class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113class HeapObject;
114class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000115}
116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000118// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
120
121/**
122 * A weak reference callback function.
123 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000124 * This callback should either explicitly invoke Dispose on |object| if
125 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
126 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127 * \param object the weak global object to be reclaimed by the garbage collector
128 * \param parameter the value passed in when making the weak global object
129 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000130typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 void* parameter);
132
133
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000134// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000136#define TYPE_CHECK(T, S) \
137 while (false) { \
138 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 }
140
141/**
142 * An object reference managed by the v8 garbage collector.
143 *
144 * All objects returned from v8 have to be tracked by the garbage
145 * collector so that it knows that the objects are still alive. Also,
146 * because the garbage collector may move objects, it is unsafe to
147 * point directly to an object. Instead, all objects are stored in
148 * handles which are known by the garbage collector and updated
149 * whenever an object moves. Handles should always be passed by value
150 * (except in cases like out-parameters) and they should never be
151 * allocated on the heap.
152 *
153 * There are two types of handles: local and persistent handles.
154 * Local handles are light-weight and transient and typically used in
155 * local operations. They are managed by HandleScopes. Persistent
156 * handles can be used when storing objects across several independent
157 * operations and have to be explicitly deallocated when they're no
158 * longer used.
159 *
160 * It is safe to extract the object stored in the handle by
161 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000162 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 * behind the scenes and the same rules apply to these values as to
164 * their handles.
165 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000166template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 /**
169 * Creates an empty handle.
170 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000171 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
173 /**
174 * Creates a new handle for the specified value.
175 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000176 inline explicit Handle(T* val) : val_(val) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177
178 /**
179 * Creates a handle for the contents of the specified handle. This
180 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000181 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000183 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 * between compatible handles, for instance assigning a
185 * Handle<String> to a variable declared as Handle<Value>, is legal
186 * because String is a subclass of Value.
187 */
188 template <class S> inline Handle(Handle<S> that)
189 : val_(reinterpret_cast<T*>(*that)) {
190 /**
191 * This check fails when trying to convert between incompatible
192 * handles. For example, converting from a Handle<String> to a
193 * Handle<Number>.
194 */
195 TYPE_CHECK(T, S);
196 }
197
198 /**
199 * Returns true if the handle is empty.
200 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000201 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000203 inline T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000205 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
207 /**
208 * Sets the handle to be empty. IsEmpty() will then return true.
209 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000210 inline void Clear() { this->val_ = 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211
212 /**
213 * Checks whether two handles are the same.
214 * Returns true if both are empty, or if the objects
215 * to which they refer are identical.
216 * The handles' references are not checked.
217 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000218 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000219 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
220 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 if (a == 0) return b == 0;
222 if (b == 0) return false;
223 return *a == *b;
224 }
225
226 /**
227 * Checks whether two handles are different.
228 * Returns true if only one of the handles is empty, or if
229 * the objects to which they refer are different.
230 * The handles' references are not checked.
231 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000232 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 return !operator==(that);
234 }
235
236 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000237#ifdef V8_ENABLE_CHECKS
238 // If we're going to perform the type check then we have to check
239 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000241#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 return Handle<T>(T::Cast(*that));
243 }
244
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000245 template <class S> inline Handle<S> As() {
246 return Handle<S>::Cast(*this);
247 }
248
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 private:
250 T* val_;
251};
252
253
254/**
255 * A light-weight stack-allocated object handle. All operations
256 * that return objects from within v8 return them in local handles. They
257 * are created within HandleScopes, and all local handles allocated within a
258 * handle scope are destroyed when the handle scope is destroyed. Hence it
259 * is not necessary to explicitly deallocate local handles.
260 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000261template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000263 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 template <class S> inline Local(Local<S> that)
265 : Handle<T>(reinterpret_cast<T*>(*that)) {
266 /**
267 * This check fails when trying to convert between incompatible
268 * handles. For example, converting from a Handle<String> to a
269 * Handle<Number>.
270 */
271 TYPE_CHECK(T, S);
272 }
273 template <class S> inline Local(S* that) : Handle<T>(that) { }
274 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000275#ifdef V8_ENABLE_CHECKS
276 // If we're going to perform the type check then we have to check
277 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000279#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 return Local<T>(T::Cast(*that));
281 }
282
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000283 template <class S> inline Local<S> As() {
284 return Local<S>::Cast(*this);
285 }
286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 /** Create a local handle for the content of another handle.
288 * The referee is kept alive by the local handle even when
289 * the original handle is destroyed/disposed.
290 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000291 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292};
293
294
295/**
296 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000297 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 * allocated, a Persistent handle remains valid until it is explicitly
299 * disposed.
300 *
301 * A persistent handle contains a reference to a storage cell within
302 * the v8 engine which holds an object value and which is updated by
303 * the garbage collector whenever the object is moved. A new storage
304 * cell can be created using Persistent::New and existing handles can
305 * be disposed using Persistent::Dispose. Since persistent handles
306 * are passed by value you may have many persistent handle objects
307 * that point to the same storage cell. For instance, if you pass a
308 * persistent handle as an argument to a function you will not get two
309 * different storage cells but rather two references to the same
310 * storage cell.
311 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000312template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 /**
315 * Creates an empty persistent handle that doesn't point to any
316 * storage cell.
317 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000318 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319
320 /**
321 * Creates a persistent handle for the same storage cell as the
322 * specified handle. This constructor allows you to pass persistent
323 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000324 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000326 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 * between compatible persistent handles, for instance assigning a
328 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000329 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 */
331 template <class S> inline Persistent(Persistent<S> that)
332 : Handle<T>(reinterpret_cast<T*>(*that)) {
333 /**
334 * This check fails when trying to convert between incompatible
335 * handles. For example, converting from a Handle<String> to a
336 * Handle<Number>.
337 */
338 TYPE_CHECK(T, S);
339 }
340
341 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
342
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000343 /**
344 * "Casts" a plain handle which is known to be a persistent handle
345 * to a persistent handle.
346 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 template <class S> explicit inline Persistent(Handle<S> that)
348 : Handle<T>(*that) { }
349
350 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000351#ifdef V8_ENABLE_CHECKS
352 // If we're going to perform the type check then we have to check
353 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000355#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 return Persistent<T>(T::Cast(*that));
357 }
358
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000359 template <class S> inline Persistent<S> As() {
360 return Persistent<S>::Cast(*this);
361 }
362
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000364 * Creates a new persistent handle for an existing local or
365 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000367 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368
369 /**
370 * Releases the storage cell referenced by this persistent handle.
371 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000372 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 * cell remain and IsEmpty will still return false.
374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Make the reference to this object weak. When only weak handles
379 * refer to the object, the garbage collector will perform a
380 * callback to the given V8::WeakReferenceCallback function, passing
381 * it the object reference and the given parameters.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000386 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000389 * Marks the reference to this object independent. Garbage collector
390 * is free to ignore any object groups containing this object.
391 * Weak callback for an independent handle should not
392 * assume that it will be preceded by a global GC prologue callback
393 * or followed by a global GC epilogue callback.
394 */
395 inline void MarkIndependent();
396
397 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 *Checks if the handle holds the only reference to an object.
399 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000400 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401
402 /**
403 * Returns true if the handle's reference is weak.
404 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000405 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000407 /**
408 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
409 * interface description in v8-profiler.h for details.
410 */
411 inline void SetWrapperClassId(uint16_t class_id);
412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 private:
414 friend class ImplementationUtilities;
415 friend class ObjectTemplate;
416};
417
418
v8.team.kasperl727e9952008-09-02 14:56:44 +0000419 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 * A stack-allocated class that governs a number of local handles.
421 * After a handle scope has been created, all local handles will be
422 * allocated within that handle scope until either the handle scope is
423 * deleted or another handle scope is created. If there is already a
424 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000425 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 * new handles will again be allocated in the original handle scope.
427 *
428 * After the handle scope of a local handle has been deleted the
429 * garbage collector will no longer track the object stored in the
430 * handle and may deallocate it. The behavior of accessing a handle
431 * for which the handle scope has been deleted is undefined.
432 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000433class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000435 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000437 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
439 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 * Closes the handle scope and returns the value as a handle in the
441 * previous scope, which is the new current scope after the call.
442 */
443 template <class T> Local<T> Close(Handle<T> value);
444
445 /**
446 * Counts the number of allocated handles.
447 */
448 static int NumberOfHandles();
449
450 /**
451 * Creates a new handle with the given value.
452 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000453 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000454 // Faster version, uses HeapObject to obtain the current Isolate.
455 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456
457 private:
458 // Make it impossible to create heap-allocated or illegal handle
459 // scopes by disallowing certain operations.
460 HandleScope(const HandleScope&);
461 void operator=(const HandleScope&);
462 void* operator new(size_t size);
463 void operator delete(void*, size_t);
464
ager@chromium.org3811b432009-10-28 14:53:37 +0000465 // This Data class is accessible internally as HandleScopeData through a
466 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000467 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000469 internal::Object** next;
470 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000471 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000474 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 }
476 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000477
lrn@chromium.org303ada72010-10-27 09:33:13 +0000478 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000480 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000481 internal::Object** prev_next_;
482 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000484 // Allow for the active closing of HandleScopes which allows to pass a handle
485 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000487 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 friend class ImplementationUtilities;
490};
491
492
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000493// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494
495
496/**
497 * The superclass of values and API object templates.
498 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000499class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 private:
501 Data();
502};
503
504
505/**
506 * Pre-compilation data that can be associated with a script. This
507 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000508 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000509 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000511class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 public:
513 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000514
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000515 /**
516 * Pre-compiles the specified script (context-independent).
517 *
518 * \param input Pointer to UTF-8 script source code.
519 * \param length Length of UTF-8 script source code.
520 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000523 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000524 * Pre-compiles the specified script (context-independent).
525 *
526 * NOTE: Pre-compilation using this method cannot happen on another thread
527 * without using Lockers.
528 *
529 * \param source Script source code.
530 */
531 static ScriptData* PreCompile(Handle<String> source);
532
533 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000534 * Load previous pre-compilation data.
535 *
536 * \param data Pointer to data returned by a call to Data() of a previous
537 * ScriptData. Ownership is not transferred.
538 * \param length Length of data.
539 */
540 static ScriptData* New(const char* data, int length);
541
542 /**
543 * Returns the length of Data().
544 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000546
547 /**
548 * Returns a serialized representation of this ScriptData that can later be
549 * passed to New(). NOTE: Serialized data is platform-dependent.
550 */
551 virtual const char* Data() = 0;
552
553 /**
554 * Returns true if the source code could not be parsed.
555 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000556 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557};
558
559
560/**
561 * The origin, within a file, of a script.
562 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000563class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000565 inline ScriptOrigin(
566 Handle<Value> resource_name,
567 Handle<Integer> resource_line_offset = Handle<Integer>(),
568 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 : resource_name_(resource_name),
570 resource_line_offset_(resource_line_offset),
571 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000572 inline Handle<Value> ResourceName() const;
573 inline Handle<Integer> ResourceLineOffset() const;
574 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000576 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 Handle<Integer> resource_line_offset_;
578 Handle<Integer> resource_column_offset_;
579};
580
581
582/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000583 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000585class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000588 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000589 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000590 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000591 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000592 * when New() returns
593 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
594 * using pre_data speeds compilation if it's done multiple times.
595 * Owned by caller, no references are kept when New() returns.
596 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000597 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000598 * available to compile event handlers.
599 * \return Compiled script object (context independent; when run it
600 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 static Local<Script> New(Handle<String> source,
603 ScriptOrigin* origin = NULL,
604 ScriptData* pre_data = NULL,
605 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
mads.s.agercbaa0602008-08-14 13:41:48 +0000607 /**
608 * Compiles the specified script using the specified file name
609 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000610 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000611 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000612 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000613 * as the script's origin.
614 * \return Compiled script object (context independent; when run it
615 * will use the currently entered context).
616 */
617 static Local<Script> New(Handle<String> source,
618 Handle<Value> file_name);
619
620 /**
621 * Compiles the specified script (bound to current context).
622 *
623 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000624 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 * when Compile() returns
626 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
627 * using pre_data speeds compilation if it's done multiple times.
628 * Owned by caller, no references are kept when Compile() returns.
629 * \param script_data Arbitrary data associated with script. Using
630 * this has same effect as calling SetData(), but makes data available
631 * earlier (i.e. to compile event handlers).
632 * \return Compiled script object, bound to the context that was active
633 * when this function was called. When run it will always use this
634 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000635 */
636 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000637 ScriptOrigin* origin = NULL,
638 ScriptData* pre_data = NULL,
639 Handle<String> script_data = Handle<String>());
640
641 /**
642 * Compiles the specified script using the specified file name
643 * object (typically a string) as the script's origin.
644 *
645 * \param source Script source code.
646 * \param file_name File name to use as script's origin
647 * \param script_data Arbitrary data associated with script. Using
648 * this has same effect as calling SetData(), but makes data available
649 * earlier (i.e. to compile event handlers).
650 * \return Compiled script object, bound to the context that was active
651 * when this function was called. When run it will always use this
652 * context.
653 */
654 static Local<Script> Compile(Handle<String> source,
655 Handle<Value> file_name,
656 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000657
v8.team.kasperl727e9952008-09-02 14:56:44 +0000658 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000659 * Runs the script returning the resulting value. If the script is
660 * context independent (created using ::New) it will be run in the
661 * currently entered context. If it is context specific (created
662 * using ::Compile) it will be run in the context in which it was
663 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000664 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000666
667 /**
668 * Returns the script id value.
669 */
670 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000671
672 /**
673 * Associate an additional data object with the script. This is mainly used
674 * with the debugger as this data object is only available through the
675 * debugger API.
676 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000677 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678};
679
680
681/**
682 * An error message.
683 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000684class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000686 Local<String> Get() const;
687 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000689 /**
690 * Returns the resource name for the script from where the function causing
691 * the error originates.
692 */
ager@chromium.org32912102009-01-16 10:38:43 +0000693 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000695 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000696 * Returns the resource data for the script from where the function causing
697 * the error originates.
698 */
699 Handle<Value> GetScriptData() const;
700
701 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000702 * Exception stack trace. By default stack traces are not captured for
703 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
704 * to change this option.
705 */
706 Handle<StackTrace> GetStackTrace() const;
707
708 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000709 * Returns the number, 1-based, of the line where the error occurred.
710 */
ager@chromium.org32912102009-01-16 10:38:43 +0000711 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000713 /**
714 * Returns the index within the script of the first character where
715 * the error occurred.
716 */
ager@chromium.org32912102009-01-16 10:38:43 +0000717 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000718
719 /**
720 * Returns the index within the script of the last character where
721 * the error occurred.
722 */
ager@chromium.org32912102009-01-16 10:38:43 +0000723 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000724
725 /**
726 * Returns the index within the line of the first character where
727 * the error occurred.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000730
731 /**
732 * Returns the index within the line of the last character where
733 * the error occurred.
734 */
ager@chromium.org32912102009-01-16 10:38:43 +0000735 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737 // TODO(1245381): Print to a string instead of on a FILE.
738 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000739
740 static const int kNoLineNumberInfo = 0;
741 static const int kNoColumnInfo = 0;
742};
743
744
745/**
746 * Representation of a JavaScript stack trace. The information collected is a
747 * snapshot of the execution stack and the information remains valid after
748 * execution continues.
749 */
750class V8EXPORT StackTrace {
751 public:
752 /**
753 * Flags that determine what information is placed captured for each
754 * StackFrame when grabbing the current stack trace.
755 */
756 enum StackTraceOptions {
757 kLineNumber = 1,
758 kColumnOffset = 1 << 1 | kLineNumber,
759 kScriptName = 1 << 2,
760 kFunctionName = 1 << 3,
761 kIsEval = 1 << 4,
762 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000763 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000764 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000765 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000766 };
767
768 /**
769 * Returns a StackFrame at a particular index.
770 */
771 Local<StackFrame> GetFrame(uint32_t index) const;
772
773 /**
774 * Returns the number of StackFrames.
775 */
776 int GetFrameCount() const;
777
778 /**
779 * Returns StackTrace as a v8::Array that contains StackFrame objects.
780 */
781 Local<Array> AsArray();
782
783 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000784 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000785 *
786 * \param frame_limit The maximum number of stack frames we want to capture.
787 * \param options Enumerates the set of things we will capture for each
788 * StackFrame.
789 */
790 static Local<StackTrace> CurrentStackTrace(
791 int frame_limit,
792 StackTraceOptions options = kOverview);
793};
794
795
796/**
797 * A single JavaScript stack frame.
798 */
799class V8EXPORT StackFrame {
800 public:
801 /**
802 * Returns the number, 1-based, of the line for the associate function call.
803 * This method will return Message::kNoLineNumberInfo if it is unable to
804 * retrieve the line number, or if kLineNumber was not passed as an option
805 * when capturing the StackTrace.
806 */
807 int GetLineNumber() const;
808
809 /**
810 * Returns the 1-based column offset on the line for the associated function
811 * call.
812 * This method will return Message::kNoColumnInfo if it is unable to retrieve
813 * the column number, or if kColumnOffset was not passed as an option when
814 * capturing the StackTrace.
815 */
816 int GetColumn() const;
817
818 /**
819 * Returns the name of the resource that contains the script for the
820 * function for this StackFrame.
821 */
822 Local<String> GetScriptName() const;
823
824 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000825 * Returns the name of the resource that contains the script for the
826 * function for this StackFrame or sourceURL value if the script name
827 * is undefined and its source ends with //@ sourceURL=... string.
828 */
829 Local<String> GetScriptNameOrSourceURL() const;
830
831 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000832 * Returns the name of the function associated with this stack frame.
833 */
834 Local<String> GetFunctionName() const;
835
836 /**
837 * Returns whether or not the associated function is compiled via a call to
838 * eval().
839 */
840 bool IsEval() const;
841
842 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000843 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000844 * constructor via "new".
845 */
846 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847};
848
849
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000850// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
852
853/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000854 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000856class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 /**
859 * Returns true if this value is the undefined value. See ECMA-262
860 * 4.3.10.
861 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000862 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
864 /**
865 * Returns true if this value is the null value. See ECMA-262
866 * 4.3.11.
867 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000868 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
870 /**
871 * Returns true if this value is true.
872 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000873 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875 /**
876 * Returns true if this value is false.
877 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000878 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
880 /**
881 * Returns true if this value is an instance of the String type.
882 * See ECMA-262 8.4.
883 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000884 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885
886 /**
887 * Returns true if this value is a function.
888 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000889 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
891 /**
892 * Returns true if this value is an array.
893 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000894 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
v8.team.kasperl727e9952008-09-02 14:56:44 +0000896 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 * Returns true if this value is an object.
898 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000899 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
v8.team.kasperl727e9952008-09-02 14:56:44 +0000901 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 * Returns true if this value is boolean.
903 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000904 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000905
v8.team.kasperl727e9952008-09-02 14:56:44 +0000906 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 * Returns true if this value is a number.
908 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000909 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
v8.team.kasperl727e9952008-09-02 14:56:44 +0000911 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 * Returns true if this value is external.
913 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000914 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
v8.team.kasperl727e9952008-09-02 14:56:44 +0000916 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 * Returns true if this value is a 32-bit signed integer.
918 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000919 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000921 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000922 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000923 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000924 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000925
926 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000927 * Returns true if this value is a Date.
928 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000929 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000930
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000931 /**
932 * Returns true if this value is a RegExp.
933 */
934 V8EXPORT bool IsRegExp() const;
935
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000936 V8EXPORT Local<Boolean> ToBoolean() const;
937 V8EXPORT Local<Number> ToNumber() const;
938 V8EXPORT Local<String> ToString() const;
939 V8EXPORT Local<String> ToDetailString() const;
940 V8EXPORT Local<Object> ToObject() const;
941 V8EXPORT Local<Integer> ToInteger() const;
942 V8EXPORT Local<Uint32> ToUint32() const;
943 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
945 /**
946 * Attempts to convert a string to an array index.
947 * Returns an empty handle if the conversion fails.
948 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000949 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000951 V8EXPORT bool BooleanValue() const;
952 V8EXPORT double NumberValue() const;
953 V8EXPORT int64_t IntegerValue() const;
954 V8EXPORT uint32_t Uint32Value() const;
955 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
957 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000958 V8EXPORT bool Equals(Handle<Value> that) const;
959 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000960
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000961 private:
962 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000963 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964};
965
966
967/**
968 * The superclass of primitive values. See ECMA-262 4.3.2.
969 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000970class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
972
973/**
974 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
975 * or false value.
976 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000977class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000979 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 static inline Handle<Boolean> New(bool value);
981};
982
983
984/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000985 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000987class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000989 /**
990 * Returns the number of characters in this string.
991 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000992 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993
v8.team.kasperl727e9952008-09-02 14:56:44 +0000994 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000995 * Returns the number of bytes in the UTF-8 encoded
996 * representation of this string.
997 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000998 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000999
1000 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001001 * Write the contents of the string to an external buffer.
1002 * If no arguments are given, expects the buffer to be large
1003 * enough to hold the entire string and NULL terminator. Copies
1004 * the contents of the string and the NULL terminator into the
1005 * buffer.
1006 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001007 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1008 * before the end of the buffer.
1009 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001010 * Copies up to length characters into the output buffer.
1011 * Only null-terminates if there is enough space in the buffer.
1012 *
1013 * \param buffer The buffer into which the string will be copied.
1014 * \param start The starting position within the string at which
1015 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001016 * \param length The number of characters to copy from the string. For
1017 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001018 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001019 * \param hints Various hints that might affect performance of this or
1020 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001021 * \return The number of characters copied to the buffer excluding the null
1022 * terminator. For WriteUtf8: The number of bytes copied to the buffer
1023 * including the null terminator.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001024 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001025 enum WriteHints {
1026 NO_HINTS = 0,
1027 HINT_MANY_WRITES_EXPECTED = 1
1028 };
1029
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001030 V8EXPORT int Write(uint16_t* buffer,
1031 int start = 0,
1032 int length = -1,
1033 WriteHints hints = NO_HINTS) const; // UTF-16
1034 V8EXPORT int WriteAscii(char* buffer,
1035 int start = 0,
1036 int length = -1,
1037 WriteHints hints = NO_HINTS) const; // ASCII
1038 V8EXPORT int WriteUtf8(char* buffer,
1039 int length = -1,
1040 int* nchars_ref = NULL,
1041 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042
v8.team.kasperl727e9952008-09-02 14:56:44 +00001043 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001044 * A zero length string.
1045 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001046 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001047
1048 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001049 * Returns true if the string is external
1050 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001051 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
v8.team.kasperl727e9952008-09-02 14:56:44 +00001053 /**
1054 * Returns true if the string is both external and ascii
1055 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001056 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001057
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001058 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001059 public:
1060 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001061
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001062 protected:
1063 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001064
1065 /**
1066 * Internally V8 will call this Dispose method when the external string
1067 * resource is no longer needed. The default implementation will use the
1068 * delete operator. This method can be overridden in subclasses to
1069 * control how allocated external string resources are disposed.
1070 */
1071 virtual void Dispose() { delete this; }
1072
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001073 private:
1074 // Disallow copying and assigning.
1075 ExternalStringResourceBase(const ExternalStringResourceBase&);
1076 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001077
1078 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001079 };
1080
v8.team.kasperl727e9952008-09-02 14:56:44 +00001081 /**
1082 * An ExternalStringResource is a wrapper around a two-byte string
1083 * buffer that resides outside V8's heap. Implement an
1084 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001085 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001087 class V8EXPORT ExternalStringResource
1088 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
1090 /**
1091 * Override the destructor to manage the life cycle of the underlying
1092 * buffer.
1093 */
1094 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001095
1096 /**
1097 * The string data from the underlying buffer.
1098 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001100
1101 /**
1102 * The length of the string. That is, the number of two-byte characters.
1103 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 protected:
1107 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 };
1109
1110 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001111 * An ExternalAsciiStringResource is a wrapper around an ascii
1112 * string buffer that resides outside V8's heap. Implement an
1113 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001114 * underlying buffer. Note that the string data must be immutable
1115 * and that the data must be strict 7-bit ASCII, not Latin1 or
1116 * UTF-8, which would require special treatment internally in the
1117 * engine and, in the case of UTF-8, do not allow efficient indexing.
1118 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001119 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001121 class V8EXPORT ExternalAsciiStringResource
1122 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 public:
1124 /**
1125 * Override the destructor to manage the life cycle of the underlying
1126 * buffer.
1127 */
1128 virtual ~ExternalAsciiStringResource() {}
1129 /** The string data from the underlying buffer.*/
1130 virtual const char* data() const = 0;
1131 /** The number of ascii characters in the string.*/
1132 virtual size_t length() const = 0;
1133 protected:
1134 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 };
1136
1137 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001138 * Get the ExternalStringResource for an external string. Returns
1139 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001141 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142
1143 /**
1144 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001145 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001147 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001149 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150
1151 /**
1152 * Allocates a new string from either utf-8 encoded or ascii data.
1153 * The second parameter 'length' gives the buffer length.
1154 * If the data is utf-8 encoded, the caller must
1155 * be careful to supply the length parameter.
1156 * If it is not given, the function calls
1157 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001158 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001160 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161
1162 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001163 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164
1165 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001166 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
v8.team.kasperl727e9952008-09-02 14:56:44 +00001168 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001169 * Creates a new string by concatenating the left and the right strings
1170 * passed in as parameters.
1171 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001172 V8EXPORT static Local<String> Concat(Handle<String> left,
1173 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001174
1175 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001176 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001177 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001178 * resource will be disposed by calling its Dispose method. The caller of
1179 * this function should not otherwise delete or modify the resource. Neither
1180 * should the underlying buffer be deallocated or modified except through the
1181 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001182 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001183 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001184
ager@chromium.org6f10e412009-02-13 10:11:16 +00001185 /**
1186 * Associate an external string resource with this string by transforming it
1187 * in place so that existing references to this string in the JavaScript heap
1188 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001189 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001190 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001191 * The string is not modified if the operation fails. See NewExternal for
1192 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001193 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001194 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196 /**
1197 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001198 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001199 * resource will be disposed by calling its Dispose method. The caller of
1200 * this function should not otherwise delete or modify the resource. Neither
1201 * should the underlying buffer be deallocated or modified except through the
1202 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001203 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001204 V8EXPORT static Local<String> NewExternal(
1205 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001206
ager@chromium.org6f10e412009-02-13 10:11:16 +00001207 /**
1208 * Associate an external string resource with this string by transforming it
1209 * in place so that existing references to this string in the JavaScript heap
1210 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001211 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001212 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001213 * The string is not modified if the operation fails. See NewExternal for
1214 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001215 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001216 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001218 /**
1219 * Returns true if this string can be made external.
1220 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001221 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001222
kasper.lund7276f142008-07-30 08:49:36 +00001223 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001224 V8EXPORT static Local<String> NewUndetectable(const char* data,
1225 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226
kasper.lund7276f142008-07-30 08:49:36 +00001227 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001228 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1229 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230
1231 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001232 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001233 * you want to print the object. If conversion to a string fails
1234 * (eg. due to an exception in the toString() method of the object)
1235 * then the length() method returns 0 and the * operator returns
1236 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001237 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001238 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001239 public:
1240 explicit Utf8Value(Handle<v8::Value> obj);
1241 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001242 char* operator*() { return str_; }
1243 const char* operator*() const { return str_; }
1244 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001245 private:
1246 char* str_;
1247 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001248
1249 // Disallow copying and assigning.
1250 Utf8Value(const Utf8Value&);
1251 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001252 };
1253
1254 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 * Converts an object to an ascii string.
1256 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001257 * If conversion to a string fails (eg. due to an exception in the toString()
1258 * method of the object) then the length() method returns 0 and the * operator
1259 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001261 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 public:
1263 explicit AsciiValue(Handle<v8::Value> obj);
1264 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001265 char* operator*() { return str_; }
1266 const char* operator*() const { return str_; }
1267 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 private:
1269 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001270 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001271
1272 // Disallow copying and assigning.
1273 AsciiValue(const AsciiValue&);
1274 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 };
1276
1277 /**
1278 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001279 * If conversion to a string fails (eg. due to an exception in the toString()
1280 * method of the object) then the length() method returns 0 and the * operator
1281 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001283 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 public:
1285 explicit Value(Handle<v8::Value> obj);
1286 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001287 uint16_t* operator*() { return str_; }
1288 const uint16_t* operator*() const { return str_; }
1289 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 private:
1291 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001292 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001293
1294 // Disallow copying and assigning.
1295 Value(const Value&);
1296 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001298
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001299 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001300 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1301 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302};
1303
1304
1305/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001306 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001308class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001310 V8EXPORT double Value() const;
1311 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001312 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001314 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001315 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316};
1317
1318
1319/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001320 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001322class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001324 V8EXPORT static Local<Integer> New(int32_t value);
1325 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1326 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001327 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001329 V8EXPORT Integer();
1330 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331};
1332
1333
1334/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001335 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001337class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001339 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001341 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342};
1343
1344
1345/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001346 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001348class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001350 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001352 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353};
1354
1355
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356enum PropertyAttribute {
1357 None = 0,
1358 ReadOnly = 1 << 0,
1359 DontEnum = 1 << 1,
1360 DontDelete = 1 << 2
1361};
1362
ager@chromium.org3811b432009-10-28 14:53:37 +00001363enum ExternalArrayType {
1364 kExternalByteArray = 1,
1365 kExternalUnsignedByteArray,
1366 kExternalShortArray,
1367 kExternalUnsignedShortArray,
1368 kExternalIntArray,
1369 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001370 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001371 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001372 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001373};
1374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001376 * Accessor[Getter|Setter] are used as callback functions when
1377 * setting|getting a particular property. See Object and ObjectTemplate's
1378 * method SetAccessor.
1379 */
1380typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1381 const AccessorInfo& info);
1382
1383
1384typedef void (*AccessorSetter)(Local<String> property,
1385 Local<Value> value,
1386 const AccessorInfo& info);
1387
1388
1389/**
1390 * Access control specifications.
1391 *
1392 * Some accessors should be accessible across contexts. These
1393 * accessors have an explicit access control parameter which specifies
1394 * the kind of cross-context access that should be allowed.
1395 *
1396 * Additionally, for security, accessors can prohibit overwriting by
1397 * accessors defined in JavaScript. For objects that have such
1398 * accessors either locally or in their prototype chain it is not
1399 * possible to overwrite the accessor by using __defineGetter__ or
1400 * __defineSetter__ from JavaScript code.
1401 */
1402enum AccessControl {
1403 DEFAULT = 0,
1404 ALL_CAN_READ = 1,
1405 ALL_CAN_WRITE = 1 << 1,
1406 PROHIBITS_OVERWRITING = 1 << 2
1407};
1408
1409
1410/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001411 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001413class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001415 V8EXPORT bool Set(Handle<Value> key,
1416 Handle<Value> value,
1417 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001418
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001419 V8EXPORT bool Set(uint32_t index,
1420 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001421
ager@chromium.orge2902be2009-06-08 12:21:35 +00001422 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001423 // overriding accessors or read-only properties.
1424 //
1425 // Note that if the object has an interceptor the property will be set
1426 // locally, but since the interceptor takes precedence the local property
1427 // will only be returned if the interceptor doesn't return a value.
1428 //
1429 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001430 V8EXPORT bool ForceSet(Handle<Value> key,
1431 Handle<Value> value,
1432 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001433
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001434 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001436 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 // TODO(1245389): Replace the type-specific versions of these
1439 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001440 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001441
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001442 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001443
1444 // Delete a property on this object bypassing interceptors and
1445 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001446 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001447
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001448 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001449
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001450 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001452 V8EXPORT bool SetAccessor(Handle<String> name,
1453 AccessorGetter getter,
1454 AccessorSetter setter = 0,
1455 Handle<Value> data = Handle<Value>(),
1456 AccessControl settings = DEFAULT,
1457 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001460 * Returns an array containing the names of the enumerable properties
1461 * of this object, including properties from prototype objects. The
1462 * array returned by this method contains the same values as would
1463 * be enumerated by a for-in statement over this object.
1464 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001465 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001466
1467 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001468 * This function has the same functionality as GetPropertyNames but
1469 * the returned array doesn't contain the names of properties from
1470 * prototype objects.
1471 */
1472 V8EXPORT Local<Array> GetOwnPropertyNames();
1473
1474 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 * Get the prototype object. This does not skip objects marked to
1476 * be skipped by __proto__ and it does not consult the security
1477 * handler.
1478 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001479 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480
1481 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001482 * Set the prototype object. This does not skip objects marked to
1483 * be skipped by __proto__ and it does not consult the security
1484 * handler.
1485 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001486 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001487
1488 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001489 * Finds an instance of the given function template in the prototype
1490 * chain.
1491 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001492 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1493 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001494
1495 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 * Call builtin Object.prototype.toString on this object.
1497 * This is different from Value::ToString() that may call
1498 * user-defined toString function. This one does not.
1499 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001500 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001502 /**
1503 * Returns the name of the function invoked as a constructor for this object.
1504 */
1505 V8EXPORT Local<String> GetConstructorName();
1506
kasper.lund212ac232008-07-16 07:07:30 +00001507 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001508 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001509 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001510 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001511 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001512 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001514 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001515 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001516
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001517 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001518 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001519
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001521 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001522 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1523 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1524 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525
1526 /**
1527 * If result.IsEmpty() no real property was located in the prototype chain.
1528 * This means interceptors in the prototype chain are not called.
1529 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001530 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1531 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001532
1533 /**
1534 * If result.IsEmpty() no real property was located on the object or
1535 * in the prototype chain.
1536 * This means interceptors in the prototype chain are not called.
1537 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001538 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539
1540 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001541 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542
kasper.lund212ac232008-07-16 07:07:30 +00001543 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001544 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001546 /**
1547 * Turns on access check on the object if the object is an instance of
1548 * a template that has access check callbacks. If an object has no
1549 * access check info, the object cannot be accessed by anyone.
1550 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001551 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001552
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001553 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001554 * Returns the identity hash for this object. The current implementation
1555 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001556 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001557 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001558 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001559 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001560 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001561
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001562 /**
1563 * Access hidden properties on JavaScript objects. These properties are
1564 * hidden from the executing JavaScript and only accessible through the V8
1565 * C++ API. Hidden properties introduced by V8 internally (for example the
1566 * identity hash) are prefixed with "v8::".
1567 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1569 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1570 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001571
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001572 /**
1573 * Returns true if this is an instance of an api function (one
1574 * created from a function created from a function template) and has
1575 * been modified since it was created. Note that this method is
1576 * conservative and may return true for objects that haven't actually
1577 * been modified.
1578 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001579 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001580
1581 /**
1582 * Clone this object with a fast but shallow copy. Values will point
1583 * to the same values as the original object.
1584 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001585 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001587 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001588 * Returns the context in which the object was created.
1589 */
1590 V8EXPORT Local<Context> CreationContext();
1591
1592 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001593 * Set the backing store of the indexed properties to be managed by the
1594 * embedding layer. Access to the indexed properties will follow the rules
1595 * spelled out in CanvasPixelArray.
1596 * Note: The embedding program still owns the data and needs to ensure that
1597 * the backing store is preserved while V8 has a reference.
1598 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001599 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001600 V8EXPORT bool HasIndexedPropertiesInPixelData();
1601 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1602 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001603
ager@chromium.org3811b432009-10-28 14:53:37 +00001604 /**
1605 * Set the backing store of the indexed properties to be managed by the
1606 * embedding layer. Access to the indexed properties will follow the rules
1607 * spelled out for the CanvasArray subtypes in the WebGL specification.
1608 * Note: The embedding program still owns the data and needs to ensure that
1609 * the backing store is preserved while V8 has a reference.
1610 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001611 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1612 void* data,
1613 ExternalArrayType array_type,
1614 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001615 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1616 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1617 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1618 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001619
lrn@chromium.org1c092762011-05-09 09:42:16 +00001620 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001621 * Checks whether a callback is set by the
1622 * ObjectTemplate::SetCallAsFunctionHandler method.
1623 * When an Object is callable this method returns true.
1624 */
1625 V8EXPORT bool IsCallable();
1626
1627 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00001628 * Call an Object as a function if a callback is set by the
1629 * ObjectTemplate::SetCallAsFunctionHandler method.
1630 */
1631 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1632 int argc,
1633 Handle<Value> argv[]);
1634
1635 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001636 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001637 * ObjectTemplate::SetCallAsFunctionHandler method.
1638 * Note: This method behaves like the Function::NewInstance method.
1639 */
1640 V8EXPORT Local<Value> CallAsConstructor(int argc,
1641 Handle<Value> argv[]);
1642
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001643 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001644 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001647 V8EXPORT Object();
1648 V8EXPORT static void CheckCast(Value* obj);
1649 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1650 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001651
1652 /**
1653 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001654 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001655 */
1656 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657};
1658
1659
1660/**
1661 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1662 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001663class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001665 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666
ager@chromium.org3e875802009-06-29 08:26:34 +00001667 /**
1668 * Clones an element at index |index|. Returns an empty
1669 * handle if cloning fails (for any reason).
1670 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001671 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001672
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001673 /**
1674 * Creates a JavaScript array with the given length. If the length
1675 * is negative the returned array will have length 0.
1676 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001677 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001678
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001679 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001681 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001682 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683};
1684
1685
1686/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001687 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001689class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001691 V8EXPORT Local<Object> NewInstance() const;
1692 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1693 V8EXPORT Local<Value> Call(Handle<Object> recv,
1694 int argc,
1695 Handle<Value> argv[]);
1696 V8EXPORT void SetName(Handle<String> name);
1697 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001698
1699 /**
1700 * Returns zero based line number of function body and
1701 * kLineOffsetNotFound if no information available.
1702 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001703 V8EXPORT int GetScriptLineNumber() const;
1704 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001705 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001708 V8EXPORT Function();
1709 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710};
1711
1712
1713/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001714 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1715 */
1716class Date : public Object {
1717 public:
1718 V8EXPORT static Local<Value> New(double time);
1719
1720 /**
1721 * A specialization of Value::NumberValue that is more efficient
1722 * because we know the structure of this object.
1723 */
1724 V8EXPORT double NumberValue() const;
1725
1726 static inline Date* Cast(v8::Value* obj);
1727
1728 /**
1729 * Notification that the embedder has changed the time zone,
1730 * daylight savings time, or other date / time configuration
1731 * parameters. V8 keeps a cache of various values used for
1732 * date / time computation. This notification will reset
1733 * those cached values for the current context so that date /
1734 * time configuration changes would be reflected in the Date
1735 * object.
1736 *
1737 * This API should not be called more than needed as it will
1738 * negatively impact the performance of date operations.
1739 */
1740 V8EXPORT static void DateTimeConfigurationChangeNotification();
1741
1742 private:
1743 V8EXPORT static void CheckCast(v8::Value* obj);
1744};
1745
1746
1747/**
1748 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1749 */
1750class RegExp : public Object {
1751 public:
1752 /**
1753 * Regular expression flag bits. They can be or'ed to enable a set
1754 * of flags.
1755 */
1756 enum Flags {
1757 kNone = 0,
1758 kGlobal = 1,
1759 kIgnoreCase = 2,
1760 kMultiline = 4
1761 };
1762
1763 /**
1764 * Creates a regular expression from the given pattern string and
1765 * the flags bit field. May throw a JavaScript exception as
1766 * described in ECMA-262, 15.10.4.1.
1767 *
1768 * For example,
1769 * RegExp::New(v8::String::New("foo"),
1770 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1771 * is equivalent to evaluating "/foo/gm".
1772 */
1773 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1774 Flags flags);
1775
1776 /**
1777 * Returns the value of the source property: a string representing
1778 * the regular expression.
1779 */
1780 V8EXPORT Local<String> GetSource() const;
1781
1782 /**
1783 * Returns the flags bit field.
1784 */
1785 V8EXPORT Flags GetFlags() const;
1786
1787 static inline RegExp* Cast(v8::Value* obj);
1788
1789 private:
1790 V8EXPORT static void CheckCast(v8::Value* obj);
1791};
1792
1793
1794/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001795 * A JavaScript value that wraps a C++ void*. This type of value is
1796 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001798 *
1799 * The Wrap function V8 will return the most optimal Value object wrapping the
1800 * C++ void*. The type of the value is not guaranteed to be an External object
1801 * and no assumptions about its type should be made. To access the wrapped
1802 * value Unwrap should be used, all other operations on that object will lead
1803 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001805class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001807 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001808 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001809
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001810 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001811 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001812 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001814 V8EXPORT External();
1815 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001816 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001817 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818};
1819
1820
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001821// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
1823
1824/**
1825 * The superclass of object and function templates.
1826 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001827class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 public:
1829 /** Adds a property to each instance created by this template.*/
1830 void Set(Handle<String> name, Handle<Data> value,
1831 PropertyAttribute attributes = None);
1832 inline void Set(const char* name, Handle<Data> value);
1833 private:
1834 Template();
1835
1836 friend class ObjectTemplate;
1837 friend class FunctionTemplate;
1838};
1839
1840
1841/**
1842 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001843 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 * including the receiver, the number and values of arguments, and
1845 * the holder of the function.
1846 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001847class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 public:
1849 inline int Length() const;
1850 inline Local<Value> operator[](int i) const;
1851 inline Local<Function> Callee() const;
1852 inline Local<Object> This() const;
1853 inline Local<Object> Holder() const;
1854 inline bool IsConstructCall() const;
1855 inline Local<Value> Data() const;
1856 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001857 static const int kDataIndex = 0;
1858 static const int kCalleeIndex = -1;
1859 static const int kHolderIndex = -2;
1860
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001862 inline Arguments(internal::Object** implicit_args,
1863 internal::Object** values,
1864 int length,
1865 bool is_construct_call);
1866 internal::Object** implicit_args_;
1867 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001869 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870};
1871
1872
1873/**
1874 * The information passed to an accessor callback about the context
1875 * of the property access.
1876 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001877class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001879 inline AccessorInfo(internal::Object** args)
1880 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 inline Local<Value> Data() const;
1882 inline Local<Object> This() const;
1883 inline Local<Object> Holder() const;
1884 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001885 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886};
1887
1888
1889typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 * NamedProperty[Getter|Setter] are used as interceptors on object.
1893 * See ObjectTemplate::SetNamedPropertyHandler.
1894 */
1895typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1896 const AccessorInfo& info);
1897
1898
1899/**
1900 * Returns the value if the setter intercepts the request.
1901 * Otherwise, returns an empty handle.
1902 */
1903typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1904 Local<Value> value,
1905 const AccessorInfo& info);
1906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907/**
1908 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001909 * The result is an integer encoding property attributes (like v8::None,
1910 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001912typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1913 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914
1915
1916/**
1917 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001918 * The return value is true if the property could be deleted and false
1919 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 */
1921typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1922 const AccessorInfo& info);
1923
1924/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001925 * Returns an array containing the names of the properties the named
1926 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 */
1928typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1929
v8.team.kasperl727e9952008-09-02 14:56:44 +00001930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001932 * Returns the value of the property if the getter intercepts the
1933 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 */
1935typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1936 const AccessorInfo& info);
1937
1938
1939/**
1940 * Returns the value if the setter intercepts the request.
1941 * Otherwise, returns an empty handle.
1942 */
1943typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1944 Local<Value> value,
1945 const AccessorInfo& info);
1946
1947
1948/**
1949 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001950 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001952typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1953 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954
1955/**
1956 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 * The return value is true if the property could be deleted and false
1958 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 */
1960typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1961 const AccessorInfo& info);
1962
v8.team.kasperl727e9952008-09-02 14:56:44 +00001963/**
1964 * Returns an array containing the indices of the properties the
1965 * indexed property getter intercepts.
1966 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1968
1969
1970/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001971 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972 */
1973enum AccessType {
1974 ACCESS_GET,
1975 ACCESS_SET,
1976 ACCESS_HAS,
1977 ACCESS_DELETE,
1978 ACCESS_KEYS
1979};
1980
v8.team.kasperl727e9952008-09-02 14:56:44 +00001981
1982/**
1983 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001984 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001985 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001986typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 Local<Value> key,
1988 AccessType type,
1989 Local<Value> data);
1990
v8.team.kasperl727e9952008-09-02 14:56:44 +00001991
1992/**
1993 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001994 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001995 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001996typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 uint32_t index,
1998 AccessType type,
1999 Local<Value> data);
2000
2001
2002/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002003 * A FunctionTemplate is used to create functions at runtime. There
2004 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002005 * context. The lifetime of the created function is equal to the
2006 * lifetime of the context. So in case the embedder needs to create
2007 * temporary functions that can be collected using Scripts is
2008 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 *
2010 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002011 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002013 * A FunctionTemplate has a corresponding instance template which is
2014 * used to create object instances when the function is used as a
2015 * constructor. Properties added to the instance template are added to
2016 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 *
2018 * A FunctionTemplate can have a prototype template. The prototype template
2019 * is used to create the prototype object of the function.
2020 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002021 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002023 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2025 * t->Set("func_property", v8::Number::New(1));
2026 *
2027 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2028 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2029 * proto_t->Set("proto_const", v8::Number::New(2));
2030 *
2031 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2032 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2033 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2034 * instance_t->Set("instance_property", Number::New(3));
2035 *
2036 * v8::Local<v8::Function> function = t->GetFunction();
2037 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 *
2040 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002041 * and "instance" for the instance object created above. The function
2042 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002044 * \code
2045 * func_property in function == true;
2046 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 * function.prototype.proto_method() invokes 'InvokeCallback'
2049 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002051 * instance instanceof function == true;
2052 * instance.instance_accessor calls 'InstanceAccessorCallback'
2053 * instance.instance_property == 3;
2054 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002056 * A FunctionTemplate can inherit from another one by calling the
2057 * FunctionTemplate::Inherit method. The following graph illustrates
2058 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002060 * \code
2061 * FunctionTemplate Parent -> Parent() . prototype -> { }
2062 * ^ ^
2063 * | Inherit(Parent) | .__proto__
2064 * | |
2065 * FunctionTemplate Child -> Child() . prototype -> { }
2066 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002068 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2069 * object of the Child() function has __proto__ pointing to the
2070 * Parent() function's prototype object. An instance of the Child
2071 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * Let Parent be the FunctionTemplate initialized in the previous
2074 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002076 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 * Local<FunctionTemplate> parent = t;
2078 * Local<FunctionTemplate> child = FunctionTemplate::New();
2079 * child->Inherit(parent);
2080 *
2081 * Local<Function> child_function = child->GetFunction();
2082 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002085 * The Child function and Child instance will have the following
2086 * properties:
2087 *
2088 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002090 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002092 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002094class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 public:
2096 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002097 static Local<FunctionTemplate> New(
2098 InvocationCallback callback = 0,
2099 Handle<Value> data = Handle<Value>(),
2100 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 /** Returns the unique function instance in the current execution context.*/
2102 Local<Function> GetFunction();
2103
v8.team.kasperl727e9952008-09-02 14:56:44 +00002104 /**
2105 * Set the call-handler callback for a FunctionTemplate. This
2106 * callback is called whenever the function created from this
2107 * FunctionTemplate is called.
2108 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 void SetCallHandler(InvocationCallback callback,
2110 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 Local<ObjectTemplate> InstanceTemplate();
2114
2115 /** Causes the function template to inherit from a parent function template.*/
2116 void Inherit(Handle<FunctionTemplate> parent);
2117
2118 /**
2119 * A PrototypeTemplate is the template used to create the prototype object
2120 * of the function created by this template.
2121 */
2122 Local<ObjectTemplate> PrototypeTemplate();
2123
v8.team.kasperl727e9952008-09-02 14:56:44 +00002124
2125 /**
2126 * Set the class name of the FunctionTemplate. This is used for
2127 * printing objects created with the function created from the
2128 * FunctionTemplate as its constructor.
2129 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 void SetClassName(Handle<String> name);
2131
2132 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002133 * Determines whether the __proto__ accessor ignores instances of
2134 * the function template. If instances of the function template are
2135 * ignored, __proto__ skips all instances and instead returns the
2136 * next object in the prototype chain.
2137 *
2138 * Call with a value of true to make the __proto__ accessor ignore
2139 * instances of the function template. Call with a value of false
2140 * to make the __proto__ accessor not ignore instances of the
2141 * function template. By default, instances of a function template
2142 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 */
2144 void SetHiddenPrototype(bool value);
2145
2146 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002147 * Returns true if the given object is an instance of this function
2148 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 */
2150 bool HasInstance(Handle<Value> object);
2151
2152 private:
2153 FunctionTemplate();
2154 void AddInstancePropertyAccessor(Handle<String> name,
2155 AccessorGetter getter,
2156 AccessorSetter setter,
2157 Handle<Value> data,
2158 AccessControl settings,
2159 PropertyAttribute attributes);
2160 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2161 NamedPropertySetter setter,
2162 NamedPropertyQuery query,
2163 NamedPropertyDeleter remover,
2164 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002165 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2167 IndexedPropertySetter setter,
2168 IndexedPropertyQuery query,
2169 IndexedPropertyDeleter remover,
2170 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002171 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2173 Handle<Value> data);
2174
2175 friend class Context;
2176 friend class ObjectTemplate;
2177};
2178
2179
2180/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002181 * An ObjectTemplate is used to create objects at runtime.
2182 *
2183 * Properties added to an ObjectTemplate are added to each object
2184 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002186class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 /** Creates a new instance of this template.*/
2192 Local<Object> NewInstance();
2193
2194 /**
2195 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 *
2197 * Whenever the property with the given name is accessed on objects
2198 * created from this ObjectTemplate the getter and setter callbacks
2199 * are called instead of getting and setting the property directly
2200 * on the JavaScript object.
2201 *
2202 * \param name The name of the property for which an accessor is added.
2203 * \param getter The callback to invoke when getting the property.
2204 * \param setter The callback to invoke when setting the property.
2205 * \param data A piece of data that will be passed to the getter and setter
2206 * callbacks whenever they are invoked.
2207 * \param settings Access control settings for the accessor. This is a bit
2208 * field consisting of one of more of
2209 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2210 * The default is to not allow cross-context access.
2211 * ALL_CAN_READ means that all cross-context reads are allowed.
2212 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2213 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2214 * cross-context access.
2215 * \param attribute The attributes of the property for which an accessor
2216 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 */
2218 void SetAccessor(Handle<String> name,
2219 AccessorGetter getter,
2220 AccessorSetter setter = 0,
2221 Handle<Value> data = Handle<Value>(),
2222 AccessControl settings = DEFAULT,
2223 PropertyAttribute attribute = None);
2224
2225 /**
2226 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002227 *
2228 * Whenever a named property is accessed on objects created from
2229 * this object template, the provided callback is invoked instead of
2230 * accessing the property directly on the JavaScript object.
2231 *
2232 * \param getter The callback to invoke when getting a property.
2233 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002234 * \param query The callback to invoke to check if a property is present,
2235 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002236 * \param deleter The callback to invoke when deleting a property.
2237 * \param enumerator The callback to invoke to enumerate all the named
2238 * properties of an object.
2239 * \param data A piece of data that will be passed to the callbacks
2240 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 */
2242 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2243 NamedPropertySetter setter = 0,
2244 NamedPropertyQuery query = 0,
2245 NamedPropertyDeleter deleter = 0,
2246 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002247 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248
2249 /**
2250 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002251 *
2252 * Whenever an indexed property is accessed on objects created from
2253 * this object template, the provided callback is invoked instead of
2254 * accessing the property directly on the JavaScript object.
2255 *
2256 * \param getter The callback to invoke when getting a property.
2257 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002258 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002259 * \param deleter The callback to invoke when deleting a property.
2260 * \param enumerator The callback to invoke to enumerate all the indexed
2261 * properties of an object.
2262 * \param data A piece of data that will be passed to the callbacks
2263 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 */
2265 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2266 IndexedPropertySetter setter = 0,
2267 IndexedPropertyQuery query = 0,
2268 IndexedPropertyDeleter deleter = 0,
2269 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002270 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 /**
2273 * Sets the callback to be used when calling instances created from
2274 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002275 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 * function.
2277 */
2278 void SetCallAsFunctionHandler(InvocationCallback callback,
2279 Handle<Value> data = Handle<Value>());
2280
v8.team.kasperl727e9952008-09-02 14:56:44 +00002281 /**
2282 * Mark object instances of the template as undetectable.
2283 *
2284 * In many ways, undetectable objects behave as though they are not
2285 * there. They behave like 'undefined' in conditionals and when
2286 * printed. However, properties can be accessed and called as on
2287 * normal objects.
2288 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 void MarkAsUndetectable();
2290
v8.team.kasperl727e9952008-09-02 14:56:44 +00002291 /**
2292 * Sets access check callbacks on the object template.
2293 *
2294 * When accessing properties on instances of this object template,
2295 * the access check callback will be called to determine whether or
2296 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002297 * The last parameter specifies whether access checks are turned
2298 * on by default on instances. If access checks are off by default,
2299 * they can be turned on on individual instances by calling
2300 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002301 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2303 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002304 Handle<Value> data = Handle<Value>(),
2305 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306
kasper.lund212ac232008-07-16 07:07:30 +00002307 /**
2308 * Gets the number of internal fields for objects generated from
2309 * this template.
2310 */
2311 int InternalFieldCount();
2312
2313 /**
2314 * Sets the number of internal fields for objects generated from
2315 * this template.
2316 */
2317 void SetInternalFieldCount(int value);
2318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 private:
2320 ObjectTemplate();
2321 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2322 friend class FunctionTemplate;
2323};
2324
2325
2326/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002327 * A Signature specifies which receivers and arguments a function can
2328 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002330class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 public:
2332 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2333 Handle<FunctionTemplate>(),
2334 int argc = 0,
2335 Handle<FunctionTemplate> argv[] = 0);
2336 private:
2337 Signature();
2338};
2339
2340
2341/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002342 * A utility for determining the type of objects based on the template
2343 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002345class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 public:
2347 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2348 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2349 int match(Handle<Value> value);
2350 private:
2351 TypeSwitch();
2352};
2353
2354
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002355// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356
2357
2358/**
2359 * Ignore
2360 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002361class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 public:
2363 Extension(const char* name,
2364 const char* source = 0,
2365 int dep_count = 0,
2366 const char** deps = 0);
2367 virtual ~Extension() { }
2368 virtual v8::Handle<v8::FunctionTemplate>
2369 GetNativeFunction(v8::Handle<v8::String> name) {
2370 return v8::Handle<v8::FunctionTemplate>();
2371 }
2372
2373 const char* name() { return name_; }
2374 const char* source() { return source_; }
2375 int dependency_count() { return dep_count_; }
2376 const char** dependencies() { return deps_; }
2377 void set_auto_enable(bool value) { auto_enable_ = value; }
2378 bool auto_enable() { return auto_enable_; }
2379
2380 private:
2381 const char* name_;
2382 const char* source_;
2383 int dep_count_;
2384 const char** deps_;
2385 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002386
2387 // Disallow copying and assigning.
2388 Extension(const Extension&);
2389 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390};
2391
2392
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002393void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394
2395
2396/**
2397 * Ignore
2398 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002399class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002400 public:
2401 inline DeclareExtension(Extension* extension) {
2402 RegisterExtension(extension);
2403 }
2404};
2405
2406
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002407// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002408
2409
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002410Handle<Primitive> V8EXPORT Undefined();
2411Handle<Primitive> V8EXPORT Null();
2412Handle<Boolean> V8EXPORT True();
2413Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414
2415
2416/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002417 * A set of constraints that specifies the limits of the runtime's memory use.
2418 * You must set the heap size before initializing the VM - the size cannot be
2419 * adjusted after the VM is initialized.
2420 *
2421 * If you are using threads then you should hold the V8::Locker lock while
2422 * setting the stack limit and you must set a non-default stack limit separately
2423 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002425class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 public:
2427 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002428 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002430 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002432 int max_executable_size() { return max_executable_size_; }
2433 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002434 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002435 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2437 private:
2438 int max_young_space_size_;
2439 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002440 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 uint32_t* stack_limit_;
2442};
2443
2444
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002445bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446
2447
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002448// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449
2450
2451typedef void (*FatalErrorCallback)(const char* location, const char* message);
2452
2453
2454typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2455
2456
2457/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002458 * Schedules an exception to be thrown when returning to JavaScript. When an
2459 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002461 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002463Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464
2465/**
2466 * Create new error objects by calling the corresponding error object
2467 * constructor with the message.
2468 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002469class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 public:
2471 static Local<Value> RangeError(Handle<String> message);
2472 static Local<Value> ReferenceError(Handle<String> message);
2473 static Local<Value> SyntaxError(Handle<String> message);
2474 static Local<Value> TypeError(Handle<String> message);
2475 static Local<Value> Error(Handle<String> message);
2476};
2477
2478
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002479// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002481typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002483typedef void* (*CreateHistogramCallback)(const char* name,
2484 int min,
2485 int max,
2486 size_t buckets);
2487
2488typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2489
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002490// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002491 enum ObjectSpace {
2492 kObjectSpaceNewSpace = 1 << 0,
2493 kObjectSpaceOldPointerSpace = 1 << 1,
2494 kObjectSpaceOldDataSpace = 1 << 2,
2495 kObjectSpaceCodeSpace = 1 << 3,
2496 kObjectSpaceMapSpace = 1 << 4,
2497 kObjectSpaceLoSpace = 1 << 5,
2498
2499 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2500 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2501 kObjectSpaceLoSpace
2502 };
2503
2504 enum AllocationAction {
2505 kAllocationActionAllocate = 1 << 0,
2506 kAllocationActionFree = 1 << 1,
2507 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2508 };
2509
2510typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2511 AllocationAction action,
2512 int size);
2513
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002514// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2516 AccessType type,
2517 Local<Value> data);
2518
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002519// --- AllowCodeGenerationFromStrings callbacks ---
2520
2521/**
2522 * Callback to check if code generation from strings is allowed. See
2523 * Context::AllowCodeGenerationFromStrings.
2524 */
2525typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2526
2527// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528
2529/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002530 * Applications can register callback functions which will be called
2531 * before and after a garbage collection. Allocations are not
2532 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002533 * objects (set or delete properties for example) since it is possible
2534 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002536enum GCType {
2537 kGCTypeScavenge = 1 << 0,
2538 kGCTypeMarkSweepCompact = 1 << 1,
2539 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2540};
2541
2542enum GCCallbackFlags {
2543 kNoGCCallbackFlags = 0,
2544 kGCCallbackFlagCompacted = 1 << 0
2545};
2546
2547typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2548typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550typedef void (*GCCallback)();
2551
2552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002554 * Profiler modules.
2555 *
2556 * In V8, profiler consists of several modules: CPU profiler, and different
2557 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002558 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2559 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002560 */
2561enum ProfilerModules {
2562 PROFILER_MODULE_NONE = 0,
2563 PROFILER_MODULE_CPU = 1,
2564 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002565 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2566 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002567};
2568
2569
2570/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002571 * Collection of V8 heap information.
2572 *
2573 * Instances of this class can be passed to v8::V8::HeapStatistics to
2574 * get heap statistics from V8.
2575 */
2576class V8EXPORT HeapStatistics {
2577 public:
2578 HeapStatistics();
2579 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002580 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002581 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002582 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002583
2584 private:
2585 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002586 void set_total_heap_size_executable(size_t size) {
2587 total_heap_size_executable_ = size;
2588 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002589 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002590 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002591
2592 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002593 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002594 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002595 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002596
2597 friend class V8;
2598};
2599
2600
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002601class RetainedObjectInfo;
2602
ager@chromium.org3811b432009-10-28 14:53:37 +00002603/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002604 * Isolate represents an isolated instance of the V8 engine. V8
2605 * isolates have completely separate states. Objects from one isolate
2606 * must not be used in other isolates. When V8 is initialized a
2607 * default isolate is implicitly created and entered. The embedder
2608 * can create additional isolates and use them in parallel in multiple
2609 * threads. An isolate can be entered by at most one thread at any
2610 * given time. The Locker/Unlocker API can be used to synchronize.
2611 */
2612class V8EXPORT Isolate {
2613 public:
2614 /**
2615 * Stack-allocated class which sets the isolate for all operations
2616 * executed within a local scope.
2617 */
2618 class V8EXPORT Scope {
2619 public:
2620 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2621 isolate->Enter();
2622 }
2623
2624 ~Scope() { isolate_->Exit(); }
2625
2626 private:
2627 Isolate* const isolate_;
2628
2629 // Prevent copying of Scope objects.
2630 Scope(const Scope&);
2631 Scope& operator=(const Scope&);
2632 };
2633
2634 /**
2635 * Creates a new isolate. Does not change the currently entered
2636 * isolate.
2637 *
2638 * When an isolate is no longer used its resources should be freed
2639 * by calling Dispose(). Using the delete operator is not allowed.
2640 */
2641 static Isolate* New();
2642
2643 /**
2644 * Returns the entered isolate for the current thread or NULL in
2645 * case there is no current isolate.
2646 */
2647 static Isolate* GetCurrent();
2648
2649 /**
2650 * Methods below this point require holding a lock (using Locker) in
2651 * a multi-threaded environment.
2652 */
2653
2654 /**
2655 * Sets this isolate as the entered one for the current thread.
2656 * Saves the previously entered one (if any), so that it can be
2657 * restored when exiting. Re-entering an isolate is allowed.
2658 */
2659 void Enter();
2660
2661 /**
2662 * Exits this isolate by restoring the previously entered one in the
2663 * current thread. The isolate may still stay the same, if it was
2664 * entered more than once.
2665 *
2666 * Requires: this == Isolate::GetCurrent().
2667 */
2668 void Exit();
2669
2670 /**
2671 * Disposes the isolate. The isolate must not be entered by any
2672 * thread to be disposable.
2673 */
2674 void Dispose();
2675
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002676 /**
2677 * Associate embedder-specific data with the isolate
2678 */
2679 void SetData(void* data);
2680
2681 /**
2682 * Retrive embedder-specific data from the isolate.
2683 * Returns NULL if SetData has never been called.
2684 */
2685 void* GetData();
2686
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002687 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002688 Isolate();
2689 Isolate(const Isolate&);
2690 ~Isolate();
2691 Isolate& operator=(const Isolate&);
2692 void* operator new(size_t size);
2693 void operator delete(void*, size_t);
2694};
2695
2696
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002697class StartupData {
2698 public:
2699 enum CompressionAlgorithm {
2700 kUncompressed,
2701 kBZip2
2702 };
2703
2704 const char* data;
2705 int compressed_size;
2706 int raw_size;
2707};
2708
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002709
2710/**
2711 * A helper class for driving V8 startup data decompression. It is based on
2712 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2713 * for an embedder to use this class, instead, API functions can be used
2714 * directly.
2715 *
2716 * For an example of the class usage, see the "shell.cc" sample application.
2717 */
2718class V8EXPORT StartupDataDecompressor { // NOLINT
2719 public:
2720 StartupDataDecompressor();
2721 virtual ~StartupDataDecompressor();
2722 int Decompress();
2723
2724 protected:
2725 virtual int DecompressData(char* raw_data,
2726 int* raw_data_size,
2727 const char* compressed_data,
2728 int compressed_data_size) = 0;
2729
2730 private:
2731 char** raw_data;
2732};
2733
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002734/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735 * Container class for static utility functions.
2736 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002737class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002739 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 static void SetFatalErrorHandler(FatalErrorCallback that);
2741
v8.team.kasperl727e9952008-09-02 14:56:44 +00002742 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002743 * Set the callback to invoke to check if code generation from
2744 * strings should be allowed.
2745 */
2746 static void SetAllowCodeGenerationFromStringsCallback(
2747 AllowCodeGenerationFromStringsCallback that);
2748
2749 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002750 * Ignore out-of-memory exceptions.
2751 *
2752 * V8 running out of memory is treated as a fatal error by default.
2753 * This means that the fatal error handler is called and that V8 is
2754 * terminated.
2755 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002756 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002757 * out-of-memory situation as a fatal error. This way, the contexts
2758 * that did not cause the out of memory problem might be able to
2759 * continue execution.
2760 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 static void IgnoreOutOfMemoryException();
2762
v8.team.kasperl727e9952008-09-02 14:56:44 +00002763 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002764 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002765 * fatal errors such as out-of-memory situations.
2766 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 static bool IsDead();
2768
2769 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002770 * The following 4 functions are to be used when V8 is built with
2771 * the 'compress_startup_data' flag enabled. In this case, the
2772 * embedder must decompress startup data prior to initializing V8.
2773 *
2774 * This is how interaction with V8 should look like:
2775 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2776 * v8::StartupData* compressed_data =
2777 * new v8::StartupData[compressed_data_count];
2778 * v8::V8::GetCompressedStartupData(compressed_data);
2779 * ... decompress data (compressed_data can be updated in-place) ...
2780 * v8::V8::SetDecompressedStartupData(compressed_data);
2781 * ... now V8 can be initialized
2782 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002783 *
2784 * A helper class StartupDataDecompressor is provided. It implements
2785 * the protocol of the interaction described above, and can be used in
2786 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002787 */
2788 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2789 static int GetCompressedStartupDataCount();
2790 static void GetCompressedStartupData(StartupData* compressed_data);
2791 static void SetDecompressedStartupData(StartupData* decompressed_data);
2792
2793 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002794 * Adds a message listener.
2795 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002796 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002797 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 */
2799 static bool AddMessageListener(MessageCallback that,
2800 Handle<Value> data = Handle<Value>());
2801
2802 /**
2803 * Remove all message listeners from the specified callback function.
2804 */
2805 static void RemoveMessageListeners(MessageCallback that);
2806
2807 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002808 * Tells V8 to capture current stack trace when uncaught exception occurs
2809 * and report it to the message listeners. The option is off by default.
2810 */
2811 static void SetCaptureStackTraceForUncaughtExceptions(
2812 bool capture,
2813 int frame_limit = 10,
2814 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2815
2816 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002817 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818 */
2819 static void SetFlagsFromString(const char* str, int length);
2820
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002821 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002822 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002823 */
2824 static void SetFlagsFromCommandLine(int* argc,
2825 char** argv,
2826 bool remove_flags);
2827
kasper.lund7276f142008-07-30 08:49:36 +00002828 /** Get the version string. */
2829 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830
2831 /**
2832 * Enables the host application to provide a mechanism for recording
2833 * statistics counters.
2834 */
2835 static void SetCounterFunction(CounterLookupCallback);
2836
2837 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002838 * Enables the host application to provide a mechanism for recording
2839 * histograms. The CreateHistogram function returns a
2840 * histogram which will later be passed to the AddHistogramSample
2841 * function.
2842 */
2843 static void SetCreateHistogramFunction(CreateHistogramCallback);
2844 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2845
2846 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 * Enables the computation of a sliding window of states. The sliding
2848 * window information is recorded in statistics counters.
2849 */
2850 static void EnableSlidingStateWindow();
2851
2852 /** Callback function for reporting failed access checks.*/
2853 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2854
2855 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002856 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002857 * garbage collection. Allocations are not allowed in the
2858 * callback function, you therefore cannot manipulate objects (set
2859 * or delete properties for example) since it is possible such
2860 * operations will result in the allocation of objects. It is possible
2861 * to specify the GCType filter for your callback. But it is not possible to
2862 * register the same callback function two times with different
2863 * GCType filters.
2864 */
2865 static void AddGCPrologueCallback(
2866 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2867
2868 /**
2869 * This function removes callback which was installed by
2870 * AddGCPrologueCallback function.
2871 */
2872 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2873
2874 /**
2875 * The function is deprecated. Please use AddGCPrologueCallback instead.
2876 * Enables the host application to receive a notification before a
2877 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002878 * callback function, you therefore cannot manipulate objects (set
2879 * or delete properties for example) since it is possible such
2880 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881 */
2882 static void SetGlobalGCPrologueCallback(GCCallback);
2883
2884 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002885 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002886 * garbage collection. Allocations are not allowed in the
2887 * callback function, you therefore cannot manipulate objects (set
2888 * or delete properties for example) since it is possible such
2889 * operations will result in the allocation of objects. It is possible
2890 * to specify the GCType filter for your callback. But it is not possible to
2891 * register the same callback function two times with different
2892 * GCType filters.
2893 */
2894 static void AddGCEpilogueCallback(
2895 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2896
2897 /**
2898 * This function removes callback which was installed by
2899 * AddGCEpilogueCallback function.
2900 */
2901 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2902
2903 /**
2904 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2905 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002906 * major garbage collection. Allocations are not allowed in the
2907 * callback function, you therefore cannot manipulate objects (set
2908 * or delete properties for example) since it is possible such
2909 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 */
2911 static void SetGlobalGCEpilogueCallback(GCCallback);
2912
2913 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002914 * Enables the host application to provide a mechanism to be notified
2915 * and perform custom logging when V8 Allocates Executable Memory.
2916 */
2917 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2918 ObjectSpace space,
2919 AllocationAction action);
2920
2921 /**
2922 * This function removes callback which was installed by
2923 * AddMemoryAllocationCallback function.
2924 */
2925 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2926
2927 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002928 * Allows the host application to group objects together. If one
2929 * object in the group is alive, all objects in the group are alive.
2930 * After each garbage collection, object groups are removed. It is
2931 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002932 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002933 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002934 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002936 static void AddObjectGroup(Persistent<Value>* objects,
2937 size_t length,
2938 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002939
2940 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002941 * Allows the host application to declare implicit references between
2942 * the objects: if |parent| is alive, all |children| are alive too.
2943 * After each garbage collection, all implicit references
2944 * are removed. It is intended to be used in the before-garbage-collection
2945 * callback function.
2946 */
2947 static void AddImplicitReferences(Persistent<Object> parent,
2948 Persistent<Value>* children,
2949 size_t length);
2950
2951 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002952 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002953 * initialize from scratch. This function is called implicitly if
2954 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 */
2956 static bool Initialize();
2957
kasper.lund7276f142008-07-30 08:49:36 +00002958 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002959 * Adjusts the amount of registered external memory. Used to give
2960 * V8 an indication of the amount of externally allocated memory
2961 * that is kept alive by JavaScript objects. V8 uses this to decide
2962 * when to perform global garbage collections. Registering
2963 * externally allocated memory will trigger global garbage
2964 * collections more often than otherwise in an attempt to garbage
2965 * collect the JavaScript objects keeping the externally allocated
2966 * memory alive.
2967 *
2968 * \param change_in_bytes the change in externally allocated memory
2969 * that is kept alive by JavaScript objects.
2970 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002971 */
2972 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2973
iposva@chromium.org245aa852009-02-10 00:49:54 +00002974 /**
2975 * Suspends recording of tick samples in the profiler.
2976 * When the V8 profiling mode is enabled (usually via command line
2977 * switches) this function suspends recording of tick samples.
2978 * Profiling ticks are discarded until ResumeProfiler() is called.
2979 *
2980 * See also the --prof and --prof_auto command line switches to
2981 * enable V8 profiling.
2982 */
2983 static void PauseProfiler();
2984
2985 /**
2986 * Resumes recording of tick samples in the profiler.
2987 * See also PauseProfiler().
2988 */
2989 static void ResumeProfiler();
2990
ager@chromium.org41826e72009-03-30 13:30:57 +00002991 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002992 * Return whether profiler is currently paused.
2993 */
2994 static bool IsProfilerPaused();
2995
2996 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002997 * Resumes specified profiler modules. Can be called several times to
2998 * mark the opening of a profiler events block with the given tag.
2999 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003000 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
3001 * See ProfilerModules enum.
3002 *
3003 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00003004 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003005 */
ager@chromium.org5c838252010-02-19 08:53:10 +00003006 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003007
3008 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00003009 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
3010 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
3011 * same tag value. There is no need for blocks to be properly nested.
3012 * The profiler is paused when the last opened block is closed.
3013 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003014 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
3015 * See ProfilerModules enum.
3016 *
3017 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00003018 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003019 */
ager@chromium.org5c838252010-02-19 08:53:10 +00003020 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003021
3022 /**
3023 * Returns active (resumed) profiler modules.
3024 * See ProfilerModules enum.
3025 *
3026 * \returns active profiler modules.
3027 */
3028 static int GetActiveProfilerModules();
3029
3030 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003031 * If logging is performed into a memory buffer (via --logfile=*), allows to
3032 * retrieve previously written messages. This can be used for retrieving
3033 * profiler log data in the application. This function is thread-safe.
3034 *
3035 * Caller provides a destination buffer that must exist during GetLogLines
3036 * call. Only whole log lines are copied into the buffer.
3037 *
3038 * \param from_pos specified a point in a buffer to read from, 0 is the
3039 * beginning of a buffer. It is assumed that caller updates its current
3040 * position using returned size value from the previous call.
3041 * \param dest_buf destination buffer for log data.
3042 * \param max_size size of the destination buffer.
3043 * \returns actual size of log data copied into buffer.
3044 */
3045 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
3046
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003047 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003048 * The minimum allowed size for a log lines buffer. If the size of
3049 * the buffer given will not be enough to hold a line of the maximum
3050 * length, an attempt to find a log line end in GetLogLines will
3051 * fail, and an empty result will be returned.
3052 */
3053 static const int kMinimumSizeForLogLinesBuffer = 2048;
3054
3055 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003056 * Retrieve the V8 thread id of the calling thread.
3057 *
3058 * The thread id for a thread should only be retrieved after the V8
3059 * lock has been acquired with a Locker object with that thread.
3060 */
3061 static int GetCurrentThreadId();
3062
3063 /**
3064 * Forcefully terminate execution of a JavaScript thread. This can
3065 * be used to terminate long-running scripts.
3066 *
3067 * TerminateExecution should only be called when then V8 lock has
3068 * been acquired with a Locker object. Therefore, in order to be
3069 * able to terminate long-running threads, preemption must be
3070 * enabled to allow the user of TerminateExecution to acquire the
3071 * lock.
3072 *
3073 * The termination is achieved by throwing an exception that is
3074 * uncatchable by JavaScript exception handlers. Termination
3075 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003076 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003077 * exception handler that catches an exception should check if that
3078 * exception is a termination exception and immediately return if
3079 * that is the case. Returning immediately in that case will
3080 * continue the propagation of the termination exception if needed.
3081 *
3082 * The thread id passed to TerminateExecution must have been
3083 * obtained by calling GetCurrentThreadId on the thread in question.
3084 *
3085 * \param thread_id The thread id of the thread to terminate.
3086 */
3087 static void TerminateExecution(int thread_id);
3088
3089 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003090 * Forcefully terminate the current thread of JavaScript execution
3091 * in the given isolate. If no isolate is provided, the default
3092 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003093 *
3094 * This method can be used by any thread even if that thread has not
3095 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003096 *
3097 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003098 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003099 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003100
3101 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003102 * Is V8 terminating JavaScript execution.
3103 *
3104 * Returns true if JavaScript execution is currently terminating
3105 * because of a call to TerminateExecution. In that case there are
3106 * still JavaScript frames on the stack and the termination
3107 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003108 *
3109 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003110 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003111 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003112
3113 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003114 * Releases any resources used by v8 and stops any utility threads
3115 * that may be running. Note that disposing v8 is permanent, it
3116 * cannot be reinitialized.
3117 *
3118 * It should generally not be necessary to dispose v8 before exiting
3119 * a process, this should happen automatically. It is only necessary
3120 * to use if the process needs the resources taken up by v8.
3121 */
3122 static bool Dispose();
3123
ager@chromium.org3811b432009-10-28 14:53:37 +00003124 /**
3125 * Get statistics about the heap memory usage.
3126 */
3127 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003128
3129 /**
3130 * Optional notification that the embedder is idle.
3131 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003132 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003133 * Returns true if the embedder should stop calling IdleNotification
3134 * until real work has been done. This indicates that V8 has done
3135 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003136 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003137 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003138
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003139 /**
3140 * Optional notification that the system is running low on memory.
3141 * V8 uses these notifications to attempt to free memory.
3142 */
3143 static void LowMemoryNotification();
3144
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003145 /**
3146 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003147 * these notifications to guide the GC heuristic. Returns the number
3148 * of context disposals - including this one - since the last time
3149 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003150 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003151 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003152
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153 private:
3154 V8();
3155
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003156 static internal::Object** GlobalizeReference(internal::Object** handle);
3157 static void DisposeGlobal(internal::Object** global_handle);
3158 static void MakeWeak(internal::Object** global_handle,
3159 void* data,
3160 WeakReferenceCallback);
3161 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003162 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003163 static bool IsGlobalNearDeath(internal::Object** global_handle);
3164 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003165 static void SetWrapperClassId(internal::Object** global_handle,
3166 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003167
3168 template <class T> friend class Handle;
3169 template <class T> friend class Local;
3170 template <class T> friend class Persistent;
3171 friend class Context;
3172};
3173
3174
3175/**
3176 * An external exception handler.
3177 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003178class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003179 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180 /**
3181 * Creates a new try/catch block and registers it with v8.
3182 */
3183 TryCatch();
3184
3185 /**
3186 * Unregisters and deletes this try/catch block.
3187 */
3188 ~TryCatch();
3189
3190 /**
3191 * Returns true if an exception has been caught by this try/catch block.
3192 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003193 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194
3195 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003196 * For certain types of exceptions, it makes no sense to continue
3197 * execution.
3198 *
3199 * Currently, the only type of exception that can be caught by a
3200 * TryCatch handler and for which it does not make sense to continue
3201 * is termination exception. Such exceptions are thrown when the
3202 * TerminateExecution methods are called to terminate a long-running
3203 * script.
3204 *
3205 * If CanContinue returns false, the correct action is to perform
3206 * any C++ cleanup needed and then return.
3207 */
3208 bool CanContinue() const;
3209
3210 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003211 * Throws the exception caught by this TryCatch in a way that avoids
3212 * it being caught again by this same TryCatch. As with ThrowException
3213 * it is illegal to execute any JavaScript operations after calling
3214 * ReThrow; the caller must return immediately to where the exception
3215 * is caught.
3216 */
3217 Handle<Value> ReThrow();
3218
3219 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220 * Returns the exception caught by this try/catch block. If no exception has
3221 * been caught an empty handle is returned.
3222 *
3223 * The returned handle is valid until this TryCatch block has been destroyed.
3224 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003225 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226
3227 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003228 * Returns the .stack property of the thrown object. If no .stack
3229 * property is present an empty handle is returned.
3230 */
3231 Local<Value> StackTrace() const;
3232
3233 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003234 * Returns the message associated with this exception. If there is
3235 * no message associated an empty handle is returned.
3236 *
3237 * The returned handle is valid until this TryCatch block has been
3238 * destroyed.
3239 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003240 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003241
3242 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243 * Clears any exceptions that may have been caught by this try/catch block.
3244 * After this method has been called, HasCaught() will return false.
3245 *
3246 * It is not necessary to clear a try/catch block before using it again; if
3247 * another exception is thrown the previously caught exception will just be
3248 * overwritten. However, it is often a good idea since it makes it easier
3249 * to determine which operation threw a given exception.
3250 */
3251 void Reset();
3252
v8.team.kasperl727e9952008-09-02 14:56:44 +00003253 /**
3254 * Set verbosity of the external exception handler.
3255 *
3256 * By default, exceptions that are caught by an external exception
3257 * handler are not reported. Call SetVerbose with true on an
3258 * external exception handler to have exceptions caught by the
3259 * handler reported as if they were not caught.
3260 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003261 void SetVerbose(bool value);
3262
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003263 /**
3264 * Set whether or not this TryCatch should capture a Message object
3265 * which holds source information about where the exception
3266 * occurred. True by default.
3267 */
3268 void SetCaptureMessage(bool value);
3269
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003270 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003271 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003272 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003274 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003275 bool is_verbose_ : 1;
3276 bool can_continue_ : 1;
3277 bool capture_message_ : 1;
3278 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003279
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003280 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281};
3282
3283
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003284// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285
3286
3287/**
3288 * Ignore
3289 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003290class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003291 public:
3292 ExtensionConfiguration(int name_count, const char* names[])
3293 : name_count_(name_count), names_(names) { }
3294 private:
3295 friend class ImplementationUtilities;
3296 int name_count_;
3297 const char** names_;
3298};
3299
3300
3301/**
3302 * A sandboxed execution context with its own set of built-in objects
3303 * and functions.
3304 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003305class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003306 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003307 /**
3308 * Returns the global proxy object or global object itself for
3309 * detached contexts.
3310 *
3311 * Global proxy object is a thin wrapper whose prototype points to
3312 * actual context's global object with the properties like Object, etc.
3313 * This is done that way for security reasons (for more details see
3314 * https://wiki.mozilla.org/Gecko:SplitWindow).
3315 *
3316 * Please note that changes to global proxy object prototype most probably
3317 * would break VM---v8 expects only global object as a prototype of
3318 * global proxy object.
3319 *
3320 * If DetachGlobal() has been invoked, Global() would return actual global
3321 * object until global is reattached with ReattachGlobal().
3322 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003323 Local<Object> Global();
3324
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003325 /**
3326 * Detaches the global object from its context before
3327 * the global object can be reused to create a new context.
3328 */
3329 void DetachGlobal();
3330
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003331 /**
3332 * Reattaches a global object to a context. This can be used to
3333 * restore the connection between a global object and a context
3334 * after DetachGlobal has been called.
3335 *
3336 * \param global_object The global object to reattach to the
3337 * context. For this to work, the global object must be the global
3338 * object that was associated with this context before a call to
3339 * DetachGlobal.
3340 */
3341 void ReattachGlobal(Handle<Object> global_object);
3342
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003343 /** Creates a new context.
3344 *
3345 * Returns a persistent handle to the newly allocated context. This
3346 * persistent handle has to be disposed when the context is no
3347 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003348 *
3349 * \param extensions An optional extension configuration containing
3350 * the extensions to be installed in the newly created context.
3351 *
3352 * \param global_template An optional object template from which the
3353 * global object for the newly created context will be created.
3354 *
3355 * \param global_object An optional global object to be reused for
3356 * the newly created context. This global object must have been
3357 * created by a previous call to Context::New with the same global
3358 * template. The state of the global object will be completely reset
3359 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003360 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003361 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003362 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003363 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3364 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365
kasper.lund44510672008-07-25 07:37:58 +00003366 /** Returns the last entered context. */
3367 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003368
kasper.lund44510672008-07-25 07:37:58 +00003369 /** Returns the context that is on the top of the stack. */
3370 static Local<Context> GetCurrent();
3371
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003373 * Returns the context of the calling JavaScript code. That is the
3374 * context of the top-most JavaScript frame. If there are no
3375 * JavaScript frames an empty handle is returned.
3376 */
3377 static Local<Context> GetCalling();
3378
3379 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003380 * Sets the security token for the context. To access an object in
3381 * another context, the security tokens must match.
3382 */
3383 void SetSecurityToken(Handle<Value> token);
3384
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003385 /** Restores the security token to the default value. */
3386 void UseDefaultSecurityToken();
3387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003388 /** Returns the security token of this context.*/
3389 Handle<Value> GetSecurityToken();
3390
v8.team.kasperl727e9952008-09-02 14:56:44 +00003391 /**
3392 * Enter this context. After entering a context, all code compiled
3393 * and run is compiled and run in this context. If another context
3394 * is already entered, this old context is saved so it can be
3395 * restored when the new context is exited.
3396 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003397 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003398
3399 /**
3400 * Exit this context. Exiting the current context restores the
3401 * context that was in place when entering the current context.
3402 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003403 void Exit();
3404
v8.team.kasperl727e9952008-09-02 14:56:44 +00003405 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 bool HasOutOfMemoryException();
3407
v8.team.kasperl727e9952008-09-02 14:56:44 +00003408 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003409 static bool InContext();
3410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003412 * Associate an additional data object with the context. This is mainly used
3413 * with the debugger to provide additional information on the context through
3414 * the debugger API.
3415 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003416 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003417 Local<Value> GetData();
3418
3419 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003420 * Control whether code generation from strings is allowed. Calling
3421 * this method with false will disable 'eval' and the 'Function'
3422 * constructor for code running in this context. If 'eval' or the
3423 * 'Function' constructor are used an exception will be thrown.
3424 *
3425 * If code generation from strings is not allowed the
3426 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3427 * set before blocking the call to 'eval' or the 'Function'
3428 * constructor. If that callback returns true, the call will be
3429 * allowed, otherwise an exception will be thrown. If no callback is
3430 * set an exception will be thrown.
3431 */
3432 void AllowCodeGenerationFromStrings(bool allow);
3433
3434 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003435 * Stack-allocated class which sets the execution context for all
3436 * operations executed within a local scope.
3437 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003438 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003439 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003440 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 context_->Enter();
3442 }
3443 inline ~Scope() { context_->Exit(); }
3444 private:
3445 Handle<Context> context_;
3446 };
3447
3448 private:
3449 friend class Value;
3450 friend class Script;
3451 friend class Object;
3452 friend class Function;
3453};
3454
3455
3456/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003457 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003458 * is allowed to use any given V8 isolate. See Isolate class
3459 * comments. The definition of 'using V8 isolate' includes
3460 * accessing handles or holding onto object pointers obtained
3461 * from V8 handles while in the particular V8 isolate. It is up
3462 * to the user of V8 to ensure (perhaps with locking) that this
3463 * constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003464 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003465 * v8::Locker is a scoped lock object. While it's
3466 * active (i.e. between its construction and destruction) the current thread is
3467 * allowed to use the locked isolate. V8 guarantees that an isolate can be locked
3468 * by at most one thread at any time. In other words, the scope of a v8::Locker is
3469 * a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003470 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003471 * Sample usage:
3472* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473 * ...
3474 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003475 * v8::Locker locker(isolate);
3476 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003478 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003479 * ...
3480 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003481 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003482 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003483 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484 * by destroying the v8::Locker object as above or by constructing a
3485 * v8::Unlocker object:
3486 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003487 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003488 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003489 * isolate->Exit();
3490 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491 * ...
3492 * // Code not using V8 goes here while V8 can run in another thread.
3493 * ...
3494 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003495 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003496 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003497 *
3498 * The Unlocker object is intended for use in a long-running callback
3499 * from V8, where you want to release the V8 lock for other threads to
3500 * use.
3501 *
3502 * The v8::Locker is a recursive lock. That is, you can lock more than
3503 * once in a given thread. This can be useful if you have code that can
3504 * be called either from code that holds the lock or from code that does
3505 * not. The Unlocker is not recursive so you can not have several
3506 * Unlockers on the stack at once, and you can not use an Unlocker in a
3507 * thread that is not inside a Locker's scope.
3508 *
3509 * An unlocker will unlock several lockers if it has to and reinstate
3510 * the correct depth of locking on its destruction. eg.:
3511 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003512 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003513 * // V8 not locked.
3514 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003515 * v8::Locker locker(isolate);
3516 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003517 * // V8 locked.
3518 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003519 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003520 * // V8 still locked (2 levels).
3521 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003522 * isolate->Exit();
3523 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524 * // V8 not locked.
3525 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003526 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527 * // V8 locked again (2 levels).
3528 * }
3529 * // V8 still locked (1 level).
3530 * }
3531 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003532 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003533 *
3534 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003535 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003536class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003538 /**
3539 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3540 */
3541 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003542 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003543 private:
3544 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545};
3546
3547
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003548class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003550 /**
3551 * Initialize Locker for a given Isolate. NULL means default isolate.
3552 */
3553 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003555
3556 /**
3557 * Start preemption.
3558 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003559 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003560 * that will switch between multiple threads that are in contention
3561 * for the V8 lock.
3562 */
3563 static void StartPreemption(int every_n_ms);
3564
3565 /**
3566 * Stop preemption.
3567 */
3568 static void StopPreemption();
3569
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003570 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00003571 * Returns whether or not the locker for a given isolate, or default isolate if NULL is given,
3572 * is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003573 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003574 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003575
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003576 /**
3577 * Returns whether v8::Locker is being used by this V8 instance.
3578 */
3579 static bool IsActive() { return active_; }
3580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003581 private:
3582 bool has_lock_;
3583 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003584 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003585
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003586 static bool active_;
3587
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003588 // Disallow copying and assigning.
3589 Locker(const Locker&);
3590 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591};
3592
3593
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003594/**
3595 * An interface for exporting data from V8, using "push" model.
3596 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003597class V8EXPORT OutputStream { // NOLINT
3598 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003599 enum OutputEncoding {
3600 kAscii = 0 // 7-bit ASCII.
3601 };
3602 enum WriteResult {
3603 kContinue = 0,
3604 kAbort = 1
3605 };
3606 virtual ~OutputStream() {}
3607 /** Notify about the end of stream. */
3608 virtual void EndOfStream() = 0;
3609 /** Get preferred output chunk size. Called only once. */
3610 virtual int GetChunkSize() { return 1024; }
3611 /** Get preferred output encoding. Called only once. */
3612 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3613 /**
3614 * Writes the next chunk of snapshot data into the stream. Writing
3615 * can be stopped by returning kAbort as function result. EndOfStream
3616 * will not be called in case writing was aborted.
3617 */
3618 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3619};
3620
3621
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003622/**
3623 * An interface for reporting progress and controlling long-running
3624 * activities.
3625 */
3626class V8EXPORT ActivityControl { // NOLINT
3627 public:
3628 enum ControlOption {
3629 kContinue = 0,
3630 kAbort = 1
3631 };
3632 virtual ~ActivityControl() {}
3633 /**
3634 * Notify about current progress. The activity can be stopped by
3635 * returning kAbort as the callback result.
3636 */
3637 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3638};
3639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003640
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003641// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003642
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003643
3644namespace internal {
3645
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003646static const int kApiPointerSize = sizeof(void*); // NOLINT
3647static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003648
3649// Tag information for HeapObject.
3650const int kHeapObjectTag = 1;
3651const int kHeapObjectTagSize = 2;
3652const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3653
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003654// Tag information for Smi.
3655const int kSmiTag = 0;
3656const int kSmiTagSize = 1;
3657const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3658
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003659template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003660
3661// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003662template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003663 static const int kSmiShiftSize = 0;
3664 static const int kSmiValueSize = 31;
3665 static inline int SmiToInt(internal::Object* value) {
3666 int shift_bits = kSmiTagSize + kSmiShiftSize;
3667 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3668 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3669 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003670
3671 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3672 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003673 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003674 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003675};
3676
3677// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003678template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003679 static const int kSmiShiftSize = 31;
3680 static const int kSmiValueSize = 32;
3681 static inline int SmiToInt(internal::Object* value) {
3682 int shift_bits = kSmiTagSize + kSmiShiftSize;
3683 // Shift down and throw away top 32 bits.
3684 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3685 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003686
3687 // To maximize the range of pointers that can be encoded
3688 // in the available 32 bits, we require them to be 8 bytes aligned.
3689 // This gives 2 ^ (32 + 3) = 32G address space covered.
3690 // It might be not enough to cover stack allocated objects on some platforms.
3691 static const int kPointerAlignment = 3;
3692
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003693 static const uintptr_t kEncodablePointerMask =
3694 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003695
3696 static const int kPointerToSmiShift =
3697 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003698};
3699
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003700typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3701const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3702const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003703const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003704 PlatformSmiTagging::kEncodablePointerMask;
3705const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003706
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003707template <size_t ptr_size> struct InternalConstants;
3708
3709// Internal constants for 32-bit systems.
3710template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003711 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003712};
3713
3714// Internal constants for 64-bit systems.
3715template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003716 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003717};
3718
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003719/**
3720 * This class exports constants and functionality from within v8 that
3721 * is necessary to implement inline functions in the v8 api. Don't
3722 * depend on functions and constants defined here.
3723 */
3724class Internals {
3725 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003726 // These values match non-compiler-dependent values defined within
3727 // the implementation of v8.
3728 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003729 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003730 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003731 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003732
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003733 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003734 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003735 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003736 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003737
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003738 static const int kJSObjectType = 0xa3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003739 static const int kFirstNonstringType = 0x80;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003740 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003741
3742 static inline bool HasHeapObjectTag(internal::Object* value) {
3743 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3744 kHeapObjectTag);
3745 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003746
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003747 static inline bool HasSmiTag(internal::Object* value) {
3748 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3749 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003750
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003751 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003752 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003753 }
3754
3755 static inline int GetInstanceType(internal::Object* obj) {
3756 typedef internal::Object O;
3757 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3758 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3759 }
3760
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003761 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003762 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003763 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3764 }
3765
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003766 static inline void* GetExternalPointer(internal::Object* obj) {
3767 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003768 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003769 } else if (GetInstanceType(obj) == kForeignType) {
3770 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003771 } else {
3772 return NULL;
3773 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003774 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003775
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003776 static inline bool IsExternalTwoByteString(int instance_type) {
3777 int representation = (instance_type & kFullStringRepresentationMask);
3778 return representation == kExternalTwoByteRepresentationTag;
3779 }
3780
3781 template <typename T>
3782 static inline T ReadField(Object* ptr, int offset) {
3783 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3784 return *reinterpret_cast<T*>(addr);
3785 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003786
3787 static inline bool CanCastToHeapObject(void* o) { return false; }
3788 static inline bool CanCastToHeapObject(Context* o) { return true; }
3789 static inline bool CanCastToHeapObject(String* o) { return true; }
3790 static inline bool CanCastToHeapObject(Object* o) { return true; }
3791 static inline bool CanCastToHeapObject(Message* o) { return true; }
3792 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3793 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003794};
3795
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003796} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003797
3798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003799template <class T>
3800Handle<T>::Handle() : val_(0) { }
3801
3802
3803template <class T>
3804Local<T>::Local() : Handle<T>() { }
3805
3806
3807template <class T>
3808Local<T> Local<T>::New(Handle<T> that) {
3809 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003810 T* that_ptr = *that;
3811 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3812 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3813 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3814 reinterpret_cast<internal::HeapObject*>(*p))));
3815 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3817}
3818
3819
3820template <class T>
3821Persistent<T> Persistent<T>::New(Handle<T> that) {
3822 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003823 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3825}
3826
3827
3828template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003829bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003831 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832}
3833
3834
3835template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003836bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003837 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003838 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003839}
3840
3841
3842template <class T>
3843void Persistent<T>::Dispose() {
3844 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003845 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846}
3847
3848
3849template <class T>
3850Persistent<T>::Persistent() : Handle<T>() { }
3851
3852template <class T>
3853void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003854 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3855 parameters,
3856 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003857}
3858
3859template <class T>
3860void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003861 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862}
3863
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003864template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003865void Persistent<T>::MarkIndependent() {
3866 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
3867}
3868
3869template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003870void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
3871 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
3872}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003873
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003874Arguments::Arguments(internal::Object** implicit_args,
3875 internal::Object** values, int length,
3876 bool is_construct_call)
3877 : implicit_args_(implicit_args),
3878 values_(values),
3879 length_(length),
3880 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003881
3882
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883Local<Value> Arguments::operator[](int i) const {
3884 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3885 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3886}
3887
3888
3889Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003890 return Local<Function>(reinterpret_cast<Function*>(
3891 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892}
3893
3894
3895Local<Object> Arguments::This() const {
3896 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3897}
3898
3899
3900Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003901 return Local<Object>(reinterpret_cast<Object*>(
3902 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903}
3904
3905
3906Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003907 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908}
3909
3910
3911bool Arguments::IsConstructCall() const {
3912 return is_construct_call_;
3913}
3914
3915
3916int Arguments::Length() const {
3917 return length_;
3918}
3919
3920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921template <class T>
3922Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003923 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3924 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003925 return Local<T>(reinterpret_cast<T*>(after));
3926}
3927
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003928Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929 return resource_name_;
3930}
3931
3932
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003933Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003934 return resource_line_offset_;
3935}
3936
3937
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003938Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939 return resource_column_offset_;
3940}
3941
3942
3943Handle<Boolean> Boolean::New(bool value) {
3944 return value ? True() : False();
3945}
3946
3947
3948void Template::Set(const char* name, v8::Handle<Data> value) {
3949 Set(v8::String::New(name), value);
3950}
3951
3952
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003953Local<Value> Object::GetInternalField(int index) {
3954#ifndef V8_ENABLE_CHECKS
3955 Local<Value> quick_result = UncheckedGetInternalField(index);
3956 if (!quick_result.IsEmpty()) return quick_result;
3957#endif
3958 return CheckedGetInternalField(index);
3959}
3960
3961
3962Local<Value> Object::UncheckedGetInternalField(int index) {
3963 typedef internal::Object O;
3964 typedef internal::Internals I;
3965 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003966 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003967 // If the object is a plain JSObject, which is the common case,
3968 // we know where to find the internal fields and can return the
3969 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003970 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003971 O* value = I::ReadField<O*>(obj, offset);
3972 O** result = HandleScope::CreateHandle(value);
3973 return Local<Value>(reinterpret_cast<Value*>(result));
3974 } else {
3975 return Local<Value>();
3976 }
3977}
3978
3979
3980void* External::Unwrap(Handle<v8::Value> obj) {
3981#ifdef V8_ENABLE_CHECKS
3982 return FullUnwrap(obj);
3983#else
3984 return QuickUnwrap(obj);
3985#endif
3986}
3987
3988
3989void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3990 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003991 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003992 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003993}
3994
3995
3996void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003997 typedef internal::Object O;
3998 typedef internal::Internals I;
3999
4000 O* obj = *reinterpret_cast<O**>(this);
4001
4002 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4003 // If the object is a plain JSObject, which is the common case,
4004 // we know where to find the internal fields and can return the
4005 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004006 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004007 O* value = I::ReadField<O*>(obj, offset);
4008 return I::GetExternalPointer(value);
4009 }
4010
4011 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004012}
4013
4014
4015String* String::Cast(v8::Value* value) {
4016#ifdef V8_ENABLE_CHECKS
4017 CheckCast(value);
4018#endif
4019 return static_cast<String*>(value);
4020}
4021
4022
4023String::ExternalStringResource* String::GetExternalStringResource() const {
4024 typedef internal::Object O;
4025 typedef internal::Internals I;
4026 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004027 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004028 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004029 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4030 result = reinterpret_cast<String::ExternalStringResource*>(value);
4031 } else {
4032 result = NULL;
4033 }
4034#ifdef V8_ENABLE_CHECKS
4035 VerifyExternalStringResource(result);
4036#endif
4037 return result;
4038}
4039
4040
4041bool Value::IsString() const {
4042#ifdef V8_ENABLE_CHECKS
4043 return FullIsString();
4044#else
4045 return QuickIsString();
4046#endif
4047}
4048
4049bool Value::QuickIsString() const {
4050 typedef internal::Object O;
4051 typedef internal::Internals I;
4052 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4053 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004054 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004055}
4056
4057
4058Number* Number::Cast(v8::Value* value) {
4059#ifdef V8_ENABLE_CHECKS
4060 CheckCast(value);
4061#endif
4062 return static_cast<Number*>(value);
4063}
4064
4065
4066Integer* Integer::Cast(v8::Value* value) {
4067#ifdef V8_ENABLE_CHECKS
4068 CheckCast(value);
4069#endif
4070 return static_cast<Integer*>(value);
4071}
4072
4073
4074Date* Date::Cast(v8::Value* value) {
4075#ifdef V8_ENABLE_CHECKS
4076 CheckCast(value);
4077#endif
4078 return static_cast<Date*>(value);
4079}
4080
4081
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004082RegExp* RegExp::Cast(v8::Value* value) {
4083#ifdef V8_ENABLE_CHECKS
4084 CheckCast(value);
4085#endif
4086 return static_cast<RegExp*>(value);
4087}
4088
4089
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004090Object* Object::Cast(v8::Value* value) {
4091#ifdef V8_ENABLE_CHECKS
4092 CheckCast(value);
4093#endif
4094 return static_cast<Object*>(value);
4095}
4096
4097
4098Array* Array::Cast(v8::Value* value) {
4099#ifdef V8_ENABLE_CHECKS
4100 CheckCast(value);
4101#endif
4102 return static_cast<Array*>(value);
4103}
4104
4105
4106Function* Function::Cast(v8::Value* value) {
4107#ifdef V8_ENABLE_CHECKS
4108 CheckCast(value);
4109#endif
4110 return static_cast<Function*>(value);
4111}
4112
4113
4114External* External::Cast(v8::Value* value) {
4115#ifdef V8_ENABLE_CHECKS
4116 CheckCast(value);
4117#endif
4118 return static_cast<External*>(value);
4119}
4120
4121
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004122Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004123 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004124}
4125
4126
4127Local<Object> AccessorInfo::This() const {
4128 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4129}
4130
4131
4132Local<Object> AccessorInfo::Holder() const {
4133 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4134}
4135
4136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004138 * \example shell.cc
4139 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004140 * command-line and executes them.
4141 */
4142
4143
4144/**
4145 * \example process.cc
4146 */
4147
4148
4149} // namespace v8
4150
4151
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004152#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004153#undef TYPE_CHECK
4154
4155
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004156#endif // V8_H_