blob: a0cc4ea8b0ac951551c7211b2ebda141ac3e1c81 [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
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000516 /**
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 * The script object returned is context independent; when run it
522 * will use the currently entered context.
523 */
524 static Local<Script> New(Handle<String> source,
525 ScriptOrigin* origin = NULL,
526 ScriptData* pre_data = NULL);
527
528 /**
529 * Compiles the specified script using the specified file name
530 * object (typically a string) as the script's origin.
531 *
532 * The script object returned is context independent; when run it
533 * will use the currently entered context.
534 */
535 static Local<Script> New(Handle<String> source,
536 Handle<Value> file_name);
537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 /**
539 * Compiles the specified script. The ScriptOrigin* and ScriptData*
540 * parameters are owned by the caller of Script::Compile. No
541 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000542 *
543 * The script object returned is bound to the context that was active
544 * when this function was called. When run it will always use this
545 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 */
547 static Local<Script> Compile(Handle<String> source,
548 ScriptOrigin* origin = NULL,
549 ScriptData* pre_data = NULL);
550
mads.s.agercbaa0602008-08-14 13:41:48 +0000551 /**
552 * Compiles the specified script using the specified file name
553 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000554 *
555 * The script object returned is bound to the context that was active
556 * when this function was called. When run it will always use this
557 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000558 */
559 static Local<Script> Compile(Handle<String> source,
560 Handle<Value> file_name);
561
v8.team.kasperl727e9952008-09-02 14:56:44 +0000562 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000563 * Runs the script returning the resulting value. If the script is
564 * context independent (created using ::New) it will be run in the
565 * currently entered context. If it is context specific (created
566 * using ::Compile) it will be run in the context in which it was
567 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000568 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570
571 /**
572 * Returns the script id value.
573 */
574 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000575
576 /**
577 * Associate an additional data object with the script. This is mainly used
578 * with the debugger as this data object is only available through the
579 * debugger API.
580 */
581 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582};
583
584
585/**
586 * An error message.
587 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000588class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000590 Local<String> Get() const;
591 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000593 /**
594 * Returns the resource name for the script from where the function causing
595 * the error originates.
596 */
ager@chromium.org32912102009-01-16 10:38:43 +0000597 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000599 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000600 * Returns the resource data for the script from where the function causing
601 * the error originates.
602 */
603 Handle<Value> GetScriptData() const;
604
605 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000606 * Returns the number, 1-based, of the line where the error occurred.
607 */
ager@chromium.org32912102009-01-16 10:38:43 +0000608 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000610 /**
611 * Returns the index within the script of the first character where
612 * the error occurred.
613 */
ager@chromium.org32912102009-01-16 10:38:43 +0000614 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000615
616 /**
617 * Returns the index within the script of the last character where
618 * the error occurred.
619 */
ager@chromium.org32912102009-01-16 10:38:43 +0000620 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000621
622 /**
623 * Returns the index within the line of the first character where
624 * the error occurred.
625 */
ager@chromium.org32912102009-01-16 10:38:43 +0000626 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000627
628 /**
629 * Returns the index within the line of the last character where
630 * the error occurred.
631 */
ager@chromium.org32912102009-01-16 10:38:43 +0000632 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000633
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 // TODO(1245381): Print to a string instead of on a FILE.
635 static void PrintCurrentStackTrace(FILE* out);
636};
637
638
639// --- V a l u e ---
640
641
642/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000643 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000645class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 public:
647
648 /**
649 * Returns true if this value is the undefined value. See ECMA-262
650 * 4.3.10.
651 */
ager@chromium.org32912102009-01-16 10:38:43 +0000652 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654 /**
655 * Returns true if this value is the null value. See ECMA-262
656 * 4.3.11.
657 */
ager@chromium.org32912102009-01-16 10:38:43 +0000658 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
660 /**
661 * Returns true if this value is true.
662 */
ager@chromium.org32912102009-01-16 10:38:43 +0000663 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664
665 /**
666 * Returns true if this value is false.
667 */
ager@chromium.org32912102009-01-16 10:38:43 +0000668 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669
670 /**
671 * Returns true if this value is an instance of the String type.
672 * See ECMA-262 8.4.
673 */
ager@chromium.org32912102009-01-16 10:38:43 +0000674 bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675
676 /**
677 * Returns true if this value is a function.
678 */
ager@chromium.org32912102009-01-16 10:38:43 +0000679 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680
681 /**
682 * Returns true if this value is an array.
683 */
ager@chromium.org32912102009-01-16 10:38:43 +0000684 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685
v8.team.kasperl727e9952008-09-02 14:56:44 +0000686 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 * Returns true if this value is an object.
688 */
ager@chromium.org32912102009-01-16 10:38:43 +0000689 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690
v8.team.kasperl727e9952008-09-02 14:56:44 +0000691 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 * Returns true if this value is boolean.
693 */
ager@chromium.org32912102009-01-16 10:38:43 +0000694 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000695
v8.team.kasperl727e9952008-09-02 14:56:44 +0000696 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 * Returns true if this value is a number.
698 */
ager@chromium.org32912102009-01-16 10:38:43 +0000699 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
v8.team.kasperl727e9952008-09-02 14:56:44 +0000701 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 * Returns true if this value is external.
703 */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
v8.team.kasperl727e9952008-09-02 14:56:44 +0000706 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 * Returns true if this value is a 32-bit signed integer.
708 */
ager@chromium.org32912102009-01-16 10:38:43 +0000709 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000711 /**
712 * Returns true if this value is a Date.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000715
ager@chromium.org32912102009-01-16 10:38:43 +0000716 Local<Boolean> ToBoolean() const;
717 Local<Number> ToNumber() const;
718 Local<String> ToString() const;
719 Local<String> ToDetailString() const;
720 Local<Object> ToObject() const;
721 Local<Integer> ToInteger() const;
722 Local<Uint32> ToUint32() const;
723 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724
725 /**
726 * Attempts to convert a string to an array index.
727 * Returns an empty handle if the conversion fails.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730
ager@chromium.org32912102009-01-16 10:38:43 +0000731 bool BooleanValue() const;
732 double NumberValue() const;
733 int64_t IntegerValue() const;
734 uint32_t Uint32Value() const;
735 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736
737 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000738 bool Equals(Handle<Value> that) const;
739 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740};
741
742
743/**
744 * The superclass of primitive values. See ECMA-262 4.3.2.
745 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000746class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747
748
749/**
750 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
751 * or false value.
752 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000753class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000755 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 static inline Handle<Boolean> New(bool value);
757};
758
759
760/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000761 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000763class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000765
766 /**
767 * Returns the number of characters in this string.
768 */
ager@chromium.org32912102009-01-16 10:38:43 +0000769 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770
v8.team.kasperl727e9952008-09-02 14:56:44 +0000771 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000772 * Returns the number of bytes in the UTF-8 encoded
773 * representation of this string.
774 */
ager@chromium.org32912102009-01-16 10:38:43 +0000775 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000776
777 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000778 * Write the contents of the string to an external buffer.
779 * If no arguments are given, expects the buffer to be large
780 * enough to hold the entire string and NULL terminator. Copies
781 * the contents of the string and the NULL terminator into the
782 * buffer.
783 *
784 * Copies up to length characters into the output buffer.
785 * Only null-terminates if there is enough space in the buffer.
786 *
787 * \param buffer The buffer into which the string will be copied.
788 * \param start The starting position within the string at which
789 * copying begins.
790 * \param length The number of bytes to copy from the string.
791 * \return The number of characters copied to the buffer
792 * excluding the NULL terminator.
793 */
ager@chromium.org32912102009-01-16 10:38:43 +0000794 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
795 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
796 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
v8.team.kasperl727e9952008-09-02 14:56:44 +0000798 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000799 * A zero length string.
800 */
801 static v8::Local<v8::String> Empty();
802
803 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000804 * Returns true if the string is external
805 */
ager@chromium.org32912102009-01-16 10:38:43 +0000806 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807
v8.team.kasperl727e9952008-09-02 14:56:44 +0000808 /**
809 * Returns true if the string is both external and ascii
810 */
ager@chromium.org32912102009-01-16 10:38:43 +0000811 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000812 /**
813 * An ExternalStringResource is a wrapper around a two-byte string
814 * buffer that resides outside V8's heap. Implement an
815 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000816 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000817 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000818 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 public:
820 /**
821 * Override the destructor to manage the life cycle of the underlying
822 * buffer.
823 */
824 virtual ~ExternalStringResource() {}
825 /** The string data from the underlying buffer.*/
826 virtual const uint16_t* data() const = 0;
827 /** The length of the string. That is, the number of two-byte characters.*/
828 virtual size_t length() const = 0;
829 protected:
830 ExternalStringResource() {}
831 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000832 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 ExternalStringResource(const ExternalStringResource&);
834 void operator=(const ExternalStringResource&);
835 };
836
837 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000838 * An ExternalAsciiStringResource is a wrapper around an ascii
839 * string buffer that resides outside V8's heap. Implement an
840 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000841 * underlying buffer. Note that the string data must be immutable
842 * and that the data must be strict 7-bit ASCII, not Latin1 or
843 * UTF-8, which would require special treatment internally in the
844 * engine and, in the case of UTF-8, do not allow efficient indexing.
845 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000846 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000848 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 public:
850 /**
851 * Override the destructor to manage the life cycle of the underlying
852 * buffer.
853 */
854 virtual ~ExternalAsciiStringResource() {}
855 /** The string data from the underlying buffer.*/
856 virtual const char* data() const = 0;
857 /** The number of ascii characters in the string.*/
858 virtual size_t length() const = 0;
859 protected:
860 ExternalAsciiStringResource() {}
861 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000862 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
864 void operator=(const ExternalAsciiStringResource&);
865 };
866
867 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000868 * Get the ExternalStringResource for an external string. Returns
869 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 */
ager@chromium.org32912102009-01-16 10:38:43 +0000871 ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
873 /**
874 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000875 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 */
ager@chromium.org32912102009-01-16 10:38:43 +0000877 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879 static String* Cast(v8::Value* obj);
880
881 /**
882 * Allocates a new string from either utf-8 encoded or ascii data.
883 * The second parameter 'length' gives the buffer length.
884 * If the data is utf-8 encoded, the caller must
885 * be careful to supply the length parameter.
886 * If it is not given, the function calls
887 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000888 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 */
890 static Local<String> New(const char* data, int length = -1);
891
892 /** Allocates a new string from utf16 data.*/
893 static Local<String> New(const uint16_t* data, int length = -1);
894
895 /** Creates a symbol. Returns one if it exists already.*/
896 static Local<String> NewSymbol(const char* data, int length = -1);
897
v8.team.kasperl727e9952008-09-02 14:56:44 +0000898 /**
899 * Creates a new external string using the data defined in the given
900 * resource. The resource is deleted when the external string is no
901 * longer live on V8's heap. The caller of this function should not
902 * delete or modify the resource. Neither should the underlying buffer be
903 * deallocated or modified except through the destructor of the
904 * external string resource.
905 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000907
ager@chromium.org6f10e412009-02-13 10:11:16 +0000908 /**
909 * Associate an external string resource with this string by transforming it
910 * in place so that existing references to this string in the JavaScript heap
911 * will use the external string resource. The external string resource's
912 * character contents needs to be equivalent to this string.
913 * Returns true if the string has been changed to be an external string.
914 * The string is not modified if the operation fails.
915 */
916 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
v8.team.kasperl727e9952008-09-02 14:56:44 +0000918 /**
919 * Creates a new external string using the ascii data defined in the given
920 * resource. The resource is deleted when the external string is no
921 * longer live on V8's heap. The caller of this function should not
922 * delete or modify the resource. Neither should the underlying buffer be
923 * deallocated or modified except through the destructor of the
924 * external string resource.
925 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000927
ager@chromium.org6f10e412009-02-13 10:11:16 +0000928 /**
929 * Associate an external string resource with this string by transforming it
930 * in place so that existing references to this string in the JavaScript heap
931 * will use the external string resource. The external string resource's
932 * character contents needs to be equivalent to this string.
933 * Returns true if the string has been changed to be an external string.
934 * The string is not modified if the operation fails.
935 */
936 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000938 /**
939 * Returns true if this string can be made external.
940 */
941 bool CanMakeExternal();
942
kasper.lund7276f142008-07-30 08:49:36 +0000943 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 static Local<String> NewUndetectable(const char* data, int length = -1);
945
kasper.lund7276f142008-07-30 08:49:36 +0000946 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
948
949 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000950 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000951 * you want to print the object. If conversion to a string fails
952 * (eg. due to an exception in the toString() method of the object)
953 * then the length() method returns 0 and the * operator returns
954 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000955 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000956 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000957 public:
958 explicit Utf8Value(Handle<v8::Value> obj);
959 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000960 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000961 int length() { return length_; }
962 private:
963 char* str_;
964 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000965
966 // Disallow copying and assigning.
967 Utf8Value(const Utf8Value&);
968 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000969 };
970
971 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 * Converts an object to an ascii string.
973 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000974 * If conversion to a string fails (eg. due to an exception in the toString()
975 * method of the object) then the length() method returns 0 and the * operator
976 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000978 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 public:
980 explicit AsciiValue(Handle<v8::Value> obj);
981 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000982 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000983 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 private:
985 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000986 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000987
988 // Disallow copying and assigning.
989 AsciiValue(const AsciiValue&);
990 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 };
992
993 /**
994 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000995 * If conversion to a string fails (eg. due to an exception in the toString()
996 * method of the object) then the length() method returns 0 and the * operator
997 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000999 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000 public:
1001 explicit Value(Handle<v8::Value> obj);
1002 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00001003 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001004 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 private:
1006 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001007 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001008
1009 // Disallow copying and assigning.
1010 Value(const Value&);
1011 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 };
1013};
1014
1015
1016/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001017 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001019class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001021 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 static Local<Number> New(double value);
1023 static Number* Cast(v8::Value* obj);
1024 private:
1025 Number();
1026};
1027
1028
1029/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001030 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001032class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033 public:
1034 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001035 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 static Integer* Cast(v8::Value* obj);
1037 private:
1038 Integer();
1039};
1040
1041
1042/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001043 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001045class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001047 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 private:
1049 Int32();
1050};
1051
1052
1053/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001054 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001056class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001058 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 private:
1060 Uint32();
1061};
1062
1063
1064/**
1065 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1066 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001067class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 public:
1069 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001070
1071 /**
1072 * A specialization of Value::NumberValue that is more efficient
1073 * because we know the structure of this object.
1074 */
ager@chromium.org32912102009-01-16 10:38:43 +00001075 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001076
1077 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078};
1079
1080
1081enum PropertyAttribute {
1082 None = 0,
1083 ReadOnly = 1 << 0,
1084 DontEnum = 1 << 1,
1085 DontDelete = 1 << 2
1086};
1087
1088/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001089 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001091class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 public:
1093 bool Set(Handle<Value> key,
1094 Handle<Value> value,
1095 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001096
ager@chromium.orge2902be2009-06-08 12:21:35 +00001097 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001098 // overriding accessors or read-only properties.
1099 //
1100 // Note that if the object has an interceptor the property will be set
1101 // locally, but since the interceptor takes precedence the local property
1102 // will only be returned if the interceptor doesn't return a value.
1103 //
1104 // Note also that this only works for named properties.
1105 bool ForceSet(Handle<Value> key,
1106 Handle<Value> value,
1107 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 Local<Value> Get(Handle<Value> key);
1110
1111 // TODO(1245389): Replace the type-specific versions of these
1112 // functions with generic ones that accept a Handle<Value> key.
1113 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001116
1117 // Delete a property on this object bypassing interceptors and
1118 // ignoring dont-delete attributes.
1119 bool ForceDelete(Handle<Value> key);
1120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 bool Delete(uint32_t index);
1124
1125 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001126 * Returns an array containing the names of the enumerable properties
1127 * of this object, including properties from prototype objects. The
1128 * array returned by this method contains the same values as would
1129 * be enumerated by a for-in statement over this object.
1130 */
1131 Local<Array> GetPropertyNames();
1132
1133 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134 * Get the prototype object. This does not skip objects marked to
1135 * be skipped by __proto__ and it does not consult the security
1136 * handler.
1137 */
1138 Local<Value> GetPrototype();
1139
1140 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001141 * Finds an instance of the given function template in the prototype
1142 * chain.
1143 */
1144 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1145
1146 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 * Call builtin Object.prototype.toString on this object.
1148 * This is different from Value::ToString() that may call
1149 * user-defined toString function. This one does not.
1150 */
1151 Local<String> ObjectProtoToString();
1152
kasper.lund212ac232008-07-16 07:07:30 +00001153 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001155 /** Gets the value in an internal field. */
1156 Local<Value> GetInternalField(int index);
1157 /** Sets the value in an internal field. */
1158 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001160 // The two functions below do not perform index bounds checks and
1161 // they do not check that the VM is still running. Use with caution.
1162 /** Gets a native pointer from an internal field. */
1163 void* GetPointerFromInternalField(int index);
1164 /** Sets a native pointer in an internal field. */
1165 void SetPointerInInternalField(int index, void* value);
1166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 // Testers for local properties.
1168 bool HasRealNamedProperty(Handle<String> key);
1169 bool HasRealIndexedProperty(uint32_t index);
1170 bool HasRealNamedCallbackProperty(Handle<String> key);
1171
1172 /**
1173 * If result.IsEmpty() no real property was located in the prototype chain.
1174 * This means interceptors in the prototype chain are not called.
1175 */
1176 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1177
1178 /** Tests for a named lookup interceptor.*/
1179 bool HasNamedLookupInterceptor();
1180
kasper.lund212ac232008-07-16 07:07:30 +00001181 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 bool HasIndexedLookupInterceptor();
1183
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001184 /**
1185 * Turns on access check on the object if the object is an instance of
1186 * a template that has access check callbacks. If an object has no
1187 * access check info, the object cannot be accessed by anyone.
1188 */
1189 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001190
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001191 /**
1192 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001193 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001194 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001195 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001196 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001197 */
1198 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001199
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001200 /**
1201 * Access hidden properties on JavaScript objects. These properties are
1202 * hidden from the executing JavaScript and only accessible through the V8
1203 * C++ API. Hidden properties introduced by V8 internally (for example the
1204 * identity hash) are prefixed with "v8::".
1205 */
1206 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1207 Local<Value> GetHiddenValue(Handle<String> key);
1208 bool DeleteHiddenValue(Handle<String> key);
1209
1210 /**
1211 * Clone this object with a fast but shallow copy. Values will point
1212 * to the same values as the original object.
1213 */
1214 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001216 /**
1217 * Set the backing store of the indexed properties to be managed by the
1218 * embedding layer. Access to the indexed properties will follow the rules
1219 * spelled out in CanvasPixelArray.
1220 * Note: The embedding program still owns the data and needs to ensure that
1221 * the backing store is preserved while V8 has a reference.
1222 */
1223 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1224
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 static Local<Object> New();
1226 static Object* Cast(Value* obj);
1227 private:
1228 Object();
1229};
1230
1231
1232/**
1233 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1234 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001235class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001237 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238
ager@chromium.org3e875802009-06-29 08:26:34 +00001239 /**
1240 * Clones an element at index |index|. Returns an empty
1241 * handle if cloning fails (for any reason).
1242 */
1243 Local<Object> CloneElementAt(uint32_t index);
1244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 static Local<Array> New(int length = 0);
1246 static Array* Cast(Value* obj);
1247 private:
1248 Array();
1249};
1250
1251
1252/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001253 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001255class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001257 Local<Object> NewInstance() const;
1258 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1260 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001261 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 static Function* Cast(Value* obj);
1263 private:
1264 Function();
1265};
1266
1267
1268/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001269 * A JavaScript value that wraps a C++ void*. This type of value is
1270 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001272 *
1273 * The Wrap function V8 will return the most optimal Value object wrapping the
1274 * C++ void*. The type of the value is not guaranteed to be an External object
1275 * and no assumptions about its type should be made. To access the wrapped
1276 * value Unwrap should be used, all other operations on that object will lead
1277 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001279class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001281 static Local<Value> Wrap(void* data);
1282 static void* Unwrap(Handle<Value> obj);
1283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 static Local<External> New(void* value);
1285 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001286 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 private:
1288 External();
1289};
1290
1291
1292// --- T e m p l a t e s ---
1293
1294
1295/**
1296 * The superclass of object and function templates.
1297 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001298class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 public:
1300 /** Adds a property to each instance created by this template.*/
1301 void Set(Handle<String> name, Handle<Data> value,
1302 PropertyAttribute attributes = None);
1303 inline void Set(const char* name, Handle<Data> value);
1304 private:
1305 Template();
1306
1307 friend class ObjectTemplate;
1308 friend class FunctionTemplate;
1309};
1310
1311
1312/**
1313 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001314 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 * including the receiver, the number and values of arguments, and
1316 * the holder of the function.
1317 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001318class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 public:
1320 inline int Length() const;
1321 inline Local<Value> operator[](int i) const;
1322 inline Local<Function> Callee() const;
1323 inline Local<Object> This() const;
1324 inline Local<Object> Holder() const;
1325 inline bool IsConstructCall() const;
1326 inline Local<Value> Data() const;
1327 private:
1328 Arguments();
1329 friend class ImplementationUtilities;
1330 inline Arguments(Local<Value> data,
1331 Local<Object> holder,
1332 Local<Function> callee,
1333 bool is_construct_call,
1334 void** values, int length);
1335 Local<Value> data_;
1336 Local<Object> holder_;
1337 Local<Function> callee_;
1338 bool is_construct_call_;
1339 void** values_;
1340 int length_;
1341};
1342
1343
1344/**
1345 * The information passed to an accessor callback about the context
1346 * of the property access.
1347 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001348class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 public:
1350 inline AccessorInfo(Local<Object> self,
1351 Local<Value> data,
1352 Local<Object> holder)
1353 : self_(self), data_(data), holder_(holder) { }
1354 inline Local<Value> Data() const;
1355 inline Local<Object> This() const;
1356 inline Local<Object> Holder() const;
1357 private:
1358 Local<Object> self_;
1359 Local<Value> data_;
1360 Local<Object> holder_;
1361};
1362
1363
1364typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1365
1366typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1367
1368/**
1369 * Accessor[Getter|Setter] are used as callback functions when
1370 * setting|getting a particular property. See objectTemplate::SetAccessor.
1371 */
1372typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1373 const AccessorInfo& info);
1374
1375
1376typedef void (*AccessorSetter)(Local<String> property,
1377 Local<Value> value,
1378 const AccessorInfo& info);
1379
1380
1381/**
1382 * NamedProperty[Getter|Setter] are used as interceptors on object.
1383 * See ObjectTemplate::SetNamedPropertyHandler.
1384 */
1385typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1386 const AccessorInfo& info);
1387
1388
1389/**
1390 * Returns the value if the setter intercepts the request.
1391 * Otherwise, returns an empty handle.
1392 */
1393typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1394 Local<Value> value,
1395 const AccessorInfo& info);
1396
1397
1398/**
1399 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001400 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 */
1402typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1403 const AccessorInfo& info);
1404
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> (*NamedPropertyDeleter)(Local<String> property,
1412 const AccessorInfo& info);
1413
1414/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001415 * Returns an array containing the names of the properties the named
1416 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 */
1418typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1419
v8.team.kasperl727e9952008-09-02 14:56:44 +00001420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001422 * Returns the value of the property if the getter intercepts the
1423 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 */
1425typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1426 const AccessorInfo& info);
1427
1428
1429/**
1430 * Returns the value if the setter intercepts the request.
1431 * Otherwise, returns an empty handle.
1432 */
1433typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1434 Local<Value> value,
1435 const AccessorInfo& info);
1436
1437
1438/**
1439 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001440 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 */
1442typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1443 const AccessorInfo& info);
1444
1445/**
1446 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001447 * The return value is true if the property could be deleted and false
1448 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 */
1450typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1451 const AccessorInfo& info);
1452
v8.team.kasperl727e9952008-09-02 14:56:44 +00001453/**
1454 * Returns an array containing the indices of the properties the
1455 * indexed property getter intercepts.
1456 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1458
1459
1460/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001461 * Access control specifications.
1462 *
1463 * Some accessors should be accessible across contexts. These
1464 * accessors have an explicit access control parameter which specifies
1465 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001466 *
1467 * Additionally, for security, accessors can prohibit overwriting by
1468 * accessors defined in JavaScript. For objects that have such
1469 * accessors either locally or in their prototype chain it is not
1470 * possible to overwrite the accessor by using __defineGetter__ or
1471 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 */
1473enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001474 DEFAULT = 0,
1475 ALL_CAN_READ = 1,
1476 ALL_CAN_WRITE = 1 << 1,
1477 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478};
1479
1480
1481/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001482 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 */
1484enum AccessType {
1485 ACCESS_GET,
1486 ACCESS_SET,
1487 ACCESS_HAS,
1488 ACCESS_DELETE,
1489 ACCESS_KEYS
1490};
1491
v8.team.kasperl727e9952008-09-02 14:56:44 +00001492
1493/**
1494 * Returns true if cross-context access should be allowed to the named
1495 * property with the given key on the global object.
1496 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497typedef bool (*NamedSecurityCallback)(Local<Object> global,
1498 Local<Value> key,
1499 AccessType type,
1500 Local<Value> data);
1501
v8.team.kasperl727e9952008-09-02 14:56:44 +00001502
1503/**
1504 * Returns true if cross-context access should be allowed to the indexed
1505 * property with the given index on the global object.
1506 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1508 uint32_t index,
1509 AccessType type,
1510 Local<Value> data);
1511
1512
1513/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001514 * A FunctionTemplate is used to create functions at runtime. There
1515 * can only be one function created from a FunctionTemplate in a
1516 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 *
1518 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001519 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001521 * A FunctionTemplate has a corresponding instance template which is
1522 * used to create object instances when the function is used as a
1523 * constructor. Properties added to the instance template are added to
1524 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 *
1526 * A FunctionTemplate can have a prototype template. The prototype template
1527 * is used to create the prototype object of the function.
1528 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001529 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001531 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1533 * t->Set("func_property", v8::Number::New(1));
1534 *
1535 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1536 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1537 * proto_t->Set("proto_const", v8::Number::New(2));
1538 *
1539 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1540 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1541 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1542 * instance_t->Set("instance_property", Number::New(3));
1543 *
1544 * v8::Local<v8::Function> function = t->GetFunction();
1545 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001546 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547 *
1548 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001549 * and "instance" for the instance object created above. The function
1550 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001552 * \code
1553 * func_property in function == true;
1554 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001556 * function.prototype.proto_method() invokes 'InvokeCallback'
1557 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001559 * instance instanceof function == true;
1560 * instance.instance_accessor calls 'InstanceAccessorCallback'
1561 * instance.instance_property == 3;
1562 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001564 * A FunctionTemplate can inherit from another one by calling the
1565 * FunctionTemplate::Inherit method. The following graph illustrates
1566 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001568 * \code
1569 * FunctionTemplate Parent -> Parent() . prototype -> { }
1570 * ^ ^
1571 * | Inherit(Parent) | .__proto__
1572 * | |
1573 * FunctionTemplate Child -> Child() . prototype -> { }
1574 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001576 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1577 * object of the Child() function has __proto__ pointing to the
1578 * Parent() function's prototype object. An instance of the Child
1579 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001581 * Let Parent be the FunctionTemplate initialized in the previous
1582 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001584 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 * Local<FunctionTemplate> parent = t;
1586 * Local<FunctionTemplate> child = FunctionTemplate::New();
1587 * child->Inherit(parent);
1588 *
1589 * Local<Function> child_function = child->GetFunction();
1590 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001591 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001593 * The Child function and Child instance will have the following
1594 * properties:
1595 *
1596 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001598 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001600 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001602class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 public:
1604 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001605 static Local<FunctionTemplate> New(
1606 InvocationCallback callback = 0,
1607 Handle<Value> data = Handle<Value>(),
1608 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 /** Returns the unique function instance in the current execution context.*/
1610 Local<Function> GetFunction();
1611
v8.team.kasperl727e9952008-09-02 14:56:44 +00001612 /**
1613 * Set the call-handler callback for a FunctionTemplate. This
1614 * callback is called whenever the function created from this
1615 * FunctionTemplate is called.
1616 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 void SetCallHandler(InvocationCallback callback,
1618 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619
v8.team.kasperl727e9952008-09-02 14:56:44 +00001620 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 Local<ObjectTemplate> InstanceTemplate();
1622
1623 /** Causes the function template to inherit from a parent function template.*/
1624 void Inherit(Handle<FunctionTemplate> parent);
1625
1626 /**
1627 * A PrototypeTemplate is the template used to create the prototype object
1628 * of the function created by this template.
1629 */
1630 Local<ObjectTemplate> PrototypeTemplate();
1631
v8.team.kasperl727e9952008-09-02 14:56:44 +00001632
1633 /**
1634 * Set the class name of the FunctionTemplate. This is used for
1635 * printing objects created with the function created from the
1636 * FunctionTemplate as its constructor.
1637 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638 void SetClassName(Handle<String> name);
1639
1640 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001641 * Determines whether the __proto__ accessor ignores instances of
1642 * the function template. If instances of the function template are
1643 * ignored, __proto__ skips all instances and instead returns the
1644 * next object in the prototype chain.
1645 *
1646 * Call with a value of true to make the __proto__ accessor ignore
1647 * instances of the function template. Call with a value of false
1648 * to make the __proto__ accessor not ignore instances of the
1649 * function template. By default, instances of a function template
1650 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 */
1652 void SetHiddenPrototype(bool value);
1653
1654 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001655 * Returns true if the given object is an instance of this function
1656 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 */
1658 bool HasInstance(Handle<Value> object);
1659
1660 private:
1661 FunctionTemplate();
1662 void AddInstancePropertyAccessor(Handle<String> name,
1663 AccessorGetter getter,
1664 AccessorSetter setter,
1665 Handle<Value> data,
1666 AccessControl settings,
1667 PropertyAttribute attributes);
1668 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1669 NamedPropertySetter setter,
1670 NamedPropertyQuery query,
1671 NamedPropertyDeleter remover,
1672 NamedPropertyEnumerator enumerator,
1673 Handle<Value> data);
1674 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1675 IndexedPropertySetter setter,
1676 IndexedPropertyQuery query,
1677 IndexedPropertyDeleter remover,
1678 IndexedPropertyEnumerator enumerator,
1679 Handle<Value> data);
1680 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1681 Handle<Value> data);
1682
1683 friend class Context;
1684 friend class ObjectTemplate;
1685};
1686
1687
1688/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001689 * An ObjectTemplate is used to create objects at runtime.
1690 *
1691 * Properties added to an ObjectTemplate are added to each object
1692 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001694class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001696 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001698
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 /** Creates a new instance of this template.*/
1700 Local<Object> NewInstance();
1701
1702 /**
1703 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001704 *
1705 * Whenever the property with the given name is accessed on objects
1706 * created from this ObjectTemplate the getter and setter callbacks
1707 * are called instead of getting and setting the property directly
1708 * on the JavaScript object.
1709 *
1710 * \param name The name of the property for which an accessor is added.
1711 * \param getter The callback to invoke when getting the property.
1712 * \param setter The callback to invoke when setting the property.
1713 * \param data A piece of data that will be passed to the getter and setter
1714 * callbacks whenever they are invoked.
1715 * \param settings Access control settings for the accessor. This is a bit
1716 * field consisting of one of more of
1717 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1718 * The default is to not allow cross-context access.
1719 * ALL_CAN_READ means that all cross-context reads are allowed.
1720 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1721 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1722 * cross-context access.
1723 * \param attribute The attributes of the property for which an accessor
1724 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725 */
1726 void SetAccessor(Handle<String> name,
1727 AccessorGetter getter,
1728 AccessorSetter setter = 0,
1729 Handle<Value> data = Handle<Value>(),
1730 AccessControl settings = DEFAULT,
1731 PropertyAttribute attribute = None);
1732
1733 /**
1734 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001735 *
1736 * Whenever a named property is accessed on objects created from
1737 * this object template, the provided callback is invoked instead of
1738 * accessing the property directly on the JavaScript object.
1739 *
1740 * \param getter The callback to invoke when getting a property.
1741 * \param setter The callback to invoke when setting a property.
1742 * \param query The callback to invoke to check is an object has a property.
1743 * \param deleter The callback to invoke when deleting a property.
1744 * \param enumerator The callback to invoke to enumerate all the named
1745 * properties of an object.
1746 * \param data A piece of data that will be passed to the callbacks
1747 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 */
1749 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1750 NamedPropertySetter setter = 0,
1751 NamedPropertyQuery query = 0,
1752 NamedPropertyDeleter deleter = 0,
1753 NamedPropertyEnumerator enumerator = 0,
1754 Handle<Value> data = Handle<Value>());
1755
1756 /**
1757 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001758 *
1759 * Whenever an indexed property is accessed on objects created from
1760 * this object template, the provided callback is invoked instead of
1761 * accessing the property directly on the JavaScript object.
1762 *
1763 * \param getter The callback to invoke when getting a property.
1764 * \param setter The callback to invoke when setting a property.
1765 * \param query The callback to invoke to check is an object has a property.
1766 * \param deleter The callback to invoke when deleting a property.
1767 * \param enumerator The callback to invoke to enumerate all the indexed
1768 * properties of an object.
1769 * \param data A piece of data that will be passed to the callbacks
1770 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 */
1772 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1773 IndexedPropertySetter setter = 0,
1774 IndexedPropertyQuery query = 0,
1775 IndexedPropertyDeleter deleter = 0,
1776 IndexedPropertyEnumerator enumerator = 0,
1777 Handle<Value> data = Handle<Value>());
1778 /**
1779 * Sets the callback to be used when calling instances created from
1780 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001781 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 * function.
1783 */
1784 void SetCallAsFunctionHandler(InvocationCallback callback,
1785 Handle<Value> data = Handle<Value>());
1786
v8.team.kasperl727e9952008-09-02 14:56:44 +00001787 /**
1788 * Mark object instances of the template as undetectable.
1789 *
1790 * In many ways, undetectable objects behave as though they are not
1791 * there. They behave like 'undefined' in conditionals and when
1792 * printed. However, properties can be accessed and called as on
1793 * normal objects.
1794 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 void MarkAsUndetectable();
1796
v8.team.kasperl727e9952008-09-02 14:56:44 +00001797 /**
1798 * Sets access check callbacks on the object template.
1799 *
1800 * When accessing properties on instances of this object template,
1801 * the access check callback will be called to determine whether or
1802 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001803 * The last parameter specifies whether access checks are turned
1804 * on by default on instances. If access checks are off by default,
1805 * they can be turned on on individual instances by calling
1806 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001807 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1809 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001810 Handle<Value> data = Handle<Value>(),
1811 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812
kasper.lund212ac232008-07-16 07:07:30 +00001813 /**
1814 * Gets the number of internal fields for objects generated from
1815 * this template.
1816 */
1817 int InternalFieldCount();
1818
1819 /**
1820 * Sets the number of internal fields for objects generated from
1821 * this template.
1822 */
1823 void SetInternalFieldCount(int value);
1824
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 private:
1826 ObjectTemplate();
1827 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1828 friend class FunctionTemplate;
1829};
1830
1831
1832/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001833 * A Signature specifies which receivers and arguments a function can
1834 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001836class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 public:
1838 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1839 Handle<FunctionTemplate>(),
1840 int argc = 0,
1841 Handle<FunctionTemplate> argv[] = 0);
1842 private:
1843 Signature();
1844};
1845
1846
1847/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001848 * A utility for determining the type of objects based on the template
1849 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001851class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 public:
1853 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1854 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1855 int match(Handle<Value> value);
1856 private:
1857 TypeSwitch();
1858};
1859
1860
1861// --- E x t e n s i o n s ---
1862
1863
1864/**
1865 * Ignore
1866 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001867class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 public:
1869 Extension(const char* name,
1870 const char* source = 0,
1871 int dep_count = 0,
1872 const char** deps = 0);
1873 virtual ~Extension() { }
1874 virtual v8::Handle<v8::FunctionTemplate>
1875 GetNativeFunction(v8::Handle<v8::String> name) {
1876 return v8::Handle<v8::FunctionTemplate>();
1877 }
1878
1879 const char* name() { return name_; }
1880 const char* source() { return source_; }
1881 int dependency_count() { return dep_count_; }
1882 const char** dependencies() { return deps_; }
1883 void set_auto_enable(bool value) { auto_enable_ = value; }
1884 bool auto_enable() { return auto_enable_; }
1885
1886 private:
1887 const char* name_;
1888 const char* source_;
1889 int dep_count_;
1890 const char** deps_;
1891 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001892
1893 // Disallow copying and assigning.
1894 Extension(const Extension&);
1895 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896};
1897
1898
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001899void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
1901
1902/**
1903 * Ignore
1904 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001905class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 public:
1907 inline DeclareExtension(Extension* extension) {
1908 RegisterExtension(extension);
1909 }
1910};
1911
1912
1913// --- S t a t i c s ---
1914
1915
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001916Handle<Primitive> V8EXPORT Undefined();
1917Handle<Primitive> V8EXPORT Null();
1918Handle<Boolean> V8EXPORT True();
1919Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920
1921
1922/**
1923 * A set of constraints that specifies the limits of the runtime's
1924 * memory use.
1925 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001926class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 public:
1928 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001929 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001931 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001933 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1935 private:
1936 int max_young_space_size_;
1937 int max_old_space_size_;
1938 uint32_t* stack_limit_;
1939};
1940
1941
1942bool SetResourceConstraints(ResourceConstraints* constraints);
1943
1944
1945// --- E x c e p t i o n s ---
1946
1947
1948typedef void (*FatalErrorCallback)(const char* location, const char* message);
1949
1950
1951typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1952
1953
1954/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001955 * Schedules an exception to be thrown when returning to JavaScript. When an
1956 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001958 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001960Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961
1962/**
1963 * Create new error objects by calling the corresponding error object
1964 * constructor with the message.
1965 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001966class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 public:
1968 static Local<Value> RangeError(Handle<String> message);
1969 static Local<Value> ReferenceError(Handle<String> message);
1970 static Local<Value> SyntaxError(Handle<String> message);
1971 static Local<Value> TypeError(Handle<String> message);
1972 static Local<Value> Error(Handle<String> message);
1973};
1974
1975
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001976// --- 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 +00001977
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001978typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001980typedef void* (*CreateHistogramCallback)(const char* name,
1981 int min,
1982 int max,
1983 size_t buckets);
1984
1985typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
1986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987// --- 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 ---
1988typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1989 AccessType type,
1990 Local<Value> data);
1991
1992// --- 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
1993
1994/**
1995 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001996 * before and after a major garbage collection. Allocations are not
1997 * allowed in the callback function, you therefore cannot manipulate
1998 * objects (set or delete properties for example) since it is possible
1999 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 */
2001typedef void (*GCCallback)();
2002
2003
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002004// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005
2006/**
2007 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002008 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010typedef Persistent<Context> (*ContextGenerator)();
2011
2012
2013/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002014 * Profiler modules.
2015 *
2016 * In V8, profiler consists of several modules: CPU profiler, and different
2017 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002018 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2019 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002020 */
2021enum ProfilerModules {
2022 PROFILER_MODULE_NONE = 0,
2023 PROFILER_MODULE_CPU = 1,
2024 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002025 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2026 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002027};
2028
2029
2030/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 * Container class for static utility functions.
2032 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002033class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002035 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 static void SetFatalErrorHandler(FatalErrorCallback that);
2037
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 /**
2039 * Ignore out-of-memory exceptions.
2040 *
2041 * V8 running out of memory is treated as a fatal error by default.
2042 * This means that the fatal error handler is called and that V8 is
2043 * terminated.
2044 *
2045 * IgnoreOutOfMemoryException can be used to not treat a
2046 * out-of-memory situation as a fatal error. This way, the contexts
2047 * that did not cause the out of memory problem might be able to
2048 * continue execution.
2049 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 static void IgnoreOutOfMemoryException();
2051
v8.team.kasperl727e9952008-09-02 14:56:44 +00002052 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002053 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002054 * fatal errors such as out-of-memory situations.
2055 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 static bool IsDead();
2057
2058 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002059 * Adds a message listener.
2060 *
2061 * The same message listener can be added more than once and it that
2062 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 */
2064 static bool AddMessageListener(MessageCallback that,
2065 Handle<Value> data = Handle<Value>());
2066
2067 /**
2068 * Remove all message listeners from the specified callback function.
2069 */
2070 static void RemoveMessageListeners(MessageCallback that);
2071
2072 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 */
2075 static void SetFlagsFromString(const char* str, int length);
2076
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002077 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002078 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002079 */
2080 static void SetFlagsFromCommandLine(int* argc,
2081 char** argv,
2082 bool remove_flags);
2083
kasper.lund7276f142008-07-30 08:49:36 +00002084 /** Get the version string. */
2085 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
2087 /**
2088 * Enables the host application to provide a mechanism for recording
2089 * statistics counters.
2090 */
2091 static void SetCounterFunction(CounterLookupCallback);
2092
2093 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002094 * Enables the host application to provide a mechanism for recording
2095 * histograms. The CreateHistogram function returns a
2096 * histogram which will later be passed to the AddHistogramSample
2097 * function.
2098 */
2099 static void SetCreateHistogramFunction(CreateHistogramCallback);
2100 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2101
2102 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 * Enables the computation of a sliding window of states. The sliding
2104 * window information is recorded in statistics counters.
2105 */
2106 static void EnableSlidingStateWindow();
2107
2108 /** Callback function for reporting failed access checks.*/
2109 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2110
2111 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112 * Enables the host application to receive a notification before a
2113 * major garbage colletion. Allocations are not allowed in the
2114 * callback function, you therefore cannot manipulate objects (set
2115 * or delete properties for example) since it is possible such
2116 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 */
2118 static void SetGlobalGCPrologueCallback(GCCallback);
2119
2120 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121 * Enables the host application to receive a notification after a
2122 * major garbage collection. Allocations are not allowed in the
2123 * callback function, you therefore cannot manipulate objects (set
2124 * or delete properties for example) since it is possible such
2125 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126 */
2127 static void SetGlobalGCEpilogueCallback(GCCallback);
2128
2129 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 * Allows the host application to group objects together. If one
2131 * object in the group is alive, all objects in the group are alive.
2132 * After each garbage collection, object groups are removed. It is
2133 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002134 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002135 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002137 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
2139 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002140 * Initializes from snapshot if possible. Otherwise, attempts to
2141 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 */
2143 static bool Initialize();
2144
kasper.lund7276f142008-07-30 08:49:36 +00002145 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002146 * Adjusts the amount of registered external memory. Used to give
2147 * V8 an indication of the amount of externally allocated memory
2148 * that is kept alive by JavaScript objects. V8 uses this to decide
2149 * when to perform global garbage collections. Registering
2150 * externally allocated memory will trigger global garbage
2151 * collections more often than otherwise in an attempt to garbage
2152 * collect the JavaScript objects keeping the externally allocated
2153 * memory alive.
2154 *
2155 * \param change_in_bytes the change in externally allocated memory
2156 * that is kept alive by JavaScript objects.
2157 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002158 */
2159 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2160
iposva@chromium.org245aa852009-02-10 00:49:54 +00002161 /**
2162 * Suspends recording of tick samples in the profiler.
2163 * When the V8 profiling mode is enabled (usually via command line
2164 * switches) this function suspends recording of tick samples.
2165 * Profiling ticks are discarded until ResumeProfiler() is called.
2166 *
2167 * See also the --prof and --prof_auto command line switches to
2168 * enable V8 profiling.
2169 */
2170 static void PauseProfiler();
2171
2172 /**
2173 * Resumes recording of tick samples in the profiler.
2174 * See also PauseProfiler().
2175 */
2176 static void ResumeProfiler();
2177
ager@chromium.org41826e72009-03-30 13:30:57 +00002178 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002179 * Return whether profiler is currently paused.
2180 */
2181 static bool IsProfilerPaused();
2182
2183 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002184 * Resumes specified profiler modules.
2185 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2186 * See ProfilerModules enum.
2187 *
2188 * \param flags Flags specifying profiler modules.
2189 */
2190 static void ResumeProfilerEx(int flags);
2191
2192 /**
2193 * Pauses specified profiler modules.
2194 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2195 * See ProfilerModules enum.
2196 *
2197 * \param flags Flags specifying profiler modules.
2198 */
2199 static void PauseProfilerEx(int flags);
2200
2201 /**
2202 * Returns active (resumed) profiler modules.
2203 * See ProfilerModules enum.
2204 *
2205 * \returns active profiler modules.
2206 */
2207 static int GetActiveProfilerModules();
2208
2209 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002210 * If logging is performed into a memory buffer (via --logfile=*), allows to
2211 * retrieve previously written messages. This can be used for retrieving
2212 * profiler log data in the application. This function is thread-safe.
2213 *
2214 * Caller provides a destination buffer that must exist during GetLogLines
2215 * call. Only whole log lines are copied into the buffer.
2216 *
2217 * \param from_pos specified a point in a buffer to read from, 0 is the
2218 * beginning of a buffer. It is assumed that caller updates its current
2219 * position using returned size value from the previous call.
2220 * \param dest_buf destination buffer for log data.
2221 * \param max_size size of the destination buffer.
2222 * \returns actual size of log data copied into buffer.
2223 */
2224 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2225
2226
2227 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002228 * Releases any resources used by v8 and stops any utility threads
2229 * that may be running. Note that disposing v8 is permanent, it
2230 * cannot be reinitialized.
2231 *
2232 * It should generally not be necessary to dispose v8 before exiting
2233 * a process, this should happen automatically. It is only necessary
2234 * to use if the process needs the resources taken up by v8.
2235 */
2236 static bool Dispose();
2237
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002238
2239 /**
2240 * Optional notification that the embedder is idle.
2241 * V8 uses the notification to reduce memory footprint.
2242 * \param is_high_priority tells whether the embedder is high priority.
2243 */
2244 static void IdleNotification(bool is_high_priority);
2245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 private:
2247 V8();
2248
2249 static void** GlobalizeReference(void** handle);
2250 static void DisposeGlobal(void** global_handle);
2251 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
2252 static void ClearWeak(void** global_handle);
2253 static bool IsGlobalNearDeath(void** global_handle);
2254 static bool IsGlobalWeak(void** global_handle);
2255
2256 template <class T> friend class Handle;
2257 template <class T> friend class Local;
2258 template <class T> friend class Persistent;
2259 friend class Context;
2260};
2261
2262
2263/**
2264 * An external exception handler.
2265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002266class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 public:
2268
2269 /**
2270 * Creates a new try/catch block and registers it with v8.
2271 */
2272 TryCatch();
2273
2274 /**
2275 * Unregisters and deletes this try/catch block.
2276 */
2277 ~TryCatch();
2278
2279 /**
2280 * Returns true if an exception has been caught by this try/catch block.
2281 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002282 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283
2284 /**
2285 * Returns the exception caught by this try/catch block. If no exception has
2286 * been caught an empty handle is returned.
2287 *
2288 * The returned handle is valid until this TryCatch block has been destroyed.
2289 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002290 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291
2292 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002293 * Returns the .stack property of the thrown object. If no .stack
2294 * property is present an empty handle is returned.
2295 */
2296 Local<Value> StackTrace() const;
2297
2298 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002299 * Returns the message associated with this exception. If there is
2300 * no message associated an empty handle is returned.
2301 *
2302 * The returned handle is valid until this TryCatch block has been
2303 * destroyed.
2304 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002305 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002306
2307 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 * Clears any exceptions that may have been caught by this try/catch block.
2309 * After this method has been called, HasCaught() will return false.
2310 *
2311 * It is not necessary to clear a try/catch block before using it again; if
2312 * another exception is thrown the previously caught exception will just be
2313 * overwritten. However, it is often a good idea since it makes it easier
2314 * to determine which operation threw a given exception.
2315 */
2316 void Reset();
2317
v8.team.kasperl727e9952008-09-02 14:56:44 +00002318 /**
2319 * Set verbosity of the external exception handler.
2320 *
2321 * By default, exceptions that are caught by an external exception
2322 * handler are not reported. Call SetVerbose with true on an
2323 * external exception handler to have exceptions caught by the
2324 * handler reported as if they were not caught.
2325 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 void SetVerbose(bool value);
2327
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002328 /**
2329 * Set whether or not this TryCatch should capture a Message object
2330 * which holds source information about where the exception
2331 * occurred. True by default.
2332 */
2333 void SetCaptureMessage(bool value);
2334
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 public:
2336 TryCatch* next_;
2337 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002338 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002340 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002341 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342};
2343
2344
2345// --- C o n t e x t ---
2346
2347
2348/**
2349 * Ignore
2350 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002351class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 public:
2353 ExtensionConfiguration(int name_count, const char* names[])
2354 : name_count_(name_count), names_(names) { }
2355 private:
2356 friend class ImplementationUtilities;
2357 int name_count_;
2358 const char** names_;
2359};
2360
2361
2362/**
2363 * A sandboxed execution context with its own set of built-in objects
2364 * and functions.
2365 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002366class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002368 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 Local<Object> Global();
2370
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002371 /**
2372 * Detaches the global object from its context before
2373 * the global object can be reused to create a new context.
2374 */
2375 void DetachGlobal();
2376
v8.team.kasperl727e9952008-09-02 14:56:44 +00002377 /** Creates a new context. */
2378 static Persistent<Context> New(
2379 ExtensionConfiguration* extensions = 0,
2380 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2381 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382
kasper.lund44510672008-07-25 07:37:58 +00002383 /** Returns the last entered context. */
2384 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385
kasper.lund44510672008-07-25 07:37:58 +00002386 /** Returns the context that is on the top of the stack. */
2387 static Local<Context> GetCurrent();
2388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002390 * Returns the context of the calling JavaScript code. That is the
2391 * context of the top-most JavaScript frame. If there are no
2392 * JavaScript frames an empty handle is returned.
2393 */
2394 static Local<Context> GetCalling();
2395
2396 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 * Sets the security token for the context. To access an object in
2398 * another context, the security tokens must match.
2399 */
2400 void SetSecurityToken(Handle<Value> token);
2401
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002402 /** Restores the security token to the default value. */
2403 void UseDefaultSecurityToken();
2404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 /** Returns the security token of this context.*/
2406 Handle<Value> GetSecurityToken();
2407
v8.team.kasperl727e9952008-09-02 14:56:44 +00002408 /**
2409 * Enter this context. After entering a context, all code compiled
2410 * and run is compiled and run in this context. If another context
2411 * is already entered, this old context is saved so it can be
2412 * restored when the new context is exited.
2413 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002415
2416 /**
2417 * Exit this context. Exiting the current context restores the
2418 * context that was in place when entering the current context.
2419 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 void Exit();
2421
v8.team.kasperl727e9952008-09-02 14:56:44 +00002422 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002423 bool HasOutOfMemoryException();
2424
v8.team.kasperl727e9952008-09-02 14:56:44 +00002425 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 static bool InContext();
2427
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002429 * Associate an additional data object with the context. This is mainly used
2430 * with the debugger to provide additional information on the context through
2431 * the debugger API.
2432 */
2433 void SetData(Handle<Value> data);
2434 Local<Value> GetData();
2435
2436 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 * Stack-allocated class which sets the execution context for all
2438 * operations executed within a local scope.
2439 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002440 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 public:
2442 inline Scope(Handle<Context> context) : context_(context) {
2443 context_->Enter();
2444 }
2445 inline ~Scope() { context_->Exit(); }
2446 private:
2447 Handle<Context> context_;
2448 };
2449
2450 private:
2451 friend class Value;
2452 friend class Script;
2453 friend class Object;
2454 friend class Function;
2455};
2456
2457
2458/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002459 * Multiple threads in V8 are allowed, but only one thread at a time
2460 * is allowed to use V8. The definition of 'using V8' includes
2461 * accessing handles or holding onto object pointers obtained from V8
2462 * handles. It is up to the user of V8 to ensure (perhaps with
2463 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 *
2465 * If you wish to start using V8 in a thread you can do this by constructing
2466 * a v8::Locker object. After the code using V8 has completed for the
2467 * current thread you can call the destructor. This can be combined
2468 * with C++ scope-based construction as follows:
2469 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002470 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471 * ...
2472 * {
2473 * v8::Locker locker;
2474 * ...
2475 * // Code using V8 goes here.
2476 * ...
2477 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002478 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479 *
2480 * If you wish to stop using V8 in a thread A you can do this by either
2481 * by destroying the v8::Locker object as above or by constructing a
2482 * v8::Unlocker object:
2483 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002484 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 * {
2486 * v8::Unlocker unlocker;
2487 * ...
2488 * // Code not using V8 goes here while V8 can run in another thread.
2489 * ...
2490 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002491 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 *
2493 * The Unlocker object is intended for use in a long-running callback
2494 * from V8, where you want to release the V8 lock for other threads to
2495 * use.
2496 *
2497 * The v8::Locker is a recursive lock. That is, you can lock more than
2498 * once in a given thread. This can be useful if you have code that can
2499 * be called either from code that holds the lock or from code that does
2500 * not. The Unlocker is not recursive so you can not have several
2501 * Unlockers on the stack at once, and you can not use an Unlocker in a
2502 * thread that is not inside a Locker's scope.
2503 *
2504 * An unlocker will unlock several lockers if it has to and reinstate
2505 * the correct depth of locking on its destruction. eg.:
2506 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002507 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 * // V8 not locked.
2509 * {
2510 * v8::Locker locker;
2511 * // V8 locked.
2512 * {
2513 * v8::Locker another_locker;
2514 * // V8 still locked (2 levels).
2515 * {
2516 * v8::Unlocker unlocker;
2517 * // V8 not locked.
2518 * }
2519 * // V8 locked again (2 levels).
2520 * }
2521 * // V8 still locked (1 level).
2522 * }
2523 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002524 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002526class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 public:
2528 Unlocker();
2529 ~Unlocker();
2530};
2531
2532
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002533class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 public:
2535 Locker();
2536 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002537
2538 /**
2539 * Start preemption.
2540 *
2541 * When preemption is started, a timer is fired every n milli seconds
2542 * that will switch between multiple threads that are in contention
2543 * for the V8 lock.
2544 */
2545 static void StartPreemption(int every_n_ms);
2546
2547 /**
2548 * Stop preemption.
2549 */
2550 static void StopPreemption();
2551
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002552 /**
2553 * Returns whether or not the locker is locked by the current thread.
2554 */
2555 static bool IsLocked();
2556
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002557 /**
2558 * Returns whether v8::Locker is being used by this V8 instance.
2559 */
2560 static bool IsActive() { return active_; }
2561
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 private:
2563 bool has_lock_;
2564 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002565
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002566 static bool active_;
2567
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002568 // Disallow copying and assigning.
2569 Locker(const Locker&);
2570 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571};
2572
2573
2574
2575// --- I m p l e m e n t a t i o n ---
2576
2577template <class T>
2578Handle<T>::Handle() : val_(0) { }
2579
2580
2581template <class T>
2582Local<T>::Local() : Handle<T>() { }
2583
2584
2585template <class T>
2586Local<T> Local<T>::New(Handle<T> that) {
2587 if (that.IsEmpty()) return Local<T>();
2588 void** p = reinterpret_cast<void**>(*that);
2589 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2590}
2591
2592
2593template <class T>
2594Persistent<T> Persistent<T>::New(Handle<T> that) {
2595 if (that.IsEmpty()) return Persistent<T>();
2596 void** p = reinterpret_cast<void**>(*that);
2597 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2598}
2599
2600
2601template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002602bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 if (this->IsEmpty()) return false;
2604 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2605}
2606
2607
2608template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002609bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 if (this->IsEmpty()) return false;
2611 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2612}
2613
2614
2615template <class T>
2616void Persistent<T>::Dispose() {
2617 if (this->IsEmpty()) return;
2618 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2619}
2620
2621
2622template <class T>
2623Persistent<T>::Persistent() : Handle<T>() { }
2624
2625template <class T>
2626void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2627 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2628}
2629
2630template <class T>
2631void Persistent<T>::ClearWeak() {
2632 V8::ClearWeak(reinterpret_cast<void**>(**this));
2633}
2634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635Local<Value> Arguments::operator[](int i) const {
2636 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2637 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2638}
2639
2640
2641Local<Function> Arguments::Callee() const {
2642 return callee_;
2643}
2644
2645
2646Local<Object> Arguments::This() const {
2647 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2648}
2649
2650
2651Local<Object> Arguments::Holder() const {
2652 return holder_;
2653}
2654
2655
2656Local<Value> Arguments::Data() const {
2657 return data_;
2658}
2659
2660
2661bool Arguments::IsConstructCall() const {
2662 return is_construct_call_;
2663}
2664
2665
2666int Arguments::Length() const {
2667 return length_;
2668}
2669
2670
2671Local<Value> AccessorInfo::Data() const {
2672 return data_;
2673}
2674
2675
2676Local<Object> AccessorInfo::This() const {
2677 return self_;
2678}
2679
2680
2681Local<Object> AccessorInfo::Holder() const {
2682 return holder_;
2683}
2684
2685
2686template <class T>
2687Local<T> HandleScope::Close(Handle<T> value) {
2688 void** after = RawClose(reinterpret_cast<void**>(*value));
2689 return Local<T>(reinterpret_cast<T*>(after));
2690}
2691
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002692Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 return resource_name_;
2694}
2695
2696
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002697Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 return resource_line_offset_;
2699}
2700
2701
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002702Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 return resource_column_offset_;
2704}
2705
2706
2707Handle<Boolean> Boolean::New(bool value) {
2708 return value ? True() : False();
2709}
2710
2711
2712void Template::Set(const char* name, v8::Handle<Data> value) {
2713 Set(v8::String::New(name), value);
2714}
2715
2716
2717/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002718 * \example shell.cc
2719 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720 * command-line and executes them.
2721 */
2722
2723
2724/**
2725 * \example process.cc
2726 */
2727
2728
2729} // namespace v8
2730
2731
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002732#undef V8EXPORT
2733#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734#undef TYPE_CHECK
2735
2736
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002737#endif // V8_H_