blob: 7b3d3f97626a6eee268253dc9f6299909eaf7cd9 [file] [log] [blame]
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001// Copyright 2007-2009 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;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000113class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000114}
115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
117// --- W e a k H a n d l e s
118
119
120/**
121 * A weak reference callback function.
122 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000123 * This callback should either explicitly invoke Dispose on |object| if
124 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
125 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 * \param object the weak global object to be reclaimed by the garbage collector
127 * \param parameter the value passed in when making the weak global object
128 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000129typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 void* parameter);
131
132
133// --- H a n d l e s ---
134
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000135#define TYPE_CHECK(T, S) \
136 while (false) { \
137 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 }
139
140/**
141 * An object reference managed by the v8 garbage collector.
142 *
143 * All objects returned from v8 have to be tracked by the garbage
144 * collector so that it knows that the objects are still alive. Also,
145 * because the garbage collector may move objects, it is unsafe to
146 * point directly to an object. Instead, all objects are stored in
147 * handles which are known by the garbage collector and updated
148 * whenever an object moves. Handles should always be passed by value
149 * (except in cases like out-parameters) and they should never be
150 * allocated on the heap.
151 *
152 * There are two types of handles: local and persistent handles.
153 * Local handles are light-weight and transient and typically used in
154 * local operations. They are managed by HandleScopes. Persistent
155 * handles can be used when storing objects across several independent
156 * operations and have to be explicitly deallocated when they're no
157 * longer used.
158 *
159 * It is safe to extract the object stored in the handle by
160 * dereferencing the handle (for instance, to extract the Object* from
161 * an Handle<Object>); the value will still be governed by a handle
162 * behind the scenes and the same rules apply to these values as to
163 * their handles.
164 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000165template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 public:
167
168 /**
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
183 * Handle<Number> it will cause a compiletime error. Assigning
184 * 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:
314
315 /**
316 * Creates an empty persistent handle that doesn't point to any
317 * storage cell.
318 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000319 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320
321 /**
322 * Creates a persistent handle for the same storage cell as the
323 * specified handle. This constructor allows you to pass persistent
324 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000325 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000327 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 * between compatible persistent handles, for instance assigning a
329 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000330 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331 */
332 template <class S> inline Persistent(Persistent<S> that)
333 : Handle<T>(reinterpret_cast<T*>(*that)) {
334 /**
335 * This check fails when trying to convert between incompatible
336 * handles. For example, converting from a Handle<String> to a
337 * Handle<Number>.
338 */
339 TYPE_CHECK(T, S);
340 }
341
342 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
343
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000344 /**
345 * "Casts" a plain handle which is known to be a persistent handle
346 * to a persistent handle.
347 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 template <class S> explicit inline Persistent(Handle<S> that)
349 : Handle<T>(*that) { }
350
351 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000352#ifdef V8_ENABLE_CHECKS
353 // If we're going to perform the type check then we have to check
354 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000356#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 return Persistent<T>(T::Cast(*that));
358 }
359
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000360 template <class S> inline Persistent<S> As() {
361 return Persistent<S>::Cast(*this);
362 }
363
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000365 * Creates a new persistent handle for an existing local or
366 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000368 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000369
370 /**
371 * Releases the storage cell referenced by this persistent handle.
372 * Does not remove the reference to the cell from any handles.
373 * This handle's reference, and any any other references to the storage
374 * cell remain and IsEmpty will still return false.
375 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000376 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377
378 /**
379 * Make the reference to this object weak. When only weak handles
380 * refer to the object, the garbage collector will perform a
381 * callback to the given V8::WeakReferenceCallback function, passing
382 * it the object reference and the given parameters.
383 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000384 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385
386 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000387 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388
389 /**
390 *Checks if the handle holds the only reference to an object.
391 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000392 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
394 /**
395 * Returns true if the handle's reference is weak.
396 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000397 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398
399 private:
400 friend class ImplementationUtilities;
401 friend class ObjectTemplate;
402};
403
404
v8.team.kasperl727e9952008-09-02 14:56:44 +0000405 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 * A stack-allocated class that governs a number of local handles.
407 * After a handle scope has been created, all local handles will be
408 * allocated within that handle scope until either the handle scope is
409 * deleted or another handle scope is created. If there is already a
410 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000411 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 * new handles will again be allocated in the original handle scope.
413 *
414 * After the handle scope of a local handle has been deleted the
415 * garbage collector will no longer track the object stored in the
416 * handle and may deallocate it. The behavior of accessing a handle
417 * for which the handle scope has been deleted is undefined.
418 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000419class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000421 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000423 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424
425 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 * Closes the handle scope and returns the value as a handle in the
427 * previous scope, which is the new current scope after the call.
428 */
429 template <class T> Local<T> Close(Handle<T> value);
430
431 /**
432 * Counts the number of allocated handles.
433 */
434 static int NumberOfHandles();
435
436 /**
437 * Creates a new handle with the given value.
438 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000439 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
441 private:
442 // Make it impossible to create heap-allocated or illegal handle
443 // scopes by disallowing certain operations.
444 HandleScope(const HandleScope&);
445 void operator=(const HandleScope&);
446 void* operator new(size_t size);
447 void operator delete(void*, size_t);
448
ager@chromium.org3811b432009-10-28 14:53:37 +0000449 // This Data class is accessible internally as HandleScopeData through a
450 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000451 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000453 internal::Object** next;
454 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000455 int level;
456
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000459 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 }
461 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000462
lrn@chromium.org303ada72010-10-27 09:33:13 +0000463 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464
lrn@chromium.org303ada72010-10-27 09:33:13 +0000465 internal::Object** prev_next_;
466 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000468 // Allow for the active closing of HandleScopes which allows to pass a handle
469 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000471 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 friend class ImplementationUtilities;
474};
475
476
477// --- S p e c i a l o b j e c t s ---
478
479
480/**
481 * The superclass of values and API object templates.
482 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000483class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 private:
485 Data();
486};
487
488
489/**
490 * Pre-compilation data that can be associated with a script. This
491 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000492 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000493 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000495class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 public:
497 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000498
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000499 /**
500 * Pre-compiles the specified script (context-independent).
501 *
502 * \param input Pointer to UTF-8 script source code.
503 * \param length Length of UTF-8 script source code.
504 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000507 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000508 * Pre-compiles the specified script (context-independent).
509 *
510 * NOTE: Pre-compilation using this method cannot happen on another thread
511 * without using Lockers.
512 *
513 * \param source Script source code.
514 */
515 static ScriptData* PreCompile(Handle<String> source);
516
517 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000518 * Load previous pre-compilation data.
519 *
520 * \param data Pointer to data returned by a call to Data() of a previous
521 * ScriptData. Ownership is not transferred.
522 * \param length Length of data.
523 */
524 static ScriptData* New(const char* data, int length);
525
526 /**
527 * Returns the length of Data().
528 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000530
531 /**
532 * Returns a serialized representation of this ScriptData that can later be
533 * passed to New(). NOTE: Serialized data is platform-dependent.
534 */
535 virtual const char* Data() = 0;
536
537 /**
538 * Returns true if the source code could not be parsed.
539 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000540 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541};
542
543
544/**
545 * The origin, within a file, of a script.
546 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000547class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000549 inline ScriptOrigin(
550 Handle<Value> resource_name,
551 Handle<Integer> resource_line_offset = Handle<Integer>(),
552 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 : resource_name_(resource_name),
554 resource_line_offset_(resource_line_offset),
555 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000556 inline Handle<Value> ResourceName() const;
557 inline Handle<Integer> ResourceLineOffset() const;
558 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000560 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 Handle<Integer> resource_line_offset_;
562 Handle<Integer> resource_column_offset_;
563};
564
565
566/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000567 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000569class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 public:
571
572 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000573 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000574 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000575 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000576 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000577 * when New() returns
578 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
579 * using pre_data speeds compilation if it's done multiple times.
580 * Owned by caller, no references are kept when New() returns.
581 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000582 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000583 * available to compile event handlers.
584 * \return Compiled script object (context independent; when run it
585 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000587 static Local<Script> New(Handle<String> source,
588 ScriptOrigin* origin = NULL,
589 ScriptData* pre_data = NULL,
590 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591
mads.s.agercbaa0602008-08-14 13:41:48 +0000592 /**
593 * Compiles the specified script using the specified file name
594 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000595 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000597 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000598 * as the script's origin.
599 * \return Compiled script object (context independent; when run it
600 * will use the currently entered context).
601 */
602 static Local<Script> New(Handle<String> source,
603 Handle<Value> file_name);
604
605 /**
606 * Compiles the specified script (bound to current context).
607 *
608 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000609 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000610 * when Compile() returns
611 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
612 * using pre_data speeds compilation if it's done multiple times.
613 * Owned by caller, no references are kept when Compile() returns.
614 * \param script_data Arbitrary data associated with script. Using
615 * this has same effect as calling SetData(), but makes data available
616 * earlier (i.e. to compile event handlers).
617 * \return Compiled script object, bound to the context that was active
618 * when this function was called. When run it will always use this
619 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000620 */
621 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000622 ScriptOrigin* origin = NULL,
623 ScriptData* pre_data = NULL,
624 Handle<String> script_data = Handle<String>());
625
626 /**
627 * Compiles the specified script using the specified file name
628 * object (typically a string) as the script's origin.
629 *
630 * \param source Script source code.
631 * \param file_name File name to use as script's origin
632 * \param script_data Arbitrary data associated with script. Using
633 * this has same effect as calling SetData(), but makes data available
634 * earlier (i.e. to compile event handlers).
635 * \return Compiled script object, bound to the context that was active
636 * when this function was called. When run it will always use this
637 * context.
638 */
639 static Local<Script> Compile(Handle<String> source,
640 Handle<Value> file_name,
641 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000642
v8.team.kasperl727e9952008-09-02 14:56:44 +0000643 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000644 * Runs the script returning the resulting value. If the script is
645 * context independent (created using ::New) it will be run in the
646 * currently entered context. If it is context specific (created
647 * using ::Compile) it will be run in the context in which it was
648 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000649 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000651
652 /**
653 * Returns the script id value.
654 */
655 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000656
657 /**
658 * Associate an additional data object with the script. This is mainly used
659 * with the debugger as this data object is only available through the
660 * debugger API.
661 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000662 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663};
664
665
666/**
667 * An error message.
668 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000669class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000671 Local<String> Get() const;
672 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000674 /**
675 * Returns the resource name for the script from where the function causing
676 * the error originates.
677 */
ager@chromium.org32912102009-01-16 10:38:43 +0000678 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000680 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000681 * Returns the resource data for the script from where the function causing
682 * the error originates.
683 */
684 Handle<Value> GetScriptData() const;
685
686 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000687 * Exception stack trace. By default stack traces are not captured for
688 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
689 * to change this option.
690 */
691 Handle<StackTrace> GetStackTrace() const;
692
693 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000694 * Returns the number, 1-based, of the line where the error occurred.
695 */
ager@chromium.org32912102009-01-16 10:38:43 +0000696 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000698 /**
699 * Returns the index within the script of the first character where
700 * the error occurred.
701 */
ager@chromium.org32912102009-01-16 10:38:43 +0000702 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000703
704 /**
705 * Returns the index within the script of the last character where
706 * the error occurred.
707 */
ager@chromium.org32912102009-01-16 10:38:43 +0000708 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000709
710 /**
711 * Returns the index within the line of the first character where
712 * the error occurred.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000715
716 /**
717 * Returns the index within the line of the last character where
718 * the error occurred.
719 */
ager@chromium.org32912102009-01-16 10:38:43 +0000720 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000721
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 // TODO(1245381): Print to a string instead of on a FILE.
723 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000724
725 static const int kNoLineNumberInfo = 0;
726 static const int kNoColumnInfo = 0;
727};
728
729
730/**
731 * Representation of a JavaScript stack trace. The information collected is a
732 * snapshot of the execution stack and the information remains valid after
733 * execution continues.
734 */
735class V8EXPORT StackTrace {
736 public:
737 /**
738 * Flags that determine what information is placed captured for each
739 * StackFrame when grabbing the current stack trace.
740 */
741 enum StackTraceOptions {
742 kLineNumber = 1,
743 kColumnOffset = 1 << 1 | kLineNumber,
744 kScriptName = 1 << 2,
745 kFunctionName = 1 << 3,
746 kIsEval = 1 << 4,
747 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000748 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000749 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000750 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000751 };
752
753 /**
754 * Returns a StackFrame at a particular index.
755 */
756 Local<StackFrame> GetFrame(uint32_t index) const;
757
758 /**
759 * Returns the number of StackFrames.
760 */
761 int GetFrameCount() const;
762
763 /**
764 * Returns StackTrace as a v8::Array that contains StackFrame objects.
765 */
766 Local<Array> AsArray();
767
768 /**
769 * Grab a snapshot of the the current JavaScript execution stack.
770 *
771 * \param frame_limit The maximum number of stack frames we want to capture.
772 * \param options Enumerates the set of things we will capture for each
773 * StackFrame.
774 */
775 static Local<StackTrace> CurrentStackTrace(
776 int frame_limit,
777 StackTraceOptions options = kOverview);
778};
779
780
781/**
782 * A single JavaScript stack frame.
783 */
784class V8EXPORT StackFrame {
785 public:
786 /**
787 * Returns the number, 1-based, of the line for the associate function call.
788 * This method will return Message::kNoLineNumberInfo if it is unable to
789 * retrieve the line number, or if kLineNumber was not passed as an option
790 * when capturing the StackTrace.
791 */
792 int GetLineNumber() const;
793
794 /**
795 * Returns the 1-based column offset on the line for the associated function
796 * call.
797 * This method will return Message::kNoColumnInfo if it is unable to retrieve
798 * the column number, or if kColumnOffset was not passed as an option when
799 * capturing the StackTrace.
800 */
801 int GetColumn() const;
802
803 /**
804 * Returns the name of the resource that contains the script for the
805 * function for this StackFrame.
806 */
807 Local<String> GetScriptName() const;
808
809 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000810 * Returns the name of the resource that contains the script for the
811 * function for this StackFrame or sourceURL value if the script name
812 * is undefined and its source ends with //@ sourceURL=... string.
813 */
814 Local<String> GetScriptNameOrSourceURL() const;
815
816 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000817 * Returns the name of the function associated with this stack frame.
818 */
819 Local<String> GetFunctionName() const;
820
821 /**
822 * Returns whether or not the associated function is compiled via a call to
823 * eval().
824 */
825 bool IsEval() const;
826
827 /**
828 * Returns whther or not the associated function is called as a
829 * constructor via "new".
830 */
831 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832};
833
834
835// --- V a l u e ---
836
837
838/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000839 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000841class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 public:
843
844 /**
845 * Returns true if this value is the undefined value. See ECMA-262
846 * 4.3.10.
847 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000848 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849
850 /**
851 * Returns true if this value is the null value. See ECMA-262
852 * 4.3.11.
853 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000854 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
856 /**
857 * Returns true if this value is true.
858 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000859 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
861 /**
862 * Returns true if this value is false.
863 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000864 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865
866 /**
867 * Returns true if this value is an instance of the String type.
868 * See ECMA-262 8.4.
869 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000870 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
872 /**
873 * Returns true if this value is a function.
874 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000875 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
877 /**
878 * Returns true if this value is an array.
879 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000880 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
v8.team.kasperl727e9952008-09-02 14:56:44 +0000882 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 * Returns true if this value is an object.
884 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000885 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
v8.team.kasperl727e9952008-09-02 14:56:44 +0000887 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 * Returns true if this value is boolean.
889 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000890 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000891
v8.team.kasperl727e9952008-09-02 14:56:44 +0000892 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 * Returns true if this value is a number.
894 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000895 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
v8.team.kasperl727e9952008-09-02 14:56:44 +0000897 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 * Returns true if this value is external.
899 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000900 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
v8.team.kasperl727e9952008-09-02 14:56:44 +0000902 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 * Returns true if this value is a 32-bit signed integer.
904 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000905 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000907 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000908 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000909 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000910 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000911
912 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000913 * Returns true if this value is a Date.
914 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000915 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000916
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000917 /**
918 * Returns true if this value is a RegExp.
919 */
920 V8EXPORT bool IsRegExp() const;
921
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000922 V8EXPORT Local<Boolean> ToBoolean() const;
923 V8EXPORT Local<Number> ToNumber() const;
924 V8EXPORT Local<String> ToString() const;
925 V8EXPORT Local<String> ToDetailString() const;
926 V8EXPORT Local<Object> ToObject() const;
927 V8EXPORT Local<Integer> ToInteger() const;
928 V8EXPORT Local<Uint32> ToUint32() const;
929 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930
931 /**
932 * Attempts to convert a string to an array index.
933 * Returns an empty handle if the conversion fails.
934 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000935 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000937 V8EXPORT bool BooleanValue() const;
938 V8EXPORT double NumberValue() const;
939 V8EXPORT int64_t IntegerValue() const;
940 V8EXPORT uint32_t Uint32Value() const;
941 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942
943 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000944 V8EXPORT bool Equals(Handle<Value> that) const;
945 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000946
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000947 private:
948 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000949 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950};
951
952
953/**
954 * The superclass of primitive values. See ECMA-262 4.3.2.
955 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000956class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957
958
959/**
960 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
961 * or false value.
962 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000963class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000965 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 static inline Handle<Boolean> New(bool value);
967};
968
969
970/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000971 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000973class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000975
976 /**
977 * Returns the number of characters in this string.
978 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000979 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
v8.team.kasperl727e9952008-09-02 14:56:44 +0000981 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000982 * Returns the number of bytes in the UTF-8 encoded
983 * representation of this string.
984 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000985 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000986
987 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000988 * Write the contents of the string to an external buffer.
989 * If no arguments are given, expects the buffer to be large
990 * enough to hold the entire string and NULL terminator. Copies
991 * the contents of the string and the NULL terminator into the
992 * buffer.
993 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000994 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
995 * before the end of the buffer.
996 *
v8.team.kasperl727e9952008-09-02 14:56:44 +0000997 * Copies up to length characters into the output buffer.
998 * Only null-terminates if there is enough space in the buffer.
999 *
1000 * \param buffer The buffer into which the string will be copied.
1001 * \param start The starting position within the string at which
1002 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001003 * \param length The number of characters to copy from the string. For
1004 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001005 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001006 * \param hints Various hints that might affect performance of this or
1007 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001008 * \return The number of characters copied to the buffer excluding the null
1009 * terminator. For WriteUtf8: The number of bytes copied to the buffer
1010 * including the null terminator.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001011 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001012 enum WriteHints {
1013 NO_HINTS = 0,
1014 HINT_MANY_WRITES_EXPECTED = 1
1015 };
1016
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001017 V8EXPORT int Write(uint16_t* buffer,
1018 int start = 0,
1019 int length = -1,
1020 WriteHints hints = NO_HINTS) const; // UTF-16
1021 V8EXPORT int WriteAscii(char* buffer,
1022 int start = 0,
1023 int length = -1,
1024 WriteHints hints = NO_HINTS) const; // ASCII
1025 V8EXPORT int WriteUtf8(char* buffer,
1026 int length = -1,
1027 int* nchars_ref = NULL,
1028 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029
v8.team.kasperl727e9952008-09-02 14:56:44 +00001030 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001031 * A zero length string.
1032 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001033 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001034
1035 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001036 * Returns true if the string is external
1037 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001038 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039
v8.team.kasperl727e9952008-09-02 14:56:44 +00001040 /**
1041 * Returns true if the string is both external and ascii
1042 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001043 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001044
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001045 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001046 public:
1047 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001048
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001049 protected:
1050 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001051
1052 /**
1053 * Internally V8 will call this Dispose method when the external string
1054 * resource is no longer needed. The default implementation will use the
1055 * delete operator. This method can be overridden in subclasses to
1056 * control how allocated external string resources are disposed.
1057 */
1058 virtual void Dispose() { delete this; }
1059
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001060 private:
1061 // Disallow copying and assigning.
1062 ExternalStringResourceBase(const ExternalStringResourceBase&);
1063 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001064
1065 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001066 };
1067
v8.team.kasperl727e9952008-09-02 14:56:44 +00001068 /**
1069 * An ExternalStringResource is a wrapper around a two-byte string
1070 * buffer that resides outside V8's heap. Implement an
1071 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001072 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001073 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001074 class V8EXPORT ExternalStringResource
1075 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 public:
1077 /**
1078 * Override the destructor to manage the life cycle of the underlying
1079 * buffer.
1080 */
1081 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001082
1083 /**
1084 * The string data from the underlying buffer.
1085 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001087
1088 /**
1089 * The length of the string. That is, the number of two-byte characters.
1090 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001092
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 protected:
1094 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 };
1096
1097 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001098 * An ExternalAsciiStringResource is a wrapper around an ascii
1099 * string buffer that resides outside V8's heap. Implement an
1100 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001101 * underlying buffer. Note that the string data must be immutable
1102 * and that the data must be strict 7-bit ASCII, not Latin1 or
1103 * UTF-8, which would require special treatment internally in the
1104 * engine and, in the case of UTF-8, do not allow efficient indexing.
1105 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001106 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001108 class V8EXPORT ExternalAsciiStringResource
1109 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 public:
1111 /**
1112 * Override the destructor to manage the life cycle of the underlying
1113 * buffer.
1114 */
1115 virtual ~ExternalAsciiStringResource() {}
1116 /** The string data from the underlying buffer.*/
1117 virtual const char* data() const = 0;
1118 /** The number of ascii characters in the string.*/
1119 virtual size_t length() const = 0;
1120 protected:
1121 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 };
1123
1124 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001125 * Get the ExternalStringResource for an external string. Returns
1126 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001128 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129
1130 /**
1131 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001132 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001134 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001136 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137
1138 /**
1139 * Allocates a new string from either utf-8 encoded or ascii data.
1140 * The second parameter 'length' gives the buffer length.
1141 * If the data is utf-8 encoded, the caller must
1142 * be careful to supply the length parameter.
1143 * If it is not given, the function calls
1144 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001145 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001147 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
1149 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001150 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001153 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154
v8.team.kasperl727e9952008-09-02 14:56:44 +00001155 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001156 * Creates a new string by concatenating the left and the right strings
1157 * passed in as parameters.
1158 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001159 V8EXPORT static Local<String> Concat(Handle<String> left,
1160 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001161
1162 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001163 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001164 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001165 * resource will be disposed by calling its Dispose method. The caller of
1166 * this function should not otherwise delete or modify the resource. Neither
1167 * should the underlying buffer be deallocated or modified except through the
1168 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001169 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001170 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001171
ager@chromium.org6f10e412009-02-13 10:11:16 +00001172 /**
1173 * Associate an external string resource with this string by transforming it
1174 * in place so that existing references to this string in the JavaScript heap
1175 * will use the external string resource. The external string resource's
1176 * character contents needs to be equivalent to this string.
1177 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001178 * The string is not modified if the operation fails. See NewExternal for
1179 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001180 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001181 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182
v8.team.kasperl727e9952008-09-02 14:56:44 +00001183 /**
1184 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001185 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001186 * resource will be disposed by calling its Dispose method. The caller of
1187 * this function should not otherwise delete or modify the resource. Neither
1188 * should the underlying buffer be deallocated or modified except through the
1189 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001190 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001191 V8EXPORT static Local<String> NewExternal(
1192 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001193
ager@chromium.org6f10e412009-02-13 10:11:16 +00001194 /**
1195 * Associate an external string resource with this string by transforming it
1196 * in place so that existing references to this string in the JavaScript heap
1197 * will use the external string resource. The external string resource's
1198 * character contents needs to be equivalent to this string.
1199 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001200 * The string is not modified if the operation fails. See NewExternal for
1201 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001202 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001203 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001205 /**
1206 * Returns true if this string can be made external.
1207 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001208 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001209
kasper.lund7276f142008-07-30 08:49:36 +00001210 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001211 V8EXPORT static Local<String> NewUndetectable(const char* data,
1212 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213
kasper.lund7276f142008-07-30 08:49:36 +00001214 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001215 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1216 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217
1218 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001219 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001220 * you want to print the object. If conversion to a string fails
1221 * (eg. due to an exception in the toString() method of the object)
1222 * then the length() method returns 0 and the * operator returns
1223 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001224 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001225 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001226 public:
1227 explicit Utf8Value(Handle<v8::Value> obj);
1228 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001229 char* operator*() { return str_; }
1230 const char* operator*() const { return str_; }
1231 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001232 private:
1233 char* str_;
1234 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001235
1236 // Disallow copying and assigning.
1237 Utf8Value(const Utf8Value&);
1238 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001239 };
1240
1241 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242 * Converts an object to an ascii string.
1243 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001244 * If conversion to a string fails (eg. due to an exception in the toString()
1245 * method of the object) then the length() method returns 0 and the * operator
1246 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001248 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 public:
1250 explicit AsciiValue(Handle<v8::Value> obj);
1251 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001252 char* operator*() { return str_; }
1253 const char* operator*() const { return str_; }
1254 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 private:
1256 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001257 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001258
1259 // Disallow copying and assigning.
1260 AsciiValue(const AsciiValue&);
1261 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 };
1263
1264 /**
1265 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001266 * If conversion to a string fails (eg. due to an exception in the toString()
1267 * method of the object) then the length() method returns 0 and the * operator
1268 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001270 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 public:
1272 explicit Value(Handle<v8::Value> obj);
1273 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001274 uint16_t* operator*() { return str_; }
1275 const uint16_t* operator*() const { return str_; }
1276 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 private:
1278 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001279 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001280
1281 // Disallow copying and assigning.
1282 Value(const Value&);
1283 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001285
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001286 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001287 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1288 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289};
1290
1291
1292/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001293 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001295class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001297 V8EXPORT double Value() const;
1298 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001299 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001301 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001302 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303};
1304
1305
1306/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001307 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001309class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001311 V8EXPORT static Local<Integer> New(int32_t value);
1312 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1313 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001314 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001316 V8EXPORT Integer();
1317 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318};
1319
1320
1321/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001322 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001324class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001326 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001328 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329};
1330
1331
1332/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001333 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001335class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001337 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001339 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340};
1341
1342
1343/**
1344 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1345 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001346class Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001348 V8EXPORT static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001349
1350 /**
1351 * A specialization of Value::NumberValue that is more efficient
1352 * because we know the structure of this object.
1353 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001354 V8EXPORT double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001355
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001356 static inline Date* Cast(v8::Value* obj);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001357
1358 /**
1359 * Notification that the embedder has changed the time zone,
1360 * daylight savings time, or other date / time configuration
1361 * parameters. V8 keeps a cache of various values used for
1362 * date / time computation. This notification will reset
1363 * those cached values for the current context so that date /
1364 * time configuration changes would be reflected in the Date
1365 * object.
1366 *
1367 * This API should not be called more than needed as it will
1368 * negatively impact the performance of date operations.
1369 */
1370 V8EXPORT static void DateTimeConfigurationChangeNotification();
1371
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001372 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001373 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374};
1375
1376
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001377/**
1378 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1379 */
1380class RegExp : public Value {
1381 public:
1382 /**
1383 * Regular expression flag bits. They can be or'ed to enable a set
1384 * of flags.
1385 */
1386 enum Flags {
1387 kNone = 0,
1388 kGlobal = 1,
1389 kIgnoreCase = 2,
1390 kMultiline = 4
1391 };
1392
1393 /**
1394 * Creates a regular expression from the given pattern string and
1395 * the flags bit field. May throw a JavaScript exception as
1396 * described in ECMA-262, 15.10.4.1.
1397 *
1398 * For example,
1399 * RegExp::New(v8::String::New("foo"),
1400 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1401 * is equivalent to evaluating "/foo/gm".
1402 */
1403 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1404 Flags flags);
1405
1406 /**
1407 * Returns the value of the source property: a string representing
1408 * the regular expression.
1409 */
1410 V8EXPORT Local<String> GetSource() const;
1411
1412 /**
1413 * Returns the flags bit field.
1414 */
1415 V8EXPORT Flags GetFlags() const;
1416
1417 static inline RegExp* Cast(v8::Value* obj);
1418
1419 private:
1420 V8EXPORT static void CheckCast(v8::Value* obj);
1421};
1422
1423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424enum PropertyAttribute {
1425 None = 0,
1426 ReadOnly = 1 << 0,
1427 DontEnum = 1 << 1,
1428 DontDelete = 1 << 2
1429};
1430
ager@chromium.org3811b432009-10-28 14:53:37 +00001431enum ExternalArrayType {
1432 kExternalByteArray = 1,
1433 kExternalUnsignedByteArray,
1434 kExternalShortArray,
1435 kExternalUnsignedShortArray,
1436 kExternalIntArray,
1437 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001438 kExternalFloatArray,
1439 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001440};
1441
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001443 * Accessor[Getter|Setter] are used as callback functions when
1444 * setting|getting a particular property. See Object and ObjectTemplate's
1445 * method SetAccessor.
1446 */
1447typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1448 const AccessorInfo& info);
1449
1450
1451typedef void (*AccessorSetter)(Local<String> property,
1452 Local<Value> value,
1453 const AccessorInfo& info);
1454
1455
1456/**
1457 * Access control specifications.
1458 *
1459 * Some accessors should be accessible across contexts. These
1460 * accessors have an explicit access control parameter which specifies
1461 * the kind of cross-context access that should be allowed.
1462 *
1463 * Additionally, for security, accessors can prohibit overwriting by
1464 * accessors defined in JavaScript. For objects that have such
1465 * accessors either locally or in their prototype chain it is not
1466 * possible to overwrite the accessor by using __defineGetter__ or
1467 * __defineSetter__ from JavaScript code.
1468 */
1469enum AccessControl {
1470 DEFAULT = 0,
1471 ALL_CAN_READ = 1,
1472 ALL_CAN_WRITE = 1 << 1,
1473 PROHIBITS_OVERWRITING = 1 << 2
1474};
1475
1476
1477/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001478 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001480class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001482 V8EXPORT bool Set(Handle<Value> key,
1483 Handle<Value> value,
1484 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001485
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001486 V8EXPORT bool Set(uint32_t index,
1487 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001488
ager@chromium.orge2902be2009-06-08 12:21:35 +00001489 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001490 // overriding accessors or read-only properties.
1491 //
1492 // Note that if the object has an interceptor the property will be set
1493 // locally, but since the interceptor takes precedence the local property
1494 // will only be returned if the interceptor doesn't return a value.
1495 //
1496 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001497 V8EXPORT bool ForceSet(Handle<Value> key,
1498 Handle<Value> value,
1499 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001500
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001501 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001503 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001504
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 // TODO(1245389): Replace the type-specific versions of these
1506 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001507 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001508
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001509 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001510
1511 // Delete a property on this object bypassing interceptors and
1512 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001513 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001514
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001515 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001516
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001517 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001519 V8EXPORT bool SetAccessor(Handle<String> name,
1520 AccessorGetter getter,
1521 AccessorSetter setter = 0,
1522 Handle<Value> data = Handle<Value>(),
1523 AccessControl settings = DEFAULT,
1524 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001527 * Returns an array containing the names of the enumerable properties
1528 * of this object, including properties from prototype objects. The
1529 * array returned by this method contains the same values as would
1530 * be enumerated by a for-in statement over this object.
1531 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001532 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001533
1534 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535 * Get the prototype object. This does not skip objects marked to
1536 * be skipped by __proto__ and it does not consult the security
1537 * handler.
1538 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
1541 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001542 * Set the prototype object. This does not skip objects marked to
1543 * be skipped by __proto__ and it does not consult the security
1544 * handler.
1545 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001546 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001547
1548 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001549 * Finds an instance of the given function template in the prototype
1550 * chain.
1551 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001552 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1553 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001554
1555 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 * Call builtin Object.prototype.toString on this object.
1557 * This is different from Value::ToString() that may call
1558 * user-defined toString function. This one does not.
1559 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001560 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001562 /**
1563 * Returns the name of the function invoked as a constructor for this object.
1564 */
1565 V8EXPORT Local<String> GetConstructorName();
1566
kasper.lund212ac232008-07-16 07:07:30 +00001567 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001569 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001570 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001571 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001572 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001574 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001575 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001576
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001577 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001578 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 // Testers for local properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001581 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1582 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1583 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584
1585 /**
1586 * If result.IsEmpty() no real property was located in the prototype chain.
1587 * This means interceptors in the prototype chain are not called.
1588 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1590 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001591
1592 /**
1593 * If result.IsEmpty() no real property was located on the object or
1594 * in the prototype chain.
1595 * This means interceptors in the prototype chain are not called.
1596 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001597 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598
1599 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001600 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601
kasper.lund212ac232008-07-16 07:07:30 +00001602 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001603 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001605 /**
1606 * Turns on access check on the object if the object is an instance of
1607 * a template that has access check callbacks. If an object has no
1608 * access check info, the object cannot be accessed by anyone.
1609 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001610 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001611
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001612 /**
1613 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001614 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001615 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001616 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001617 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001618 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001619 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001620
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001621 /**
1622 * Access hidden properties on JavaScript objects. These properties are
1623 * hidden from the executing JavaScript and only accessible through the V8
1624 * C++ API. Hidden properties introduced by V8 internally (for example the
1625 * identity hash) are prefixed with "v8::".
1626 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001627 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1628 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1629 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001630
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001631 /**
1632 * Returns true if this is an instance of an api function (one
1633 * created from a function created from a function template) and has
1634 * been modified since it was created. Note that this method is
1635 * conservative and may return true for objects that haven't actually
1636 * been modified.
1637 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001638 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001639
1640 /**
1641 * Clone this object with a fast but shallow copy. Values will point
1642 * to the same values as the original object.
1643 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001644 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001646 /**
1647 * Set the backing store of the indexed properties to be managed by the
1648 * embedding layer. Access to the indexed properties will follow the rules
1649 * spelled out in CanvasPixelArray.
1650 * Note: The embedding program still owns the data and needs to ensure that
1651 * the backing store is preserved while V8 has a reference.
1652 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001653 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001654 V8EXPORT bool HasIndexedPropertiesInPixelData();
1655 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1656 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001657
ager@chromium.org3811b432009-10-28 14:53:37 +00001658 /**
1659 * Set the backing store of the indexed properties to be managed by the
1660 * embedding layer. Access to the indexed properties will follow the rules
1661 * spelled out for the CanvasArray subtypes in the WebGL specification.
1662 * Note: The embedding program still owns the data and needs to ensure that
1663 * the backing store is preserved while V8 has a reference.
1664 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001665 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1666 void* data,
1667 ExternalArrayType array_type,
1668 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001669 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1670 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1671 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1672 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001673
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001674 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001675 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001677 V8EXPORT Object();
1678 V8EXPORT static void CheckCast(Value* obj);
1679 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1680 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001681
1682 /**
1683 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001684 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001685 */
1686 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687};
1688
1689
1690/**
1691 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1692 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001693class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001695 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696
ager@chromium.org3e875802009-06-29 08:26:34 +00001697 /**
1698 * Clones an element at index |index|. Returns an empty
1699 * handle if cloning fails (for any reason).
1700 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001701 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001702
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001703 V8EXPORT static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001704 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001707 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708};
1709
1710
1711/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001712 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001714class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001716 V8EXPORT Local<Object> NewInstance() const;
1717 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1718 V8EXPORT Local<Value> Call(Handle<Object> recv,
1719 int argc,
1720 Handle<Value> argv[]);
1721 V8EXPORT void SetName(Handle<String> name);
1722 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001723
1724 /**
1725 * Returns zero based line number of function body and
1726 * kLineOffsetNotFound if no information available.
1727 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001728 V8EXPORT int GetScriptLineNumber() const;
1729 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001730 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001731 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001732 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001733 V8EXPORT Function();
1734 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735};
1736
1737
1738/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001739 * A JavaScript value that wraps a C++ void*. This type of value is
1740 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001742 *
1743 * The Wrap function V8 will return the most optimal Value object wrapping the
1744 * C++ void*. The type of the value is not guaranteed to be an External object
1745 * and no assumptions about its type should be made. To access the wrapped
1746 * value Unwrap should be used, all other operations on that object will lead
1747 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001749class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001751 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001752 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001753
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001754 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001755 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001756 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001758 V8EXPORT External();
1759 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001760 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001761 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762};
1763
1764
1765// --- T e m p l a t e s ---
1766
1767
1768/**
1769 * The superclass of object and function templates.
1770 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001771class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 public:
1773 /** Adds a property to each instance created by this template.*/
1774 void Set(Handle<String> name, Handle<Data> value,
1775 PropertyAttribute attributes = None);
1776 inline void Set(const char* name, Handle<Data> value);
1777 private:
1778 Template();
1779
1780 friend class ObjectTemplate;
1781 friend class FunctionTemplate;
1782};
1783
1784
1785/**
1786 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001787 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 * including the receiver, the number and values of arguments, and
1789 * the holder of the function.
1790 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001791class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 public:
1793 inline int Length() const;
1794 inline Local<Value> operator[](int i) const;
1795 inline Local<Function> Callee() const;
1796 inline Local<Object> This() const;
1797 inline Local<Object> Holder() const;
1798 inline bool IsConstructCall() const;
1799 inline Local<Value> Data() const;
1800 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001801 static const int kDataIndex = 0;
1802 static const int kCalleeIndex = -1;
1803 static const int kHolderIndex = -2;
1804
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001806 inline Arguments(internal::Object** implicit_args,
1807 internal::Object** values,
1808 int length,
1809 bool is_construct_call);
1810 internal::Object** implicit_args_;
1811 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001813 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814};
1815
1816
1817/**
1818 * The information passed to an accessor callback about the context
1819 * of the property access.
1820 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001821class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001823 inline AccessorInfo(internal::Object** args)
1824 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 inline Local<Value> Data() const;
1826 inline Local<Object> This() const;
1827 inline Local<Object> Holder() const;
1828 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001829 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830};
1831
1832
1833typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1834
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 * NamedProperty[Getter|Setter] are used as interceptors on object.
1837 * See ObjectTemplate::SetNamedPropertyHandler.
1838 */
1839typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1840 const AccessorInfo& info);
1841
1842
1843/**
1844 * Returns the value if the setter intercepts the request.
1845 * Otherwise, returns an empty handle.
1846 */
1847typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1848 Local<Value> value,
1849 const AccessorInfo& info);
1850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851/**
1852 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001853 * The result is an integer encoding property attributes (like v8::None,
1854 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001856typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1857 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858
1859
1860/**
1861 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001862 * The return value is true if the property could be deleted and false
1863 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 */
1865typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1866 const AccessorInfo& info);
1867
1868/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001869 * Returns an array containing the names of the properties the named
1870 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 */
1872typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1873
v8.team.kasperl727e9952008-09-02 14:56:44 +00001874
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001876 * Returns the value of the property if the getter intercepts the
1877 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 */
1879typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1880 const AccessorInfo& info);
1881
1882
1883/**
1884 * Returns the value if the setter intercepts the request.
1885 * Otherwise, returns an empty handle.
1886 */
1887typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1888 Local<Value> value,
1889 const AccessorInfo& info);
1890
1891
1892/**
1893 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001894 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001896typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1897 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898
1899/**
1900 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001901 * The return value is true if the property could be deleted and false
1902 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 */
1904typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1905 const AccessorInfo& info);
1906
v8.team.kasperl727e9952008-09-02 14:56:44 +00001907/**
1908 * Returns an array containing the indices of the properties the
1909 * indexed property getter intercepts.
1910 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1912
1913
1914/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001915 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 */
1917enum AccessType {
1918 ACCESS_GET,
1919 ACCESS_SET,
1920 ACCESS_HAS,
1921 ACCESS_DELETE,
1922 ACCESS_KEYS
1923};
1924
v8.team.kasperl727e9952008-09-02 14:56:44 +00001925
1926/**
1927 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001928 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001929 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001930typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 Local<Value> key,
1932 AccessType type,
1933 Local<Value> data);
1934
v8.team.kasperl727e9952008-09-02 14:56:44 +00001935
1936/**
1937 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001938 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001939 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001940typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 uint32_t index,
1942 AccessType type,
1943 Local<Value> data);
1944
1945
1946/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001947 * A FunctionTemplate is used to create functions at runtime. There
1948 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001949 * context. The lifetime of the created function is equal to the
1950 * lifetime of the context. So in case the embedder needs to create
1951 * temporary functions that can be collected using Scripts is
1952 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 *
1954 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001955 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 * A FunctionTemplate has a corresponding instance template which is
1958 * used to create object instances when the function is used as a
1959 * constructor. Properties added to the instance template are added to
1960 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 *
1962 * A FunctionTemplate can have a prototype template. The prototype template
1963 * is used to create the prototype object of the function.
1964 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001965 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001967 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1969 * t->Set("func_property", v8::Number::New(1));
1970 *
1971 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1972 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1973 * proto_t->Set("proto_const", v8::Number::New(2));
1974 *
1975 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1976 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1977 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1978 * instance_t->Set("instance_property", Number::New(3));
1979 *
1980 * v8::Local<v8::Function> function = t->GetFunction();
1981 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001982 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 *
1984 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001985 * and "instance" for the instance object created above. The function
1986 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001988 * \code
1989 * func_property in function == true;
1990 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001992 * function.prototype.proto_method() invokes 'InvokeCallback'
1993 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001995 * instance instanceof function == true;
1996 * instance.instance_accessor calls 'InstanceAccessorCallback'
1997 * instance.instance_property == 3;
1998 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002000 * A FunctionTemplate can inherit from another one by calling the
2001 * FunctionTemplate::Inherit method. The following graph illustrates
2002 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002004 * \code
2005 * FunctionTemplate Parent -> Parent() . prototype -> { }
2006 * ^ ^
2007 * | Inherit(Parent) | .__proto__
2008 * | |
2009 * FunctionTemplate Child -> Child() . prototype -> { }
2010 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002012 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2013 * object of the Child() function has __proto__ pointing to the
2014 * Parent() function's prototype object. An instance of the Child
2015 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002017 * Let Parent be the FunctionTemplate initialized in the previous
2018 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002020 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 * Local<FunctionTemplate> parent = t;
2022 * Local<FunctionTemplate> child = FunctionTemplate::New();
2023 * child->Inherit(parent);
2024 *
2025 * Local<Function> child_function = child->GetFunction();
2026 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002027 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002029 * The Child function and Child instance will have the following
2030 * properties:
2031 *
2032 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002034 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002036 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002038class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 public:
2040 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002041 static Local<FunctionTemplate> New(
2042 InvocationCallback callback = 0,
2043 Handle<Value> data = Handle<Value>(),
2044 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 /** Returns the unique function instance in the current execution context.*/
2046 Local<Function> GetFunction();
2047
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 /**
2049 * Set the call-handler callback for a FunctionTemplate. This
2050 * callback is called whenever the function created from this
2051 * FunctionTemplate is called.
2052 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 void SetCallHandler(InvocationCallback callback,
2054 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055
v8.team.kasperl727e9952008-09-02 14:56:44 +00002056 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057 Local<ObjectTemplate> InstanceTemplate();
2058
2059 /** Causes the function template to inherit from a parent function template.*/
2060 void Inherit(Handle<FunctionTemplate> parent);
2061
2062 /**
2063 * A PrototypeTemplate is the template used to create the prototype object
2064 * of the function created by this template.
2065 */
2066 Local<ObjectTemplate> PrototypeTemplate();
2067
v8.team.kasperl727e9952008-09-02 14:56:44 +00002068
2069 /**
2070 * Set the class name of the FunctionTemplate. This is used for
2071 * printing objects created with the function created from the
2072 * FunctionTemplate as its constructor.
2073 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 void SetClassName(Handle<String> name);
2075
2076 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002077 * Determines whether the __proto__ accessor ignores instances of
2078 * the function template. If instances of the function template are
2079 * ignored, __proto__ skips all instances and instead returns the
2080 * next object in the prototype chain.
2081 *
2082 * Call with a value of true to make the __proto__ accessor ignore
2083 * instances of the function template. Call with a value of false
2084 * to make the __proto__ accessor not ignore instances of the
2085 * function template. By default, instances of a function template
2086 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 */
2088 void SetHiddenPrototype(bool value);
2089
2090 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002091 * Returns true if the given object is an instance of this function
2092 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 */
2094 bool HasInstance(Handle<Value> object);
2095
2096 private:
2097 FunctionTemplate();
2098 void AddInstancePropertyAccessor(Handle<String> name,
2099 AccessorGetter getter,
2100 AccessorSetter setter,
2101 Handle<Value> data,
2102 AccessControl settings,
2103 PropertyAttribute attributes);
2104 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2105 NamedPropertySetter setter,
2106 NamedPropertyQuery query,
2107 NamedPropertyDeleter remover,
2108 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002109 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2111 IndexedPropertySetter setter,
2112 IndexedPropertyQuery query,
2113 IndexedPropertyDeleter remover,
2114 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002115 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2117 Handle<Value> data);
2118
2119 friend class Context;
2120 friend class ObjectTemplate;
2121};
2122
2123
2124/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002125 * An ObjectTemplate is used to create objects at runtime.
2126 *
2127 * Properties added to an ObjectTemplate are added to each object
2128 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002130class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002132 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002134
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 /** Creates a new instance of this template.*/
2136 Local<Object> NewInstance();
2137
2138 /**
2139 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002140 *
2141 * Whenever the property with the given name is accessed on objects
2142 * created from this ObjectTemplate the getter and setter callbacks
2143 * are called instead of getting and setting the property directly
2144 * on the JavaScript object.
2145 *
2146 * \param name The name of the property for which an accessor is added.
2147 * \param getter The callback to invoke when getting the property.
2148 * \param setter The callback to invoke when setting the property.
2149 * \param data A piece of data that will be passed to the getter and setter
2150 * callbacks whenever they are invoked.
2151 * \param settings Access control settings for the accessor. This is a bit
2152 * field consisting of one of more of
2153 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2154 * The default is to not allow cross-context access.
2155 * ALL_CAN_READ means that all cross-context reads are allowed.
2156 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2157 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2158 * cross-context access.
2159 * \param attribute The attributes of the property for which an accessor
2160 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 */
2162 void SetAccessor(Handle<String> name,
2163 AccessorGetter getter,
2164 AccessorSetter setter = 0,
2165 Handle<Value> data = Handle<Value>(),
2166 AccessControl settings = DEFAULT,
2167 PropertyAttribute attribute = None);
2168
2169 /**
2170 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002171 *
2172 * Whenever a named property is accessed on objects created from
2173 * this object template, the provided callback is invoked instead of
2174 * accessing the property directly on the JavaScript object.
2175 *
2176 * \param getter The callback to invoke when getting a property.
2177 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002178 * \param query The callback to invoke to check if a property is present,
2179 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002180 * \param deleter The callback to invoke when deleting a property.
2181 * \param enumerator The callback to invoke to enumerate all the named
2182 * properties of an object.
2183 * \param data A piece of data that will be passed to the callbacks
2184 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 */
2186 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2187 NamedPropertySetter setter = 0,
2188 NamedPropertyQuery query = 0,
2189 NamedPropertyDeleter deleter = 0,
2190 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002191 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192
2193 /**
2194 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 *
2196 * Whenever an indexed property is accessed on objects created from
2197 * this object template, the provided callback is invoked instead of
2198 * accessing the property directly on the JavaScript object.
2199 *
2200 * \param getter The callback to invoke when getting a property.
2201 * \param setter The callback to invoke when setting a property.
2202 * \param query The callback to invoke to check is an object has a property.
2203 * \param deleter The callback to invoke when deleting a property.
2204 * \param enumerator The callback to invoke to enumerate all the indexed
2205 * properties of an object.
2206 * \param data A piece of data that will be passed to the callbacks
2207 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 */
2209 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2210 IndexedPropertySetter setter = 0,
2211 IndexedPropertyQuery query = 0,
2212 IndexedPropertyDeleter deleter = 0,
2213 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002214 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002215
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 /**
2217 * Sets the callback to be used when calling instances created from
2218 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 * function.
2221 */
2222 void SetCallAsFunctionHandler(InvocationCallback callback,
2223 Handle<Value> data = Handle<Value>());
2224
v8.team.kasperl727e9952008-09-02 14:56:44 +00002225 /**
2226 * Mark object instances of the template as undetectable.
2227 *
2228 * In many ways, undetectable objects behave as though they are not
2229 * there. They behave like 'undefined' in conditionals and when
2230 * printed. However, properties can be accessed and called as on
2231 * normal objects.
2232 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 void MarkAsUndetectable();
2234
v8.team.kasperl727e9952008-09-02 14:56:44 +00002235 /**
2236 * Sets access check callbacks on the object template.
2237 *
2238 * When accessing properties on instances of this object template,
2239 * the access check callback will be called to determine whether or
2240 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002241 * The last parameter specifies whether access checks are turned
2242 * on by default on instances. If access checks are off by default,
2243 * they can be turned on on individual instances by calling
2244 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002245 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2247 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002248 Handle<Value> data = Handle<Value>(),
2249 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250
kasper.lund212ac232008-07-16 07:07:30 +00002251 /**
2252 * Gets the number of internal fields for objects generated from
2253 * this template.
2254 */
2255 int InternalFieldCount();
2256
2257 /**
2258 * Sets the number of internal fields for objects generated from
2259 * this template.
2260 */
2261 void SetInternalFieldCount(int value);
2262
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 private:
2264 ObjectTemplate();
2265 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2266 friend class FunctionTemplate;
2267};
2268
2269
2270/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002271 * A Signature specifies which receivers and arguments a function can
2272 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002274class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 public:
2276 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2277 Handle<FunctionTemplate>(),
2278 int argc = 0,
2279 Handle<FunctionTemplate> argv[] = 0);
2280 private:
2281 Signature();
2282};
2283
2284
2285/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002286 * A utility for determining the type of objects based on the template
2287 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002289class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 public:
2291 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2292 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2293 int match(Handle<Value> value);
2294 private:
2295 TypeSwitch();
2296};
2297
2298
2299// --- E x t e n s i o n s ---
2300
2301
2302/**
2303 * Ignore
2304 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002305class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 public:
2307 Extension(const char* name,
2308 const char* source = 0,
2309 int dep_count = 0,
2310 const char** deps = 0);
2311 virtual ~Extension() { }
2312 virtual v8::Handle<v8::FunctionTemplate>
2313 GetNativeFunction(v8::Handle<v8::String> name) {
2314 return v8::Handle<v8::FunctionTemplate>();
2315 }
2316
2317 const char* name() { return name_; }
2318 const char* source() { return source_; }
2319 int dependency_count() { return dep_count_; }
2320 const char** dependencies() { return deps_; }
2321 void set_auto_enable(bool value) { auto_enable_ = value; }
2322 bool auto_enable() { return auto_enable_; }
2323
2324 private:
2325 const char* name_;
2326 const char* source_;
2327 int dep_count_;
2328 const char** deps_;
2329 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002330
2331 // Disallow copying and assigning.
2332 Extension(const Extension&);
2333 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334};
2335
2336
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002337void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338
2339
2340/**
2341 * Ignore
2342 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002343class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 public:
2345 inline DeclareExtension(Extension* extension) {
2346 RegisterExtension(extension);
2347 }
2348};
2349
2350
2351// --- S t a t i c s ---
2352
2353
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002354Handle<Primitive> V8EXPORT Undefined();
2355Handle<Primitive> V8EXPORT Null();
2356Handle<Boolean> V8EXPORT True();
2357Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358
2359
2360/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002361 * A set of constraints that specifies the limits of the runtime's memory use.
2362 * You must set the heap size before initializing the VM - the size cannot be
2363 * adjusted after the VM is initialized.
2364 *
2365 * If you are using threads then you should hold the V8::Locker lock while
2366 * setting the stack limit and you must set a non-default stack limit separately
2367 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002369class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 public:
2371 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002372 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002374 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002376 int max_executable_size() { return max_executable_size_; }
2377 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002378 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002379 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2381 private:
2382 int max_young_space_size_;
2383 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002384 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385 uint32_t* stack_limit_;
2386};
2387
2388
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002389bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390
2391
2392// --- E x c e p t i o n s ---
2393
2394
2395typedef void (*FatalErrorCallback)(const char* location, const char* message);
2396
2397
2398typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2399
2400
2401/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002402 * Schedules an exception to be thrown when returning to JavaScript. When an
2403 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002405 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002406 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002407Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002408
2409/**
2410 * Create new error objects by calling the corresponding error object
2411 * constructor with the message.
2412 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002413class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 public:
2415 static Local<Value> RangeError(Handle<String> message);
2416 static Local<Value> ReferenceError(Handle<String> message);
2417 static Local<Value> SyntaxError(Handle<String> message);
2418 static Local<Value> TypeError(Handle<String> message);
2419 static Local<Value> Error(Handle<String> message);
2420};
2421
2422
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002423// --- C o u n t e r s C a l l b a c k s ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002425typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002427typedef void* (*CreateHistogramCallback)(const char* name,
2428 int min,
2429 int max,
2430 size_t buckets);
2431
2432typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2433
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002434// --- M e m o r y A l l o c a t i o n C a l l b a c k ---
2435 enum ObjectSpace {
2436 kObjectSpaceNewSpace = 1 << 0,
2437 kObjectSpaceOldPointerSpace = 1 << 1,
2438 kObjectSpaceOldDataSpace = 1 << 2,
2439 kObjectSpaceCodeSpace = 1 << 3,
2440 kObjectSpaceMapSpace = 1 << 4,
2441 kObjectSpaceLoSpace = 1 << 5,
2442
2443 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2444 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2445 kObjectSpaceLoSpace
2446 };
2447
2448 enum AllocationAction {
2449 kAllocationActionAllocate = 1 << 0,
2450 kAllocationActionFree = 1 << 1,
2451 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2452 };
2453
2454typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2455 AllocationAction action,
2456 int size);
2457
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458// --- F a i l e d A c c e s s C h e c k C a l l b a c k ---
2459typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2460 AccessType type,
2461 Local<Value> data);
2462
2463// --- G a r b a g e C o l l e c t i o n C a l l b a c k s
2464
2465/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002466 * Applications can register callback functions which will be called
2467 * before and after a garbage collection. Allocations are not
2468 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002469 * objects (set or delete properties for example) since it is possible
2470 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002472enum GCType {
2473 kGCTypeScavenge = 1 << 0,
2474 kGCTypeMarkSweepCompact = 1 << 1,
2475 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2476};
2477
2478enum GCCallbackFlags {
2479 kNoGCCallbackFlags = 0,
2480 kGCCallbackFlagCompacted = 1 << 0
2481};
2482
2483typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2484typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2485
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486typedef void (*GCCallback)();
2487
2488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002490 * Profiler modules.
2491 *
2492 * In V8, profiler consists of several modules: CPU profiler, and different
2493 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002494 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2495 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002496 */
2497enum ProfilerModules {
2498 PROFILER_MODULE_NONE = 0,
2499 PROFILER_MODULE_CPU = 1,
2500 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002501 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2502 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002503};
2504
2505
2506/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002507 * Collection of V8 heap information.
2508 *
2509 * Instances of this class can be passed to v8::V8::HeapStatistics to
2510 * get heap statistics from V8.
2511 */
2512class V8EXPORT HeapStatistics {
2513 public:
2514 HeapStatistics();
2515 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002516 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002517 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002518 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002519
2520 private:
2521 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002522 void set_total_heap_size_executable(size_t size) {
2523 total_heap_size_executable_ = size;
2524 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002525 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002526 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002527
2528 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002529 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002530 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002531 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002532
2533 friend class V8;
2534};
2535
2536
2537/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 * Container class for static utility functions.
2539 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002540class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002542 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543 static void SetFatalErrorHandler(FatalErrorCallback that);
2544
v8.team.kasperl727e9952008-09-02 14:56:44 +00002545 /**
2546 * Ignore out-of-memory exceptions.
2547 *
2548 * V8 running out of memory is treated as a fatal error by default.
2549 * This means that the fatal error handler is called and that V8 is
2550 * terminated.
2551 *
2552 * IgnoreOutOfMemoryException can be used to not treat a
2553 * out-of-memory situation as a fatal error. This way, the contexts
2554 * that did not cause the out of memory problem might be able to
2555 * continue execution.
2556 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 static void IgnoreOutOfMemoryException();
2558
v8.team.kasperl727e9952008-09-02 14:56:44 +00002559 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002560 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002561 * fatal errors such as out-of-memory situations.
2562 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 static bool IsDead();
2564
2565 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002566 * Adds a message listener.
2567 *
2568 * The same message listener can be added more than once and it that
2569 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 */
2571 static bool AddMessageListener(MessageCallback that,
2572 Handle<Value> data = Handle<Value>());
2573
2574 /**
2575 * Remove all message listeners from the specified callback function.
2576 */
2577 static void RemoveMessageListeners(MessageCallback that);
2578
2579 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002580 * Tells V8 to capture current stack trace when uncaught exception occurs
2581 * and report it to the message listeners. The option is off by default.
2582 */
2583 static void SetCaptureStackTraceForUncaughtExceptions(
2584 bool capture,
2585 int frame_limit = 10,
2586 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2587
2588 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002589 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 */
2591 static void SetFlagsFromString(const char* str, int length);
2592
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002593 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002594 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002595 */
2596 static void SetFlagsFromCommandLine(int* argc,
2597 char** argv,
2598 bool remove_flags);
2599
kasper.lund7276f142008-07-30 08:49:36 +00002600 /** Get the version string. */
2601 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602
2603 /**
2604 * Enables the host application to provide a mechanism for recording
2605 * statistics counters.
2606 */
2607 static void SetCounterFunction(CounterLookupCallback);
2608
2609 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002610 * Enables the host application to provide a mechanism for recording
2611 * histograms. The CreateHistogram function returns a
2612 * histogram which will later be passed to the AddHistogramSample
2613 * function.
2614 */
2615 static void SetCreateHistogramFunction(CreateHistogramCallback);
2616 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2617
2618 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 * Enables the computation of a sliding window of states. The sliding
2620 * window information is recorded in statistics counters.
2621 */
2622 static void EnableSlidingStateWindow();
2623
2624 /** Callback function for reporting failed access checks.*/
2625 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2626
2627 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002628 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002629 * garbage collection. Allocations are not allowed in the
2630 * callback function, you therefore cannot manipulate objects (set
2631 * or delete properties for example) since it is possible such
2632 * operations will result in the allocation of objects. It is possible
2633 * to specify the GCType filter for your callback. But it is not possible to
2634 * register the same callback function two times with different
2635 * GCType filters.
2636 */
2637 static void AddGCPrologueCallback(
2638 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2639
2640 /**
2641 * This function removes callback which was installed by
2642 * AddGCPrologueCallback function.
2643 */
2644 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2645
2646 /**
2647 * The function is deprecated. Please use AddGCPrologueCallback instead.
2648 * Enables the host application to receive a notification before a
2649 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002650 * callback function, you therefore cannot manipulate objects (set
2651 * or delete properties for example) since it is possible such
2652 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 */
2654 static void SetGlobalGCPrologueCallback(GCCallback);
2655
2656 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002657 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002658 * garbage collection. Allocations are not allowed in the
2659 * callback function, you therefore cannot manipulate objects (set
2660 * or delete properties for example) since it is possible such
2661 * operations will result in the allocation of objects. It is possible
2662 * to specify the GCType filter for your callback. But it is not possible to
2663 * register the same callback function two times with different
2664 * GCType filters.
2665 */
2666 static void AddGCEpilogueCallback(
2667 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2668
2669 /**
2670 * This function removes callback which was installed by
2671 * AddGCEpilogueCallback function.
2672 */
2673 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2674
2675 /**
2676 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2677 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002678 * major garbage collection. Allocations are not allowed in the
2679 * callback function, you therefore cannot manipulate objects (set
2680 * or delete properties for example) since it is possible such
2681 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 */
2683 static void SetGlobalGCEpilogueCallback(GCCallback);
2684
2685 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002686 * Enables the host application to provide a mechanism to be notified
2687 * and perform custom logging when V8 Allocates Executable Memory.
2688 */
2689 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2690 ObjectSpace space,
2691 AllocationAction action);
2692
2693 /**
2694 * This function removes callback which was installed by
2695 * AddMemoryAllocationCallback function.
2696 */
2697 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2698
2699 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002700 * Allows the host application to group objects together. If one
2701 * object in the group is alive, all objects in the group are alive.
2702 * After each garbage collection, object groups are removed. It is
2703 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002704 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002705 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002707 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708
2709 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002710 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002711 * initialize from scratch. This function is called implicitly if
2712 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 */
2714 static bool Initialize();
2715
kasper.lund7276f142008-07-30 08:49:36 +00002716 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002717 * Adjusts the amount of registered external memory. Used to give
2718 * V8 an indication of the amount of externally allocated memory
2719 * that is kept alive by JavaScript objects. V8 uses this to decide
2720 * when to perform global garbage collections. Registering
2721 * externally allocated memory will trigger global garbage
2722 * collections more often than otherwise in an attempt to garbage
2723 * collect the JavaScript objects keeping the externally allocated
2724 * memory alive.
2725 *
2726 * \param change_in_bytes the change in externally allocated memory
2727 * that is kept alive by JavaScript objects.
2728 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002729 */
2730 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2731
iposva@chromium.org245aa852009-02-10 00:49:54 +00002732 /**
2733 * Suspends recording of tick samples in the profiler.
2734 * When the V8 profiling mode is enabled (usually via command line
2735 * switches) this function suspends recording of tick samples.
2736 * Profiling ticks are discarded until ResumeProfiler() is called.
2737 *
2738 * See also the --prof and --prof_auto command line switches to
2739 * enable V8 profiling.
2740 */
2741 static void PauseProfiler();
2742
2743 /**
2744 * Resumes recording of tick samples in the profiler.
2745 * See also PauseProfiler().
2746 */
2747 static void ResumeProfiler();
2748
ager@chromium.org41826e72009-03-30 13:30:57 +00002749 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002750 * Return whether profiler is currently paused.
2751 */
2752 static bool IsProfilerPaused();
2753
2754 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002755 * Resumes specified profiler modules. Can be called several times to
2756 * mark the opening of a profiler events block with the given tag.
2757 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002758 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2759 * See ProfilerModules enum.
2760 *
2761 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002762 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002763 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002764 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002765
2766 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002767 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2768 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2769 * same tag value. There is no need for blocks to be properly nested.
2770 * The profiler is paused when the last opened block is closed.
2771 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002772 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2773 * See ProfilerModules enum.
2774 *
2775 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002776 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002777 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002778 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002779
2780 /**
2781 * Returns active (resumed) profiler modules.
2782 * See ProfilerModules enum.
2783 *
2784 * \returns active profiler modules.
2785 */
2786 static int GetActiveProfilerModules();
2787
2788 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002789 * If logging is performed into a memory buffer (via --logfile=*), allows to
2790 * retrieve previously written messages. This can be used for retrieving
2791 * profiler log data in the application. This function is thread-safe.
2792 *
2793 * Caller provides a destination buffer that must exist during GetLogLines
2794 * call. Only whole log lines are copied into the buffer.
2795 *
2796 * \param from_pos specified a point in a buffer to read from, 0 is the
2797 * beginning of a buffer. It is assumed that caller updates its current
2798 * position using returned size value from the previous call.
2799 * \param dest_buf destination buffer for log data.
2800 * \param max_size size of the destination buffer.
2801 * \returns actual size of log data copied into buffer.
2802 */
2803 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2804
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002805 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002806 * The minimum allowed size for a log lines buffer. If the size of
2807 * the buffer given will not be enough to hold a line of the maximum
2808 * length, an attempt to find a log line end in GetLogLines will
2809 * fail, and an empty result will be returned.
2810 */
2811 static const int kMinimumSizeForLogLinesBuffer = 2048;
2812
2813 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002814 * Retrieve the V8 thread id of the calling thread.
2815 *
2816 * The thread id for a thread should only be retrieved after the V8
2817 * lock has been acquired with a Locker object with that thread.
2818 */
2819 static int GetCurrentThreadId();
2820
2821 /**
2822 * Forcefully terminate execution of a JavaScript thread. This can
2823 * be used to terminate long-running scripts.
2824 *
2825 * TerminateExecution should only be called when then V8 lock has
2826 * been acquired with a Locker object. Therefore, in order to be
2827 * able to terminate long-running threads, preemption must be
2828 * enabled to allow the user of TerminateExecution to acquire the
2829 * lock.
2830 *
2831 * The termination is achieved by throwing an exception that is
2832 * uncatchable by JavaScript exception handlers. Termination
2833 * exceptions act as if they were caught by a C++ TryCatch exception
2834 * handlers. If forceful termination is used, any C++ TryCatch
2835 * exception handler that catches an exception should check if that
2836 * exception is a termination exception and immediately return if
2837 * that is the case. Returning immediately in that case will
2838 * continue the propagation of the termination exception if needed.
2839 *
2840 * The thread id passed to TerminateExecution must have been
2841 * obtained by calling GetCurrentThreadId on the thread in question.
2842 *
2843 * \param thread_id The thread id of the thread to terminate.
2844 */
2845 static void TerminateExecution(int thread_id);
2846
2847 /**
2848 * Forcefully terminate the current thread of JavaScript execution.
2849 *
2850 * This method can be used by any thread even if that thread has not
2851 * acquired the V8 lock with a Locker object.
2852 */
2853 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002854
2855 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00002856 * Is V8 terminating JavaScript execution.
2857 *
2858 * Returns true if JavaScript execution is currently terminating
2859 * because of a call to TerminateExecution. In that case there are
2860 * still JavaScript frames on the stack and the termination
2861 * exception is still active.
2862 */
2863 static bool IsExecutionTerminating();
2864
2865 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002866 * Releases any resources used by v8 and stops any utility threads
2867 * that may be running. Note that disposing v8 is permanent, it
2868 * cannot be reinitialized.
2869 *
2870 * It should generally not be necessary to dispose v8 before exiting
2871 * a process, this should happen automatically. It is only necessary
2872 * to use if the process needs the resources taken up by v8.
2873 */
2874 static bool Dispose();
2875
ager@chromium.org3811b432009-10-28 14:53:37 +00002876 /**
2877 * Get statistics about the heap memory usage.
2878 */
2879 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002880
2881 /**
2882 * Optional notification that the embedder is idle.
2883 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002884 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002885 * Returns true if the embedder should stop calling IdleNotification
2886 * until real work has been done. This indicates that V8 has done
2887 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002888 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002889 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002890
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002891 /**
2892 * Optional notification that the system is running low on memory.
2893 * V8 uses these notifications to attempt to free memory.
2894 */
2895 static void LowMemoryNotification();
2896
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002897 /**
2898 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002899 * these notifications to guide the GC heuristic. Returns the number
2900 * of context disposals - including this one - since the last time
2901 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002902 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002903 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905 private:
2906 V8();
2907
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002908 static internal::Object** GlobalizeReference(internal::Object** handle);
2909 static void DisposeGlobal(internal::Object** global_handle);
2910 static void MakeWeak(internal::Object** global_handle,
2911 void* data,
2912 WeakReferenceCallback);
2913 static void ClearWeak(internal::Object** global_handle);
2914 static bool IsGlobalNearDeath(internal::Object** global_handle);
2915 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002916
2917 template <class T> friend class Handle;
2918 template <class T> friend class Local;
2919 template <class T> friend class Persistent;
2920 friend class Context;
2921};
2922
2923
2924/**
2925 * An external exception handler.
2926 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002927class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928 public:
2929
2930 /**
2931 * Creates a new try/catch block and registers it with v8.
2932 */
2933 TryCatch();
2934
2935 /**
2936 * Unregisters and deletes this try/catch block.
2937 */
2938 ~TryCatch();
2939
2940 /**
2941 * Returns true if an exception has been caught by this try/catch block.
2942 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002943 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944
2945 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002946 * For certain types of exceptions, it makes no sense to continue
2947 * execution.
2948 *
2949 * Currently, the only type of exception that can be caught by a
2950 * TryCatch handler and for which it does not make sense to continue
2951 * is termination exception. Such exceptions are thrown when the
2952 * TerminateExecution methods are called to terminate a long-running
2953 * script.
2954 *
2955 * If CanContinue returns false, the correct action is to perform
2956 * any C++ cleanup needed and then return.
2957 */
2958 bool CanContinue() const;
2959
2960 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002961 * Throws the exception caught by this TryCatch in a way that avoids
2962 * it being caught again by this same TryCatch. As with ThrowException
2963 * it is illegal to execute any JavaScript operations after calling
2964 * ReThrow; the caller must return immediately to where the exception
2965 * is caught.
2966 */
2967 Handle<Value> ReThrow();
2968
2969 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970 * Returns the exception caught by this try/catch block. If no exception has
2971 * been caught an empty handle is returned.
2972 *
2973 * The returned handle is valid until this TryCatch block has been destroyed.
2974 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002975 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002976
2977 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002978 * Returns the .stack property of the thrown object. If no .stack
2979 * property is present an empty handle is returned.
2980 */
2981 Local<Value> StackTrace() const;
2982
2983 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002984 * Returns the message associated with this exception. If there is
2985 * no message associated an empty handle is returned.
2986 *
2987 * The returned handle is valid until this TryCatch block has been
2988 * destroyed.
2989 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002990 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002991
2992 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 * Clears any exceptions that may have been caught by this try/catch block.
2994 * After this method has been called, HasCaught() will return false.
2995 *
2996 * It is not necessary to clear a try/catch block before using it again; if
2997 * another exception is thrown the previously caught exception will just be
2998 * overwritten. However, it is often a good idea since it makes it easier
2999 * to determine which operation threw a given exception.
3000 */
3001 void Reset();
3002
v8.team.kasperl727e9952008-09-02 14:56:44 +00003003 /**
3004 * Set verbosity of the external exception handler.
3005 *
3006 * By default, exceptions that are caught by an external exception
3007 * handler are not reported. Call SetVerbose with true on an
3008 * external exception handler to have exceptions caught by the
3009 * handler reported as if they were not caught.
3010 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 void SetVerbose(bool value);
3012
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003013 /**
3014 * Set whether or not this TryCatch should capture a Message object
3015 * which holds source information about where the exception
3016 * occurred. True by default.
3017 */
3018 void SetCaptureMessage(bool value);
3019
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003020 private:
3021 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003022 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003023 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003024 bool is_verbose_ : 1;
3025 bool can_continue_ : 1;
3026 bool capture_message_ : 1;
3027 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003028
3029 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030};
3031
3032
3033// --- C o n t e x t ---
3034
3035
3036/**
3037 * Ignore
3038 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003039class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003040 public:
3041 ExtensionConfiguration(int name_count, const char* names[])
3042 : name_count_(name_count), names_(names) { }
3043 private:
3044 friend class ImplementationUtilities;
3045 int name_count_;
3046 const char** names_;
3047};
3048
3049
3050/**
3051 * A sandboxed execution context with its own set of built-in objects
3052 * and functions.
3053 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003054class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003055 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003056 /**
3057 * Returns the global proxy object or global object itself for
3058 * detached contexts.
3059 *
3060 * Global proxy object is a thin wrapper whose prototype points to
3061 * actual context's global object with the properties like Object, etc.
3062 * This is done that way for security reasons (for more details see
3063 * https://wiki.mozilla.org/Gecko:SplitWindow).
3064 *
3065 * Please note that changes to global proxy object prototype most probably
3066 * would break VM---v8 expects only global object as a prototype of
3067 * global proxy object.
3068 *
3069 * If DetachGlobal() has been invoked, Global() would return actual global
3070 * object until global is reattached with ReattachGlobal().
3071 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003072 Local<Object> Global();
3073
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003074 /**
3075 * Detaches the global object from its context before
3076 * the global object can be reused to create a new context.
3077 */
3078 void DetachGlobal();
3079
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003080 /**
3081 * Reattaches a global object to a context. This can be used to
3082 * restore the connection between a global object and a context
3083 * after DetachGlobal has been called.
3084 *
3085 * \param global_object The global object to reattach to the
3086 * context. For this to work, the global object must be the global
3087 * object that was associated with this context before a call to
3088 * DetachGlobal.
3089 */
3090 void ReattachGlobal(Handle<Object> global_object);
3091
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003092 /** Creates a new context.
3093 *
3094 * Returns a persistent handle to the newly allocated context. This
3095 * persistent handle has to be disposed when the context is no
3096 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003097 *
3098 * \param extensions An optional extension configuration containing
3099 * the extensions to be installed in the newly created context.
3100 *
3101 * \param global_template An optional object template from which the
3102 * global object for the newly created context will be created.
3103 *
3104 * \param global_object An optional global object to be reused for
3105 * the newly created context. This global object must have been
3106 * created by a previous call to Context::New with the same global
3107 * template. The state of the global object will be completely reset
3108 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003109 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003110 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003111 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003112 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3113 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114
kasper.lund44510672008-07-25 07:37:58 +00003115 /** Returns the last entered context. */
3116 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117
kasper.lund44510672008-07-25 07:37:58 +00003118 /** Returns the context that is on the top of the stack. */
3119 static Local<Context> GetCurrent();
3120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003121 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003122 * Returns the context of the calling JavaScript code. That is the
3123 * context of the top-most JavaScript frame. If there are no
3124 * JavaScript frames an empty handle is returned.
3125 */
3126 static Local<Context> GetCalling();
3127
3128 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 * Sets the security token for the context. To access an object in
3130 * another context, the security tokens must match.
3131 */
3132 void SetSecurityToken(Handle<Value> token);
3133
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003134 /** Restores the security token to the default value. */
3135 void UseDefaultSecurityToken();
3136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137 /** Returns the security token of this context.*/
3138 Handle<Value> GetSecurityToken();
3139
v8.team.kasperl727e9952008-09-02 14:56:44 +00003140 /**
3141 * Enter this context. After entering a context, all code compiled
3142 * and run is compiled and run in this context. If another context
3143 * is already entered, this old context is saved so it can be
3144 * restored when the new context is exited.
3145 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003147
3148 /**
3149 * Exit this context. Exiting the current context restores the
3150 * context that was in place when entering the current context.
3151 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003152 void Exit();
3153
v8.team.kasperl727e9952008-09-02 14:56:44 +00003154 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155 bool HasOutOfMemoryException();
3156
v8.team.kasperl727e9952008-09-02 14:56:44 +00003157 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003158 static bool InContext();
3159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003160 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003161 * Associate an additional data object with the context. This is mainly used
3162 * with the debugger to provide additional information on the context through
3163 * the debugger API.
3164 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003165 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003166 Local<Value> GetData();
3167
3168 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 * Stack-allocated class which sets the execution context for all
3170 * operations executed within a local scope.
3171 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003172 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003173 public:
3174 inline Scope(Handle<Context> context) : context_(context) {
3175 context_->Enter();
3176 }
3177 inline ~Scope() { context_->Exit(); }
3178 private:
3179 Handle<Context> context_;
3180 };
3181
3182 private:
3183 friend class Value;
3184 friend class Script;
3185 friend class Object;
3186 friend class Function;
3187};
3188
3189
3190/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003191 * Multiple threads in V8 are allowed, but only one thread at a time
3192 * is allowed to use V8. The definition of 'using V8' includes
3193 * accessing handles or holding onto object pointers obtained from V8
3194 * handles. It is up to the user of V8 to ensure (perhaps with
3195 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003196 *
3197 * If you wish to start using V8 in a thread you can do this by constructing
3198 * a v8::Locker object. After the code using V8 has completed for the
3199 * current thread you can call the destructor. This can be combined
3200 * with C++ scope-based construction as follows:
3201 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003202 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003203 * ...
3204 * {
3205 * v8::Locker locker;
3206 * ...
3207 * // Code using V8 goes here.
3208 * ...
3209 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003210 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003211 *
3212 * If you wish to stop using V8 in a thread A you can do this by either
3213 * by destroying the v8::Locker object as above or by constructing a
3214 * v8::Unlocker object:
3215 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003216 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003217 * {
3218 * v8::Unlocker unlocker;
3219 * ...
3220 * // Code not using V8 goes here while V8 can run in another thread.
3221 * ...
3222 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003223 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003224 *
3225 * The Unlocker object is intended for use in a long-running callback
3226 * from V8, where you want to release the V8 lock for other threads to
3227 * use.
3228 *
3229 * The v8::Locker is a recursive lock. That is, you can lock more than
3230 * once in a given thread. This can be useful if you have code that can
3231 * be called either from code that holds the lock or from code that does
3232 * not. The Unlocker is not recursive so you can not have several
3233 * Unlockers on the stack at once, and you can not use an Unlocker in a
3234 * thread that is not inside a Locker's scope.
3235 *
3236 * An unlocker will unlock several lockers if it has to and reinstate
3237 * the correct depth of locking on its destruction. eg.:
3238 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003239 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003240 * // V8 not locked.
3241 * {
3242 * v8::Locker locker;
3243 * // V8 locked.
3244 * {
3245 * v8::Locker another_locker;
3246 * // V8 still locked (2 levels).
3247 * {
3248 * v8::Unlocker unlocker;
3249 * // V8 not locked.
3250 * }
3251 * // V8 locked again (2 levels).
3252 * }
3253 * // V8 still locked (1 level).
3254 * }
3255 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003256 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003258class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 public:
3260 Unlocker();
3261 ~Unlocker();
3262};
3263
3264
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003265class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 public:
3267 Locker();
3268 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003269
3270 /**
3271 * Start preemption.
3272 *
3273 * When preemption is started, a timer is fired every n milli seconds
3274 * that will switch between multiple threads that are in contention
3275 * for the V8 lock.
3276 */
3277 static void StartPreemption(int every_n_ms);
3278
3279 /**
3280 * Stop preemption.
3281 */
3282 static void StopPreemption();
3283
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003284 /**
3285 * Returns whether or not the locker is locked by the current thread.
3286 */
3287 static bool IsLocked();
3288
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003289 /**
3290 * Returns whether v8::Locker is being used by this V8 instance.
3291 */
3292 static bool IsActive() { return active_; }
3293
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294 private:
3295 bool has_lock_;
3296 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003297
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003298 static bool active_;
3299
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003300 // Disallow copying and assigning.
3301 Locker(const Locker&);
3302 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303};
3304
3305
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003306/**
3307 * An interface for exporting data from V8, using "push" model.
3308 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003309class V8EXPORT OutputStream { // NOLINT
3310 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003311 enum OutputEncoding {
3312 kAscii = 0 // 7-bit ASCII.
3313 };
3314 enum WriteResult {
3315 kContinue = 0,
3316 kAbort = 1
3317 };
3318 virtual ~OutputStream() {}
3319 /** Notify about the end of stream. */
3320 virtual void EndOfStream() = 0;
3321 /** Get preferred output chunk size. Called only once. */
3322 virtual int GetChunkSize() { return 1024; }
3323 /** Get preferred output encoding. Called only once. */
3324 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3325 /**
3326 * Writes the next chunk of snapshot data into the stream. Writing
3327 * can be stopped by returning kAbort as function result. EndOfStream
3328 * will not be called in case writing was aborted.
3329 */
3330 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3331};
3332
3333
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003334/**
3335 * An interface for reporting progress and controlling long-running
3336 * activities.
3337 */
3338class V8EXPORT ActivityControl { // NOLINT
3339 public:
3340 enum ControlOption {
3341 kContinue = 0,
3342 kAbort = 1
3343 };
3344 virtual ~ActivityControl() {}
3345 /**
3346 * Notify about current progress. The activity can be stopped by
3347 * returning kAbort as the callback result.
3348 */
3349 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3350};
3351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003352
3353// --- I m p l e m e n t a t i o n ---
3354
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003355
3356namespace internal {
3357
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003358static const int kApiPointerSize = sizeof(void*); // NOLINT
3359static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003360
3361// Tag information for HeapObject.
3362const int kHeapObjectTag = 1;
3363const int kHeapObjectTagSize = 2;
3364const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3365
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003366// Tag information for Smi.
3367const int kSmiTag = 0;
3368const int kSmiTagSize = 1;
3369const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3370
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003371template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003372
3373// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003374template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003375 static const int kSmiShiftSize = 0;
3376 static const int kSmiValueSize = 31;
3377 static inline int SmiToInt(internal::Object* value) {
3378 int shift_bits = kSmiTagSize + kSmiShiftSize;
3379 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3380 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3381 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003382
3383 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3384 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003385 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003386 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003387};
3388
3389// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003390template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003391 static const int kSmiShiftSize = 31;
3392 static const int kSmiValueSize = 32;
3393 static inline int SmiToInt(internal::Object* value) {
3394 int shift_bits = kSmiTagSize + kSmiShiftSize;
3395 // Shift down and throw away top 32 bits.
3396 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3397 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003398
3399 // To maximize the range of pointers that can be encoded
3400 // in the available 32 bits, we require them to be 8 bytes aligned.
3401 // This gives 2 ^ (32 + 3) = 32G address space covered.
3402 // It might be not enough to cover stack allocated objects on some platforms.
3403 static const int kPointerAlignment = 3;
3404
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003405 static const uintptr_t kEncodablePointerMask =
3406 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003407
3408 static const int kPointerToSmiShift =
3409 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003410};
3411
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003412typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3413const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3414const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003415const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003416 PlatformSmiTagging::kEncodablePointerMask;
3417const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003418
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003419template <size_t ptr_size> struct InternalConstants;
3420
3421// Internal constants for 32-bit systems.
3422template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003423 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003424};
3425
3426// Internal constants for 64-bit systems.
3427template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003428 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003429};
3430
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003431/**
3432 * This class exports constants and functionality from within v8 that
3433 * is necessary to implement inline functions in the v8 api. Don't
3434 * depend on functions and constants defined here.
3435 */
3436class Internals {
3437 public:
3438
3439 // These values match non-compiler-dependent values defined within
3440 // the implementation of v8.
3441 static const int kHeapObjectMapOffset = 0;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003442 static const int kMapInstanceTypeOffset = kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003443 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003444 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003445
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003446 static const int kProxyProxyOffset = kApiPointerSize;
3447 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003448 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003449 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003450
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003451 static const int kJSObjectType = 0xa0;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003452 static const int kFirstNonstringType = 0x80;
3453 static const int kProxyType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003454
3455 static inline bool HasHeapObjectTag(internal::Object* value) {
3456 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3457 kHeapObjectTag);
3458 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003459
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003460 static inline bool HasSmiTag(internal::Object* value) {
3461 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3462 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003463
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003464 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003465 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003466 }
3467
3468 static inline int GetInstanceType(internal::Object* obj) {
3469 typedef internal::Object O;
3470 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3471 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3472 }
3473
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003474 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003475 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003476 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3477 }
3478
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003479 static inline void* GetExternalPointer(internal::Object* obj) {
3480 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003481 return GetExternalPointerFromSmi(obj);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003482 } else if (GetInstanceType(obj) == kProxyType) {
3483 return ReadField<void*>(obj, kProxyProxyOffset);
3484 } else {
3485 return NULL;
3486 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003487 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003488
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003489 static inline bool IsExternalTwoByteString(int instance_type) {
3490 int representation = (instance_type & kFullStringRepresentationMask);
3491 return representation == kExternalTwoByteRepresentationTag;
3492 }
3493
3494 template <typename T>
3495 static inline T ReadField(Object* ptr, int offset) {
3496 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3497 return *reinterpret_cast<T*>(addr);
3498 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003499};
3500
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003501} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003502
3503
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504template <class T>
3505Handle<T>::Handle() : val_(0) { }
3506
3507
3508template <class T>
3509Local<T>::Local() : Handle<T>() { }
3510
3511
3512template <class T>
3513Local<T> Local<T>::New(Handle<T> that) {
3514 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003515 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3517}
3518
3519
3520template <class T>
3521Persistent<T> Persistent<T>::New(Handle<T> that) {
3522 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003523 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003524 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3525}
3526
3527
3528template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003529bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003530 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003531 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003532}
3533
3534
3535template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003536bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003538 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539}
3540
3541
3542template <class T>
3543void Persistent<T>::Dispose() {
3544 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003545 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546}
3547
3548
3549template <class T>
3550Persistent<T>::Persistent() : Handle<T>() { }
3551
3552template <class T>
3553void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003554 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3555 parameters,
3556 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003557}
3558
3559template <class T>
3560void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003561 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562}
3563
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003564
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003565Arguments::Arguments(internal::Object** implicit_args,
3566 internal::Object** values, int length,
3567 bool is_construct_call)
3568 : implicit_args_(implicit_args),
3569 values_(values),
3570 length_(length),
3571 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003572
3573
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574Local<Value> Arguments::operator[](int i) const {
3575 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3576 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3577}
3578
3579
3580Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003581 return Local<Function>(reinterpret_cast<Function*>(
3582 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583}
3584
3585
3586Local<Object> Arguments::This() const {
3587 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3588}
3589
3590
3591Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003592 return Local<Object>(reinterpret_cast<Object*>(
3593 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594}
3595
3596
3597Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003598 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599}
3600
3601
3602bool Arguments::IsConstructCall() const {
3603 return is_construct_call_;
3604}
3605
3606
3607int Arguments::Length() const {
3608 return length_;
3609}
3610
3611
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612template <class T>
3613Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003614 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3615 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616 return Local<T>(reinterpret_cast<T*>(after));
3617}
3618
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003619Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620 return resource_name_;
3621}
3622
3623
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003624Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003625 return resource_line_offset_;
3626}
3627
3628
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003629Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630 return resource_column_offset_;
3631}
3632
3633
3634Handle<Boolean> Boolean::New(bool value) {
3635 return value ? True() : False();
3636}
3637
3638
3639void Template::Set(const char* name, v8::Handle<Data> value) {
3640 Set(v8::String::New(name), value);
3641}
3642
3643
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003644Local<Value> Object::GetInternalField(int index) {
3645#ifndef V8_ENABLE_CHECKS
3646 Local<Value> quick_result = UncheckedGetInternalField(index);
3647 if (!quick_result.IsEmpty()) return quick_result;
3648#endif
3649 return CheckedGetInternalField(index);
3650}
3651
3652
3653Local<Value> Object::UncheckedGetInternalField(int index) {
3654 typedef internal::Object O;
3655 typedef internal::Internals I;
3656 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003657 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003658 // If the object is a plain JSObject, which is the common case,
3659 // we know where to find the internal fields and can return the
3660 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003661 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003662 O* value = I::ReadField<O*>(obj, offset);
3663 O** result = HandleScope::CreateHandle(value);
3664 return Local<Value>(reinterpret_cast<Value*>(result));
3665 } else {
3666 return Local<Value>();
3667 }
3668}
3669
3670
3671void* External::Unwrap(Handle<v8::Value> obj) {
3672#ifdef V8_ENABLE_CHECKS
3673 return FullUnwrap(obj);
3674#else
3675 return QuickUnwrap(obj);
3676#endif
3677}
3678
3679
3680void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3681 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003682 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003683 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003684}
3685
3686
3687void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003688 typedef internal::Object O;
3689 typedef internal::Internals I;
3690
3691 O* obj = *reinterpret_cast<O**>(this);
3692
3693 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3694 // If the object is a plain JSObject, which is the common case,
3695 // we know where to find the internal fields and can return the
3696 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003697 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003698 O* value = I::ReadField<O*>(obj, offset);
3699 return I::GetExternalPointer(value);
3700 }
3701
3702 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003703}
3704
3705
3706String* String::Cast(v8::Value* value) {
3707#ifdef V8_ENABLE_CHECKS
3708 CheckCast(value);
3709#endif
3710 return static_cast<String*>(value);
3711}
3712
3713
3714String::ExternalStringResource* String::GetExternalStringResource() const {
3715 typedef internal::Object O;
3716 typedef internal::Internals I;
3717 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003718 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003719 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003720 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3721 result = reinterpret_cast<String::ExternalStringResource*>(value);
3722 } else {
3723 result = NULL;
3724 }
3725#ifdef V8_ENABLE_CHECKS
3726 VerifyExternalStringResource(result);
3727#endif
3728 return result;
3729}
3730
3731
3732bool Value::IsString() const {
3733#ifdef V8_ENABLE_CHECKS
3734 return FullIsString();
3735#else
3736 return QuickIsString();
3737#endif
3738}
3739
3740bool Value::QuickIsString() const {
3741 typedef internal::Object O;
3742 typedef internal::Internals I;
3743 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3744 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003745 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003746}
3747
3748
3749Number* Number::Cast(v8::Value* value) {
3750#ifdef V8_ENABLE_CHECKS
3751 CheckCast(value);
3752#endif
3753 return static_cast<Number*>(value);
3754}
3755
3756
3757Integer* Integer::Cast(v8::Value* value) {
3758#ifdef V8_ENABLE_CHECKS
3759 CheckCast(value);
3760#endif
3761 return static_cast<Integer*>(value);
3762}
3763
3764
3765Date* Date::Cast(v8::Value* value) {
3766#ifdef V8_ENABLE_CHECKS
3767 CheckCast(value);
3768#endif
3769 return static_cast<Date*>(value);
3770}
3771
3772
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00003773RegExp* RegExp::Cast(v8::Value* value) {
3774#ifdef V8_ENABLE_CHECKS
3775 CheckCast(value);
3776#endif
3777 return static_cast<RegExp*>(value);
3778}
3779
3780
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003781Object* Object::Cast(v8::Value* value) {
3782#ifdef V8_ENABLE_CHECKS
3783 CheckCast(value);
3784#endif
3785 return static_cast<Object*>(value);
3786}
3787
3788
3789Array* Array::Cast(v8::Value* value) {
3790#ifdef V8_ENABLE_CHECKS
3791 CheckCast(value);
3792#endif
3793 return static_cast<Array*>(value);
3794}
3795
3796
3797Function* Function::Cast(v8::Value* value) {
3798#ifdef V8_ENABLE_CHECKS
3799 CheckCast(value);
3800#endif
3801 return static_cast<Function*>(value);
3802}
3803
3804
3805External* External::Cast(v8::Value* value) {
3806#ifdef V8_ENABLE_CHECKS
3807 CheckCast(value);
3808#endif
3809 return static_cast<External*>(value);
3810}
3811
3812
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003813Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003814 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003815}
3816
3817
3818Local<Object> AccessorInfo::This() const {
3819 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3820}
3821
3822
3823Local<Object> AccessorInfo::Holder() const {
3824 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3825}
3826
3827
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003828/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003829 * \example shell.cc
3830 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003831 * command-line and executes them.
3832 */
3833
3834
3835/**
3836 * \example process.cc
3837 */
3838
3839
3840} // namespace v8
3841
3842
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003843#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844#undef TYPE_CHECK
3845
3846
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003847#endif // V8_H_