blob: d8de00ceca2fd92c6829a36708bbcc7a086cdf5b [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2007-2008 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
41#include <stdio.h>
42
43#ifdef _WIN32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
ager@chromium.org5ec48922009-05-05 07:25:34 +000048typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052typedef int int32_t;
53typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000054typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058
59// Setup for Windows DLL export/import. When building the V8 DLL the
60// BUILDING_V8_SHARED needs to be defined. When building a program which uses
61// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
62// static library or building a program which uses the V8 static library neither
63// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000064// The reason for having both V8EXPORT and V8EXPORT_INLINE is that classes which
65// have their code inside this header file need to have __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066// when building the DLL but cannot have __declspec(dllimport) when building
67// a program which uses the DLL.
68#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
69#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
70 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071#endif
72
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT __declspec(dllexport)
75#define V8EXPORT_INLINE __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT __declspec(dllimport)
78#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000080#define V8EXPORT
81#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#endif // BUILDING_V8_SHARED
83
84#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000085
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086#include <stdint.h>
87
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000088// Setup for Linux shared library export. There is no need to distinguish
89// between building or using the V8 shared library, but we should not
90// export symbols when we are building a static library.
91#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000092#define V8EXPORT __attribute__ ((visibility("default")))
93#define V8EXPORT_INLINE __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000094#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095#define V8EXPORT
96#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000097#endif // defined(__GNUC__) && (__GNUC__ >= 4)
98
99#endif // _WIN32
100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000102 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 */
104namespace v8 {
105
106class Context;
107class String;
108class Value;
109class Utils;
110class Number;
111class Object;
112class Array;
113class Int32;
114class Uint32;
115class External;
116class Primitive;
117class Boolean;
118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
123template <class T> class Handle;
124template <class T> class Local;
125template <class T> class Persistent;
126class FunctionTemplate;
127class ObjectTemplate;
128class Data;
129
130
131// --- W e a k H a n d l e s
132
133
134/**
135 * A weak reference callback function.
136 *
137 * \param object the weak global object to be reclaimed by the garbage collector
138 * \param parameter the value passed in when making the weak global object
139 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000140typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 void* parameter);
142
143
144// --- H a n d l e s ---
145
146#define TYPE_CHECK(T, S) \
147 while (false) { \
148 *(static_cast<T**>(0)) = static_cast<S*>(0); \
149 }
150
151/**
152 * An object reference managed by the v8 garbage collector.
153 *
154 * All objects returned from v8 have to be tracked by the garbage
155 * collector so that it knows that the objects are still alive. Also,
156 * because the garbage collector may move objects, it is unsafe to
157 * point directly to an object. Instead, all objects are stored in
158 * handles which are known by the garbage collector and updated
159 * whenever an object moves. Handles should always be passed by value
160 * (except in cases like out-parameters) and they should never be
161 * allocated on the heap.
162 *
163 * There are two types of handles: local and persistent handles.
164 * Local handles are light-weight and transient and typically used in
165 * local operations. They are managed by HandleScopes. Persistent
166 * handles can be used when storing objects across several independent
167 * operations and have to be explicitly deallocated when they're no
168 * longer used.
169 *
170 * It is safe to extract the object stored in the handle by
171 * dereferencing the handle (for instance, to extract the Object* from
172 * an Handle<Object>); the value will still be governed by a handle
173 * behind the scenes and the same rules apply to these values as to
174 * their handles.
175 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000176template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 public:
178
179 /**
180 * Creates an empty handle.
181 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000182 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
184 /**
185 * Creates a new handle for the specified value.
186 */
187 explicit Handle(T* val) : val_(val) { }
188
189 /**
190 * Creates a handle for the contents of the specified handle. This
191 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000192 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 * incompatible handles, for instance from a Handle<String> to a
194 * Handle<Number> it will cause a compiletime error. Assigning
195 * between compatible handles, for instance assigning a
196 * Handle<String> to a variable declared as Handle<Value>, is legal
197 * because String is a subclass of Value.
198 */
199 template <class S> inline Handle(Handle<S> that)
200 : val_(reinterpret_cast<T*>(*that)) {
201 /**
202 * This check fails when trying to convert between incompatible
203 * handles. For example, converting from a Handle<String> to a
204 * Handle<Number>.
205 */
206 TYPE_CHECK(T, S);
207 }
208
209 /**
210 * Returns true if the handle is empty.
211 */
ager@chromium.org32912102009-01-16 10:38:43 +0000212 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000214 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000216 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
218 /**
219 * Sets the handle to be empty. IsEmpty() will then return true.
220 */
221 void Clear() { this->val_ = 0; }
222
223 /**
224 * Checks whether two handles are the same.
225 * Returns true if both are empty, or if the objects
226 * to which they refer are identical.
227 * The handles' references are not checked.
228 */
ager@chromium.org32912102009-01-16 10:38:43 +0000229 template <class S> bool operator==(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 void** a = reinterpret_cast<void**>(**this);
231 void** b = reinterpret_cast<void**>(*that);
232 if (a == 0) return b == 0;
233 if (b == 0) return false;
234 return *a == *b;
235 }
236
237 /**
238 * Checks whether two handles are different.
239 * Returns true if only one of the handles is empty, or if
240 * the objects to which they refer are different.
241 * The handles' references are not checked.
242 */
ager@chromium.org32912102009-01-16 10:38:43 +0000243 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 return !operator==(that);
245 }
246
247 template <class S> static inline Handle<T> Cast(Handle<S> that) {
248 if (that.IsEmpty()) return Handle<T>();
249 return Handle<T>(T::Cast(*that));
250 }
251
252 private:
253 T* val_;
254};
255
256
257/**
258 * A light-weight stack-allocated object handle. All operations
259 * that return objects from within v8 return them in local handles. They
260 * are created within HandleScopes, and all local handles allocated within a
261 * handle scope are destroyed when the handle scope is destroyed. Hence it
262 * is not necessary to explicitly deallocate local handles.
263 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000264template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000266 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 template <class S> inline Local(Local<S> that)
268 : Handle<T>(reinterpret_cast<T*>(*that)) {
269 /**
270 * This check fails when trying to convert between incompatible
271 * handles. For example, converting from a Handle<String> to a
272 * Handle<Number>.
273 */
274 TYPE_CHECK(T, S);
275 }
276 template <class S> inline Local(S* that) : Handle<T>(that) { }
277 template <class S> static inline Local<T> Cast(Local<S> that) {
278 if (that.IsEmpty()) return Local<T>();
279 return Local<T>(T::Cast(*that));
280 }
281
282 /** Create a local handle for the content of another handle.
283 * The referee is kept alive by the local handle even when
284 * the original handle is destroyed/disposed.
285 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000286 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287};
288
289
290/**
291 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000292 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 * allocated, a Persistent handle remains valid until it is explicitly
294 * disposed.
295 *
296 * A persistent handle contains a reference to a storage cell within
297 * the v8 engine which holds an object value and which is updated by
298 * the garbage collector whenever the object is moved. A new storage
299 * cell can be created using Persistent::New and existing handles can
300 * be disposed using Persistent::Dispose. Since persistent handles
301 * are passed by value you may have many persistent handle objects
302 * that point to the same storage cell. For instance, if you pass a
303 * persistent handle as an argument to a function you will not get two
304 * different storage cells but rather two references to the same
305 * storage cell.
306 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000307template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 public:
309
310 /**
311 * Creates an empty persistent handle that doesn't point to any
312 * storage cell.
313 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000314 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315
316 /**
317 * Creates a persistent handle for the same storage cell as the
318 * specified handle. This constructor allows you to pass persistent
319 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000320 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000322 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 * between compatible persistent handles, for instance assigning a
324 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000325 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 */
327 template <class S> inline Persistent(Persistent<S> that)
328 : Handle<T>(reinterpret_cast<T*>(*that)) {
329 /**
330 * This check fails when trying to convert between incompatible
331 * handles. For example, converting from a Handle<String> to a
332 * Handle<Number>.
333 */
334 TYPE_CHECK(T, S);
335 }
336
337 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
338
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000339 /**
340 * "Casts" a plain handle which is known to be a persistent handle
341 * to a persistent handle.
342 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 template <class S> explicit inline Persistent(Handle<S> that)
344 : Handle<T>(*that) { }
345
346 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
347 if (that.IsEmpty()) return Persistent<T>();
348 return Persistent<T>(T::Cast(*that));
349 }
350
351 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000352 * Creates a new persistent handle for an existing local or
353 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000355 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356
357 /**
358 * Releases the storage cell referenced by this persistent handle.
359 * Does not remove the reference to the cell from any handles.
360 * This handle's reference, and any any other references to the storage
361 * cell remain and IsEmpty will still return false.
362 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000363 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364
365 /**
366 * Make the reference to this object weak. When only weak handles
367 * refer to the object, the garbage collector will perform a
368 * callback to the given V8::WeakReferenceCallback function, passing
369 * it the object reference and the given parameters.
370 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000371 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372
373 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000374 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 /**
377 *Checks if the handle holds the only reference to an object.
378 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000379 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380
381 /**
382 * Returns true if the handle's reference is weak.
383 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000384 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385
386 private:
387 friend class ImplementationUtilities;
388 friend class ObjectTemplate;
389};
390
391
v8.team.kasperl727e9952008-09-02 14:56:44 +0000392 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 * A stack-allocated class that governs a number of local handles.
394 * After a handle scope has been created, all local handles will be
395 * allocated within that handle scope until either the handle scope is
396 * deleted or another handle scope is created. If there is already a
397 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000398 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 * new handles will again be allocated in the original handle scope.
400 *
401 * After the handle scope of a local handle has been deleted the
402 * garbage collector will no longer track the object stored in the
403 * handle and may deallocate it. The behavior of accessing a handle
404 * for which the handle scope has been deleted is undefined.
405 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000406class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000408 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000410 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 * Closes the handle scope and returns the value as a handle in the
414 * previous scope, which is the new current scope after the call.
415 */
416 template <class T> Local<T> Close(Handle<T> value);
417
418 /**
419 * Counts the number of allocated handles.
420 */
421 static int NumberOfHandles();
422
423 /**
424 * Creates a new handle with the given value.
425 */
426 static void** CreateHandle(void* value);
427
428 private:
429 // Make it impossible to create heap-allocated or illegal handle
430 // scopes by disallowing certain operations.
431 HandleScope(const HandleScope&);
432 void operator=(const HandleScope&);
433 void* operator new(size_t size);
434 void operator delete(void*, size_t);
435
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000436 // This Data class is accessible internally through a typedef in the
437 // ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000438 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 public:
440 int extensions;
441 void** next;
442 void** limit;
443 inline void Initialize() {
444 extensions = -1;
445 next = limit = NULL;
446 }
447 };
448
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000449 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000451 // Allow for the active closing of HandleScopes which allows to pass a handle
452 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453 bool is_closed_;
454 void** RawClose(void** value);
455
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456 friend class ImplementationUtilities;
457};
458
459
460// --- S p e c i a l o b j e c t s ---
461
462
463/**
464 * The superclass of values and API object templates.
465 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000466class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 private:
468 Data();
469};
470
471
472/**
473 * Pre-compilation data that can be associated with a script. This
474 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000475 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000476 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000478class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 public:
480 virtual ~ScriptData() { }
481 static ScriptData* PreCompile(const char* input, int length);
482 static ScriptData* New(unsigned* data, int length);
483
484 virtual int Length() = 0;
485 virtual unsigned* Data() = 0;
486};
487
488
489/**
490 * The origin, within a file, of a script.
491 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000492class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000494 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 Handle<Integer> resource_line_offset = Handle<Integer>(),
496 Handle<Integer> resource_column_offset = Handle<Integer>())
497 : resource_name_(resource_name),
498 resource_line_offset_(resource_line_offset),
499 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000500 inline Handle<Value> ResourceName() const;
501 inline Handle<Integer> ResourceLineOffset() const;
502 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000504 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 Handle<Integer> resource_line_offset_;
506 Handle<Integer> resource_column_offset_;
507};
508
509
510/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000511 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000513class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 public:
515
516 /**
517 * Compiles the specified script. The ScriptOrigin* and ScriptData*
518 * parameters are owned by the caller of Script::Compile. No
519 * references to these objects are kept after compilation finishes.
520 */
521 static Local<Script> Compile(Handle<String> source,
522 ScriptOrigin* origin = NULL,
523 ScriptData* pre_data = NULL);
524
mads.s.agercbaa0602008-08-14 13:41:48 +0000525 /**
526 * Compiles the specified script using the specified file name
527 * object (typically a string) as the script's origin.
528 */
529 static Local<Script> Compile(Handle<String> source,
530 Handle<Value> file_name);
531
v8.team.kasperl727e9952008-09-02 14:56:44 +0000532 /**
533 * Runs the script returning the resulting value.
534 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000536
537 /**
538 * Returns the script id value.
539 */
540 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000541
542 /**
543 * Associate an additional data object with the script. This is mainly used
544 * with the debugger as this data object is only available through the
545 * debugger API.
546 */
547 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548};
549
550
551/**
552 * An error message.
553 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000554class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000556 Local<String> Get() const;
557 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000559 /**
560 * Returns the resource name for the script from where the function causing
561 * the error originates.
562 */
ager@chromium.org32912102009-01-16 10:38:43 +0000563 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000565 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000566 * Returns the resource data for the script from where the function causing
567 * the error originates.
568 */
569 Handle<Value> GetScriptData() const;
570
571 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000572 * Returns the number, 1-based, of the line where the error occurred.
573 */
ager@chromium.org32912102009-01-16 10:38:43 +0000574 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000576 /**
577 * Returns the index within the script of the first character where
578 * the error occurred.
579 */
ager@chromium.org32912102009-01-16 10:38:43 +0000580 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000581
582 /**
583 * Returns the index within the script of the last character where
584 * the error occurred.
585 */
ager@chromium.org32912102009-01-16 10:38:43 +0000586 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000587
588 /**
589 * Returns the index within the line of the first character where
590 * the error occurred.
591 */
ager@chromium.org32912102009-01-16 10:38:43 +0000592 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000593
594 /**
595 * Returns the index within the line of the last character where
596 * the error occurred.
597 */
ager@chromium.org32912102009-01-16 10:38:43 +0000598 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000599
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 // TODO(1245381): Print to a string instead of on a FILE.
601 static void PrintCurrentStackTrace(FILE* out);
602};
603
604
605// --- V a l u e ---
606
607
608/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000609 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000611class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612 public:
613
614 /**
615 * Returns true if this value is the undefined value. See ECMA-262
616 * 4.3.10.
617 */
ager@chromium.org32912102009-01-16 10:38:43 +0000618 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619
620 /**
621 * Returns true if this value is the null value. See ECMA-262
622 * 4.3.11.
623 */
ager@chromium.org32912102009-01-16 10:38:43 +0000624 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
626 /**
627 * Returns true if this value is true.
628 */
ager@chromium.org32912102009-01-16 10:38:43 +0000629 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
631 /**
632 * Returns true if this value is false.
633 */
ager@chromium.org32912102009-01-16 10:38:43 +0000634 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
636 /**
637 * Returns true if this value is an instance of the String type.
638 * See ECMA-262 8.4.
639 */
ager@chromium.org32912102009-01-16 10:38:43 +0000640 bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
642 /**
643 * Returns true if this value is a function.
644 */
ager@chromium.org32912102009-01-16 10:38:43 +0000645 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
647 /**
648 * Returns true if this value is an array.
649 */
ager@chromium.org32912102009-01-16 10:38:43 +0000650 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651
v8.team.kasperl727e9952008-09-02 14:56:44 +0000652 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 * Returns true if this value is an object.
654 */
ager@chromium.org32912102009-01-16 10:38:43 +0000655 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656
v8.team.kasperl727e9952008-09-02 14:56:44 +0000657 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 * Returns true if this value is boolean.
659 */
ager@chromium.org32912102009-01-16 10:38:43 +0000660 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000661
v8.team.kasperl727e9952008-09-02 14:56:44 +0000662 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 * Returns true if this value is a number.
664 */
ager@chromium.org32912102009-01-16 10:38:43 +0000665 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666
v8.team.kasperl727e9952008-09-02 14:56:44 +0000667 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 * Returns true if this value is external.
669 */
ager@chromium.org32912102009-01-16 10:38:43 +0000670 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
v8.team.kasperl727e9952008-09-02 14:56:44 +0000672 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 * Returns true if this value is a 32-bit signed integer.
674 */
ager@chromium.org32912102009-01-16 10:38:43 +0000675 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000677 /**
678 * Returns true if this value is a Date.
679 */
ager@chromium.org32912102009-01-16 10:38:43 +0000680 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000681
ager@chromium.org32912102009-01-16 10:38:43 +0000682 Local<Boolean> ToBoolean() const;
683 Local<Number> ToNumber() const;
684 Local<String> ToString() const;
685 Local<String> ToDetailString() const;
686 Local<Object> ToObject() const;
687 Local<Integer> ToInteger() const;
688 Local<Uint32> ToUint32() const;
689 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
691 /**
692 * Attempts to convert a string to an array index.
693 * Returns an empty handle if the conversion fails.
694 */
ager@chromium.org32912102009-01-16 10:38:43 +0000695 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696
ager@chromium.org32912102009-01-16 10:38:43 +0000697 bool BooleanValue() const;
698 double NumberValue() const;
699 int64_t IntegerValue() const;
700 uint32_t Uint32Value() const;
701 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
703 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 bool Equals(Handle<Value> that) const;
705 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706};
707
708
709/**
710 * The superclass of primitive values. See ECMA-262 4.3.2.
711 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000712class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713
714
715/**
716 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
717 * or false value.
718 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000719class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000721 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 static inline Handle<Boolean> New(bool value);
723};
724
725
726/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000727 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000729class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000731
732 /**
733 * Returns the number of characters in this string.
734 */
ager@chromium.org32912102009-01-16 10:38:43 +0000735 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736
v8.team.kasperl727e9952008-09-02 14:56:44 +0000737 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000738 * Returns the number of bytes in the UTF-8 encoded
739 * representation of this string.
740 */
ager@chromium.org32912102009-01-16 10:38:43 +0000741 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000742
743 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000744 * Write the contents of the string to an external buffer.
745 * If no arguments are given, expects the buffer to be large
746 * enough to hold the entire string and NULL terminator. Copies
747 * the contents of the string and the NULL terminator into the
748 * buffer.
749 *
750 * Copies up to length characters into the output buffer.
751 * Only null-terminates if there is enough space in the buffer.
752 *
753 * \param buffer The buffer into which the string will be copied.
754 * \param start The starting position within the string at which
755 * copying begins.
756 * \param length The number of bytes to copy from the string.
757 * \return The number of characters copied to the buffer
758 * excluding the NULL terminator.
759 */
ager@chromium.org32912102009-01-16 10:38:43 +0000760 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
761 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
762 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763
v8.team.kasperl727e9952008-09-02 14:56:44 +0000764 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000765 * A zero length string.
766 */
767 static v8::Local<v8::String> Empty();
768
769 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000770 * Returns true if the string is external
771 */
ager@chromium.org32912102009-01-16 10:38:43 +0000772 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
v8.team.kasperl727e9952008-09-02 14:56:44 +0000774 /**
775 * Returns true if the string is both external and ascii
776 */
ager@chromium.org32912102009-01-16 10:38:43 +0000777 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000778 /**
779 * An ExternalStringResource is a wrapper around a two-byte string
780 * buffer that resides outside V8's heap. Implement an
781 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000782 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000783 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000784 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 public:
786 /**
787 * Override the destructor to manage the life cycle of the underlying
788 * buffer.
789 */
790 virtual ~ExternalStringResource() {}
791 /** The string data from the underlying buffer.*/
792 virtual const uint16_t* data() const = 0;
793 /** The length of the string. That is, the number of two-byte characters.*/
794 virtual size_t length() const = 0;
795 protected:
796 ExternalStringResource() {}
797 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000798 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 ExternalStringResource(const ExternalStringResource&);
800 void operator=(const ExternalStringResource&);
801 };
802
803 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000804 * An ExternalAsciiStringResource is a wrapper around an ascii
805 * string buffer that resides outside V8's heap. Implement an
806 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000807 * underlying buffer. Note that the string data must be immutable
808 * and that the data must be strict 7-bit ASCII, not Latin1 or
809 * UTF-8, which would require special treatment internally in the
810 * engine and, in the case of UTF-8, do not allow efficient indexing.
811 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000812 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000814 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 public:
816 /**
817 * Override the destructor to manage the life cycle of the underlying
818 * buffer.
819 */
820 virtual ~ExternalAsciiStringResource() {}
821 /** The string data from the underlying buffer.*/
822 virtual const char* data() const = 0;
823 /** The number of ascii characters in the string.*/
824 virtual size_t length() const = 0;
825 protected:
826 ExternalAsciiStringResource() {}
827 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000828 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
830 void operator=(const ExternalAsciiStringResource&);
831 };
832
833 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000834 * Get the ExternalStringResource for an external string. Returns
835 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 */
ager@chromium.org32912102009-01-16 10:38:43 +0000837 ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838
839 /**
840 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000841 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 */
ager@chromium.org32912102009-01-16 10:38:43 +0000843 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844
845 static String* Cast(v8::Value* obj);
846
847 /**
848 * Allocates a new string from either utf-8 encoded or ascii data.
849 * The second parameter 'length' gives the buffer length.
850 * If the data is utf-8 encoded, the caller must
851 * be careful to supply the length parameter.
852 * If it is not given, the function calls
853 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000854 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 */
856 static Local<String> New(const char* data, int length = -1);
857
858 /** Allocates a new string from utf16 data.*/
859 static Local<String> New(const uint16_t* data, int length = -1);
860
861 /** Creates a symbol. Returns one if it exists already.*/
862 static Local<String> NewSymbol(const char* data, int length = -1);
863
v8.team.kasperl727e9952008-09-02 14:56:44 +0000864 /**
865 * Creates a new external string using the data defined in the given
866 * resource. The resource is deleted when the external string is no
867 * longer live on V8's heap. The caller of this function should not
868 * delete or modify the resource. Neither should the underlying buffer be
869 * deallocated or modified except through the destructor of the
870 * external string resource.
871 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000873
ager@chromium.org6f10e412009-02-13 10:11:16 +0000874 /**
875 * Associate an external string resource with this string by transforming it
876 * in place so that existing references to this string in the JavaScript heap
877 * will use the external string resource. The external string resource's
878 * character contents needs to be equivalent to this string.
879 * Returns true if the string has been changed to be an external string.
880 * The string is not modified if the operation fails.
881 */
882 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
v8.team.kasperl727e9952008-09-02 14:56:44 +0000884 /**
885 * Creates a new external string using the ascii data defined in the given
886 * resource. The resource is deleted when the external string is no
887 * longer live on V8's heap. The caller of this function should not
888 * delete or modify the resource. Neither should the underlying buffer be
889 * deallocated or modified except through the destructor of the
890 * external string resource.
891 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000893
ager@chromium.org6f10e412009-02-13 10:11:16 +0000894 /**
895 * Associate an external string resource with this string by transforming it
896 * in place so that existing references to this string in the JavaScript heap
897 * will use the external string resource. The external string resource's
898 * character contents needs to be equivalent to this string.
899 * Returns true if the string has been changed to be an external string.
900 * The string is not modified if the operation fails.
901 */
902 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000904 /**
905 * Returns true if this string can be made external.
906 */
907 bool CanMakeExternal();
908
kasper.lund7276f142008-07-30 08:49:36 +0000909 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910 static Local<String> NewUndetectable(const char* data, int length = -1);
911
kasper.lund7276f142008-07-30 08:49:36 +0000912 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
914
915 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000916 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000917 * you want to print the object. If conversion to a string fails
918 * (eg. due to an exception in the toString() method of the object)
919 * then the length() method returns 0 and the * operator returns
920 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000921 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000922 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000923 public:
924 explicit Utf8Value(Handle<v8::Value> obj);
925 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000926 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000927 int length() { return length_; }
928 private:
929 char* str_;
930 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000931
932 // Disallow copying and assigning.
933 Utf8Value(const Utf8Value&);
934 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000935 };
936
937 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 * Converts an object to an ascii string.
939 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000940 * If conversion to a string fails (eg. due to an exception in the toString()
941 * method of the object) then the length() method returns 0 and the * operator
942 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000944 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 public:
946 explicit AsciiValue(Handle<v8::Value> obj);
947 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000948 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000949 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 private:
951 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000952 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000953
954 // Disallow copying and assigning.
955 AsciiValue(const AsciiValue&);
956 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 };
958
959 /**
960 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000961 * If conversion to a string fails (eg. due to an exception in the toString()
962 * method of the object) then the length() method returns 0 and the * operator
963 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000965 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 public:
967 explicit Value(Handle<v8::Value> obj);
968 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000969 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000970 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 private:
972 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000973 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000974
975 // Disallow copying and assigning.
976 Value(const Value&);
977 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 };
979};
980
981
982/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000983 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000985class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000987 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 static Local<Number> New(double value);
989 static Number* Cast(v8::Value* obj);
990 private:
991 Number();
992};
993
994
995/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000996 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000998class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 public:
1000 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001001 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 static Integer* Cast(v8::Value* obj);
1003 private:
1004 Integer();
1005};
1006
1007
1008/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001009 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001011class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001013 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 private:
1015 Int32();
1016};
1017
1018
1019/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001020 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001022class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001024 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 private:
1026 Uint32();
1027};
1028
1029
1030/**
1031 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1032 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001033class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 public:
1035 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001036
1037 /**
1038 * A specialization of Value::NumberValue that is more efficient
1039 * because we know the structure of this object.
1040 */
ager@chromium.org32912102009-01-16 10:38:43 +00001041 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001042
1043 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044};
1045
1046
1047enum PropertyAttribute {
1048 None = 0,
1049 ReadOnly = 1 << 0,
1050 DontEnum = 1 << 1,
1051 DontDelete = 1 << 2
1052};
1053
1054/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001055 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001057class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 public:
1059 bool Set(Handle<Value> key,
1060 Handle<Value> value,
1061 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001062
ager@chromium.orge2902be2009-06-08 12:21:35 +00001063 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001064 // overriding accessors or read-only properties.
1065 //
1066 // Note that if the object has an interceptor the property will be set
1067 // locally, but since the interceptor takes precedence the local property
1068 // will only be returned if the interceptor doesn't return a value.
1069 //
1070 // Note also that this only works for named properties.
1071 bool ForceSet(Handle<Value> key,
1072 Handle<Value> value,
1073 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 Local<Value> Get(Handle<Value> key);
1076
1077 // TODO(1245389): Replace the type-specific versions of these
1078 // functions with generic ones that accept a Handle<Value> key.
1079 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001080
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001082
1083 // Delete a property on this object bypassing interceptors and
1084 // ignoring dont-delete attributes.
1085 bool ForceDelete(Handle<Value> key);
1086
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 bool Delete(uint32_t index);
1090
1091 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001092 * Returns an array containing the names of the enumerable properties
1093 * of this object, including properties from prototype objects. The
1094 * array returned by this method contains the same values as would
1095 * be enumerated by a for-in statement over this object.
1096 */
1097 Local<Array> GetPropertyNames();
1098
1099 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 * Get the prototype object. This does not skip objects marked to
1101 * be skipped by __proto__ and it does not consult the security
1102 * handler.
1103 */
1104 Local<Value> GetPrototype();
1105
1106 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001107 * Finds an instance of the given function template in the prototype
1108 * chain.
1109 */
1110 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1111
1112 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 * Call builtin Object.prototype.toString on this object.
1114 * This is different from Value::ToString() that may call
1115 * user-defined toString function. This one does not.
1116 */
1117 Local<String> ObjectProtoToString();
1118
kasper.lund212ac232008-07-16 07:07:30 +00001119 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001121 /** Gets the value in an internal field. */
1122 Local<Value> GetInternalField(int index);
1123 /** Sets the value in an internal field. */
1124 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001126 // The two functions below do not perform index bounds checks and
1127 // they do not check that the VM is still running. Use with caution.
1128 /** Gets a native pointer from an internal field. */
1129 void* GetPointerFromInternalField(int index);
1130 /** Sets a native pointer in an internal field. */
1131 void SetPointerInInternalField(int index, void* value);
1132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 // Testers for local properties.
1134 bool HasRealNamedProperty(Handle<String> key);
1135 bool HasRealIndexedProperty(uint32_t index);
1136 bool HasRealNamedCallbackProperty(Handle<String> key);
1137
1138 /**
1139 * If result.IsEmpty() no real property was located in the prototype chain.
1140 * This means interceptors in the prototype chain are not called.
1141 */
1142 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1143
1144 /** Tests for a named lookup interceptor.*/
1145 bool HasNamedLookupInterceptor();
1146
kasper.lund212ac232008-07-16 07:07:30 +00001147 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 bool HasIndexedLookupInterceptor();
1149
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001150 /**
1151 * Turns on access check on the object if the object is an instance of
1152 * a template that has access check callbacks. If an object has no
1153 * access check info, the object cannot be accessed by anyone.
1154 */
1155 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001156
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001157 /**
1158 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001159 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001160 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001161 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001162 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001163 */
1164 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001165
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001166 /**
1167 * Access hidden properties on JavaScript objects. These properties are
1168 * hidden from the executing JavaScript and only accessible through the V8
1169 * C++ API. Hidden properties introduced by V8 internally (for example the
1170 * identity hash) are prefixed with "v8::".
1171 */
1172 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1173 Local<Value> GetHiddenValue(Handle<String> key);
1174 bool DeleteHiddenValue(Handle<String> key);
1175
1176 /**
1177 * Clone this object with a fast but shallow copy. Values will point
1178 * to the same values as the original object.
1179 */
1180 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001182 /**
1183 * Set the backing store of the indexed properties to be managed by the
1184 * embedding layer. Access to the indexed properties will follow the rules
1185 * spelled out in CanvasPixelArray.
1186 * Note: The embedding program still owns the data and needs to ensure that
1187 * the backing store is preserved while V8 has a reference.
1188 */
1189 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 static Local<Object> New();
1192 static Object* Cast(Value* obj);
1193 private:
1194 Object();
1195};
1196
1197
1198/**
1199 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1200 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001201class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001203 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204
ager@chromium.org3e875802009-06-29 08:26:34 +00001205 /**
1206 * Clones an element at index |index|. Returns an empty
1207 * handle if cloning fails (for any reason).
1208 */
1209 Local<Object> CloneElementAt(uint32_t index);
1210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 static Local<Array> New(int length = 0);
1212 static Array* Cast(Value* obj);
1213 private:
1214 Array();
1215};
1216
1217
1218/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001219 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001221class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001223 Local<Object> NewInstance() const;
1224 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1226 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001227 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 static Function* Cast(Value* obj);
1229 private:
1230 Function();
1231};
1232
1233
1234/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001235 * A JavaScript value that wraps a C++ void*. This type of value is
1236 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001238 *
1239 * The Wrap function V8 will return the most optimal Value object wrapping the
1240 * C++ void*. The type of the value is not guaranteed to be an External object
1241 * and no assumptions about its type should be made. To access the wrapped
1242 * value Unwrap should be used, all other operations on that object will lead
1243 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001245class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001247 static Local<Value> Wrap(void* data);
1248 static void* Unwrap(Handle<Value> obj);
1249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 static Local<External> New(void* value);
1251 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001252 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 private:
1254 External();
1255};
1256
1257
1258// --- T e m p l a t e s ---
1259
1260
1261/**
1262 * The superclass of object and function templates.
1263 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001264class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 public:
1266 /** Adds a property to each instance created by this template.*/
1267 void Set(Handle<String> name, Handle<Data> value,
1268 PropertyAttribute attributes = None);
1269 inline void Set(const char* name, Handle<Data> value);
1270 private:
1271 Template();
1272
1273 friend class ObjectTemplate;
1274 friend class FunctionTemplate;
1275};
1276
1277
1278/**
1279 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001280 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 * including the receiver, the number and values of arguments, and
1282 * the holder of the function.
1283 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001284class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 public:
1286 inline int Length() const;
1287 inline Local<Value> operator[](int i) const;
1288 inline Local<Function> Callee() const;
1289 inline Local<Object> This() const;
1290 inline Local<Object> Holder() const;
1291 inline bool IsConstructCall() const;
1292 inline Local<Value> Data() const;
1293 private:
1294 Arguments();
1295 friend class ImplementationUtilities;
1296 inline Arguments(Local<Value> data,
1297 Local<Object> holder,
1298 Local<Function> callee,
1299 bool is_construct_call,
1300 void** values, int length);
1301 Local<Value> data_;
1302 Local<Object> holder_;
1303 Local<Function> callee_;
1304 bool is_construct_call_;
1305 void** values_;
1306 int length_;
1307};
1308
1309
1310/**
1311 * The information passed to an accessor callback about the context
1312 * of the property access.
1313 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001314class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 public:
1316 inline AccessorInfo(Local<Object> self,
1317 Local<Value> data,
1318 Local<Object> holder)
1319 : self_(self), data_(data), holder_(holder) { }
1320 inline Local<Value> Data() const;
1321 inline Local<Object> This() const;
1322 inline Local<Object> Holder() const;
1323 private:
1324 Local<Object> self_;
1325 Local<Value> data_;
1326 Local<Object> holder_;
1327};
1328
1329
1330typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1331
1332typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1333
1334/**
1335 * Accessor[Getter|Setter] are used as callback functions when
1336 * setting|getting a particular property. See objectTemplate::SetAccessor.
1337 */
1338typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1339 const AccessorInfo& info);
1340
1341
1342typedef void (*AccessorSetter)(Local<String> property,
1343 Local<Value> value,
1344 const AccessorInfo& info);
1345
1346
1347/**
1348 * NamedProperty[Getter|Setter] are used as interceptors on object.
1349 * See ObjectTemplate::SetNamedPropertyHandler.
1350 */
1351typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1352 const AccessorInfo& info);
1353
1354
1355/**
1356 * Returns the value if the setter intercepts the request.
1357 * Otherwise, returns an empty handle.
1358 */
1359typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1360 Local<Value> value,
1361 const AccessorInfo& info);
1362
1363
1364/**
1365 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001366 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 */
1368typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1369 const AccessorInfo& info);
1370
1371
1372/**
1373 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001374 * The return value is true if the property could be deleted and false
1375 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 */
1377typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1378 const AccessorInfo& info);
1379
1380/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001381 * Returns an array containing the names of the properties the named
1382 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 */
1384typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1385
v8.team.kasperl727e9952008-09-02 14:56:44 +00001386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001388 * Returns the value of the property if the getter intercepts the
1389 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390 */
1391typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1392 const AccessorInfo& info);
1393
1394
1395/**
1396 * Returns the value if the setter intercepts the request.
1397 * Otherwise, returns an empty handle.
1398 */
1399typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1400 Local<Value> value,
1401 const AccessorInfo& info);
1402
1403
1404/**
1405 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001406 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 */
1408typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1409 const AccessorInfo& info);
1410
1411/**
1412 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001413 * The return value is true if the property could be deleted and false
1414 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 */
1416typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1417 const AccessorInfo& info);
1418
v8.team.kasperl727e9952008-09-02 14:56:44 +00001419/**
1420 * Returns an array containing the indices of the properties the
1421 * indexed property getter intercepts.
1422 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1424
1425
1426/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001427 * Access control specifications.
1428 *
1429 * Some accessors should be accessible across contexts. These
1430 * accessors have an explicit access control parameter which specifies
1431 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001432 *
1433 * Additionally, for security, accessors can prohibit overwriting by
1434 * accessors defined in JavaScript. For objects that have such
1435 * accessors either locally or in their prototype chain it is not
1436 * possible to overwrite the accessor by using __defineGetter__ or
1437 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 */
1439enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001440 DEFAULT = 0,
1441 ALL_CAN_READ = 1,
1442 ALL_CAN_WRITE = 1 << 1,
1443 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444};
1445
1446
1447/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001448 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 */
1450enum AccessType {
1451 ACCESS_GET,
1452 ACCESS_SET,
1453 ACCESS_HAS,
1454 ACCESS_DELETE,
1455 ACCESS_KEYS
1456};
1457
v8.team.kasperl727e9952008-09-02 14:56:44 +00001458
1459/**
1460 * Returns true if cross-context access should be allowed to the named
1461 * property with the given key on the global object.
1462 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463typedef bool (*NamedSecurityCallback)(Local<Object> global,
1464 Local<Value> key,
1465 AccessType type,
1466 Local<Value> data);
1467
v8.team.kasperl727e9952008-09-02 14:56:44 +00001468
1469/**
1470 * Returns true if cross-context access should be allowed to the indexed
1471 * property with the given index on the global object.
1472 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1474 uint32_t index,
1475 AccessType type,
1476 Local<Value> data);
1477
1478
1479/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001480 * A FunctionTemplate is used to create functions at runtime. There
1481 * can only be one function created from a FunctionTemplate in a
1482 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 *
1484 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001485 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001487 * A FunctionTemplate has a corresponding instance template which is
1488 * used to create object instances when the function is used as a
1489 * constructor. Properties added to the instance template are added to
1490 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 *
1492 * A FunctionTemplate can have a prototype template. The prototype template
1493 * is used to create the prototype object of the function.
1494 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001495 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001497 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1499 * t->Set("func_property", v8::Number::New(1));
1500 *
1501 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1502 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1503 * proto_t->Set("proto_const", v8::Number::New(2));
1504 *
1505 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1506 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1507 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1508 * instance_t->Set("instance_property", Number::New(3));
1509 *
1510 * v8::Local<v8::Function> function = t->GetFunction();
1511 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001512 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 *
1514 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001515 * and "instance" for the instance object created above. The function
1516 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001518 * \code
1519 * func_property in function == true;
1520 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001522 * function.prototype.proto_method() invokes 'InvokeCallback'
1523 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001525 * instance instanceof function == true;
1526 * instance.instance_accessor calls 'InstanceAccessorCallback'
1527 * instance.instance_property == 3;
1528 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001530 * A FunctionTemplate can inherit from another one by calling the
1531 * FunctionTemplate::Inherit method. The following graph illustrates
1532 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001534 * \code
1535 * FunctionTemplate Parent -> Parent() . prototype -> { }
1536 * ^ ^
1537 * | Inherit(Parent) | .__proto__
1538 * | |
1539 * FunctionTemplate Child -> Child() . prototype -> { }
1540 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001542 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1543 * object of the Child() function has __proto__ pointing to the
1544 * Parent() function's prototype object. An instance of the Child
1545 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001547 * Let Parent be the FunctionTemplate initialized in the previous
1548 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001550 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551 * Local<FunctionTemplate> parent = t;
1552 * Local<FunctionTemplate> child = FunctionTemplate::New();
1553 * child->Inherit(parent);
1554 *
1555 * Local<Function> child_function = child->GetFunction();
1556 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001557 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001559 * The Child function and Child instance will have the following
1560 * properties:
1561 *
1562 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001564 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001566 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001568class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569 public:
1570 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001571 static Local<FunctionTemplate> New(
1572 InvocationCallback callback = 0,
1573 Handle<Value> data = Handle<Value>(),
1574 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575 /** Returns the unique function instance in the current execution context.*/
1576 Local<Function> GetFunction();
1577
v8.team.kasperl727e9952008-09-02 14:56:44 +00001578 /**
1579 * Set the call-handler callback for a FunctionTemplate. This
1580 * callback is called whenever the function created from this
1581 * FunctionTemplate is called.
1582 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583 void SetCallHandler(InvocationCallback callback,
1584 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585
v8.team.kasperl727e9952008-09-02 14:56:44 +00001586 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 Local<ObjectTemplate> InstanceTemplate();
1588
1589 /** Causes the function template to inherit from a parent function template.*/
1590 void Inherit(Handle<FunctionTemplate> parent);
1591
1592 /**
1593 * A PrototypeTemplate is the template used to create the prototype object
1594 * of the function created by this template.
1595 */
1596 Local<ObjectTemplate> PrototypeTemplate();
1597
v8.team.kasperl727e9952008-09-02 14:56:44 +00001598
1599 /**
1600 * Set the class name of the FunctionTemplate. This is used for
1601 * printing objects created with the function created from the
1602 * FunctionTemplate as its constructor.
1603 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 void SetClassName(Handle<String> name);
1605
1606 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001607 * Determines whether the __proto__ accessor ignores instances of
1608 * the function template. If instances of the function template are
1609 * ignored, __proto__ skips all instances and instead returns the
1610 * next object in the prototype chain.
1611 *
1612 * Call with a value of true to make the __proto__ accessor ignore
1613 * instances of the function template. Call with a value of false
1614 * to make the __proto__ accessor not ignore instances of the
1615 * function template. By default, instances of a function template
1616 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 */
1618 void SetHiddenPrototype(bool value);
1619
1620 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001621 * Returns true if the given object is an instance of this function
1622 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623 */
1624 bool HasInstance(Handle<Value> object);
1625
1626 private:
1627 FunctionTemplate();
1628 void AddInstancePropertyAccessor(Handle<String> name,
1629 AccessorGetter getter,
1630 AccessorSetter setter,
1631 Handle<Value> data,
1632 AccessControl settings,
1633 PropertyAttribute attributes);
1634 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1635 NamedPropertySetter setter,
1636 NamedPropertyQuery query,
1637 NamedPropertyDeleter remover,
1638 NamedPropertyEnumerator enumerator,
1639 Handle<Value> data);
1640 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1641 IndexedPropertySetter setter,
1642 IndexedPropertyQuery query,
1643 IndexedPropertyDeleter remover,
1644 IndexedPropertyEnumerator enumerator,
1645 Handle<Value> data);
1646 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1647 Handle<Value> data);
1648
1649 friend class Context;
1650 friend class ObjectTemplate;
1651};
1652
1653
1654/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001655 * An ObjectTemplate is used to create objects at runtime.
1656 *
1657 * Properties added to an ObjectTemplate are added to each object
1658 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001660class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001662 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001664
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 /** Creates a new instance of this template.*/
1666 Local<Object> NewInstance();
1667
1668 /**
1669 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001670 *
1671 * Whenever the property with the given name is accessed on objects
1672 * created from this ObjectTemplate the getter and setter callbacks
1673 * are called instead of getting and setting the property directly
1674 * on the JavaScript object.
1675 *
1676 * \param name The name of the property for which an accessor is added.
1677 * \param getter The callback to invoke when getting the property.
1678 * \param setter The callback to invoke when setting the property.
1679 * \param data A piece of data that will be passed to the getter and setter
1680 * callbacks whenever they are invoked.
1681 * \param settings Access control settings for the accessor. This is a bit
1682 * field consisting of one of more of
1683 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1684 * The default is to not allow cross-context access.
1685 * ALL_CAN_READ means that all cross-context reads are allowed.
1686 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1687 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1688 * cross-context access.
1689 * \param attribute The attributes of the property for which an accessor
1690 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 */
1692 void SetAccessor(Handle<String> name,
1693 AccessorGetter getter,
1694 AccessorSetter setter = 0,
1695 Handle<Value> data = Handle<Value>(),
1696 AccessControl settings = DEFAULT,
1697 PropertyAttribute attribute = None);
1698
1699 /**
1700 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001701 *
1702 * Whenever a named property is accessed on objects created from
1703 * this object template, the provided callback is invoked instead of
1704 * accessing the property directly on the JavaScript object.
1705 *
1706 * \param getter The callback to invoke when getting a property.
1707 * \param setter The callback to invoke when setting a property.
1708 * \param query The callback to invoke to check is an object has a property.
1709 * \param deleter The callback to invoke when deleting a property.
1710 * \param enumerator The callback to invoke to enumerate all the named
1711 * properties of an object.
1712 * \param data A piece of data that will be passed to the callbacks
1713 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 */
1715 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1716 NamedPropertySetter setter = 0,
1717 NamedPropertyQuery query = 0,
1718 NamedPropertyDeleter deleter = 0,
1719 NamedPropertyEnumerator enumerator = 0,
1720 Handle<Value> data = Handle<Value>());
1721
1722 /**
1723 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001724 *
1725 * Whenever an indexed property is accessed on objects created from
1726 * this object template, the provided callback is invoked instead of
1727 * accessing the property directly on the JavaScript object.
1728 *
1729 * \param getter The callback to invoke when getting a property.
1730 * \param setter The callback to invoke when setting a property.
1731 * \param query The callback to invoke to check is an object has a property.
1732 * \param deleter The callback to invoke when deleting a property.
1733 * \param enumerator The callback to invoke to enumerate all the indexed
1734 * properties of an object.
1735 * \param data A piece of data that will be passed to the callbacks
1736 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 */
1738 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1739 IndexedPropertySetter setter = 0,
1740 IndexedPropertyQuery query = 0,
1741 IndexedPropertyDeleter deleter = 0,
1742 IndexedPropertyEnumerator enumerator = 0,
1743 Handle<Value> data = Handle<Value>());
1744 /**
1745 * Sets the callback to be used when calling instances created from
1746 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001747 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 * function.
1749 */
1750 void SetCallAsFunctionHandler(InvocationCallback callback,
1751 Handle<Value> data = Handle<Value>());
1752
v8.team.kasperl727e9952008-09-02 14:56:44 +00001753 /**
1754 * Mark object instances of the template as undetectable.
1755 *
1756 * In many ways, undetectable objects behave as though they are not
1757 * there. They behave like 'undefined' in conditionals and when
1758 * printed. However, properties can be accessed and called as on
1759 * normal objects.
1760 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 void MarkAsUndetectable();
1762
v8.team.kasperl727e9952008-09-02 14:56:44 +00001763 /**
1764 * Sets access check callbacks on the object template.
1765 *
1766 * When accessing properties on instances of this object template,
1767 * the access check callback will be called to determine whether or
1768 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001769 * The last parameter specifies whether access checks are turned
1770 * on by default on instances. If access checks are off by default,
1771 * they can be turned on on individual instances by calling
1772 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001773 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1775 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001776 Handle<Value> data = Handle<Value>(),
1777 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778
kasper.lund212ac232008-07-16 07:07:30 +00001779 /**
1780 * Gets the number of internal fields for objects generated from
1781 * this template.
1782 */
1783 int InternalFieldCount();
1784
1785 /**
1786 * Sets the number of internal fields for objects generated from
1787 * this template.
1788 */
1789 void SetInternalFieldCount(int value);
1790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 private:
1792 ObjectTemplate();
1793 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1794 friend class FunctionTemplate;
1795};
1796
1797
1798/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001799 * A Signature specifies which receivers and arguments a function can
1800 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001802class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 public:
1804 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1805 Handle<FunctionTemplate>(),
1806 int argc = 0,
1807 Handle<FunctionTemplate> argv[] = 0);
1808 private:
1809 Signature();
1810};
1811
1812
1813/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001814 * A utility for determining the type of objects based on the template
1815 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001817class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 public:
1819 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1820 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1821 int match(Handle<Value> value);
1822 private:
1823 TypeSwitch();
1824};
1825
1826
1827// --- E x t e n s i o n s ---
1828
1829
1830/**
1831 * Ignore
1832 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001833class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834 public:
1835 Extension(const char* name,
1836 const char* source = 0,
1837 int dep_count = 0,
1838 const char** deps = 0);
1839 virtual ~Extension() { }
1840 virtual v8::Handle<v8::FunctionTemplate>
1841 GetNativeFunction(v8::Handle<v8::String> name) {
1842 return v8::Handle<v8::FunctionTemplate>();
1843 }
1844
1845 const char* name() { return name_; }
1846 const char* source() { return source_; }
1847 int dependency_count() { return dep_count_; }
1848 const char** dependencies() { return deps_; }
1849 void set_auto_enable(bool value) { auto_enable_ = value; }
1850 bool auto_enable() { return auto_enable_; }
1851
1852 private:
1853 const char* name_;
1854 const char* source_;
1855 int dep_count_;
1856 const char** deps_;
1857 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001858
1859 // Disallow copying and assigning.
1860 Extension(const Extension&);
1861 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862};
1863
1864
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001865void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866
1867
1868/**
1869 * Ignore
1870 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001871class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 public:
1873 inline DeclareExtension(Extension* extension) {
1874 RegisterExtension(extension);
1875 }
1876};
1877
1878
1879// --- S t a t i c s ---
1880
1881
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001882Handle<Primitive> V8EXPORT Undefined();
1883Handle<Primitive> V8EXPORT Null();
1884Handle<Boolean> V8EXPORT True();
1885Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886
1887
1888/**
1889 * A set of constraints that specifies the limits of the runtime's
1890 * memory use.
1891 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001892class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 public:
1894 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001895 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001897 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001899 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1901 private:
1902 int max_young_space_size_;
1903 int max_old_space_size_;
1904 uint32_t* stack_limit_;
1905};
1906
1907
1908bool SetResourceConstraints(ResourceConstraints* constraints);
1909
1910
1911// --- E x c e p t i o n s ---
1912
1913
1914typedef void (*FatalErrorCallback)(const char* location, const char* message);
1915
1916
1917typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1918
1919
1920/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001921 * Schedules an exception to be thrown when returning to JavaScript. When an
1922 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001924 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001926Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927
1928/**
1929 * Create new error objects by calling the corresponding error object
1930 * constructor with the message.
1931 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001932class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 public:
1934 static Local<Value> RangeError(Handle<String> message);
1935 static Local<Value> ReferenceError(Handle<String> message);
1936 static Local<Value> SyntaxError(Handle<String> message);
1937 static Local<Value> TypeError(Handle<String> message);
1938 static Local<Value> Error(Handle<String> message);
1939};
1940
1941
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001942// --- 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 +00001943
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001944typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001946typedef void* (*CreateHistogramCallback)(const char* name,
1947 int min,
1948 int max,
1949 size_t buckets);
1950
1951typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
1952
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953// --- 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 ---
1954typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1955 AccessType type,
1956 Local<Value> data);
1957
1958// --- 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
1959
1960/**
1961 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001962 * before and after a major garbage collection. Allocations are not
1963 * allowed in the callback function, you therefore cannot manipulate
1964 * objects (set or delete properties for example) since it is possible
1965 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 */
1967typedef void (*GCCallback)();
1968
1969
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001970// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971
1972/**
1973 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001974 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976typedef Persistent<Context> (*ContextGenerator)();
1977
1978
1979/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001980 * Profiler modules.
1981 *
1982 * In V8, profiler consists of several modules: CPU profiler, and different
1983 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00001984 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
1985 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001986 */
1987enum ProfilerModules {
1988 PROFILER_MODULE_NONE = 0,
1989 PROFILER_MODULE_CPU = 1,
1990 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00001991 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
1992 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001993};
1994
1995
1996/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 * Container class for static utility functions.
1998 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001999class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002001 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002 static void SetFatalErrorHandler(FatalErrorCallback that);
2003
v8.team.kasperl727e9952008-09-02 14:56:44 +00002004 /**
2005 * Ignore out-of-memory exceptions.
2006 *
2007 * V8 running out of memory is treated as a fatal error by default.
2008 * This means that the fatal error handler is called and that V8 is
2009 * terminated.
2010 *
2011 * IgnoreOutOfMemoryException can be used to not treat a
2012 * out-of-memory situation as a fatal error. This way, the contexts
2013 * that did not cause the out of memory problem might be able to
2014 * continue execution.
2015 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 static void IgnoreOutOfMemoryException();
2017
v8.team.kasperl727e9952008-09-02 14:56:44 +00002018 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002019 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002020 * fatal errors such as out-of-memory situations.
2021 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 static bool IsDead();
2023
2024 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002025 * Adds a message listener.
2026 *
2027 * The same message listener can be added more than once and it that
2028 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 */
2030 static bool AddMessageListener(MessageCallback that,
2031 Handle<Value> data = Handle<Value>());
2032
2033 /**
2034 * Remove all message listeners from the specified callback function.
2035 */
2036 static void RemoveMessageListeners(MessageCallback that);
2037
2038 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002039 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 */
2041 static void SetFlagsFromString(const char* str, int length);
2042
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002043 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002044 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002045 */
2046 static void SetFlagsFromCommandLine(int* argc,
2047 char** argv,
2048 bool remove_flags);
2049
kasper.lund7276f142008-07-30 08:49:36 +00002050 /** Get the version string. */
2051 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052
2053 /**
2054 * Enables the host application to provide a mechanism for recording
2055 * statistics counters.
2056 */
2057 static void SetCounterFunction(CounterLookupCallback);
2058
2059 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002060 * Enables the host application to provide a mechanism for recording
2061 * histograms. The CreateHistogram function returns a
2062 * histogram which will later be passed to the AddHistogramSample
2063 * function.
2064 */
2065 static void SetCreateHistogramFunction(CreateHistogramCallback);
2066 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2067
2068 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 * Enables the computation of a sliding window of states. The sliding
2070 * window information is recorded in statistics counters.
2071 */
2072 static void EnableSlidingStateWindow();
2073
2074 /** Callback function for reporting failed access checks.*/
2075 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2076
2077 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002078 * Enables the host application to receive a notification before a
2079 * major garbage colletion. Allocations are not allowed in the
2080 * callback function, you therefore cannot manipulate objects (set
2081 * or delete properties for example) since it is possible such
2082 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 */
2084 static void SetGlobalGCPrologueCallback(GCCallback);
2085
2086 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002087 * Enables the host application to receive a notification after a
2088 * major garbage collection. Allocations are not allowed in the
2089 * callback function, you therefore cannot manipulate objects (set
2090 * or delete properties for example) since it is possible such
2091 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 */
2093 static void SetGlobalGCEpilogueCallback(GCCallback);
2094
2095 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002096 * Allows the host application to group objects together. If one
2097 * object in the group is alive, all objects in the group are alive.
2098 * After each garbage collection, object groups are removed. It is
2099 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002100 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002101 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002103 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104
2105 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002106 * Initializes from snapshot if possible. Otherwise, attempts to
2107 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108 */
2109 static bool Initialize();
2110
kasper.lund7276f142008-07-30 08:49:36 +00002111 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112 * Adjusts the amount of registered external memory. Used to give
2113 * V8 an indication of the amount of externally allocated memory
2114 * that is kept alive by JavaScript objects. V8 uses this to decide
2115 * when to perform global garbage collections. Registering
2116 * externally allocated memory will trigger global garbage
2117 * collections more often than otherwise in an attempt to garbage
2118 * collect the JavaScript objects keeping the externally allocated
2119 * memory alive.
2120 *
2121 * \param change_in_bytes the change in externally allocated memory
2122 * that is kept alive by JavaScript objects.
2123 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002124 */
2125 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2126
iposva@chromium.org245aa852009-02-10 00:49:54 +00002127 /**
2128 * Suspends recording of tick samples in the profiler.
2129 * When the V8 profiling mode is enabled (usually via command line
2130 * switches) this function suspends recording of tick samples.
2131 * Profiling ticks are discarded until ResumeProfiler() is called.
2132 *
2133 * See also the --prof and --prof_auto command line switches to
2134 * enable V8 profiling.
2135 */
2136 static void PauseProfiler();
2137
2138 /**
2139 * Resumes recording of tick samples in the profiler.
2140 * See also PauseProfiler().
2141 */
2142 static void ResumeProfiler();
2143
ager@chromium.org41826e72009-03-30 13:30:57 +00002144 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002145 * Return whether profiler is currently paused.
2146 */
2147 static bool IsProfilerPaused();
2148
2149 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002150 * Resumes specified profiler modules.
2151 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2152 * See ProfilerModules enum.
2153 *
2154 * \param flags Flags specifying profiler modules.
2155 */
2156 static void ResumeProfilerEx(int flags);
2157
2158 /**
2159 * Pauses specified profiler modules.
2160 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2161 * See ProfilerModules enum.
2162 *
2163 * \param flags Flags specifying profiler modules.
2164 */
2165 static void PauseProfilerEx(int flags);
2166
2167 /**
2168 * Returns active (resumed) profiler modules.
2169 * See ProfilerModules enum.
2170 *
2171 * \returns active profiler modules.
2172 */
2173 static int GetActiveProfilerModules();
2174
2175 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002176 * If logging is performed into a memory buffer (via --logfile=*), allows to
2177 * retrieve previously written messages. This can be used for retrieving
2178 * profiler log data in the application. This function is thread-safe.
2179 *
2180 * Caller provides a destination buffer that must exist during GetLogLines
2181 * call. Only whole log lines are copied into the buffer.
2182 *
2183 * \param from_pos specified a point in a buffer to read from, 0 is the
2184 * beginning of a buffer. It is assumed that caller updates its current
2185 * position using returned size value from the previous call.
2186 * \param dest_buf destination buffer for log data.
2187 * \param max_size size of the destination buffer.
2188 * \returns actual size of log data copied into buffer.
2189 */
2190 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2191
2192
2193 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002194 * Releases any resources used by v8 and stops any utility threads
2195 * that may be running. Note that disposing v8 is permanent, it
2196 * cannot be reinitialized.
2197 *
2198 * It should generally not be necessary to dispose v8 before exiting
2199 * a process, this should happen automatically. It is only necessary
2200 * to use if the process needs the resources taken up by v8.
2201 */
2202 static bool Dispose();
2203
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002204
2205 /**
2206 * Optional notification that the embedder is idle.
2207 * V8 uses the notification to reduce memory footprint.
2208 * \param is_high_priority tells whether the embedder is high priority.
2209 */
2210 static void IdleNotification(bool is_high_priority);
2211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 private:
2213 V8();
2214
2215 static void** GlobalizeReference(void** handle);
2216 static void DisposeGlobal(void** global_handle);
2217 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
2218 static void ClearWeak(void** global_handle);
2219 static bool IsGlobalNearDeath(void** global_handle);
2220 static bool IsGlobalWeak(void** global_handle);
2221
2222 template <class T> friend class Handle;
2223 template <class T> friend class Local;
2224 template <class T> friend class Persistent;
2225 friend class Context;
2226};
2227
2228
2229/**
2230 * An external exception handler.
2231 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002232class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 public:
2234
2235 /**
2236 * Creates a new try/catch block and registers it with v8.
2237 */
2238 TryCatch();
2239
2240 /**
2241 * Unregisters and deletes this try/catch block.
2242 */
2243 ~TryCatch();
2244
2245 /**
2246 * Returns true if an exception has been caught by this try/catch block.
2247 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002248 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249
2250 /**
2251 * Returns the exception caught by this try/catch block. If no exception has
2252 * been caught an empty handle is returned.
2253 *
2254 * The returned handle is valid until this TryCatch block has been destroyed.
2255 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002256 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257
2258 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002259 * Returns the message associated with this exception. If there is
2260 * no message associated an empty handle is returned.
2261 *
2262 * The returned handle is valid until this TryCatch block has been
2263 * destroyed.
2264 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002265 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002266
2267 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 * Clears any exceptions that may have been caught by this try/catch block.
2269 * After this method has been called, HasCaught() will return false.
2270 *
2271 * It is not necessary to clear a try/catch block before using it again; if
2272 * another exception is thrown the previously caught exception will just be
2273 * overwritten. However, it is often a good idea since it makes it easier
2274 * to determine which operation threw a given exception.
2275 */
2276 void Reset();
2277
v8.team.kasperl727e9952008-09-02 14:56:44 +00002278 /**
2279 * Set verbosity of the external exception handler.
2280 *
2281 * By default, exceptions that are caught by an external exception
2282 * handler are not reported. Call SetVerbose with true on an
2283 * external exception handler to have exceptions caught by the
2284 * handler reported as if they were not caught.
2285 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 void SetVerbose(bool value);
2287
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002288 /**
2289 * Set whether or not this TryCatch should capture a Message object
2290 * which holds source information about where the exception
2291 * occurred. True by default.
2292 */
2293 void SetCaptureMessage(bool value);
2294
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 public:
2296 TryCatch* next_;
2297 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002298 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002300 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002301 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302};
2303
2304
2305// --- C o n t e x t ---
2306
2307
2308/**
2309 * Ignore
2310 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002311class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 public:
2313 ExtensionConfiguration(int name_count, const char* names[])
2314 : name_count_(name_count), names_(names) { }
2315 private:
2316 friend class ImplementationUtilities;
2317 int name_count_;
2318 const char** names_;
2319};
2320
2321
2322/**
2323 * A sandboxed execution context with its own set of built-in objects
2324 * and functions.
2325 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002326class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002328 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 Local<Object> Global();
2330
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002331 /**
2332 * Detaches the global object from its context before
2333 * the global object can be reused to create a new context.
2334 */
2335 void DetachGlobal();
2336
v8.team.kasperl727e9952008-09-02 14:56:44 +00002337 /** Creates a new context. */
2338 static Persistent<Context> New(
2339 ExtensionConfiguration* extensions = 0,
2340 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2341 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342
kasper.lund44510672008-07-25 07:37:58 +00002343 /** Returns the last entered context. */
2344 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345
kasper.lund44510672008-07-25 07:37:58 +00002346 /** Returns the context that is on the top of the stack. */
2347 static Local<Context> GetCurrent();
2348
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002350 * Returns the context of the calling JavaScript code. That is the
2351 * context of the top-most JavaScript frame. If there are no
2352 * JavaScript frames an empty handle is returned.
2353 */
2354 static Local<Context> GetCalling();
2355
2356 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 * Sets the security token for the context. To access an object in
2358 * another context, the security tokens must match.
2359 */
2360 void SetSecurityToken(Handle<Value> token);
2361
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002362 /** Restores the security token to the default value. */
2363 void UseDefaultSecurityToken();
2364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 /** Returns the security token of this context.*/
2366 Handle<Value> GetSecurityToken();
2367
v8.team.kasperl727e9952008-09-02 14:56:44 +00002368 /**
2369 * Enter this context. After entering a context, all code compiled
2370 * and run is compiled and run in this context. If another context
2371 * is already entered, this old context is saved so it can be
2372 * restored when the new context is exited.
2373 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002375
2376 /**
2377 * Exit this context. Exiting the current context restores the
2378 * context that was in place when entering the current context.
2379 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380 void Exit();
2381
v8.team.kasperl727e9952008-09-02 14:56:44 +00002382 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 bool HasOutOfMemoryException();
2384
v8.team.kasperl727e9952008-09-02 14:56:44 +00002385 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 static bool InContext();
2387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002389 * Associate an additional data object with the context. This is mainly used
2390 * with the debugger to provide additional information on the context through
2391 * the debugger API.
2392 */
2393 void SetData(Handle<Value> data);
2394 Local<Value> GetData();
2395
2396 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 * Stack-allocated class which sets the execution context for all
2398 * operations executed within a local scope.
2399 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002400 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 public:
2402 inline Scope(Handle<Context> context) : context_(context) {
2403 context_->Enter();
2404 }
2405 inline ~Scope() { context_->Exit(); }
2406 private:
2407 Handle<Context> context_;
2408 };
2409
2410 private:
2411 friend class Value;
2412 friend class Script;
2413 friend class Object;
2414 friend class Function;
2415};
2416
2417
2418/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002419 * Multiple threads in V8 are allowed, but only one thread at a time
2420 * is allowed to use V8. The definition of 'using V8' includes
2421 * accessing handles or holding onto object pointers obtained from V8
2422 * handles. It is up to the user of V8 to ensure (perhaps with
2423 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 *
2425 * If you wish to start using V8 in a thread you can do this by constructing
2426 * a v8::Locker object. After the code using V8 has completed for the
2427 * current thread you can call the destructor. This can be combined
2428 * with C++ scope-based construction as follows:
2429 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002430 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 * ...
2432 * {
2433 * v8::Locker locker;
2434 * ...
2435 * // Code using V8 goes here.
2436 * ...
2437 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002438 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 *
2440 * If you wish to stop using V8 in a thread A you can do this by either
2441 * by destroying the v8::Locker object as above or by constructing a
2442 * v8::Unlocker object:
2443 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002444 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445 * {
2446 * v8::Unlocker unlocker;
2447 * ...
2448 * // Code not using V8 goes here while V8 can run in another thread.
2449 * ...
2450 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002451 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 *
2453 * The Unlocker object is intended for use in a long-running callback
2454 * from V8, where you want to release the V8 lock for other threads to
2455 * use.
2456 *
2457 * The v8::Locker is a recursive lock. That is, you can lock more than
2458 * once in a given thread. This can be useful if you have code that can
2459 * be called either from code that holds the lock or from code that does
2460 * not. The Unlocker is not recursive so you can not have several
2461 * Unlockers on the stack at once, and you can not use an Unlocker in a
2462 * thread that is not inside a Locker's scope.
2463 *
2464 * An unlocker will unlock several lockers if it has to and reinstate
2465 * the correct depth of locking on its destruction. eg.:
2466 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002467 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 * // V8 not locked.
2469 * {
2470 * v8::Locker locker;
2471 * // V8 locked.
2472 * {
2473 * v8::Locker another_locker;
2474 * // V8 still locked (2 levels).
2475 * {
2476 * v8::Unlocker unlocker;
2477 * // V8 not locked.
2478 * }
2479 * // V8 locked again (2 levels).
2480 * }
2481 * // V8 still locked (1 level).
2482 * }
2483 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002484 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002486class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 public:
2488 Unlocker();
2489 ~Unlocker();
2490};
2491
2492
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002493class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002494 public:
2495 Locker();
2496 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002497
2498 /**
2499 * Start preemption.
2500 *
2501 * When preemption is started, a timer is fired every n milli seconds
2502 * that will switch between multiple threads that are in contention
2503 * for the V8 lock.
2504 */
2505 static void StartPreemption(int every_n_ms);
2506
2507 /**
2508 * Stop preemption.
2509 */
2510 static void StopPreemption();
2511
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002512 /**
2513 * Returns whether or not the locker is locked by the current thread.
2514 */
2515 static bool IsLocked();
2516
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002517 /**
2518 * Returns whether v8::Locker is being used by this V8 instance.
2519 */
2520 static bool IsActive() { return active_; }
2521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 private:
2523 bool has_lock_;
2524 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002525
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002526 static bool active_;
2527
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002528 // Disallow copying and assigning.
2529 Locker(const Locker&);
2530 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531};
2532
2533
2534
2535// --- I m p l e m e n t a t i o n ---
2536
2537template <class T>
2538Handle<T>::Handle() : val_(0) { }
2539
2540
2541template <class T>
2542Local<T>::Local() : Handle<T>() { }
2543
2544
2545template <class T>
2546Local<T> Local<T>::New(Handle<T> that) {
2547 if (that.IsEmpty()) return Local<T>();
2548 void** p = reinterpret_cast<void**>(*that);
2549 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2550}
2551
2552
2553template <class T>
2554Persistent<T> Persistent<T>::New(Handle<T> that) {
2555 if (that.IsEmpty()) return Persistent<T>();
2556 void** p = reinterpret_cast<void**>(*that);
2557 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2558}
2559
2560
2561template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002562bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 if (this->IsEmpty()) return false;
2564 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2565}
2566
2567
2568template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002569bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002570 if (this->IsEmpty()) return false;
2571 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2572}
2573
2574
2575template <class T>
2576void Persistent<T>::Dispose() {
2577 if (this->IsEmpty()) return;
2578 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2579}
2580
2581
2582template <class T>
2583Persistent<T>::Persistent() : Handle<T>() { }
2584
2585template <class T>
2586void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2587 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2588}
2589
2590template <class T>
2591void Persistent<T>::ClearWeak() {
2592 V8::ClearWeak(reinterpret_cast<void**>(**this));
2593}
2594
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595Local<Value> Arguments::operator[](int i) const {
2596 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2597 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2598}
2599
2600
2601Local<Function> Arguments::Callee() const {
2602 return callee_;
2603}
2604
2605
2606Local<Object> Arguments::This() const {
2607 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2608}
2609
2610
2611Local<Object> Arguments::Holder() const {
2612 return holder_;
2613}
2614
2615
2616Local<Value> Arguments::Data() const {
2617 return data_;
2618}
2619
2620
2621bool Arguments::IsConstructCall() const {
2622 return is_construct_call_;
2623}
2624
2625
2626int Arguments::Length() const {
2627 return length_;
2628}
2629
2630
2631Local<Value> AccessorInfo::Data() const {
2632 return data_;
2633}
2634
2635
2636Local<Object> AccessorInfo::This() const {
2637 return self_;
2638}
2639
2640
2641Local<Object> AccessorInfo::Holder() const {
2642 return holder_;
2643}
2644
2645
2646template <class T>
2647Local<T> HandleScope::Close(Handle<T> value) {
2648 void** after = RawClose(reinterpret_cast<void**>(*value));
2649 return Local<T>(reinterpret_cast<T*>(after));
2650}
2651
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002652Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 return resource_name_;
2654}
2655
2656
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002657Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 return resource_line_offset_;
2659}
2660
2661
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002662Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 return resource_column_offset_;
2664}
2665
2666
2667Handle<Boolean> Boolean::New(bool value) {
2668 return value ? True() : False();
2669}
2670
2671
2672void Template::Set(const char* name, v8::Handle<Data> value) {
2673 Set(v8::String::New(name), value);
2674}
2675
2676
2677/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002678 * \example shell.cc
2679 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680 * command-line and executes them.
2681 */
2682
2683
2684/**
2685 * \example process.cc
2686 */
2687
2688
2689} // namespace v8
2690
2691
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002692#undef V8EXPORT
2693#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694#undef TYPE_CHECK
2695
2696
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002697#endif // V8_H_