blob: 83d5bedce2df6780bcf27053a111fa63e2515089 [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
kasper.lund7276f142008-07-30 08:49:36 +0000904 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 static Local<String> NewUndetectable(const char* data, int length = -1);
906
kasper.lund7276f142008-07-30 08:49:36 +0000907 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
909
910 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000911 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000912 * you want to print the object. If conversion to a string fails
913 * (eg. due to an exception in the toString() method of the object)
914 * then the length() method returns 0 and the * operator returns
915 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000916 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000917 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000918 public:
919 explicit Utf8Value(Handle<v8::Value> obj);
920 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000921 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000922 int length() { return length_; }
923 private:
924 char* str_;
925 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000926
927 // Disallow copying and assigning.
928 Utf8Value(const Utf8Value&);
929 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000930 };
931
932 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933 * Converts an object to an ascii string.
934 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000935 * If conversion to a string fails (eg. due to an exception in the toString()
936 * method of the object) then the length() method returns 0 and the * operator
937 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000939 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 public:
941 explicit AsciiValue(Handle<v8::Value> obj);
942 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000943 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000944 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 private:
946 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000947 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000948
949 // Disallow copying and assigning.
950 AsciiValue(const AsciiValue&);
951 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 };
953
954 /**
955 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000956 * If conversion to a string fails (eg. due to an exception in the toString()
957 * method of the object) then the length() method returns 0 and the * operator
958 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000960 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 public:
962 explicit Value(Handle<v8::Value> obj);
963 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000964 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000965 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 private:
967 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000968 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000969
970 // Disallow copying and assigning.
971 Value(const Value&);
972 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 };
974};
975
976
977/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000978 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000980class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000982 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 static Local<Number> New(double value);
984 static Number* Cast(v8::Value* obj);
985 private:
986 Number();
987};
988
989
990/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000991 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000993class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 public:
995 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +0000996 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997 static Integer* Cast(v8::Value* obj);
998 private:
999 Integer();
1000};
1001
1002
1003/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001004 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001006class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001008 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 private:
1010 Int32();
1011};
1012
1013
1014/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001015 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001017class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001019 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 private:
1021 Uint32();
1022};
1023
1024
1025/**
1026 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1027 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001028class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 public:
1030 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001031
1032 /**
1033 * A specialization of Value::NumberValue that is more efficient
1034 * because we know the structure of this object.
1035 */
ager@chromium.org32912102009-01-16 10:38:43 +00001036 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001037
1038 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039};
1040
1041
1042enum PropertyAttribute {
1043 None = 0,
1044 ReadOnly = 1 << 0,
1045 DontEnum = 1 << 1,
1046 DontDelete = 1 << 2
1047};
1048
1049/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001050 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001052class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 public:
1054 bool Set(Handle<Value> key,
1055 Handle<Value> value,
1056 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001057
ager@chromium.orge2902be2009-06-08 12:21:35 +00001058 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001059 // overriding accessors or read-only properties.
1060 //
1061 // Note that if the object has an interceptor the property will be set
1062 // locally, but since the interceptor takes precedence the local property
1063 // will only be returned if the interceptor doesn't return a value.
1064 //
1065 // Note also that this only works for named properties.
1066 bool ForceSet(Handle<Value> key,
1067 Handle<Value> value,
1068 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 Local<Value> Get(Handle<Value> key);
1071
1072 // TODO(1245389): Replace the type-specific versions of these
1073 // functions with generic ones that accept a Handle<Value> key.
1074 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001077
1078 // Delete a property on this object bypassing interceptors and
1079 // ignoring dont-delete attributes.
1080 bool ForceDelete(Handle<Value> key);
1081
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001083
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 bool Delete(uint32_t index);
1085
1086 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001087 * Returns an array containing the names of the enumerable properties
1088 * of this object, including properties from prototype objects. The
1089 * array returned by this method contains the same values as would
1090 * be enumerated by a for-in statement over this object.
1091 */
1092 Local<Array> GetPropertyNames();
1093
1094 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 * Get the prototype object. This does not skip objects marked to
1096 * be skipped by __proto__ and it does not consult the security
1097 * handler.
1098 */
1099 Local<Value> GetPrototype();
1100
1101 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001102 * Finds an instance of the given function template in the prototype
1103 * chain.
1104 */
1105 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1106
1107 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 * Call builtin Object.prototype.toString on this object.
1109 * This is different from Value::ToString() that may call
1110 * user-defined toString function. This one does not.
1111 */
1112 Local<String> ObjectProtoToString();
1113
kasper.lund212ac232008-07-16 07:07:30 +00001114 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001116 /** Gets the value in an internal field. */
1117 Local<Value> GetInternalField(int index);
1118 /** Sets the value in an internal field. */
1119 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001121 // The two functions below do not perform index bounds checks and
1122 // they do not check that the VM is still running. Use with caution.
1123 /** Gets a native pointer from an internal field. */
1124 void* GetPointerFromInternalField(int index);
1125 /** Sets a native pointer in an internal field. */
1126 void SetPointerInInternalField(int index, void* value);
1127
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 // Testers for local properties.
1129 bool HasRealNamedProperty(Handle<String> key);
1130 bool HasRealIndexedProperty(uint32_t index);
1131 bool HasRealNamedCallbackProperty(Handle<String> key);
1132
1133 /**
1134 * If result.IsEmpty() no real property was located in the prototype chain.
1135 * This means interceptors in the prototype chain are not called.
1136 */
1137 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1138
1139 /** Tests for a named lookup interceptor.*/
1140 bool HasNamedLookupInterceptor();
1141
kasper.lund212ac232008-07-16 07:07:30 +00001142 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 bool HasIndexedLookupInterceptor();
1144
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001145 /**
1146 * Turns on access check on the object if the object is an instance of
1147 * a template that has access check callbacks. If an object has no
1148 * access check info, the object cannot be accessed by anyone.
1149 */
1150 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001151
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001152 /**
1153 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001154 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001155 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001156 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001157 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001158 */
1159 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001160
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001161 /**
1162 * Access hidden properties on JavaScript objects. These properties are
1163 * hidden from the executing JavaScript and only accessible through the V8
1164 * C++ API. Hidden properties introduced by V8 internally (for example the
1165 * identity hash) are prefixed with "v8::".
1166 */
1167 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1168 Local<Value> GetHiddenValue(Handle<String> key);
1169 bool DeleteHiddenValue(Handle<String> key);
1170
1171 /**
1172 * Clone this object with a fast but shallow copy. Values will point
1173 * to the same values as the original object.
1174 */
1175 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001177 /**
1178 * Set the backing store of the indexed properties to be managed by the
1179 * embedding layer. Access to the indexed properties will follow the rules
1180 * spelled out in CanvasPixelArray.
1181 * Note: The embedding program still owns the data and needs to ensure that
1182 * the backing store is preserved while V8 has a reference.
1183 */
1184 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 static Local<Object> New();
1187 static Object* Cast(Value* obj);
1188 private:
1189 Object();
1190};
1191
1192
1193/**
1194 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1195 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001196class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001197 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001198 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199
ager@chromium.org3e875802009-06-29 08:26:34 +00001200 /**
1201 * Clones an element at index |index|. Returns an empty
1202 * handle if cloning fails (for any reason).
1203 */
1204 Local<Object> CloneElementAt(uint32_t index);
1205
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 static Local<Array> New(int length = 0);
1207 static Array* Cast(Value* obj);
1208 private:
1209 Array();
1210};
1211
1212
1213/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001214 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001216class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001218 Local<Object> NewInstance() const;
1219 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1221 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001222 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 static Function* Cast(Value* obj);
1224 private:
1225 Function();
1226};
1227
1228
1229/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001230 * A JavaScript value that wraps a C++ void*. This type of value is
1231 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001233 *
1234 * The Wrap function V8 will return the most optimal Value object wrapping the
1235 * C++ void*. The type of the value is not guaranteed to be an External object
1236 * and no assumptions about its type should be made. To access the wrapped
1237 * value Unwrap should be used, all other operations on that object will lead
1238 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001240class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001242 static Local<Value> Wrap(void* data);
1243 static void* Unwrap(Handle<Value> obj);
1244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 static Local<External> New(void* value);
1246 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001247 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 private:
1249 External();
1250};
1251
1252
1253// --- T e m p l a t e s ---
1254
1255
1256/**
1257 * The superclass of object and function templates.
1258 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001259class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 public:
1261 /** Adds a property to each instance created by this template.*/
1262 void Set(Handle<String> name, Handle<Data> value,
1263 PropertyAttribute attributes = None);
1264 inline void Set(const char* name, Handle<Data> value);
1265 private:
1266 Template();
1267
1268 friend class ObjectTemplate;
1269 friend class FunctionTemplate;
1270};
1271
1272
1273/**
1274 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001275 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 * including the receiver, the number and values of arguments, and
1277 * the holder of the function.
1278 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001279class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 public:
1281 inline int Length() const;
1282 inline Local<Value> operator[](int i) const;
1283 inline Local<Function> Callee() const;
1284 inline Local<Object> This() const;
1285 inline Local<Object> Holder() const;
1286 inline bool IsConstructCall() const;
1287 inline Local<Value> Data() const;
1288 private:
1289 Arguments();
1290 friend class ImplementationUtilities;
1291 inline Arguments(Local<Value> data,
1292 Local<Object> holder,
1293 Local<Function> callee,
1294 bool is_construct_call,
1295 void** values, int length);
1296 Local<Value> data_;
1297 Local<Object> holder_;
1298 Local<Function> callee_;
1299 bool is_construct_call_;
1300 void** values_;
1301 int length_;
1302};
1303
1304
1305/**
1306 * The information passed to an accessor callback about the context
1307 * of the property access.
1308 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001309class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 public:
1311 inline AccessorInfo(Local<Object> self,
1312 Local<Value> data,
1313 Local<Object> holder)
1314 : self_(self), data_(data), holder_(holder) { }
1315 inline Local<Value> Data() const;
1316 inline Local<Object> This() const;
1317 inline Local<Object> Holder() const;
1318 private:
1319 Local<Object> self_;
1320 Local<Value> data_;
1321 Local<Object> holder_;
1322};
1323
1324
1325typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1326
1327typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1328
1329/**
1330 * Accessor[Getter|Setter] are used as callback functions when
1331 * setting|getting a particular property. See objectTemplate::SetAccessor.
1332 */
1333typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1334 const AccessorInfo& info);
1335
1336
1337typedef void (*AccessorSetter)(Local<String> property,
1338 Local<Value> value,
1339 const AccessorInfo& info);
1340
1341
1342/**
1343 * NamedProperty[Getter|Setter] are used as interceptors on object.
1344 * See ObjectTemplate::SetNamedPropertyHandler.
1345 */
1346typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1347 const AccessorInfo& info);
1348
1349
1350/**
1351 * Returns the value if the setter intercepts the request.
1352 * Otherwise, returns an empty handle.
1353 */
1354typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1355 Local<Value> value,
1356 const AccessorInfo& info);
1357
1358
1359/**
1360 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001361 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 */
1363typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1364 const AccessorInfo& info);
1365
1366
1367/**
1368 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001369 * The return value is true if the property could be deleted and false
1370 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371 */
1372typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1373 const AccessorInfo& info);
1374
1375/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001376 * Returns an array containing the names of the properties the named
1377 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 */
1379typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1380
v8.team.kasperl727e9952008-09-02 14:56:44 +00001381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001383 * Returns the value of the property if the getter intercepts the
1384 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 */
1386typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1387 const AccessorInfo& info);
1388
1389
1390/**
1391 * Returns the value if the setter intercepts the request.
1392 * Otherwise, returns an empty handle.
1393 */
1394typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1395 Local<Value> value,
1396 const AccessorInfo& info);
1397
1398
1399/**
1400 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001401 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402 */
1403typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1404 const AccessorInfo& info);
1405
1406/**
1407 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001408 * The return value is true if the property could be deleted and false
1409 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 */
1411typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1412 const AccessorInfo& info);
1413
v8.team.kasperl727e9952008-09-02 14:56:44 +00001414/**
1415 * Returns an array containing the indices of the properties the
1416 * indexed property getter intercepts.
1417 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1419
1420
1421/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001422 * Access control specifications.
1423 *
1424 * Some accessors should be accessible across contexts. These
1425 * accessors have an explicit access control parameter which specifies
1426 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001427 *
1428 * Additionally, for security, accessors can prohibit overwriting by
1429 * accessors defined in JavaScript. For objects that have such
1430 * accessors either locally or in their prototype chain it is not
1431 * possible to overwrite the accessor by using __defineGetter__ or
1432 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 */
1434enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001435 DEFAULT = 0,
1436 ALL_CAN_READ = 1,
1437 ALL_CAN_WRITE = 1 << 1,
1438 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439};
1440
1441
1442/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001443 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 */
1445enum AccessType {
1446 ACCESS_GET,
1447 ACCESS_SET,
1448 ACCESS_HAS,
1449 ACCESS_DELETE,
1450 ACCESS_KEYS
1451};
1452
v8.team.kasperl727e9952008-09-02 14:56:44 +00001453
1454/**
1455 * Returns true if cross-context access should be allowed to the named
1456 * property with the given key on the global object.
1457 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458typedef bool (*NamedSecurityCallback)(Local<Object> global,
1459 Local<Value> key,
1460 AccessType type,
1461 Local<Value> data);
1462
v8.team.kasperl727e9952008-09-02 14:56:44 +00001463
1464/**
1465 * Returns true if cross-context access should be allowed to the indexed
1466 * property with the given index on the global object.
1467 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1469 uint32_t index,
1470 AccessType type,
1471 Local<Value> data);
1472
1473
1474/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001475 * A FunctionTemplate is used to create functions at runtime. There
1476 * can only be one function created from a FunctionTemplate in a
1477 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 *
1479 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001480 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001482 * A FunctionTemplate has a corresponding instance template which is
1483 * used to create object instances when the function is used as a
1484 * constructor. Properties added to the instance template are added to
1485 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 *
1487 * A FunctionTemplate can have a prototype template. The prototype template
1488 * is used to create the prototype object of the function.
1489 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001490 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001492 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1494 * t->Set("func_property", v8::Number::New(1));
1495 *
1496 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1497 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1498 * proto_t->Set("proto_const", v8::Number::New(2));
1499 *
1500 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1501 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1502 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1503 * instance_t->Set("instance_property", Number::New(3));
1504 *
1505 * v8::Local<v8::Function> function = t->GetFunction();
1506 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001507 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 *
1509 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001510 * and "instance" for the instance object created above. The function
1511 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001513 * \code
1514 * func_property in function == true;
1515 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001517 * function.prototype.proto_method() invokes 'InvokeCallback'
1518 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001520 * instance instanceof function == true;
1521 * instance.instance_accessor calls 'InstanceAccessorCallback'
1522 * instance.instance_property == 3;
1523 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001525 * A FunctionTemplate can inherit from another one by calling the
1526 * FunctionTemplate::Inherit method. The following graph illustrates
1527 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001529 * \code
1530 * FunctionTemplate Parent -> Parent() . prototype -> { }
1531 * ^ ^
1532 * | Inherit(Parent) | .__proto__
1533 * | |
1534 * FunctionTemplate Child -> Child() . prototype -> { }
1535 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001537 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1538 * object of the Child() function has __proto__ pointing to the
1539 * Parent() function's prototype object. An instance of the Child
1540 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001542 * Let Parent be the FunctionTemplate initialized in the previous
1543 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001545 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546 * Local<FunctionTemplate> parent = t;
1547 * Local<FunctionTemplate> child = FunctionTemplate::New();
1548 * child->Inherit(parent);
1549 *
1550 * Local<Function> child_function = child->GetFunction();
1551 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001552 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001554 * The Child function and Child instance will have the following
1555 * properties:
1556 *
1557 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001559 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001561 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001563class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 public:
1565 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001566 static Local<FunctionTemplate> New(
1567 InvocationCallback callback = 0,
1568 Handle<Value> data = Handle<Value>(),
1569 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 /** Returns the unique function instance in the current execution context.*/
1571 Local<Function> GetFunction();
1572
v8.team.kasperl727e9952008-09-02 14:56:44 +00001573 /**
1574 * Set the call-handler callback for a FunctionTemplate. This
1575 * callback is called whenever the function created from this
1576 * FunctionTemplate is called.
1577 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 void SetCallHandler(InvocationCallback callback,
1579 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580
v8.team.kasperl727e9952008-09-02 14:56:44 +00001581 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 Local<ObjectTemplate> InstanceTemplate();
1583
1584 /** Causes the function template to inherit from a parent function template.*/
1585 void Inherit(Handle<FunctionTemplate> parent);
1586
1587 /**
1588 * A PrototypeTemplate is the template used to create the prototype object
1589 * of the function created by this template.
1590 */
1591 Local<ObjectTemplate> PrototypeTemplate();
1592
v8.team.kasperl727e9952008-09-02 14:56:44 +00001593
1594 /**
1595 * Set the class name of the FunctionTemplate. This is used for
1596 * printing objects created with the function created from the
1597 * FunctionTemplate as its constructor.
1598 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599 void SetClassName(Handle<String> name);
1600
1601 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001602 * Determines whether the __proto__ accessor ignores instances of
1603 * the function template. If instances of the function template are
1604 * ignored, __proto__ skips all instances and instead returns the
1605 * next object in the prototype chain.
1606 *
1607 * Call with a value of true to make the __proto__ accessor ignore
1608 * instances of the function template. Call with a value of false
1609 * to make the __proto__ accessor not ignore instances of the
1610 * function template. By default, instances of a function template
1611 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 */
1613 void SetHiddenPrototype(bool value);
1614
1615 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001616 * Returns true if the given object is an instance of this function
1617 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 */
1619 bool HasInstance(Handle<Value> object);
1620
1621 private:
1622 FunctionTemplate();
1623 void AddInstancePropertyAccessor(Handle<String> name,
1624 AccessorGetter getter,
1625 AccessorSetter setter,
1626 Handle<Value> data,
1627 AccessControl settings,
1628 PropertyAttribute attributes);
1629 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1630 NamedPropertySetter setter,
1631 NamedPropertyQuery query,
1632 NamedPropertyDeleter remover,
1633 NamedPropertyEnumerator enumerator,
1634 Handle<Value> data);
1635 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1636 IndexedPropertySetter setter,
1637 IndexedPropertyQuery query,
1638 IndexedPropertyDeleter remover,
1639 IndexedPropertyEnumerator enumerator,
1640 Handle<Value> data);
1641 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1642 Handle<Value> data);
1643
1644 friend class Context;
1645 friend class ObjectTemplate;
1646};
1647
1648
1649/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001650 * An ObjectTemplate is used to create objects at runtime.
1651 *
1652 * Properties added to an ObjectTemplate are added to each object
1653 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001655class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001657 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001659
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 /** Creates a new instance of this template.*/
1661 Local<Object> NewInstance();
1662
1663 /**
1664 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001665 *
1666 * Whenever the property with the given name is accessed on objects
1667 * created from this ObjectTemplate the getter and setter callbacks
1668 * are called instead of getting and setting the property directly
1669 * on the JavaScript object.
1670 *
1671 * \param name The name of the property for which an accessor is added.
1672 * \param getter The callback to invoke when getting the property.
1673 * \param setter The callback to invoke when setting the property.
1674 * \param data A piece of data that will be passed to the getter and setter
1675 * callbacks whenever they are invoked.
1676 * \param settings Access control settings for the accessor. This is a bit
1677 * field consisting of one of more of
1678 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1679 * The default is to not allow cross-context access.
1680 * ALL_CAN_READ means that all cross-context reads are allowed.
1681 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1682 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1683 * cross-context access.
1684 * \param attribute The attributes of the property for which an accessor
1685 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 */
1687 void SetAccessor(Handle<String> name,
1688 AccessorGetter getter,
1689 AccessorSetter setter = 0,
1690 Handle<Value> data = Handle<Value>(),
1691 AccessControl settings = DEFAULT,
1692 PropertyAttribute attribute = None);
1693
1694 /**
1695 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001696 *
1697 * Whenever a named property is accessed on objects created from
1698 * this object template, the provided callback is invoked instead of
1699 * accessing the property directly on the JavaScript object.
1700 *
1701 * \param getter The callback to invoke when getting a property.
1702 * \param setter The callback to invoke when setting a property.
1703 * \param query The callback to invoke to check is an object has a property.
1704 * \param deleter The callback to invoke when deleting a property.
1705 * \param enumerator The callback to invoke to enumerate all the named
1706 * properties of an object.
1707 * \param data A piece of data that will be passed to the callbacks
1708 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 */
1710 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1711 NamedPropertySetter setter = 0,
1712 NamedPropertyQuery query = 0,
1713 NamedPropertyDeleter deleter = 0,
1714 NamedPropertyEnumerator enumerator = 0,
1715 Handle<Value> data = Handle<Value>());
1716
1717 /**
1718 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001719 *
1720 * Whenever an indexed property is accessed on objects created from
1721 * this object template, the provided callback is invoked instead of
1722 * accessing the property directly on the JavaScript object.
1723 *
1724 * \param getter The callback to invoke when getting a property.
1725 * \param setter The callback to invoke when setting a property.
1726 * \param query The callback to invoke to check is an object has a property.
1727 * \param deleter The callback to invoke when deleting a property.
1728 * \param enumerator The callback to invoke to enumerate all the indexed
1729 * properties of an object.
1730 * \param data A piece of data that will be passed to the callbacks
1731 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001732 */
1733 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1734 IndexedPropertySetter setter = 0,
1735 IndexedPropertyQuery query = 0,
1736 IndexedPropertyDeleter deleter = 0,
1737 IndexedPropertyEnumerator enumerator = 0,
1738 Handle<Value> data = Handle<Value>());
1739 /**
1740 * Sets the callback to be used when calling instances created from
1741 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001742 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 * function.
1744 */
1745 void SetCallAsFunctionHandler(InvocationCallback callback,
1746 Handle<Value> data = Handle<Value>());
1747
v8.team.kasperl727e9952008-09-02 14:56:44 +00001748 /**
1749 * Mark object instances of the template as undetectable.
1750 *
1751 * In many ways, undetectable objects behave as though they are not
1752 * there. They behave like 'undefined' in conditionals and when
1753 * printed. However, properties can be accessed and called as on
1754 * normal objects.
1755 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 void MarkAsUndetectable();
1757
v8.team.kasperl727e9952008-09-02 14:56:44 +00001758 /**
1759 * Sets access check callbacks on the object template.
1760 *
1761 * When accessing properties on instances of this object template,
1762 * the access check callback will be called to determine whether or
1763 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001764 * The last parameter specifies whether access checks are turned
1765 * on by default on instances. If access checks are off by default,
1766 * they can be turned on on individual instances by calling
1767 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001768 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1770 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001771 Handle<Value> data = Handle<Value>(),
1772 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773
kasper.lund212ac232008-07-16 07:07:30 +00001774 /**
1775 * Gets the number of internal fields for objects generated from
1776 * this template.
1777 */
1778 int InternalFieldCount();
1779
1780 /**
1781 * Sets the number of internal fields for objects generated from
1782 * this template.
1783 */
1784 void SetInternalFieldCount(int value);
1785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 private:
1787 ObjectTemplate();
1788 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1789 friend class FunctionTemplate;
1790};
1791
1792
1793/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001794 * A Signature specifies which receivers and arguments a function can
1795 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001797class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 public:
1799 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1800 Handle<FunctionTemplate>(),
1801 int argc = 0,
1802 Handle<FunctionTemplate> argv[] = 0);
1803 private:
1804 Signature();
1805};
1806
1807
1808/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001809 * A utility for determining the type of objects based on the template
1810 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001812class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 public:
1814 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1815 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1816 int match(Handle<Value> value);
1817 private:
1818 TypeSwitch();
1819};
1820
1821
1822// --- E x t e n s i o n s ---
1823
1824
1825/**
1826 * Ignore
1827 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001828class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 public:
1830 Extension(const char* name,
1831 const char* source = 0,
1832 int dep_count = 0,
1833 const char** deps = 0);
1834 virtual ~Extension() { }
1835 virtual v8::Handle<v8::FunctionTemplate>
1836 GetNativeFunction(v8::Handle<v8::String> name) {
1837 return v8::Handle<v8::FunctionTemplate>();
1838 }
1839
1840 const char* name() { return name_; }
1841 const char* source() { return source_; }
1842 int dependency_count() { return dep_count_; }
1843 const char** dependencies() { return deps_; }
1844 void set_auto_enable(bool value) { auto_enable_ = value; }
1845 bool auto_enable() { return auto_enable_; }
1846
1847 private:
1848 const char* name_;
1849 const char* source_;
1850 int dep_count_;
1851 const char** deps_;
1852 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001853
1854 // Disallow copying and assigning.
1855 Extension(const Extension&);
1856 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857};
1858
1859
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001860void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861
1862
1863/**
1864 * Ignore
1865 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001866class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 public:
1868 inline DeclareExtension(Extension* extension) {
1869 RegisterExtension(extension);
1870 }
1871};
1872
1873
1874// --- S t a t i c s ---
1875
1876
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001877Handle<Primitive> V8EXPORT Undefined();
1878Handle<Primitive> V8EXPORT Null();
1879Handle<Boolean> V8EXPORT True();
1880Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881
1882
1883/**
1884 * A set of constraints that specifies the limits of the runtime's
1885 * memory use.
1886 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001887class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 public:
1889 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001890 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001892 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001894 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1896 private:
1897 int max_young_space_size_;
1898 int max_old_space_size_;
1899 uint32_t* stack_limit_;
1900};
1901
1902
1903bool SetResourceConstraints(ResourceConstraints* constraints);
1904
1905
1906// --- E x c e p t i o n s ---
1907
1908
1909typedef void (*FatalErrorCallback)(const char* location, const char* message);
1910
1911
1912typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1913
1914
1915/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001916 * Schedules an exception to be thrown when returning to JavaScript. When an
1917 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001919 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001921Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922
1923/**
1924 * Create new error objects by calling the corresponding error object
1925 * constructor with the message.
1926 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001927class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 public:
1929 static Local<Value> RangeError(Handle<String> message);
1930 static Local<Value> ReferenceError(Handle<String> message);
1931 static Local<Value> SyntaxError(Handle<String> message);
1932 static Local<Value> TypeError(Handle<String> message);
1933 static Local<Value> Error(Handle<String> message);
1934};
1935
1936
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001937// --- 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 +00001938
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001939typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001941typedef void* (*CreateHistogramCallback)(const char* name,
1942 int min,
1943 int max,
1944 size_t buckets);
1945
1946typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
1947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948// --- 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 ---
1949typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1950 AccessType type,
1951 Local<Value> data);
1952
1953// --- 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
1954
1955/**
1956 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 * before and after a major garbage collection. Allocations are not
1958 * allowed in the callback function, you therefore cannot manipulate
1959 * objects (set or delete properties for example) since it is possible
1960 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 */
1962typedef void (*GCCallback)();
1963
1964
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001965// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966
1967/**
1968 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001969 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971typedef Persistent<Context> (*ContextGenerator)();
1972
1973
1974/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001975 * Profiler modules.
1976 *
1977 * In V8, profiler consists of several modules: CPU profiler, and different
1978 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00001979 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
1980 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001981 */
1982enum ProfilerModules {
1983 PROFILER_MODULE_NONE = 0,
1984 PROFILER_MODULE_CPU = 1,
1985 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00001986 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
1987 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001988};
1989
1990
1991/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 * Container class for static utility functions.
1993 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001994class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001996 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 static void SetFatalErrorHandler(FatalErrorCallback that);
1998
v8.team.kasperl727e9952008-09-02 14:56:44 +00001999 /**
2000 * Ignore out-of-memory exceptions.
2001 *
2002 * V8 running out of memory is treated as a fatal error by default.
2003 * This means that the fatal error handler is called and that V8 is
2004 * terminated.
2005 *
2006 * IgnoreOutOfMemoryException can be used to not treat a
2007 * out-of-memory situation as a fatal error. This way, the contexts
2008 * that did not cause the out of memory problem might be able to
2009 * continue execution.
2010 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 static void IgnoreOutOfMemoryException();
2012
v8.team.kasperl727e9952008-09-02 14:56:44 +00002013 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002014 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002015 * fatal errors such as out-of-memory situations.
2016 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 static bool IsDead();
2018
2019 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002020 * Adds a message listener.
2021 *
2022 * The same message listener can be added more than once and it that
2023 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 */
2025 static bool AddMessageListener(MessageCallback that,
2026 Handle<Value> data = Handle<Value>());
2027
2028 /**
2029 * Remove all message listeners from the specified callback function.
2030 */
2031 static void RemoveMessageListeners(MessageCallback that);
2032
2033 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002034 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 */
2036 static void SetFlagsFromString(const char* str, int length);
2037
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002038 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002039 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002040 */
2041 static void SetFlagsFromCommandLine(int* argc,
2042 char** argv,
2043 bool remove_flags);
2044
kasper.lund7276f142008-07-30 08:49:36 +00002045 /** Get the version string. */
2046 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
2048 /**
2049 * Enables the host application to provide a mechanism for recording
2050 * statistics counters.
2051 */
2052 static void SetCounterFunction(CounterLookupCallback);
2053
2054 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002055 * Enables the host application to provide a mechanism for recording
2056 * histograms. The CreateHistogram function returns a
2057 * histogram which will later be passed to the AddHistogramSample
2058 * function.
2059 */
2060 static void SetCreateHistogramFunction(CreateHistogramCallback);
2061 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2062
2063 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 * Enables the computation of a sliding window of states. The sliding
2065 * window information is recorded in statistics counters.
2066 */
2067 static void EnableSlidingStateWindow();
2068
2069 /** Callback function for reporting failed access checks.*/
2070 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2071
2072 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * Enables the host application to receive a notification before a
2074 * major garbage colletion. Allocations are not allowed in the
2075 * callback function, you therefore cannot manipulate objects (set
2076 * or delete properties for example) since it is possible such
2077 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 */
2079 static void SetGlobalGCPrologueCallback(GCCallback);
2080
2081 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002082 * Enables the host application to receive a notification after a
2083 * major garbage collection. Allocations are not allowed in the
2084 * callback function, you therefore cannot manipulate objects (set
2085 * or delete properties for example) since it is possible such
2086 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 */
2088 static void SetGlobalGCEpilogueCallback(GCCallback);
2089
2090 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002091 * Allows the host application to group objects together. If one
2092 * object in the group is alive, all objects in the group are alive.
2093 * After each garbage collection, object groups are removed. It is
2094 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002095 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002096 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002098 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099
2100 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002101 * Initializes from snapshot if possible. Otherwise, attempts to
2102 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 */
2104 static bool Initialize();
2105
kasper.lund7276f142008-07-30 08:49:36 +00002106 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002107 * Adjusts the amount of registered external memory. Used to give
2108 * V8 an indication of the amount of externally allocated memory
2109 * that is kept alive by JavaScript objects. V8 uses this to decide
2110 * when to perform global garbage collections. Registering
2111 * externally allocated memory will trigger global garbage
2112 * collections more often than otherwise in an attempt to garbage
2113 * collect the JavaScript objects keeping the externally allocated
2114 * memory alive.
2115 *
2116 * \param change_in_bytes the change in externally allocated memory
2117 * that is kept alive by JavaScript objects.
2118 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002119 */
2120 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2121
iposva@chromium.org245aa852009-02-10 00:49:54 +00002122 /**
2123 * Suspends recording of tick samples in the profiler.
2124 * When the V8 profiling mode is enabled (usually via command line
2125 * switches) this function suspends recording of tick samples.
2126 * Profiling ticks are discarded until ResumeProfiler() is called.
2127 *
2128 * See also the --prof and --prof_auto command line switches to
2129 * enable V8 profiling.
2130 */
2131 static void PauseProfiler();
2132
2133 /**
2134 * Resumes recording of tick samples in the profiler.
2135 * See also PauseProfiler().
2136 */
2137 static void ResumeProfiler();
2138
ager@chromium.org41826e72009-03-30 13:30:57 +00002139 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002140 * Return whether profiler is currently paused.
2141 */
2142 static bool IsProfilerPaused();
2143
2144 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002145 * Resumes specified profiler modules.
2146 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2147 * See ProfilerModules enum.
2148 *
2149 * \param flags Flags specifying profiler modules.
2150 */
2151 static void ResumeProfilerEx(int flags);
2152
2153 /**
2154 * Pauses specified profiler modules.
2155 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2156 * See ProfilerModules enum.
2157 *
2158 * \param flags Flags specifying profiler modules.
2159 */
2160 static void PauseProfilerEx(int flags);
2161
2162 /**
2163 * Returns active (resumed) profiler modules.
2164 * See ProfilerModules enum.
2165 *
2166 * \returns active profiler modules.
2167 */
2168 static int GetActiveProfilerModules();
2169
2170 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002171 * If logging is performed into a memory buffer (via --logfile=*), allows to
2172 * retrieve previously written messages. This can be used for retrieving
2173 * profiler log data in the application. This function is thread-safe.
2174 *
2175 * Caller provides a destination buffer that must exist during GetLogLines
2176 * call. Only whole log lines are copied into the buffer.
2177 *
2178 * \param from_pos specified a point in a buffer to read from, 0 is the
2179 * beginning of a buffer. It is assumed that caller updates its current
2180 * position using returned size value from the previous call.
2181 * \param dest_buf destination buffer for log data.
2182 * \param max_size size of the destination buffer.
2183 * \returns actual size of log data copied into buffer.
2184 */
2185 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2186
2187
2188 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002189 * Releases any resources used by v8 and stops any utility threads
2190 * that may be running. Note that disposing v8 is permanent, it
2191 * cannot be reinitialized.
2192 *
2193 * It should generally not be necessary to dispose v8 before exiting
2194 * a process, this should happen automatically. It is only necessary
2195 * to use if the process needs the resources taken up by v8.
2196 */
2197 static bool Dispose();
2198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 private:
2200 V8();
2201
2202 static void** GlobalizeReference(void** handle);
2203 static void DisposeGlobal(void** global_handle);
2204 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
2205 static void ClearWeak(void** global_handle);
2206 static bool IsGlobalNearDeath(void** global_handle);
2207 static bool IsGlobalWeak(void** global_handle);
2208
2209 template <class T> friend class Handle;
2210 template <class T> friend class Local;
2211 template <class T> friend class Persistent;
2212 friend class Context;
2213};
2214
2215
2216/**
2217 * An external exception handler.
2218 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002219class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 public:
2221
2222 /**
2223 * Creates a new try/catch block and registers it with v8.
2224 */
2225 TryCatch();
2226
2227 /**
2228 * Unregisters and deletes this try/catch block.
2229 */
2230 ~TryCatch();
2231
2232 /**
2233 * Returns true if an exception has been caught by this try/catch block.
2234 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002235 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236
2237 /**
2238 * Returns the exception caught by this try/catch block. If no exception has
2239 * been caught an empty handle is returned.
2240 *
2241 * The returned handle is valid until this TryCatch block has been destroyed.
2242 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002243 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244
2245 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002246 * Returns the message associated with this exception. If there is
2247 * no message associated an empty handle is returned.
2248 *
2249 * The returned handle is valid until this TryCatch block has been
2250 * destroyed.
2251 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002252 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002253
2254 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 * Clears any exceptions that may have been caught by this try/catch block.
2256 * After this method has been called, HasCaught() will return false.
2257 *
2258 * It is not necessary to clear a try/catch block before using it again; if
2259 * another exception is thrown the previously caught exception will just be
2260 * overwritten. However, it is often a good idea since it makes it easier
2261 * to determine which operation threw a given exception.
2262 */
2263 void Reset();
2264
v8.team.kasperl727e9952008-09-02 14:56:44 +00002265 /**
2266 * Set verbosity of the external exception handler.
2267 *
2268 * By default, exceptions that are caught by an external exception
2269 * handler are not reported. Call SetVerbose with true on an
2270 * external exception handler to have exceptions caught by the
2271 * handler reported as if they were not caught.
2272 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 void SetVerbose(bool value);
2274
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002275 /**
2276 * Set whether or not this TryCatch should capture a Message object
2277 * which holds source information about where the exception
2278 * occurred. True by default.
2279 */
2280 void SetCaptureMessage(bool value);
2281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 public:
2283 TryCatch* next_;
2284 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002285 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002287 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002288 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289};
2290
2291
2292// --- C o n t e x t ---
2293
2294
2295/**
2296 * Ignore
2297 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002298class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 public:
2300 ExtensionConfiguration(int name_count, const char* names[])
2301 : name_count_(name_count), names_(names) { }
2302 private:
2303 friend class ImplementationUtilities;
2304 int name_count_;
2305 const char** names_;
2306};
2307
2308
2309/**
2310 * A sandboxed execution context with its own set of built-in objects
2311 * and functions.
2312 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002313class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002315 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 Local<Object> Global();
2317
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002318 /**
2319 * Detaches the global object from its context before
2320 * the global object can be reused to create a new context.
2321 */
2322 void DetachGlobal();
2323
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 /** Creates a new context. */
2325 static Persistent<Context> New(
2326 ExtensionConfiguration* extensions = 0,
2327 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2328 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329
kasper.lund44510672008-07-25 07:37:58 +00002330 /** Returns the last entered context. */
2331 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332
kasper.lund44510672008-07-25 07:37:58 +00002333 /** Returns the context that is on the top of the stack. */
2334 static Local<Context> GetCurrent();
2335
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002337 * Returns the context of the calling JavaScript code. That is the
2338 * context of the top-most JavaScript frame. If there are no
2339 * JavaScript frames an empty handle is returned.
2340 */
2341 static Local<Context> GetCalling();
2342
2343 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 * Sets the security token for the context. To access an object in
2345 * another context, the security tokens must match.
2346 */
2347 void SetSecurityToken(Handle<Value> token);
2348
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002349 /** Restores the security token to the default value. */
2350 void UseDefaultSecurityToken();
2351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 /** Returns the security token of this context.*/
2353 Handle<Value> GetSecurityToken();
2354
v8.team.kasperl727e9952008-09-02 14:56:44 +00002355 /**
2356 * Enter this context. After entering a context, all code compiled
2357 * and run is compiled and run in this context. If another context
2358 * is already entered, this old context is saved so it can be
2359 * restored when the new context is exited.
2360 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002362
2363 /**
2364 * Exit this context. Exiting the current context restores the
2365 * context that was in place when entering the current context.
2366 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 void Exit();
2368
v8.team.kasperl727e9952008-09-02 14:56:44 +00002369 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 bool HasOutOfMemoryException();
2371
v8.team.kasperl727e9952008-09-02 14:56:44 +00002372 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002373 static bool InContext();
2374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002376 * Associate an additional data object with the context. This is mainly used
2377 * with the debugger to provide additional information on the context through
2378 * the debugger API.
2379 */
2380 void SetData(Handle<Value> data);
2381 Local<Value> GetData();
2382
2383 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 * Stack-allocated class which sets the execution context for all
2385 * operations executed within a local scope.
2386 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002387 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 public:
2389 inline Scope(Handle<Context> context) : context_(context) {
2390 context_->Enter();
2391 }
2392 inline ~Scope() { context_->Exit(); }
2393 private:
2394 Handle<Context> context_;
2395 };
2396
2397 private:
2398 friend class Value;
2399 friend class Script;
2400 friend class Object;
2401 friend class Function;
2402};
2403
2404
2405/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002406 * Multiple threads in V8 are allowed, but only one thread at a time
2407 * is allowed to use V8. The definition of 'using V8' includes
2408 * accessing handles or holding onto object pointers obtained from V8
2409 * handles. It is up to the user of V8 to ensure (perhaps with
2410 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411 *
2412 * If you wish to start using V8 in a thread you can do this by constructing
2413 * a v8::Locker object. After the code using V8 has completed for the
2414 * current thread you can call the destructor. This can be combined
2415 * with C++ scope-based construction as follows:
2416 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002417 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 * ...
2419 * {
2420 * v8::Locker locker;
2421 * ...
2422 * // Code using V8 goes here.
2423 * ...
2424 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002425 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 *
2427 * If you wish to stop using V8 in a thread A you can do this by either
2428 * by destroying the v8::Locker object as above or by constructing a
2429 * v8::Unlocker object:
2430 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002431 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432 * {
2433 * v8::Unlocker unlocker;
2434 * ...
2435 * // Code not using V8 goes here while V8 can run in another thread.
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 * The Unlocker object is intended for use in a long-running callback
2441 * from V8, where you want to release the V8 lock for other threads to
2442 * use.
2443 *
2444 * The v8::Locker is a recursive lock. That is, you can lock more than
2445 * once in a given thread. This can be useful if you have code that can
2446 * be called either from code that holds the lock or from code that does
2447 * not. The Unlocker is not recursive so you can not have several
2448 * Unlockers on the stack at once, and you can not use an Unlocker in a
2449 * thread that is not inside a Locker's scope.
2450 *
2451 * An unlocker will unlock several lockers if it has to and reinstate
2452 * the correct depth of locking on its destruction. eg.:
2453 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002454 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 * // V8 not locked.
2456 * {
2457 * v8::Locker locker;
2458 * // V8 locked.
2459 * {
2460 * v8::Locker another_locker;
2461 * // V8 still locked (2 levels).
2462 * {
2463 * v8::Unlocker unlocker;
2464 * // V8 not locked.
2465 * }
2466 * // V8 locked again (2 levels).
2467 * }
2468 * // V8 still locked (1 level).
2469 * }
2470 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002471 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002473class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 public:
2475 Unlocker();
2476 ~Unlocker();
2477};
2478
2479
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002480class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 public:
2482 Locker();
2483 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002484
2485 /**
2486 * Start preemption.
2487 *
2488 * When preemption is started, a timer is fired every n milli seconds
2489 * that will switch between multiple threads that are in contention
2490 * for the V8 lock.
2491 */
2492 static void StartPreemption(int every_n_ms);
2493
2494 /**
2495 * Stop preemption.
2496 */
2497 static void StopPreemption();
2498
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002499 /**
2500 * Returns whether or not the locker is locked by the current thread.
2501 */
2502 static bool IsLocked();
2503
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002504 /**
2505 * Returns whether v8::Locker is being used by this V8 instance.
2506 */
2507 static bool IsActive() { return active_; }
2508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 private:
2510 bool has_lock_;
2511 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002512
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002513 static bool active_;
2514
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002515 // Disallow copying and assigning.
2516 Locker(const Locker&);
2517 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518};
2519
2520
2521
2522// --- I m p l e m e n t a t i o n ---
2523
2524template <class T>
2525Handle<T>::Handle() : val_(0) { }
2526
2527
2528template <class T>
2529Local<T>::Local() : Handle<T>() { }
2530
2531
2532template <class T>
2533Local<T> Local<T>::New(Handle<T> that) {
2534 if (that.IsEmpty()) return Local<T>();
2535 void** p = reinterpret_cast<void**>(*that);
2536 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2537}
2538
2539
2540template <class T>
2541Persistent<T> Persistent<T>::New(Handle<T> that) {
2542 if (that.IsEmpty()) return Persistent<T>();
2543 void** p = reinterpret_cast<void**>(*that);
2544 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2545}
2546
2547
2548template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002549bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 if (this->IsEmpty()) return false;
2551 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2552}
2553
2554
2555template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002556bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 if (this->IsEmpty()) return false;
2558 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2559}
2560
2561
2562template <class T>
2563void Persistent<T>::Dispose() {
2564 if (this->IsEmpty()) return;
2565 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2566}
2567
2568
2569template <class T>
2570Persistent<T>::Persistent() : Handle<T>() { }
2571
2572template <class T>
2573void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2574 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2575}
2576
2577template <class T>
2578void Persistent<T>::ClearWeak() {
2579 V8::ClearWeak(reinterpret_cast<void**>(**this));
2580}
2581
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582Local<Value> Arguments::operator[](int i) const {
2583 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2584 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2585}
2586
2587
2588Local<Function> Arguments::Callee() const {
2589 return callee_;
2590}
2591
2592
2593Local<Object> Arguments::This() const {
2594 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2595}
2596
2597
2598Local<Object> Arguments::Holder() const {
2599 return holder_;
2600}
2601
2602
2603Local<Value> Arguments::Data() const {
2604 return data_;
2605}
2606
2607
2608bool Arguments::IsConstructCall() const {
2609 return is_construct_call_;
2610}
2611
2612
2613int Arguments::Length() const {
2614 return length_;
2615}
2616
2617
2618Local<Value> AccessorInfo::Data() const {
2619 return data_;
2620}
2621
2622
2623Local<Object> AccessorInfo::This() const {
2624 return self_;
2625}
2626
2627
2628Local<Object> AccessorInfo::Holder() const {
2629 return holder_;
2630}
2631
2632
2633template <class T>
2634Local<T> HandleScope::Close(Handle<T> value) {
2635 void** after = RawClose(reinterpret_cast<void**>(*value));
2636 return Local<T>(reinterpret_cast<T*>(after));
2637}
2638
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002639Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 return resource_name_;
2641}
2642
2643
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002644Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 return resource_line_offset_;
2646}
2647
2648
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002649Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 return resource_column_offset_;
2651}
2652
2653
2654Handle<Boolean> Boolean::New(bool value) {
2655 return value ? True() : False();
2656}
2657
2658
2659void Template::Set(const char* name, v8::Handle<Data> value) {
2660 Set(v8::String::New(name), value);
2661}
2662
2663
2664/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002665 * \example shell.cc
2666 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 * command-line and executes them.
2668 */
2669
2670
2671/**
2672 * \example process.cc
2673 */
2674
2675
2676} // namespace v8
2677
2678
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002679#undef V8EXPORT
2680#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681#undef TYPE_CHECK
2682
2683
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002684#endif // V8_H_