blob: 3ac10ab917d8801b5904343f897c91c0f6f37f14 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2007-2009 the V8 project authors. All rights reserved.
2// 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
28/** \mainpage V8 API Reference Guide
29 *
30 * V8 is Google's open source JavaScript engine.
31 *
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/
36 */
37
38#ifndef V8_H_
39#define V8_H_
40
41#include <stdio.h>
42
43#ifdef _WIN32
44// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
48typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
52typedef int int32_t;
53typedef unsigned int uint32_t;
54typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
56// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
58
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.
Steve Blocka7e24c12009-10-30 11:49:00 +000064#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
65#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
66 build configuration to ensure that at most one of these is set
67#endif
68
69#ifdef BUILDING_V8_SHARED
70#define V8EXPORT __declspec(dllexport)
Steve Blocka7e24c12009-10-30 11:49:00 +000071#elif USING_V8_SHARED
72#define V8EXPORT __declspec(dllimport)
Steve Blocka7e24c12009-10-30 11:49:00 +000073#else
74#define V8EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +000075#endif // BUILDING_V8_SHARED
76
77#else // _WIN32
78
79#include <stdint.h>
80
81// Setup for Linux shared library export. There is no need to distinguish
82// between building or using the V8 shared library, but we should not
83// export symbols when we are building a static library.
84#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
85#define V8EXPORT __attribute__ ((visibility("default")))
Steve Blocka7e24c12009-10-30 11:49:00 +000086#else // defined(__GNUC__) && (__GNUC__ >= 4)
87#define V8EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +000088#endif // defined(__GNUC__) && (__GNUC__ >= 4)
89
90#endif // _WIN32
91
92/**
93 * The v8 JavaScript engine.
94 */
95namespace v8 {
96
97class Context;
98class String;
99class Value;
100class Utils;
101class Number;
102class Object;
103class Array;
104class Int32;
105class Uint32;
106class External;
107class Primitive;
108class Boolean;
109class Integer;
110class Function;
111class Date;
112class ImplementationUtilities;
113class Signature;
114template <class T> class Handle;
115template <class T> class Local;
116template <class T> class Persistent;
117class FunctionTemplate;
118class ObjectTemplate;
119class Data;
Leon Clarkef7060e22010-06-03 12:02:55 +0100120class AccessorInfo;
Kristian Monsen25f61362010-05-21 11:50:48 +0100121class StackTrace;
122class StackFrame;
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
124namespace internal {
125
Steve Blocka7e24c12009-10-30 11:49:00 +0000126class Arguments;
Steve Blockd0582a62009-12-15 09:54:21 +0000127class Object;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100128class Heap;
Steve Blockd0582a62009-12-15 09:54:21 +0000129class Top;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131}
132
133
134// --- W e a k H a n d l e s
135
136
137/**
138 * A weak reference callback function.
139 *
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100140 * This callback should either explicitly invoke Dispose on |object| if
141 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
142 *
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 * \param object the weak global object to be reclaimed by the garbage collector
144 * \param parameter the value passed in when making the weak global object
145 */
146typedef void (*WeakReferenceCallback)(Persistent<Value> object,
147 void* parameter);
148
149
150// --- H a n d l e s ---
151
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100152#define TYPE_CHECK(T, S) \
153 while (false) { \
154 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 }
156
157/**
158 * An object reference managed by the v8 garbage collector.
159 *
160 * All objects returned from v8 have to be tracked by the garbage
161 * collector so that it knows that the objects are still alive. Also,
162 * because the garbage collector may move objects, it is unsafe to
163 * point directly to an object. Instead, all objects are stored in
164 * handles which are known by the garbage collector and updated
165 * whenever an object moves. Handles should always be passed by value
166 * (except in cases like out-parameters) and they should never be
167 * allocated on the heap.
168 *
169 * There are two types of handles: local and persistent handles.
170 * Local handles are light-weight and transient and typically used in
171 * local operations. They are managed by HandleScopes. Persistent
172 * handles can be used when storing objects across several independent
173 * operations and have to be explicitly deallocated when they're no
174 * longer used.
175 *
176 * It is safe to extract the object stored in the handle by
177 * dereferencing the handle (for instance, to extract the Object* from
178 * an Handle<Object>); the value will still be governed by a handle
179 * behind the scenes and the same rules apply to these values as to
180 * their handles.
181 */
Steve Block8defd9f2010-07-08 12:39:36 +0100182template <class T> class Handle {
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 public:
184
185 /**
186 * Creates an empty handle.
187 */
188 inline Handle();
189
190 /**
191 * Creates a new handle for the specified value.
192 */
Steve Block8defd9f2010-07-08 12:39:36 +0100193 inline explicit Handle(T* val) : val_(val) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
195 /**
196 * Creates a handle for the contents of the specified handle. This
197 * constructor allows you to pass handles as arguments by value and
198 * to assign between handles. However, if you try to assign between
199 * incompatible handles, for instance from a Handle<String> to a
200 * Handle<Number> it will cause a compiletime error. Assigning
201 * between compatible handles, for instance assigning a
202 * Handle<String> to a variable declared as Handle<Value>, is legal
203 * because String is a subclass of Value.
204 */
205 template <class S> inline Handle(Handle<S> that)
206 : val_(reinterpret_cast<T*>(*that)) {
207 /**
208 * This check fails when trying to convert between incompatible
209 * handles. For example, converting from a Handle<String> to a
210 * Handle<Number>.
211 */
212 TYPE_CHECK(T, S);
213 }
214
215 /**
216 * Returns true if the handle is empty.
217 */
Steve Block8defd9f2010-07-08 12:39:36 +0100218 inline bool IsEmpty() const { return val_ == 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000219
Steve Block8defd9f2010-07-08 12:39:36 +0100220 inline T* operator->() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
Steve Block8defd9f2010-07-08 12:39:36 +0100222 inline T* operator*() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000223
224 /**
225 * Sets the handle to be empty. IsEmpty() will then return true.
226 */
Steve Block8defd9f2010-07-08 12:39:36 +0100227 inline void Clear() { this->val_ = 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000228
229 /**
230 * Checks whether two handles are the same.
231 * Returns true if both are empty, or if the objects
232 * to which they refer are identical.
233 * The handles' references are not checked.
234 */
Steve Block8defd9f2010-07-08 12:39:36 +0100235 template <class S> inline bool operator==(Handle<S> that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
237 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
238 if (a == 0) return b == 0;
239 if (b == 0) return false;
240 return *a == *b;
241 }
242
243 /**
244 * Checks whether two handles are different.
245 * Returns true if only one of the handles is empty, or if
246 * the objects to which they refer are different.
247 * The handles' references are not checked.
248 */
Steve Block8defd9f2010-07-08 12:39:36 +0100249 template <class S> inline bool operator!=(Handle<S> that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 return !operator==(that);
251 }
252
253 template <class S> static inline Handle<T> Cast(Handle<S> that) {
254#ifdef V8_ENABLE_CHECKS
255 // If we're going to perform the type check then we have to check
256 // that the handle isn't empty before doing the checked cast.
257 if (that.IsEmpty()) return Handle<T>();
258#endif
259 return Handle<T>(T::Cast(*that));
260 }
261
Steve Block6ded16b2010-05-10 14:33:55 +0100262 template <class S> inline Handle<S> As() {
263 return Handle<S>::Cast(*this);
264 }
265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266 private:
267 T* val_;
268};
269
270
271/**
272 * A light-weight stack-allocated object handle. All operations
273 * that return objects from within v8 return them in local handles. They
274 * are created within HandleScopes, and all local handles allocated within a
275 * handle scope are destroyed when the handle scope is destroyed. Hence it
276 * is not necessary to explicitly deallocate local handles.
277 */
Steve Block8defd9f2010-07-08 12:39:36 +0100278template <class T> class Local : public Handle<T> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 public:
280 inline Local();
281 template <class S> inline Local(Local<S> that)
282 : Handle<T>(reinterpret_cast<T*>(*that)) {
283 /**
284 * This check fails when trying to convert between incompatible
285 * handles. For example, converting from a Handle<String> to a
286 * Handle<Number>.
287 */
288 TYPE_CHECK(T, S);
289 }
290 template <class S> inline Local(S* that) : Handle<T>(that) { }
291 template <class S> static inline Local<T> Cast(Local<S> that) {
292#ifdef V8_ENABLE_CHECKS
293 // If we're going to perform the type check then we have to check
294 // that the handle isn't empty before doing the checked cast.
295 if (that.IsEmpty()) return Local<T>();
296#endif
297 return Local<T>(T::Cast(*that));
298 }
299
Steve Block6ded16b2010-05-10 14:33:55 +0100300 template <class S> inline Local<S> As() {
301 return Local<S>::Cast(*this);
302 }
303
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 /** Create a local handle for the content of another handle.
305 * The referee is kept alive by the local handle even when
306 * the original handle is destroyed/disposed.
307 */
308 inline static Local<T> New(Handle<T> that);
309};
310
311
312/**
313 * An object reference that is independent of any handle scope. Where
314 * a Local handle only lives as long as the HandleScope in which it was
315 * allocated, a Persistent handle remains valid until it is explicitly
316 * disposed.
317 *
318 * A persistent handle contains a reference to a storage cell within
319 * the v8 engine which holds an object value and which is updated by
320 * the garbage collector whenever the object is moved. A new storage
321 * cell can be created using Persistent::New and existing handles can
322 * be disposed using Persistent::Dispose. Since persistent handles
323 * are passed by value you may have many persistent handle objects
324 * that point to the same storage cell. For instance, if you pass a
325 * persistent handle as an argument to a function you will not get two
326 * different storage cells but rather two references to the same
327 * storage cell.
328 */
Steve Block8defd9f2010-07-08 12:39:36 +0100329template <class T> class Persistent : public Handle<T> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000330 public:
331
332 /**
333 * Creates an empty persistent handle that doesn't point to any
334 * storage cell.
335 */
336 inline Persistent();
337
338 /**
339 * Creates a persistent handle for the same storage cell as the
340 * specified handle. This constructor allows you to pass persistent
341 * handles as arguments by value and to assign between persistent
342 * handles. However, attempting to assign between incompatible
343 * persistent handles, for instance from a Persistent<String> to a
344 * Persistent<Number> will cause a compiletime error. Assigning
345 * between compatible persistent handles, for instance assigning a
346 * Persistent<String> to a variable declared as Persistent<Value>,
347 * is allowed as String is a subclass of Value.
348 */
349 template <class S> inline Persistent(Persistent<S> that)
350 : Handle<T>(reinterpret_cast<T*>(*that)) {
351 /**
352 * This check fails when trying to convert between incompatible
353 * handles. For example, converting from a Handle<String> to a
354 * Handle<Number>.
355 */
356 TYPE_CHECK(T, S);
357 }
358
359 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
360
361 /**
362 * "Casts" a plain handle which is known to be a persistent handle
363 * to a persistent handle.
364 */
365 template <class S> explicit inline Persistent(Handle<S> that)
366 : Handle<T>(*that) { }
367
368 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
369#ifdef V8_ENABLE_CHECKS
370 // If we're going to perform the type check then we have to check
371 // that the handle isn't empty before doing the checked cast.
372 if (that.IsEmpty()) return Persistent<T>();
373#endif
374 return Persistent<T>(T::Cast(*that));
375 }
376
Steve Block6ded16b2010-05-10 14:33:55 +0100377 template <class S> inline Persistent<S> As() {
378 return Persistent<S>::Cast(*this);
379 }
380
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 /**
382 * Creates a new persistent handle for an existing local or
383 * persistent handle.
384 */
385 inline static Persistent<T> New(Handle<T> that);
386
387 /**
388 * Releases the storage cell referenced by this persistent handle.
389 * Does not remove the reference to the cell from any handles.
390 * This handle's reference, and any any other references to the storage
391 * cell remain and IsEmpty will still return false.
392 */
393 inline void Dispose();
394
395 /**
396 * Make the reference to this object weak. When only weak handles
397 * refer to the object, the garbage collector will perform a
398 * callback to the given V8::WeakReferenceCallback function, passing
399 * it the object reference and the given parameters.
400 */
401 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
402
403 /** Clears the weak reference to this object.*/
404 inline void ClearWeak();
405
406 /**
407 *Checks if the handle holds the only reference to an object.
408 */
409 inline bool IsNearDeath() const;
410
411 /**
412 * Returns true if the handle's reference is weak.
413 */
414 inline bool IsWeak() const;
415
416 private:
417 friend class ImplementationUtilities;
418 friend class ObjectTemplate;
419};
420
421
422 /**
423 * A stack-allocated class that governs a number of local handles.
424 * After a handle scope has been created, all local handles will be
425 * allocated within that handle scope until either the handle scope is
426 * deleted or another handle scope is created. If there is already a
427 * handle scope and a new one is created, all allocations will take
428 * place in the new handle scope until it is deleted. After that,
429 * new handles will again be allocated in the original handle scope.
430 *
431 * After the handle scope of a local handle has been deleted the
432 * garbage collector will no longer track the object stored in the
433 * handle and may deallocate it. The behavior of accessing a handle
434 * for which the handle scope has been deleted is undefined.
435 */
436class V8EXPORT HandleScope {
437 public:
438 HandleScope();
439
440 ~HandleScope();
441
442 /**
443 * Closes the handle scope and returns the value as a handle in the
444 * previous scope, which is the new current scope after the call.
445 */
446 template <class T> Local<T> Close(Handle<T> value);
447
448 /**
449 * Counts the number of allocated handles.
450 */
451 static int NumberOfHandles();
452
453 /**
454 * Creates a new handle with the given value.
455 */
456 static internal::Object** CreateHandle(internal::Object* value);
457
458 private:
459 // Make it impossible to create heap-allocated or illegal handle
460 // scopes by disallowing certain operations.
461 HandleScope(const HandleScope&);
462 void operator=(const HandleScope&);
463 void* operator new(size_t size);
464 void operator delete(void*, size_t);
465
Steve Blockd0582a62009-12-15 09:54:21 +0000466 // This Data class is accessible internally as HandleScopeData through a
467 // typedef in the ImplementationUtilities class.
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 class V8EXPORT Data {
469 public:
470 int extensions;
471 internal::Object** next;
472 internal::Object** limit;
473 inline void Initialize() {
474 extensions = -1;
475 next = limit = NULL;
476 }
477 };
478
479 Data previous_;
480
481 // Allow for the active closing of HandleScopes which allows to pass a handle
482 // from the HandleScope being closed to the next top most HandleScope.
483 bool is_closed_;
484 internal::Object** RawClose(internal::Object** value);
485
486 friend class ImplementationUtilities;
487};
488
489
490// --- S p e c i a l o b j e c t s ---
491
492
493/**
494 * The superclass of values and API object templates.
495 */
496class V8EXPORT Data {
497 private:
498 Data();
499};
500
501
502/**
503 * Pre-compilation data that can be associated with a script. This
504 * data can be calculated for a script in advance of actually
505 * compiling it, and can be stored between compilations. When script
506 * data is given to the compile method compilation will be faster.
507 */
508class V8EXPORT ScriptData { // NOLINT
509 public:
510 virtual ~ScriptData() { }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100511
Leon Clarkef7060e22010-06-03 12:02:55 +0100512 /**
513 * Pre-compiles the specified script (context-independent).
514 *
515 * \param input Pointer to UTF-8 script source code.
516 * \param length Length of UTF-8 script source code.
517 */
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 static ScriptData* PreCompile(const char* input, int length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000519
Leon Clarkef7060e22010-06-03 12:02:55 +0100520 /**
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100521 * Pre-compiles the specified script (context-independent).
522 *
523 * NOTE: Pre-compilation using this method cannot happen on another thread
524 * without using Lockers.
525 *
526 * \param source Script source code.
527 */
528 static ScriptData* PreCompile(Handle<String> source);
529
530 /**
Leon Clarkef7060e22010-06-03 12:02:55 +0100531 * Load previous pre-compilation data.
532 *
533 * \param data Pointer to data returned by a call to Data() of a previous
534 * ScriptData. Ownership is not transferred.
535 * \param length Length of data.
536 */
537 static ScriptData* New(const char* data, int length);
538
539 /**
540 * Returns the length of Data().
541 */
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 virtual int Length() = 0;
Leon Clarkef7060e22010-06-03 12:02:55 +0100543
544 /**
545 * Returns a serialized representation of this ScriptData that can later be
546 * passed to New(). NOTE: Serialized data is platform-dependent.
547 */
548 virtual const char* Data() = 0;
549
550 /**
551 * Returns true if the source code could not be parsed.
552 */
Leon Clarkee46be812010-01-19 14:06:41 +0000553 virtual bool HasError() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000554};
555
556
557/**
558 * The origin, within a file, of a script.
559 */
Steve Block8defd9f2010-07-08 12:39:36 +0100560class ScriptOrigin {
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 public:
Steve Block8defd9f2010-07-08 12:39:36 +0100562 inline ScriptOrigin(
563 Handle<Value> resource_name,
564 Handle<Integer> resource_line_offset = Handle<Integer>(),
565 Handle<Integer> resource_column_offset = Handle<Integer>())
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 : resource_name_(resource_name),
567 resource_line_offset_(resource_line_offset),
568 resource_column_offset_(resource_column_offset) { }
569 inline Handle<Value> ResourceName() const;
570 inline Handle<Integer> ResourceLineOffset() const;
571 inline Handle<Integer> ResourceColumnOffset() const;
572 private:
573 Handle<Value> resource_name_;
574 Handle<Integer> resource_line_offset_;
575 Handle<Integer> resource_column_offset_;
576};
577
578
579/**
580 * A compiled JavaScript script.
581 */
582class V8EXPORT Script {
583 public:
584
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 /**
Andrei Popescu402d9372010-02-26 13:31:12 +0000586 * Compiles the specified script (context-independent).
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 *
Andrei Popescu402d9372010-02-26 13:31:12 +0000588 * \param source Script source code.
Steve Block6ded16b2010-05-10 14:33:55 +0100589 * \param origin Script origin, owned by caller, no references are kept
Andrei Popescu402d9372010-02-26 13:31:12 +0000590 * when New() returns
591 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
592 * using pre_data speeds compilation if it's done multiple times.
593 * Owned by caller, no references are kept when New() returns.
594 * \param script_data Arbitrary data associated with script. Using
Steve Block6ded16b2010-05-10 14:33:55 +0100595 * this has same effect as calling SetData(), but allows data to be
Andrei Popescu402d9372010-02-26 13:31:12 +0000596 * available to compile event handlers.
597 * \return Compiled script object (context independent; when run it
598 * will use the currently entered context).
Steve Blocka7e24c12009-10-30 11:49:00 +0000599 */
Andrei Popescu402d9372010-02-26 13:31:12 +0000600 static Local<Script> New(Handle<String> source,
601 ScriptOrigin* origin = NULL,
602 ScriptData* pre_data = NULL,
603 Handle<String> script_data = Handle<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000604
605 /**
606 * Compiles the specified script using the specified file name
607 * object (typically a string) as the script's origin.
608 *
Andrei Popescu402d9372010-02-26 13:31:12 +0000609 * \param source Script source code.
Steve Block6ded16b2010-05-10 14:33:55 +0100610 * \param file_name file name object (typically a string) to be used
Andrei Popescu402d9372010-02-26 13:31:12 +0000611 * as the script's origin.
612 * \return Compiled script object (context independent; when run it
613 * will use the currently entered context).
614 */
615 static Local<Script> New(Handle<String> source,
616 Handle<Value> file_name);
617
618 /**
619 * Compiles the specified script (bound to current context).
620 *
621 * \param source Script source code.
Steve Block6ded16b2010-05-10 14:33:55 +0100622 * \param origin Script origin, owned by caller, no references are kept
Andrei Popescu402d9372010-02-26 13:31:12 +0000623 * when Compile() returns
624 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
625 * using pre_data speeds compilation if it's done multiple times.
626 * Owned by caller, no references are kept when Compile() returns.
627 * \param script_data Arbitrary data associated with script. Using
628 * this has same effect as calling SetData(), but makes data available
629 * earlier (i.e. to compile event handlers).
630 * \return Compiled script object, bound to the context that was active
631 * when this function was called. When run it will always use this
632 * context.
Steve Blocka7e24c12009-10-30 11:49:00 +0000633 */
634 static Local<Script> Compile(Handle<String> source,
Andrei Popescu402d9372010-02-26 13:31:12 +0000635 ScriptOrigin* origin = NULL,
636 ScriptData* pre_data = NULL,
637 Handle<String> script_data = Handle<String>());
638
639 /**
640 * Compiles the specified script using the specified file name
641 * object (typically a string) as the script's origin.
642 *
643 * \param source Script source code.
644 * \param file_name File name to use as script's origin
645 * \param script_data Arbitrary data associated with script. Using
646 * this has same effect as calling SetData(), but makes data available
647 * earlier (i.e. to compile event handlers).
648 * \return Compiled script object, bound to the context that was active
649 * when this function was called. When run it will always use this
650 * context.
651 */
652 static Local<Script> Compile(Handle<String> source,
653 Handle<Value> file_name,
654 Handle<String> script_data = Handle<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000655
656 /**
657 * Runs the script returning the resulting value. If the script is
658 * context independent (created using ::New) it will be run in the
659 * currently entered context. If it is context specific (created
660 * using ::Compile) it will be run in the context in which it was
661 * compiled.
662 */
663 Local<Value> Run();
664
665 /**
666 * Returns the script id value.
667 */
668 Local<Value> Id();
669
670 /**
671 * Associate an additional data object with the script. This is mainly used
672 * with the debugger as this data object is only available through the
673 * debugger API.
674 */
Steve Blockd0582a62009-12-15 09:54:21 +0000675 void SetData(Handle<String> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000676};
677
678
679/**
680 * An error message.
681 */
682class V8EXPORT Message {
683 public:
684 Local<String> Get() const;
685 Local<String> GetSourceLine() const;
686
687 /**
688 * Returns the resource name for the script from where the function causing
689 * the error originates.
690 */
691 Handle<Value> GetScriptResourceName() const;
692
693 /**
694 * Returns the resource data for the script from where the function causing
695 * the error originates.
696 */
697 Handle<Value> GetScriptData() const;
698
699 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100700 * Exception stack trace. By default stack traces are not captured for
701 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
702 * to change this option.
703 */
704 Handle<StackTrace> GetStackTrace() const;
705
706 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 * Returns the number, 1-based, of the line where the error occurred.
708 */
709 int GetLineNumber() const;
710
711 /**
712 * Returns the index within the script of the first character where
713 * the error occurred.
714 */
715 int GetStartPosition() const;
716
717 /**
718 * Returns the index within the script of the last character where
719 * the error occurred.
720 */
721 int GetEndPosition() const;
722
723 /**
724 * Returns the index within the line of the first character where
725 * the error occurred.
726 */
727 int GetStartColumn() const;
728
729 /**
730 * Returns the index within the line of the last character where
731 * the error occurred.
732 */
733 int GetEndColumn() const;
734
735 // TODO(1245381): Print to a string instead of on a FILE.
736 static void PrintCurrentStackTrace(FILE* out);
Kristian Monsen25f61362010-05-21 11:50:48 +0100737
738 static const int kNoLineNumberInfo = 0;
739 static const int kNoColumnInfo = 0;
740};
741
742
743/**
744 * Representation of a JavaScript stack trace. The information collected is a
745 * snapshot of the execution stack and the information remains valid after
746 * execution continues.
747 */
748class V8EXPORT StackTrace {
749 public:
750 /**
751 * Flags that determine what information is placed captured for each
752 * StackFrame when grabbing the current stack trace.
753 */
754 enum StackTraceOptions {
755 kLineNumber = 1,
756 kColumnOffset = 1 << 1 | kLineNumber,
757 kScriptName = 1 << 2,
758 kFunctionName = 1 << 3,
759 kIsEval = 1 << 4,
760 kIsConstructor = 1 << 5,
761 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
762 kDetailed = kOverview | kIsEval | kIsConstructor
763 };
764
765 /**
766 * Returns a StackFrame at a particular index.
767 */
768 Local<StackFrame> GetFrame(uint32_t index) const;
769
770 /**
771 * Returns the number of StackFrames.
772 */
773 int GetFrameCount() const;
774
775 /**
776 * Returns StackTrace as a v8::Array that contains StackFrame objects.
777 */
778 Local<Array> AsArray();
779
780 /**
781 * Grab a snapshot of the the current JavaScript execution stack.
782 *
783 * \param frame_limit The maximum number of stack frames we want to capture.
784 * \param options Enumerates the set of things we will capture for each
785 * StackFrame.
786 */
787 static Local<StackTrace> CurrentStackTrace(
788 int frame_limit,
789 StackTraceOptions options = kOverview);
790};
791
792
793/**
794 * A single JavaScript stack frame.
795 */
796class V8EXPORT StackFrame {
797 public:
798 /**
799 * Returns the number, 1-based, of the line for the associate function call.
800 * This method will return Message::kNoLineNumberInfo if it is unable to
801 * retrieve the line number, or if kLineNumber was not passed as an option
802 * when capturing the StackTrace.
803 */
804 int GetLineNumber() const;
805
806 /**
807 * Returns the 1-based column offset on the line for the associated function
808 * call.
809 * This method will return Message::kNoColumnInfo if it is unable to retrieve
810 * the column number, or if kColumnOffset was not passed as an option when
811 * capturing the StackTrace.
812 */
813 int GetColumn() const;
814
815 /**
816 * Returns the name of the resource that contains the script for the
817 * function for this StackFrame.
818 */
819 Local<String> GetScriptName() const;
820
821 /**
822 * Returns the name of the function associated with this stack frame.
823 */
824 Local<String> GetFunctionName() const;
825
826 /**
827 * Returns whether or not the associated function is compiled via a call to
828 * eval().
829 */
830 bool IsEval() const;
831
832 /**
833 * Returns whther or not the associated function is called as a
834 * constructor via "new".
835 */
836 bool IsConstructor() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000837};
838
839
840// --- V a l u e ---
841
842
843/**
844 * The superclass of all JavaScript values and objects.
845 */
Steve Block8defd9f2010-07-08 12:39:36 +0100846class Value : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 public:
848
849 /**
850 * Returns true if this value is the undefined value. See ECMA-262
851 * 4.3.10.
852 */
Steve Block8defd9f2010-07-08 12:39:36 +0100853 V8EXPORT bool IsUndefined() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000854
855 /**
856 * Returns true if this value is the null value. See ECMA-262
857 * 4.3.11.
858 */
Steve Block8defd9f2010-07-08 12:39:36 +0100859 V8EXPORT bool IsNull() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000860
861 /**
862 * Returns true if this value is true.
863 */
Steve Block8defd9f2010-07-08 12:39:36 +0100864 V8EXPORT bool IsTrue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000865
866 /**
867 * Returns true if this value is false.
868 */
Steve Block8defd9f2010-07-08 12:39:36 +0100869 V8EXPORT bool IsFalse() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000870
871 /**
872 * Returns true if this value is an instance of the String type.
873 * See ECMA-262 8.4.
874 */
875 inline bool IsString() const;
876
877 /**
878 * Returns true if this value is a function.
879 */
Steve Block8defd9f2010-07-08 12:39:36 +0100880 V8EXPORT bool IsFunction() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000881
882 /**
883 * Returns true if this value is an array.
884 */
Steve Block8defd9f2010-07-08 12:39:36 +0100885 V8EXPORT bool IsArray() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000886
887 /**
888 * Returns true if this value is an object.
889 */
Steve Block8defd9f2010-07-08 12:39:36 +0100890 V8EXPORT bool IsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000891
892 /**
893 * Returns true if this value is boolean.
894 */
Steve Block8defd9f2010-07-08 12:39:36 +0100895 V8EXPORT bool IsBoolean() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000896
897 /**
898 * Returns true if this value is a number.
899 */
Steve Block8defd9f2010-07-08 12:39:36 +0100900 V8EXPORT bool IsNumber() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000901
902 /**
903 * Returns true if this value is external.
904 */
Steve Block8defd9f2010-07-08 12:39:36 +0100905 V8EXPORT bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000906
907 /**
908 * Returns true if this value is a 32-bit signed integer.
909 */
Steve Block8defd9f2010-07-08 12:39:36 +0100910 V8EXPORT bool IsInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000911
912 /**
Steve Block6ded16b2010-05-10 14:33:55 +0100913 * Returns true if this value is a 32-bit unsigned integer.
914 */
Steve Block8defd9f2010-07-08 12:39:36 +0100915 V8EXPORT bool IsUint32() const;
Steve Block6ded16b2010-05-10 14:33:55 +0100916
917 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 * Returns true if this value is a Date.
919 */
Steve Block8defd9f2010-07-08 12:39:36 +0100920 V8EXPORT bool IsDate() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000921
Steve Block8defd9f2010-07-08 12:39:36 +0100922 V8EXPORT Local<Boolean> ToBoolean() const;
923 V8EXPORT Local<Number> ToNumber() const;
924 V8EXPORT Local<String> ToString() const;
925 V8EXPORT Local<String> ToDetailString() const;
926 V8EXPORT Local<Object> ToObject() const;
927 V8EXPORT Local<Integer> ToInteger() const;
928 V8EXPORT Local<Uint32> ToUint32() const;
929 V8EXPORT Local<Int32> ToInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000930
931 /**
932 * Attempts to convert a string to an array index.
933 * Returns an empty handle if the conversion fails.
934 */
Steve Block8defd9f2010-07-08 12:39:36 +0100935 V8EXPORT Local<Uint32> ToArrayIndex() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000936
Steve Block8defd9f2010-07-08 12:39:36 +0100937 V8EXPORT bool BooleanValue() const;
938 V8EXPORT double NumberValue() const;
939 V8EXPORT int64_t IntegerValue() const;
940 V8EXPORT uint32_t Uint32Value() const;
941 V8EXPORT int32_t Int32Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000942
943 /** JS == */
Steve Block8defd9f2010-07-08 12:39:36 +0100944 V8EXPORT bool Equals(Handle<Value> that) const;
945 V8EXPORT bool StrictEquals(Handle<Value> that) const;
Steve Block3ce2e202009-11-05 08:53:23 +0000946
Steve Blocka7e24c12009-10-30 11:49:00 +0000947 private:
948 inline bool QuickIsString() const;
Steve Block8defd9f2010-07-08 12:39:36 +0100949 V8EXPORT bool FullIsString() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000950};
951
952
953/**
954 * The superclass of primitive values. See ECMA-262 4.3.2.
955 */
Steve Block8defd9f2010-07-08 12:39:36 +0100956class Primitive : public Value { };
Steve Blocka7e24c12009-10-30 11:49:00 +0000957
958
959/**
960 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
961 * or false value.
962 */
Steve Block8defd9f2010-07-08 12:39:36 +0100963class Boolean : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +0000964 public:
Steve Block8defd9f2010-07-08 12:39:36 +0100965 V8EXPORT bool Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000966 static inline Handle<Boolean> New(bool value);
967};
968
969
970/**
971 * A JavaScript string value (ECMA-262, 4.3.17).
972 */
Steve Block8defd9f2010-07-08 12:39:36 +0100973class String : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 public:
975
976 /**
977 * Returns the number of characters in this string.
978 */
Steve Block8defd9f2010-07-08 12:39:36 +0100979 V8EXPORT int Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000980
981 /**
982 * Returns the number of bytes in the UTF-8 encoded
983 * representation of this string.
984 */
Steve Block8defd9f2010-07-08 12:39:36 +0100985 V8EXPORT int Utf8Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000986
987 /**
988 * Write the contents of the string to an external buffer.
989 * If no arguments are given, expects the buffer to be large
990 * enough to hold the entire string and NULL terminator. Copies
991 * the contents of the string and the NULL terminator into the
992 * buffer.
993 *
994 * Copies up to length characters into the output buffer.
995 * Only null-terminates if there is enough space in the buffer.
996 *
997 * \param buffer The buffer into which the string will be copied.
998 * \param start The starting position within the string at which
999 * copying begins.
1000 * \param length The number of bytes to copy from the string.
Steve Block6ded16b2010-05-10 14:33:55 +01001001 * \param nchars_ref The number of characters written, can be NULL.
1002 * \param hints Various hints that might affect performance of this or
1003 * subsequent operations.
1004 * \return The number of bytes copied to the buffer
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 * excluding the NULL terminator.
1006 */
Steve Block6ded16b2010-05-10 14:33:55 +01001007 enum WriteHints {
1008 NO_HINTS = 0,
1009 HINT_MANY_WRITES_EXPECTED = 1
1010 };
1011
Steve Block8defd9f2010-07-08 12:39:36 +01001012 V8EXPORT int Write(uint16_t* buffer,
1013 int start = 0,
1014 int length = -1,
1015 WriteHints hints = NO_HINTS) const; // UTF-16
1016 V8EXPORT int WriteAscii(char* buffer,
1017 int start = 0,
1018 int length = -1,
1019 WriteHints hints = NO_HINTS) const; // ASCII
1020 V8EXPORT int WriteUtf8(char* buffer,
1021 int length = -1,
1022 int* nchars_ref = NULL,
1023 WriteHints hints = NO_HINTS) const; // UTF-8
Steve Blocka7e24c12009-10-30 11:49:00 +00001024
1025 /**
1026 * A zero length string.
1027 */
Steve Block8defd9f2010-07-08 12:39:36 +01001028 V8EXPORT static v8::Local<v8::String> Empty();
Steve Blocka7e24c12009-10-30 11:49:00 +00001029
1030 /**
1031 * Returns true if the string is external
1032 */
Steve Block8defd9f2010-07-08 12:39:36 +01001033 V8EXPORT bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001034
1035 /**
1036 * Returns true if the string is both external and ascii
1037 */
Steve Block8defd9f2010-07-08 12:39:36 +01001038 V8EXPORT bool IsExternalAscii() const;
Leon Clarkee46be812010-01-19 14:06:41 +00001039
1040 class V8EXPORT ExternalStringResourceBase {
1041 public:
1042 virtual ~ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001043
Leon Clarkee46be812010-01-19 14:06:41 +00001044 protected:
1045 ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001046
1047 /**
1048 * Internally V8 will call this Dispose method when the external string
1049 * resource is no longer needed. The default implementation will use the
1050 * delete operator. This method can be overridden in subclasses to
1051 * control how allocated external string resources are disposed.
1052 */
1053 virtual void Dispose() { delete this; }
1054
Leon Clarkee46be812010-01-19 14:06:41 +00001055 private:
1056 // Disallow copying and assigning.
1057 ExternalStringResourceBase(const ExternalStringResourceBase&);
1058 void operator=(const ExternalStringResourceBase&);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001059
1060 friend class v8::internal::Heap;
Leon Clarkee46be812010-01-19 14:06:41 +00001061 };
1062
Steve Blocka7e24c12009-10-30 11:49:00 +00001063 /**
1064 * An ExternalStringResource is a wrapper around a two-byte string
1065 * buffer that resides outside V8's heap. Implement an
1066 * ExternalStringResource to manage the life cycle of the underlying
1067 * buffer. Note that the string data must be immutable.
1068 */
Leon Clarkee46be812010-01-19 14:06:41 +00001069 class V8EXPORT ExternalStringResource
1070 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00001071 public:
1072 /**
1073 * Override the destructor to manage the life cycle of the underlying
1074 * buffer.
1075 */
1076 virtual ~ExternalStringResource() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001077
1078 /**
1079 * The string data from the underlying buffer.
1080 */
Steve Blocka7e24c12009-10-30 11:49:00 +00001081 virtual const uint16_t* data() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001082
1083 /**
1084 * The length of the string. That is, the number of two-byte characters.
1085 */
Steve Blocka7e24c12009-10-30 11:49:00 +00001086 virtual size_t length() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001087
Steve Blocka7e24c12009-10-30 11:49:00 +00001088 protected:
1089 ExternalStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001090 };
1091
1092 /**
1093 * An ExternalAsciiStringResource is a wrapper around an ascii
1094 * string buffer that resides outside V8's heap. Implement an
1095 * ExternalAsciiStringResource to manage the life cycle of the
1096 * underlying buffer. Note that the string data must be immutable
1097 * and that the data must be strict 7-bit ASCII, not Latin1 or
1098 * UTF-8, which would require special treatment internally in the
1099 * engine and, in the case of UTF-8, do not allow efficient indexing.
1100 * Use String::New or convert to 16 bit data for non-ASCII.
1101 */
1102
Leon Clarkee46be812010-01-19 14:06:41 +00001103 class V8EXPORT ExternalAsciiStringResource
1104 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 public:
1106 /**
1107 * Override the destructor to manage the life cycle of the underlying
1108 * buffer.
1109 */
1110 virtual ~ExternalAsciiStringResource() {}
1111 /** The string data from the underlying buffer.*/
1112 virtual const char* data() const = 0;
1113 /** The number of ascii characters in the string.*/
1114 virtual size_t length() const = 0;
1115 protected:
1116 ExternalAsciiStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001117 };
1118
1119 /**
1120 * Get the ExternalStringResource for an external string. Returns
1121 * NULL if IsExternal() doesn't return true.
1122 */
1123 inline ExternalStringResource* GetExternalStringResource() const;
1124
1125 /**
1126 * Get the ExternalAsciiStringResource for an external ascii string.
1127 * Returns NULL if IsExternalAscii() doesn't return true.
1128 */
Steve Block8defd9f2010-07-08 12:39:36 +01001129 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001130
1131 static inline String* Cast(v8::Value* obj);
1132
1133 /**
1134 * Allocates a new string from either utf-8 encoded or ascii data.
1135 * The second parameter 'length' gives the buffer length.
1136 * If the data is utf-8 encoded, the caller must
1137 * be careful to supply the length parameter.
1138 * If it is not given, the function calls
1139 * 'strlen' to determine the buffer length, it might be
1140 * wrong if 'data' contains a null character.
1141 */
Steve Block8defd9f2010-07-08 12:39:36 +01001142 V8EXPORT static Local<String> New(const char* data, int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001143
1144 /** Allocates a new string from utf16 data.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001145 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001146
1147 /** Creates a symbol. Returns one if it exists already.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001148 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001149
1150 /**
Steve Block3ce2e202009-11-05 08:53:23 +00001151 * Creates a new string by concatenating the left and the right strings
1152 * passed in as parameters.
1153 */
Steve Block8defd9f2010-07-08 12:39:36 +01001154 V8EXPORT static Local<String> Concat(Handle<String> left,
1155 Handle<String>right);
Steve Block3ce2e202009-11-05 08:53:23 +00001156
1157 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001158 * Creates a new external string using the data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001159 * resource. When the external string is no longer live on V8's heap the
1160 * resource will be disposed by calling its Dispose method. The caller of
1161 * this function should not otherwise delete or modify the resource. Neither
1162 * should the underlying buffer be deallocated or modified except through the
1163 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001164 */
Steve Block8defd9f2010-07-08 12:39:36 +01001165 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001166
1167 /**
1168 * Associate an external string resource with this string by transforming it
1169 * in place so that existing references to this string in the JavaScript heap
1170 * will use the external string resource. The external string resource's
1171 * character contents needs to be equivalent to this string.
1172 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001173 * The string is not modified if the operation fails. See NewExternal for
1174 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001175 */
Steve Block8defd9f2010-07-08 12:39:36 +01001176 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001177
1178 /**
1179 * Creates a new external string using the ascii data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001180 * resource. When the external string is no longer live on V8's heap the
1181 * resource will be disposed by calling its Dispose method. The caller of
1182 * this function should not otherwise delete or modify the resource. Neither
1183 * should the underlying buffer be deallocated or modified except through the
1184 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001185 */
Steve Block8defd9f2010-07-08 12:39:36 +01001186 V8EXPORT static Local<String> NewExternal(
1187 ExternalAsciiStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001188
1189 /**
1190 * Associate an external string resource with this string by transforming it
1191 * in place so that existing references to this string in the JavaScript heap
1192 * will use the external string resource. The external string resource's
1193 * character contents needs to be equivalent to this string.
1194 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001195 * The string is not modified if the operation fails. See NewExternal for
1196 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001197 */
Steve Block8defd9f2010-07-08 12:39:36 +01001198 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001199
1200 /**
1201 * Returns true if this string can be made external.
1202 */
Steve Block8defd9f2010-07-08 12:39:36 +01001203 V8EXPORT bool CanMakeExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00001204
1205 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001206 V8EXPORT static Local<String> NewUndetectable(const char* data,
1207 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001208
1209 /** Creates an undetectable string from the supplied utf-16 data.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001210 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1211 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001212
1213 /**
1214 * Converts an object to a utf8-encoded character array. Useful if
1215 * you want to print the object. If conversion to a string fails
1216 * (eg. due to an exception in the toString() method of the object)
1217 * then the length() method returns 0 and the * operator returns
1218 * NULL.
1219 */
1220 class V8EXPORT Utf8Value {
1221 public:
1222 explicit Utf8Value(Handle<v8::Value> obj);
1223 ~Utf8Value();
1224 char* operator*() { return str_; }
1225 const char* operator*() const { return str_; }
1226 int length() const { return length_; }
1227 private:
1228 char* str_;
1229 int length_;
1230
1231 // Disallow copying and assigning.
1232 Utf8Value(const Utf8Value&);
1233 void operator=(const Utf8Value&);
1234 };
1235
1236 /**
1237 * Converts an object to an ascii string.
1238 * Useful if you want to print the object.
1239 * If conversion to a string fails (eg. due to an exception in the toString()
1240 * method of the object) then the length() method returns 0 and the * operator
1241 * returns NULL.
1242 */
1243 class V8EXPORT AsciiValue {
1244 public:
1245 explicit AsciiValue(Handle<v8::Value> obj);
1246 ~AsciiValue();
1247 char* operator*() { return str_; }
1248 const char* operator*() const { return str_; }
1249 int length() const { return length_; }
1250 private:
1251 char* str_;
1252 int length_;
1253
1254 // Disallow copying and assigning.
1255 AsciiValue(const AsciiValue&);
1256 void operator=(const AsciiValue&);
1257 };
1258
1259 /**
1260 * Converts an object to a two-byte string.
1261 * If conversion to a string fails (eg. due to an exception in the toString()
1262 * method of the object) then the length() method returns 0 and the * operator
1263 * returns NULL.
1264 */
1265 class V8EXPORT Value {
1266 public:
1267 explicit Value(Handle<v8::Value> obj);
1268 ~Value();
1269 uint16_t* operator*() { return str_; }
1270 const uint16_t* operator*() const { return str_; }
1271 int length() const { return length_; }
1272 private:
1273 uint16_t* str_;
1274 int length_;
1275
1276 // Disallow copying and assigning.
1277 Value(const Value&);
1278 void operator=(const Value&);
1279 };
Steve Block3ce2e202009-11-05 08:53:23 +00001280
Steve Blocka7e24c12009-10-30 11:49:00 +00001281 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001282 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1283 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001284};
1285
1286
1287/**
1288 * A JavaScript number value (ECMA-262, 4.3.20)
1289 */
Steve Block8defd9f2010-07-08 12:39:36 +01001290class Number : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00001291 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001292 V8EXPORT double Value() const;
1293 V8EXPORT static Local<Number> New(double value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 static inline Number* Cast(v8::Value* obj);
1295 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001296 V8EXPORT Number();
Steve Blocka7e24c12009-10-30 11:49:00 +00001297 static void CheckCast(v8::Value* obj);
1298};
1299
1300
1301/**
1302 * A JavaScript value representing a signed integer.
1303 */
Steve Block8defd9f2010-07-08 12:39:36 +01001304class Integer : public Number {
Steve Blocka7e24c12009-10-30 11:49:00 +00001305 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001306 V8EXPORT static Local<Integer> New(int32_t value);
1307 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1308 V8EXPORT int64_t Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 static inline Integer* Cast(v8::Value* obj);
1310 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001311 V8EXPORT Integer();
1312 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001313};
1314
1315
1316/**
1317 * A JavaScript value representing a 32-bit signed integer.
1318 */
Steve Block8defd9f2010-07-08 12:39:36 +01001319class Int32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00001320 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001321 V8EXPORT int32_t Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001322 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001323 V8EXPORT Int32();
Steve Blocka7e24c12009-10-30 11:49:00 +00001324};
1325
1326
1327/**
1328 * A JavaScript value representing a 32-bit unsigned integer.
1329 */
Steve Block8defd9f2010-07-08 12:39:36 +01001330class Uint32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00001331 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001332 V8EXPORT uint32_t Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001333 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001334 V8EXPORT Uint32();
Steve Blocka7e24c12009-10-30 11:49:00 +00001335};
1336
1337
1338/**
1339 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1340 */
Steve Block8defd9f2010-07-08 12:39:36 +01001341class Date : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00001342 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001343 V8EXPORT static Local<Value> New(double time);
Steve Blocka7e24c12009-10-30 11:49:00 +00001344
1345 /**
1346 * A specialization of Value::NumberValue that is more efficient
1347 * because we know the structure of this object.
1348 */
Steve Block8defd9f2010-07-08 12:39:36 +01001349 V8EXPORT double NumberValue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001350
1351 static inline Date* Cast(v8::Value* obj);
1352 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001353 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001354};
1355
1356
1357enum PropertyAttribute {
1358 None = 0,
1359 ReadOnly = 1 << 0,
1360 DontEnum = 1 << 1,
1361 DontDelete = 1 << 2
1362};
1363
Steve Block3ce2e202009-11-05 08:53:23 +00001364enum ExternalArrayType {
1365 kExternalByteArray = 1,
1366 kExternalUnsignedByteArray,
1367 kExternalShortArray,
1368 kExternalUnsignedShortArray,
1369 kExternalIntArray,
1370 kExternalUnsignedIntArray,
1371 kExternalFloatArray
1372};
1373
Steve Blocka7e24c12009-10-30 11:49:00 +00001374/**
Leon Clarkef7060e22010-06-03 12:02:55 +01001375 * Accessor[Getter|Setter] are used as callback functions when
1376 * setting|getting a particular property. See Object and ObjectTemplate's
1377 * method SetAccessor.
1378 */
1379typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1380 const AccessorInfo& info);
1381
1382
1383typedef void (*AccessorSetter)(Local<String> property,
1384 Local<Value> value,
1385 const AccessorInfo& info);
1386
1387
1388/**
1389 * Access control specifications.
1390 *
1391 * Some accessors should be accessible across contexts. These
1392 * accessors have an explicit access control parameter which specifies
1393 * the kind of cross-context access that should be allowed.
1394 *
1395 * Additionally, for security, accessors can prohibit overwriting by
1396 * accessors defined in JavaScript. For objects that have such
1397 * accessors either locally or in their prototype chain it is not
1398 * possible to overwrite the accessor by using __defineGetter__ or
1399 * __defineSetter__ from JavaScript code.
1400 */
1401enum AccessControl {
1402 DEFAULT = 0,
1403 ALL_CAN_READ = 1,
1404 ALL_CAN_WRITE = 1 << 1,
1405 PROHIBITS_OVERWRITING = 1 << 2
1406};
1407
1408
1409/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 * A JavaScript object (ECMA-262, 4.3.3)
1411 */
Steve Block8defd9f2010-07-08 12:39:36 +01001412class Object : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00001413 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001414 V8EXPORT bool Set(Handle<Value> key,
1415 Handle<Value> value,
1416 PropertyAttribute attribs = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00001417
Steve Block8defd9f2010-07-08 12:39:36 +01001418 V8EXPORT bool Set(uint32_t index,
1419 Handle<Value> value);
Steve Block6ded16b2010-05-10 14:33:55 +01001420
Steve Blocka7e24c12009-10-30 11:49:00 +00001421 // Sets a local property on this object bypassing interceptors and
1422 // overriding accessors or read-only properties.
1423 //
1424 // Note that if the object has an interceptor the property will be set
1425 // locally, but since the interceptor takes precedence the local property
1426 // will only be returned if the interceptor doesn't return a value.
1427 //
1428 // Note also that this only works for named properties.
Steve Block8defd9f2010-07-08 12:39:36 +01001429 V8EXPORT bool ForceSet(Handle<Value> key,
1430 Handle<Value> value,
1431 PropertyAttribute attribs = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00001432
Steve Block8defd9f2010-07-08 12:39:36 +01001433 V8EXPORT Local<Value> Get(Handle<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001434
Steve Block8defd9f2010-07-08 12:39:36 +01001435 V8EXPORT Local<Value> Get(uint32_t index);
Steve Block6ded16b2010-05-10 14:33:55 +01001436
Steve Blocka7e24c12009-10-30 11:49:00 +00001437 // TODO(1245389): Replace the type-specific versions of these
1438 // functions with generic ones that accept a Handle<Value> key.
Steve Block8defd9f2010-07-08 12:39:36 +01001439 V8EXPORT bool Has(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001440
Steve Block8defd9f2010-07-08 12:39:36 +01001441 V8EXPORT bool Delete(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001442
1443 // Delete a property on this object bypassing interceptors and
1444 // ignoring dont-delete attributes.
Steve Block8defd9f2010-07-08 12:39:36 +01001445 V8EXPORT bool ForceDelete(Handle<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001446
Steve Block8defd9f2010-07-08 12:39:36 +01001447 V8EXPORT bool Has(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001448
Steve Block8defd9f2010-07-08 12:39:36 +01001449 V8EXPORT bool Delete(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001450
Steve Block8defd9f2010-07-08 12:39:36 +01001451 V8EXPORT bool SetAccessor(Handle<String> name,
1452 AccessorGetter getter,
1453 AccessorSetter setter = 0,
1454 Handle<Value> data = Handle<Value>(),
1455 AccessControl settings = DEFAULT,
1456 PropertyAttribute attribute = None);
Leon Clarkef7060e22010-06-03 12:02:55 +01001457
Steve Blocka7e24c12009-10-30 11:49:00 +00001458 /**
1459 * Returns an array containing the names of the enumerable properties
1460 * of this object, including properties from prototype objects. The
1461 * array returned by this method contains the same values as would
1462 * be enumerated by a for-in statement over this object.
1463 */
Steve Block8defd9f2010-07-08 12:39:36 +01001464 V8EXPORT Local<Array> GetPropertyNames();
Steve Blocka7e24c12009-10-30 11:49:00 +00001465
1466 /**
1467 * Get the prototype object. This does not skip objects marked to
1468 * be skipped by __proto__ and it does not consult the security
1469 * handler.
1470 */
Steve Block8defd9f2010-07-08 12:39:36 +01001471 V8EXPORT Local<Value> GetPrototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
1473 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00001474 * Set the prototype object. This does not skip objects marked to
1475 * be skipped by __proto__ and it does not consult the security
1476 * handler.
1477 */
Steve Block8defd9f2010-07-08 12:39:36 +01001478 V8EXPORT bool SetPrototype(Handle<Value> prototype);
Andrei Popescu402d9372010-02-26 13:31:12 +00001479
1480 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001481 * Finds an instance of the given function template in the prototype
1482 * chain.
1483 */
Steve Block8defd9f2010-07-08 12:39:36 +01001484 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1485 Handle<FunctionTemplate> tmpl);
Steve Blocka7e24c12009-10-30 11:49:00 +00001486
1487 /**
1488 * Call builtin Object.prototype.toString on this object.
1489 * This is different from Value::ToString() that may call
1490 * user-defined toString function. This one does not.
1491 */
Steve Block8defd9f2010-07-08 12:39:36 +01001492 V8EXPORT Local<String> ObjectProtoToString();
Steve Blocka7e24c12009-10-30 11:49:00 +00001493
1494 /** Gets the number of internal fields for this Object. */
Steve Block8defd9f2010-07-08 12:39:36 +01001495 V8EXPORT int InternalFieldCount();
Steve Blocka7e24c12009-10-30 11:49:00 +00001496 /** Gets the value in an internal field. */
1497 inline Local<Value> GetInternalField(int index);
1498 /** Sets the value in an internal field. */
Steve Block8defd9f2010-07-08 12:39:36 +01001499 V8EXPORT void SetInternalField(int index, Handle<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001500
1501 /** Gets a native pointer from an internal field. */
1502 inline void* GetPointerFromInternalField(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00001503
Steve Blocka7e24c12009-10-30 11:49:00 +00001504 /** Sets a native pointer in an internal field. */
Steve Block8defd9f2010-07-08 12:39:36 +01001505 V8EXPORT void SetPointerInInternalField(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001506
1507 // Testers for local properties.
Steve Block8defd9f2010-07-08 12:39:36 +01001508 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1509 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1510 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001511
1512 /**
1513 * If result.IsEmpty() no real property was located in the prototype chain.
1514 * This means interceptors in the prototype chain are not called.
1515 */
Steve Block8defd9f2010-07-08 12:39:36 +01001516 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1517 Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001518
1519 /**
1520 * If result.IsEmpty() no real property was located on the object or
1521 * in the prototype chain.
1522 * This means interceptors in the prototype chain are not called.
1523 */
Steve Block8defd9f2010-07-08 12:39:36 +01001524 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001525
1526 /** Tests for a named lookup interceptor.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001527 V8EXPORT bool HasNamedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001528
1529 /** Tests for an index lookup interceptor.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001530 V8EXPORT bool HasIndexedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001531
1532 /**
1533 * Turns on access check on the object if the object is an instance of
1534 * a template that has access check callbacks. If an object has no
1535 * access check info, the object cannot be accessed by anyone.
1536 */
Steve Block8defd9f2010-07-08 12:39:36 +01001537 V8EXPORT void TurnOnAccessCheck();
Steve Blocka7e24c12009-10-30 11:49:00 +00001538
1539 /**
1540 * Returns the identity hash for this object. The current implemenation uses
1541 * a hidden property on the object to store the identity hash.
1542 *
1543 * The return value will never be 0. Also, it is not guaranteed to be
1544 * unique.
1545 */
Steve Block8defd9f2010-07-08 12:39:36 +01001546 V8EXPORT int GetIdentityHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00001547
1548 /**
1549 * Access hidden properties on JavaScript objects. These properties are
1550 * hidden from the executing JavaScript and only accessible through the V8
1551 * C++ API. Hidden properties introduced by V8 internally (for example the
1552 * identity hash) are prefixed with "v8::".
1553 */
Steve Block8defd9f2010-07-08 12:39:36 +01001554 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1555 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1556 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
Steve Block3ce2e202009-11-05 08:53:23 +00001557
Steve Blocka7e24c12009-10-30 11:49:00 +00001558 /**
1559 * Returns true if this is an instance of an api function (one
1560 * created from a function created from a function template) and has
1561 * been modified since it was created. Note that this method is
1562 * conservative and may return true for objects that haven't actually
1563 * been modified.
1564 */
Steve Block8defd9f2010-07-08 12:39:36 +01001565 V8EXPORT bool IsDirty();
Steve Blocka7e24c12009-10-30 11:49:00 +00001566
1567 /**
1568 * Clone this object with a fast but shallow copy. Values will point
1569 * to the same values as the original object.
1570 */
Steve Block8defd9f2010-07-08 12:39:36 +01001571 V8EXPORT Local<Object> Clone();
Steve Blocka7e24c12009-10-30 11:49:00 +00001572
1573 /**
1574 * Set the backing store of the indexed properties to be managed by the
1575 * embedding layer. Access to the indexed properties will follow the rules
1576 * spelled out in CanvasPixelArray.
1577 * Note: The embedding program still owns the data and needs to ensure that
1578 * the backing store is preserved while V8 has a reference.
1579 */
Steve Block8defd9f2010-07-08 12:39:36 +01001580 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001581 bool HasIndexedPropertiesInPixelData();
1582 uint8_t* GetIndexedPropertiesPixelData();
1583 int GetIndexedPropertiesPixelDataLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001584
Steve Block3ce2e202009-11-05 08:53:23 +00001585 /**
1586 * Set the backing store of the indexed properties to be managed by the
1587 * embedding layer. Access to the indexed properties will follow the rules
1588 * spelled out for the CanvasArray subtypes in the WebGL specification.
1589 * Note: The embedding program still owns the data and needs to ensure that
1590 * the backing store is preserved while V8 has a reference.
1591 */
Steve Block8defd9f2010-07-08 12:39:36 +01001592 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1593 void* data,
1594 ExternalArrayType array_type,
1595 int number_of_elements);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001596 bool HasIndexedPropertiesInExternalArrayData();
1597 void* GetIndexedPropertiesExternalArrayData();
1598 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1599 int GetIndexedPropertiesExternalArrayDataLength();
Steve Block3ce2e202009-11-05 08:53:23 +00001600
Steve Block8defd9f2010-07-08 12:39:36 +01001601 V8EXPORT static Local<Object> New();
Steve Blocka7e24c12009-10-30 11:49:00 +00001602 static inline Object* Cast(Value* obj);
1603 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001604 V8EXPORT Object();
1605 V8EXPORT static void CheckCast(Value* obj);
1606 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1607 V8EXPORT void* SlowGetPointerFromInternalField(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001608
1609 /**
1610 * If quick access to the internal field is possible this method
Steve Block3ce2e202009-11-05 08:53:23 +00001611 * returns the value. Otherwise an empty handle is returned.
Steve Blocka7e24c12009-10-30 11:49:00 +00001612 */
1613 inline Local<Value> UncheckedGetInternalField(int index);
1614};
1615
1616
1617/**
1618 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1619 */
Steve Block8defd9f2010-07-08 12:39:36 +01001620class Array : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00001621 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001622 V8EXPORT uint32_t Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001623
1624 /**
1625 * Clones an element at index |index|. Returns an empty
1626 * handle if cloning fails (for any reason).
1627 */
Steve Block8defd9f2010-07-08 12:39:36 +01001628 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001629
Steve Block8defd9f2010-07-08 12:39:36 +01001630 V8EXPORT static Local<Array> New(int length = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001631 static inline Array* Cast(Value* obj);
1632 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001633 V8EXPORT Array();
Steve Blocka7e24c12009-10-30 11:49:00 +00001634 static void CheckCast(Value* obj);
1635};
1636
1637
1638/**
1639 * A JavaScript function object (ECMA-262, 15.3).
1640 */
Steve Block8defd9f2010-07-08 12:39:36 +01001641class Function : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00001642 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001643 V8EXPORT Local<Object> NewInstance() const;
1644 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1645 V8EXPORT Local<Value> Call(Handle<Object> recv,
1646 int argc,
1647 Handle<Value> argv[]);
1648 V8EXPORT void SetName(Handle<String> name);
1649 V8EXPORT Handle<Value> GetName() const;
Andrei Popescu402d9372010-02-26 13:31:12 +00001650
1651 /**
1652 * Returns zero based line number of function body and
1653 * kLineOffsetNotFound if no information available.
1654 */
Steve Block8defd9f2010-07-08 12:39:36 +01001655 V8EXPORT int GetScriptLineNumber() const;
1656 V8EXPORT ScriptOrigin GetScriptOrigin() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001657 static inline Function* Cast(Value* obj);
Steve Block8defd9f2010-07-08 12:39:36 +01001658 V8EXPORT static const int kLineOffsetNotFound;
Steve Blocka7e24c12009-10-30 11:49:00 +00001659 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001660 V8EXPORT Function();
1661 V8EXPORT static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001662};
1663
1664
1665/**
1666 * A JavaScript value that wraps a C++ void*. This type of value is
1667 * mainly used to associate C++ data structures with JavaScript
1668 * objects.
1669 *
1670 * The Wrap function V8 will return the most optimal Value object wrapping the
1671 * C++ void*. The type of the value is not guaranteed to be an External object
1672 * and no assumptions about its type should be made. To access the wrapped
1673 * value Unwrap should be used, all other operations on that object will lead
1674 * to unpredictable results.
1675 */
Steve Block8defd9f2010-07-08 12:39:36 +01001676class External : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00001677 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001678 V8EXPORT static Local<Value> Wrap(void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 static inline void* Unwrap(Handle<Value> obj);
1680
Steve Block8defd9f2010-07-08 12:39:36 +01001681 V8EXPORT static Local<External> New(void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001682 static inline External* Cast(Value* obj);
Steve Block8defd9f2010-07-08 12:39:36 +01001683 V8EXPORT void* Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001684 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001685 V8EXPORT External();
1686 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001687 static inline void* QuickUnwrap(Handle<v8::Value> obj);
Steve Block8defd9f2010-07-08 12:39:36 +01001688 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001689};
1690
1691
1692// --- T e m p l a t e s ---
1693
1694
1695/**
1696 * The superclass of object and function templates.
1697 */
1698class V8EXPORT Template : public Data {
1699 public:
1700 /** Adds a property to each instance created by this template.*/
1701 void Set(Handle<String> name, Handle<Data> value,
1702 PropertyAttribute attributes = None);
1703 inline void Set(const char* name, Handle<Data> value);
1704 private:
1705 Template();
1706
1707 friend class ObjectTemplate;
1708 friend class FunctionTemplate;
1709};
1710
1711
1712/**
1713 * The argument information given to function call callbacks. This
1714 * class provides access to information about the context of the call,
1715 * including the receiver, the number and values of arguments, and
1716 * the holder of the function.
1717 */
Steve Block8defd9f2010-07-08 12:39:36 +01001718class Arguments {
Steve Blocka7e24c12009-10-30 11:49:00 +00001719 public:
1720 inline int Length() const;
1721 inline Local<Value> operator[](int i) const;
1722 inline Local<Function> Callee() const;
1723 inline Local<Object> This() const;
1724 inline Local<Object> Holder() const;
1725 inline bool IsConstructCall() const;
1726 inline Local<Value> Data() const;
1727 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00001728 friend class ImplementationUtilities;
1729 inline Arguments(Local<Value> data,
1730 Local<Object> holder,
1731 Local<Function> callee,
1732 bool is_construct_call,
1733 void** values, int length);
1734 Local<Value> data_;
1735 Local<Object> holder_;
1736 Local<Function> callee_;
1737 bool is_construct_call_;
1738 void** values_;
1739 int length_;
1740};
1741
1742
1743/**
1744 * The information passed to an accessor callback about the context
1745 * of the property access.
1746 */
1747class V8EXPORT AccessorInfo {
1748 public:
1749 inline AccessorInfo(internal::Object** args)
1750 : args_(args) { }
1751 inline Local<Value> Data() const;
1752 inline Local<Object> This() const;
1753 inline Local<Object> Holder() const;
1754 private:
1755 internal::Object** args_;
1756};
1757
1758
1759typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1760
1761typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1762
1763/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001764 * NamedProperty[Getter|Setter] are used as interceptors on object.
1765 * See ObjectTemplate::SetNamedPropertyHandler.
1766 */
1767typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1768 const AccessorInfo& info);
1769
1770
1771/**
1772 * Returns the value if the setter intercepts the request.
1773 * Otherwise, returns an empty handle.
1774 */
1775typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1776 Local<Value> value,
1777 const AccessorInfo& info);
1778
Steve Blocka7e24c12009-10-30 11:49:00 +00001779/**
1780 * Returns a non-empty handle if the interceptor intercepts the request.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001781 * The result is an integer encoding property attributes (like v8::None,
1782 * v8::DontEnum, etc.)
Steve Blocka7e24c12009-10-30 11:49:00 +00001783 */
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001784typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1785 const AccessorInfo& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001786
1787
1788/**
1789 * Returns a non-empty handle if the deleter intercepts the request.
1790 * The return value is true if the property could be deleted and false
1791 * otherwise.
1792 */
1793typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1794 const AccessorInfo& info);
1795
1796/**
1797 * Returns an array containing the names of the properties the named
1798 * property getter intercepts.
1799 */
1800typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1801
1802
1803/**
1804 * Returns the value of the property if the getter intercepts the
1805 * request. Otherwise, returns an empty handle.
1806 */
1807typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1808 const AccessorInfo& info);
1809
1810
1811/**
1812 * Returns the value if the setter intercepts the request.
1813 * Otherwise, returns an empty handle.
1814 */
1815typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1816 Local<Value> value,
1817 const AccessorInfo& info);
1818
1819
1820/**
1821 * Returns a non-empty handle if the interceptor intercepts the request.
1822 * The result is true if the property exists and false otherwise.
1823 */
1824typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1825 const AccessorInfo& info);
1826
1827/**
1828 * Returns a non-empty handle if the deleter intercepts the request.
1829 * The return value is true if the property could be deleted and false
1830 * otherwise.
1831 */
1832typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1833 const AccessorInfo& info);
1834
1835/**
1836 * Returns an array containing the indices of the properties the
1837 * indexed property getter intercepts.
1838 */
1839typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1840
1841
1842/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001843 * Access type specification.
1844 */
1845enum AccessType {
1846 ACCESS_GET,
1847 ACCESS_SET,
1848 ACCESS_HAS,
1849 ACCESS_DELETE,
1850 ACCESS_KEYS
1851};
1852
1853
1854/**
1855 * Returns true if cross-context access should be allowed to the named
1856 * property with the given key on the host object.
1857 */
1858typedef bool (*NamedSecurityCallback)(Local<Object> host,
1859 Local<Value> key,
1860 AccessType type,
1861 Local<Value> data);
1862
1863
1864/**
1865 * Returns true if cross-context access should be allowed to the indexed
1866 * property with the given index on the host object.
1867 */
1868typedef bool (*IndexedSecurityCallback)(Local<Object> host,
1869 uint32_t index,
1870 AccessType type,
1871 Local<Value> data);
1872
1873
1874/**
1875 * A FunctionTemplate is used to create functions at runtime. There
1876 * can only be one function created from a FunctionTemplate in a
1877 * context. The lifetime of the created function is equal to the
1878 * lifetime of the context. So in case the embedder needs to create
1879 * temporary functions that can be collected using Scripts is
1880 * preferred.
1881 *
1882 * A FunctionTemplate can have properties, these properties are added to the
1883 * function object when it is created.
1884 *
1885 * A FunctionTemplate has a corresponding instance template which is
1886 * used to create object instances when the function is used as a
1887 * constructor. Properties added to the instance template are added to
1888 * each object instance.
1889 *
1890 * A FunctionTemplate can have a prototype template. The prototype template
1891 * is used to create the prototype object of the function.
1892 *
1893 * The following example shows how to use a FunctionTemplate:
1894 *
1895 * \code
1896 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1897 * t->Set("func_property", v8::Number::New(1));
1898 *
1899 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1900 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1901 * proto_t->Set("proto_const", v8::Number::New(2));
1902 *
1903 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1904 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1905 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1906 * instance_t->Set("instance_property", Number::New(3));
1907 *
1908 * v8::Local<v8::Function> function = t->GetFunction();
1909 * v8::Local<v8::Object> instance = function->NewInstance();
1910 * \endcode
1911 *
1912 * Let's use "function" as the JS variable name of the function object
1913 * and "instance" for the instance object created above. The function
1914 * and the instance will have the following properties:
1915 *
1916 * \code
1917 * func_property in function == true;
1918 * function.func_property == 1;
1919 *
1920 * function.prototype.proto_method() invokes 'InvokeCallback'
1921 * function.prototype.proto_const == 2;
1922 *
1923 * instance instanceof function == true;
1924 * instance.instance_accessor calls 'InstanceAccessorCallback'
1925 * instance.instance_property == 3;
1926 * \endcode
1927 *
1928 * A FunctionTemplate can inherit from another one by calling the
1929 * FunctionTemplate::Inherit method. The following graph illustrates
1930 * the semantics of inheritance:
1931 *
1932 * \code
1933 * FunctionTemplate Parent -> Parent() . prototype -> { }
1934 * ^ ^
1935 * | Inherit(Parent) | .__proto__
1936 * | |
1937 * FunctionTemplate Child -> Child() . prototype -> { }
1938 * \endcode
1939 *
1940 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1941 * object of the Child() function has __proto__ pointing to the
1942 * Parent() function's prototype object. An instance of the Child
1943 * function has all properties on Parent's instance templates.
1944 *
1945 * Let Parent be the FunctionTemplate initialized in the previous
1946 * section and create a Child FunctionTemplate by:
1947 *
1948 * \code
1949 * Local<FunctionTemplate> parent = t;
1950 * Local<FunctionTemplate> child = FunctionTemplate::New();
1951 * child->Inherit(parent);
1952 *
1953 * Local<Function> child_function = child->GetFunction();
1954 * Local<Object> child_instance = child_function->NewInstance();
1955 * \endcode
1956 *
1957 * The Child function and Child instance will have the following
1958 * properties:
1959 *
1960 * \code
1961 * child_func.prototype.__proto__ == function.prototype;
1962 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
1963 * child_instance.instance_property == 3;
1964 * \endcode
1965 */
1966class V8EXPORT FunctionTemplate : public Template {
1967 public:
1968 /** Creates a function template.*/
1969 static Local<FunctionTemplate> New(
1970 InvocationCallback callback = 0,
1971 Handle<Value> data = Handle<Value>(),
1972 Handle<Signature> signature = Handle<Signature>());
1973 /** Returns the unique function instance in the current execution context.*/
1974 Local<Function> GetFunction();
1975
1976 /**
1977 * Set the call-handler callback for a FunctionTemplate. This
1978 * callback is called whenever the function created from this
1979 * FunctionTemplate is called.
1980 */
1981 void SetCallHandler(InvocationCallback callback,
1982 Handle<Value> data = Handle<Value>());
1983
1984 /** Get the InstanceTemplate. */
1985 Local<ObjectTemplate> InstanceTemplate();
1986
1987 /** Causes the function template to inherit from a parent function template.*/
1988 void Inherit(Handle<FunctionTemplate> parent);
1989
1990 /**
1991 * A PrototypeTemplate is the template used to create the prototype object
1992 * of the function created by this template.
1993 */
1994 Local<ObjectTemplate> PrototypeTemplate();
1995
1996
1997 /**
1998 * Set the class name of the FunctionTemplate. This is used for
1999 * printing objects created with the function created from the
2000 * FunctionTemplate as its constructor.
2001 */
2002 void SetClassName(Handle<String> name);
2003
2004 /**
2005 * Determines whether the __proto__ accessor ignores instances of
2006 * the function template. If instances of the function template are
2007 * ignored, __proto__ skips all instances and instead returns the
2008 * next object in the prototype chain.
2009 *
2010 * Call with a value of true to make the __proto__ accessor ignore
2011 * instances of the function template. Call with a value of false
2012 * to make the __proto__ accessor not ignore instances of the
2013 * function template. By default, instances of a function template
2014 * are not ignored.
2015 */
2016 void SetHiddenPrototype(bool value);
2017
2018 /**
2019 * Returns true if the given object is an instance of this function
2020 * template.
2021 */
2022 bool HasInstance(Handle<Value> object);
2023
2024 private:
2025 FunctionTemplate();
2026 void AddInstancePropertyAccessor(Handle<String> name,
2027 AccessorGetter getter,
2028 AccessorSetter setter,
2029 Handle<Value> data,
2030 AccessControl settings,
2031 PropertyAttribute attributes);
2032 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2033 NamedPropertySetter setter,
2034 NamedPropertyQuery query,
2035 NamedPropertyDeleter remover,
2036 NamedPropertyEnumerator enumerator,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002037 Handle<Value> data);
Steve Blocka7e24c12009-10-30 11:49:00 +00002038 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2039 IndexedPropertySetter setter,
2040 IndexedPropertyQuery query,
2041 IndexedPropertyDeleter remover,
2042 IndexedPropertyEnumerator enumerator,
2043 Handle<Value> data);
2044 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2045 Handle<Value> data);
2046
2047 friend class Context;
2048 friend class ObjectTemplate;
2049};
2050
2051
2052/**
2053 * An ObjectTemplate is used to create objects at runtime.
2054 *
2055 * Properties added to an ObjectTemplate are added to each object
2056 * created from the ObjectTemplate.
2057 */
2058class V8EXPORT ObjectTemplate : public Template {
2059 public:
2060 /** Creates an ObjectTemplate. */
2061 static Local<ObjectTemplate> New();
2062
2063 /** Creates a new instance of this template.*/
2064 Local<Object> NewInstance();
2065
2066 /**
2067 * Sets an accessor on the object template.
2068 *
2069 * Whenever the property with the given name is accessed on objects
2070 * created from this ObjectTemplate the getter and setter callbacks
2071 * are called instead of getting and setting the property directly
2072 * on the JavaScript object.
2073 *
2074 * \param name The name of the property for which an accessor is added.
2075 * \param getter The callback to invoke when getting the property.
2076 * \param setter The callback to invoke when setting the property.
2077 * \param data A piece of data that will be passed to the getter and setter
2078 * callbacks whenever they are invoked.
2079 * \param settings Access control settings for the accessor. This is a bit
2080 * field consisting of one of more of
2081 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2082 * The default is to not allow cross-context access.
2083 * ALL_CAN_READ means that all cross-context reads are allowed.
2084 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2085 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2086 * cross-context access.
2087 * \param attribute The attributes of the property for which an accessor
2088 * is added.
2089 */
2090 void SetAccessor(Handle<String> name,
2091 AccessorGetter getter,
2092 AccessorSetter setter = 0,
2093 Handle<Value> data = Handle<Value>(),
2094 AccessControl settings = DEFAULT,
2095 PropertyAttribute attribute = None);
2096
2097 /**
2098 * Sets a named property handler on the object template.
2099 *
2100 * Whenever a named property is accessed on objects created from
2101 * this object template, the provided callback is invoked instead of
2102 * accessing the property directly on the JavaScript object.
2103 *
2104 * \param getter The callback to invoke when getting a property.
2105 * \param setter The callback to invoke when setting a property.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002106 * \param query The callback to invoke to check if a property is present,
2107 * and if present, get its attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00002108 * \param deleter The callback to invoke when deleting a property.
2109 * \param enumerator The callback to invoke to enumerate all the named
2110 * properties of an object.
2111 * \param data A piece of data that will be passed to the callbacks
2112 * whenever they are invoked.
2113 */
2114 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2115 NamedPropertySetter setter = 0,
2116 NamedPropertyQuery query = 0,
2117 NamedPropertyDeleter deleter = 0,
2118 NamedPropertyEnumerator enumerator = 0,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002119 Handle<Value> data = Handle<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002120
2121 /**
2122 * Sets an indexed property handler on the object template.
2123 *
2124 * Whenever an indexed property is accessed on objects created from
2125 * this object template, the provided callback is invoked instead of
2126 * accessing the property directly on the JavaScript object.
2127 *
2128 * \param getter The callback to invoke when getting a property.
2129 * \param setter The callback to invoke when setting a property.
2130 * \param query The callback to invoke to check is an object has a property.
2131 * \param deleter The callback to invoke when deleting a property.
2132 * \param enumerator The callback to invoke to enumerate all the indexed
2133 * properties of an object.
2134 * \param data A piece of data that will be passed to the callbacks
2135 * whenever they are invoked.
2136 */
2137 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2138 IndexedPropertySetter setter = 0,
2139 IndexedPropertyQuery query = 0,
2140 IndexedPropertyDeleter deleter = 0,
2141 IndexedPropertyEnumerator enumerator = 0,
2142 Handle<Value> data = Handle<Value>());
2143 /**
2144 * Sets the callback to be used when calling instances created from
2145 * this template as a function. If no callback is set, instances
2146 * behave like normal JavaScript objects that cannot be called as a
2147 * function.
2148 */
2149 void SetCallAsFunctionHandler(InvocationCallback callback,
2150 Handle<Value> data = Handle<Value>());
2151
2152 /**
2153 * Mark object instances of the template as undetectable.
2154 *
2155 * In many ways, undetectable objects behave as though they are not
2156 * there. They behave like 'undefined' in conditionals and when
2157 * printed. However, properties can be accessed and called as on
2158 * normal objects.
2159 */
2160 void MarkAsUndetectable();
2161
2162 /**
2163 * Sets access check callbacks on the object template.
2164 *
2165 * When accessing properties on instances of this object template,
2166 * the access check callback will be called to determine whether or
2167 * not to allow cross-context access to the properties.
2168 * The last parameter specifies whether access checks are turned
2169 * on by default on instances. If access checks are off by default,
2170 * they can be turned on on individual instances by calling
2171 * Object::TurnOnAccessCheck().
2172 */
2173 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2174 IndexedSecurityCallback indexed_handler,
2175 Handle<Value> data = Handle<Value>(),
2176 bool turned_on_by_default = true);
2177
2178 /**
2179 * Gets the number of internal fields for objects generated from
2180 * this template.
2181 */
2182 int InternalFieldCount();
2183
2184 /**
2185 * Sets the number of internal fields for objects generated from
2186 * this template.
2187 */
2188 void SetInternalFieldCount(int value);
2189
2190 private:
2191 ObjectTemplate();
2192 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2193 friend class FunctionTemplate;
2194};
2195
2196
2197/**
2198 * A Signature specifies which receivers and arguments a function can
2199 * legally be called with.
2200 */
2201class V8EXPORT Signature : public Data {
2202 public:
2203 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2204 Handle<FunctionTemplate>(),
2205 int argc = 0,
2206 Handle<FunctionTemplate> argv[] = 0);
2207 private:
2208 Signature();
2209};
2210
2211
2212/**
2213 * A utility for determining the type of objects based on the template
2214 * they were constructed from.
2215 */
2216class V8EXPORT TypeSwitch : public Data {
2217 public:
2218 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2219 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2220 int match(Handle<Value> value);
2221 private:
2222 TypeSwitch();
2223};
2224
2225
2226// --- E x t e n s i o n s ---
2227
2228
2229/**
2230 * Ignore
2231 */
2232class V8EXPORT Extension { // NOLINT
2233 public:
2234 Extension(const char* name,
2235 const char* source = 0,
2236 int dep_count = 0,
2237 const char** deps = 0);
2238 virtual ~Extension() { }
2239 virtual v8::Handle<v8::FunctionTemplate>
2240 GetNativeFunction(v8::Handle<v8::String> name) {
2241 return v8::Handle<v8::FunctionTemplate>();
2242 }
2243
2244 const char* name() { return name_; }
2245 const char* source() { return source_; }
2246 int dependency_count() { return dep_count_; }
2247 const char** dependencies() { return deps_; }
2248 void set_auto_enable(bool value) { auto_enable_ = value; }
2249 bool auto_enable() { return auto_enable_; }
2250
2251 private:
2252 const char* name_;
2253 const char* source_;
2254 int dep_count_;
2255 const char** deps_;
2256 bool auto_enable_;
2257
2258 // Disallow copying and assigning.
2259 Extension(const Extension&);
2260 void operator=(const Extension&);
2261};
2262
2263
2264void V8EXPORT RegisterExtension(Extension* extension);
2265
2266
2267/**
2268 * Ignore
2269 */
2270class V8EXPORT DeclareExtension {
2271 public:
2272 inline DeclareExtension(Extension* extension) {
2273 RegisterExtension(extension);
2274 }
2275};
2276
2277
2278// --- S t a t i c s ---
2279
2280
2281Handle<Primitive> V8EXPORT Undefined();
2282Handle<Primitive> V8EXPORT Null();
2283Handle<Boolean> V8EXPORT True();
2284Handle<Boolean> V8EXPORT False();
2285
2286
2287/**
2288 * A set of constraints that specifies the limits of the runtime's memory use.
2289 * You must set the heap size before initializing the VM - the size cannot be
2290 * adjusted after the VM is initialized.
2291 *
2292 * If you are using threads then you should hold the V8::Locker lock while
2293 * setting the stack limit and you must set a non-default stack limit separately
2294 * for each thread.
2295 */
2296class V8EXPORT ResourceConstraints {
2297 public:
2298 ResourceConstraints();
2299 int max_young_space_size() const { return max_young_space_size_; }
2300 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
2301 int max_old_space_size() const { return max_old_space_size_; }
2302 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
2303 uint32_t* stack_limit() const { return stack_limit_; }
2304 // Sets an address beyond which the VM's stack may not grow.
2305 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2306 private:
2307 int max_young_space_size_;
2308 int max_old_space_size_;
2309 uint32_t* stack_limit_;
2310};
2311
2312
Kristian Monsen25f61362010-05-21 11:50:48 +01002313bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
Steve Blocka7e24c12009-10-30 11:49:00 +00002314
2315
2316// --- E x c e p t i o n s ---
2317
2318
2319typedef void (*FatalErrorCallback)(const char* location, const char* message);
2320
2321
2322typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2323
2324
2325/**
2326 * Schedules an exception to be thrown when returning to JavaScript. When an
2327 * exception has been scheduled it is illegal to invoke any JavaScript
2328 * operation; the caller must return immediately and only after the exception
2329 * has been handled does it become legal to invoke JavaScript operations.
2330 */
2331Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
2332
2333/**
2334 * Create new error objects by calling the corresponding error object
2335 * constructor with the message.
2336 */
2337class V8EXPORT Exception {
2338 public:
2339 static Local<Value> RangeError(Handle<String> message);
2340 static Local<Value> ReferenceError(Handle<String> message);
2341 static Local<Value> SyntaxError(Handle<String> message);
2342 static Local<Value> TypeError(Handle<String> message);
2343 static Local<Value> Error(Handle<String> message);
2344};
2345
2346
2347// --- C o u n t e r s C a l l b a c k s ---
2348
2349typedef int* (*CounterLookupCallback)(const char* name);
2350
2351typedef void* (*CreateHistogramCallback)(const char* name,
2352 int min,
2353 int max,
2354 size_t buckets);
2355
2356typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2357
2358// --- 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 ---
2359typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2360 AccessType type,
2361 Local<Value> data);
2362
2363// --- 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
2364
2365/**
Steve Block6ded16b2010-05-10 14:33:55 +01002366 * Applications can register callback functions which will be called
2367 * before and after a garbage collection. Allocations are not
2368 * allowed in the callback functions, you therefore cannot manipulate
Steve Blocka7e24c12009-10-30 11:49:00 +00002369 * objects (set or delete properties for example) since it is possible
2370 * such operations will result in the allocation of objects.
2371 */
Steve Block6ded16b2010-05-10 14:33:55 +01002372enum GCType {
2373 kGCTypeScavenge = 1 << 0,
2374 kGCTypeMarkSweepCompact = 1 << 1,
2375 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2376};
2377
2378enum GCCallbackFlags {
2379 kNoGCCallbackFlags = 0,
2380 kGCCallbackFlagCompacted = 1 << 0
2381};
2382
2383typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2384typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2385
Steve Blocka7e24c12009-10-30 11:49:00 +00002386typedef void (*GCCallback)();
2387
2388
Steve Blocka7e24c12009-10-30 11:49:00 +00002389/**
2390 * Profiler modules.
2391 *
2392 * In V8, profiler consists of several modules: CPU profiler, and different
2393 * kinds of heap profiling. Each can be turned on / off independently.
2394 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2395 * modules are enabled only temporarily for making a snapshot of the heap.
2396 */
2397enum ProfilerModules {
2398 PROFILER_MODULE_NONE = 0,
2399 PROFILER_MODULE_CPU = 1,
2400 PROFILER_MODULE_HEAP_STATS = 1 << 1,
2401 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2402 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
2403};
2404
2405
2406/**
Steve Block3ce2e202009-11-05 08:53:23 +00002407 * Collection of V8 heap information.
2408 *
2409 * Instances of this class can be passed to v8::V8::HeapStatistics to
2410 * get heap statistics from V8.
2411 */
2412class V8EXPORT HeapStatistics {
2413 public:
2414 HeapStatistics();
2415 size_t total_heap_size() { return total_heap_size_; }
2416 size_t used_heap_size() { return used_heap_size_; }
2417
2418 private:
2419 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2420 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2421
2422 size_t total_heap_size_;
2423 size_t used_heap_size_;
2424
2425 friend class V8;
2426};
2427
2428
2429/**
Steve Blocka7e24c12009-10-30 11:49:00 +00002430 * Container class for static utility functions.
2431 */
2432class V8EXPORT V8 {
2433 public:
2434 /** Set the callback to invoke in case of fatal errors. */
2435 static void SetFatalErrorHandler(FatalErrorCallback that);
2436
2437 /**
2438 * Ignore out-of-memory exceptions.
2439 *
2440 * V8 running out of memory is treated as a fatal error by default.
2441 * This means that the fatal error handler is called and that V8 is
2442 * terminated.
2443 *
2444 * IgnoreOutOfMemoryException can be used to not treat a
2445 * out-of-memory situation as a fatal error. This way, the contexts
2446 * that did not cause the out of memory problem might be able to
2447 * continue execution.
2448 */
2449 static void IgnoreOutOfMemoryException();
2450
2451 /**
2452 * Check if V8 is dead and therefore unusable. This is the case after
2453 * fatal errors such as out-of-memory situations.
2454 */
2455 static bool IsDead();
2456
2457 /**
2458 * Adds a message listener.
2459 *
2460 * The same message listener can be added more than once and it that
2461 * case it will be called more than once for each message.
2462 */
2463 static bool AddMessageListener(MessageCallback that,
2464 Handle<Value> data = Handle<Value>());
2465
2466 /**
2467 * Remove all message listeners from the specified callback function.
2468 */
2469 static void RemoveMessageListeners(MessageCallback that);
2470
2471 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002472 * Tells V8 to capture current stack trace when uncaught exception occurs
2473 * and report it to the message listeners. The option is off by default.
2474 */
2475 static void SetCaptureStackTraceForUncaughtExceptions(
2476 bool capture,
2477 int frame_limit = 10,
2478 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2479
2480 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002481 * Sets V8 flags from a string.
2482 */
2483 static void SetFlagsFromString(const char* str, int length);
2484
2485 /**
2486 * Sets V8 flags from the command line.
2487 */
2488 static void SetFlagsFromCommandLine(int* argc,
2489 char** argv,
2490 bool remove_flags);
2491
2492 /** Get the version string. */
2493 static const char* GetVersion();
2494
2495 /**
2496 * Enables the host application to provide a mechanism for recording
2497 * statistics counters.
2498 */
2499 static void SetCounterFunction(CounterLookupCallback);
2500
2501 /**
2502 * Enables the host application to provide a mechanism for recording
2503 * histograms. The CreateHistogram function returns a
2504 * histogram which will later be passed to the AddHistogramSample
2505 * function.
2506 */
2507 static void SetCreateHistogramFunction(CreateHistogramCallback);
2508 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2509
2510 /**
2511 * Enables the computation of a sliding window of states. The sliding
2512 * window information is recorded in statistics counters.
2513 */
2514 static void EnableSlidingStateWindow();
2515
2516 /** Callback function for reporting failed access checks.*/
2517 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2518
2519 /**
2520 * Enables the host application to receive a notification before a
Steve Block6ded16b2010-05-10 14:33:55 +01002521 * garbage collection. Allocations are not allowed in the
2522 * callback function, you therefore cannot manipulate objects (set
2523 * or delete properties for example) since it is possible such
2524 * operations will result in the allocation of objects. It is possible
2525 * to specify the GCType filter for your callback. But it is not possible to
2526 * register the same callback function two times with different
2527 * GCType filters.
2528 */
2529 static void AddGCPrologueCallback(
2530 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2531
2532 /**
2533 * This function removes callback which was installed by
2534 * AddGCPrologueCallback function.
2535 */
2536 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2537
2538 /**
2539 * The function is deprecated. Please use AddGCPrologueCallback instead.
2540 * Enables the host application to receive a notification before a
2541 * garbage collection. Allocations are not allowed in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002542 * callback function, you therefore cannot manipulate objects (set
2543 * or delete properties for example) since it is possible such
2544 * operations will result in the allocation of objects.
2545 */
2546 static void SetGlobalGCPrologueCallback(GCCallback);
2547
2548 /**
2549 * Enables the host application to receive a notification after a
Steve Block6ded16b2010-05-10 14:33:55 +01002550 * garbage collection. Allocations are not allowed in the
2551 * callback function, you therefore cannot manipulate objects (set
2552 * or delete properties for example) since it is possible such
2553 * operations will result in the allocation of objects. It is possible
2554 * to specify the GCType filter for your callback. But it is not possible to
2555 * register the same callback function two times with different
2556 * GCType filters.
2557 */
2558 static void AddGCEpilogueCallback(
2559 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2560
2561 /**
2562 * This function removes callback which was installed by
2563 * AddGCEpilogueCallback function.
2564 */
2565 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2566
2567 /**
2568 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2569 * Enables the host application to receive a notification after a
Steve Blocka7e24c12009-10-30 11:49:00 +00002570 * major garbage collection. Allocations are not allowed in the
2571 * callback function, you therefore cannot manipulate objects (set
2572 * or delete properties for example) since it is possible such
2573 * operations will result in the allocation of objects.
2574 */
2575 static void SetGlobalGCEpilogueCallback(GCCallback);
2576
2577 /**
2578 * Allows the host application to group objects together. If one
2579 * object in the group is alive, all objects in the group are alive.
2580 * After each garbage collection, object groups are removed. It is
2581 * intended to be used in the before-garbage-collection callback
2582 * function, for instance to simulate DOM tree connections among JS
2583 * wrapper objects.
2584 */
2585 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
2586
2587 /**
2588 * Initializes from snapshot if possible. Otherwise, attempts to
2589 * initialize from scratch. This function is called implicitly if
2590 * you use the API without calling it first.
2591 */
2592 static bool Initialize();
2593
2594 /**
2595 * Adjusts the amount of registered external memory. Used to give
2596 * V8 an indication of the amount of externally allocated memory
2597 * that is kept alive by JavaScript objects. V8 uses this to decide
2598 * when to perform global garbage collections. Registering
2599 * externally allocated memory will trigger global garbage
2600 * collections more often than otherwise in an attempt to garbage
2601 * collect the JavaScript objects keeping the externally allocated
2602 * memory alive.
2603 *
2604 * \param change_in_bytes the change in externally allocated memory
2605 * that is kept alive by JavaScript objects.
2606 * \returns the adjusted value.
2607 */
2608 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2609
2610 /**
2611 * Suspends recording of tick samples in the profiler.
2612 * When the V8 profiling mode is enabled (usually via command line
2613 * switches) this function suspends recording of tick samples.
2614 * Profiling ticks are discarded until ResumeProfiler() is called.
2615 *
2616 * See also the --prof and --prof_auto command line switches to
2617 * enable V8 profiling.
2618 */
2619 static void PauseProfiler();
2620
2621 /**
2622 * Resumes recording of tick samples in the profiler.
2623 * See also PauseProfiler().
2624 */
2625 static void ResumeProfiler();
2626
2627 /**
2628 * Return whether profiler is currently paused.
2629 */
2630 static bool IsProfilerPaused();
2631
2632 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002633 * Resumes specified profiler modules. Can be called several times to
2634 * mark the opening of a profiler events block with the given tag.
2635 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002636 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2637 * See ProfilerModules enum.
2638 *
2639 * \param flags Flags specifying profiler modules.
Andrei Popescu402d9372010-02-26 13:31:12 +00002640 * \param tag Profile tag.
Steve Blocka7e24c12009-10-30 11:49:00 +00002641 */
Andrei Popescu402d9372010-02-26 13:31:12 +00002642 static void ResumeProfilerEx(int flags, int tag = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002643
2644 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002645 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2646 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2647 * same tag value. There is no need for blocks to be properly nested.
2648 * The profiler is paused when the last opened block is closed.
2649 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002650 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2651 * See ProfilerModules enum.
2652 *
2653 * \param flags Flags specifying profiler modules.
Andrei Popescu402d9372010-02-26 13:31:12 +00002654 * \param tag Profile tag.
Steve Blocka7e24c12009-10-30 11:49:00 +00002655 */
Andrei Popescu402d9372010-02-26 13:31:12 +00002656 static void PauseProfilerEx(int flags, int tag = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002657
2658 /**
2659 * Returns active (resumed) profiler modules.
2660 * See ProfilerModules enum.
2661 *
2662 * \returns active profiler modules.
2663 */
2664 static int GetActiveProfilerModules();
2665
2666 /**
2667 * If logging is performed into a memory buffer (via --logfile=*), allows to
2668 * retrieve previously written messages. This can be used for retrieving
2669 * profiler log data in the application. This function is thread-safe.
2670 *
2671 * Caller provides a destination buffer that must exist during GetLogLines
2672 * call. Only whole log lines are copied into the buffer.
2673 *
2674 * \param from_pos specified a point in a buffer to read from, 0 is the
2675 * beginning of a buffer. It is assumed that caller updates its current
2676 * position using returned size value from the previous call.
2677 * \param dest_buf destination buffer for log data.
2678 * \param max_size size of the destination buffer.
2679 * \returns actual size of log data copied into buffer.
2680 */
2681 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2682
2683 /**
Steve Block6ded16b2010-05-10 14:33:55 +01002684 * The minimum allowed size for a log lines buffer. If the size of
2685 * the buffer given will not be enough to hold a line of the maximum
2686 * length, an attempt to find a log line end in GetLogLines will
2687 * fail, and an empty result will be returned.
2688 */
2689 static const int kMinimumSizeForLogLinesBuffer = 2048;
2690
2691 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002692 * Retrieve the V8 thread id of the calling thread.
2693 *
2694 * The thread id for a thread should only be retrieved after the V8
2695 * lock has been acquired with a Locker object with that thread.
2696 */
2697 static int GetCurrentThreadId();
2698
2699 /**
2700 * Forcefully terminate execution of a JavaScript thread. This can
2701 * be used to terminate long-running scripts.
2702 *
2703 * TerminateExecution should only be called when then V8 lock has
2704 * been acquired with a Locker object. Therefore, in order to be
2705 * able to terminate long-running threads, preemption must be
2706 * enabled to allow the user of TerminateExecution to acquire the
2707 * lock.
2708 *
2709 * The termination is achieved by throwing an exception that is
2710 * uncatchable by JavaScript exception handlers. Termination
2711 * exceptions act as if they were caught by a C++ TryCatch exception
2712 * handlers. If forceful termination is used, any C++ TryCatch
2713 * exception handler that catches an exception should check if that
2714 * exception is a termination exception and immediately return if
2715 * that is the case. Returning immediately in that case will
2716 * continue the propagation of the termination exception if needed.
2717 *
2718 * The thread id passed to TerminateExecution must have been
2719 * obtained by calling GetCurrentThreadId on the thread in question.
2720 *
2721 * \param thread_id The thread id of the thread to terminate.
2722 */
2723 static void TerminateExecution(int thread_id);
2724
2725 /**
2726 * Forcefully terminate the current thread of JavaScript execution.
2727 *
2728 * This method can be used by any thread even if that thread has not
2729 * acquired the V8 lock with a Locker object.
2730 */
2731 static void TerminateExecution();
2732
2733 /**
Steve Block6ded16b2010-05-10 14:33:55 +01002734 * Is V8 terminating JavaScript execution.
2735 *
2736 * Returns true if JavaScript execution is currently terminating
2737 * because of a call to TerminateExecution. In that case there are
2738 * still JavaScript frames on the stack and the termination
2739 * exception is still active.
2740 */
2741 static bool IsExecutionTerminating();
2742
2743 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002744 * Releases any resources used by v8 and stops any utility threads
2745 * that may be running. Note that disposing v8 is permanent, it
2746 * cannot be reinitialized.
2747 *
2748 * It should generally not be necessary to dispose v8 before exiting
2749 * a process, this should happen automatically. It is only necessary
2750 * to use if the process needs the resources taken up by v8.
2751 */
2752 static bool Dispose();
2753
Steve Block3ce2e202009-11-05 08:53:23 +00002754 /**
2755 * Get statistics about the heap memory usage.
2756 */
2757 static void GetHeapStatistics(HeapStatistics* heap_statistics);
Steve Blocka7e24c12009-10-30 11:49:00 +00002758
2759 /**
2760 * Optional notification that the embedder is idle.
2761 * V8 uses the notification to reduce memory footprint.
2762 * This call can be used repeatedly if the embedder remains idle.
Steve Blocka7e24c12009-10-30 11:49:00 +00002763 * Returns true if the embedder should stop calling IdleNotification
2764 * until real work has been done. This indicates that V8 has done
2765 * as much cleanup as it will be able to do.
2766 */
Steve Block3ce2e202009-11-05 08:53:23 +00002767 static bool IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +00002768
2769 /**
2770 * Optional notification that the system is running low on memory.
2771 * V8 uses these notifications to attempt to free memory.
2772 */
2773 static void LowMemoryNotification();
2774
Steve Block6ded16b2010-05-10 14:33:55 +01002775 /**
2776 * Optional notification that a context has been disposed. V8 uses
2777 * these notifications to guide the GC heuristic. Returns the number
2778 * of context disposals - including this one - since the last time
2779 * V8 had a chance to clean up.
2780 */
2781 static int ContextDisposedNotification();
2782
Steve Blocka7e24c12009-10-30 11:49:00 +00002783 private:
2784 V8();
2785
2786 static internal::Object** GlobalizeReference(internal::Object** handle);
2787 static void DisposeGlobal(internal::Object** global_handle);
2788 static void MakeWeak(internal::Object** global_handle,
2789 void* data,
2790 WeakReferenceCallback);
2791 static void ClearWeak(internal::Object** global_handle);
2792 static bool IsGlobalNearDeath(internal::Object** global_handle);
2793 static bool IsGlobalWeak(internal::Object** global_handle);
2794
2795 template <class T> friend class Handle;
2796 template <class T> friend class Local;
2797 template <class T> friend class Persistent;
2798 friend class Context;
2799};
2800
2801
2802/**
2803 * An external exception handler.
2804 */
2805class V8EXPORT TryCatch {
2806 public:
2807
2808 /**
2809 * Creates a new try/catch block and registers it with v8.
2810 */
2811 TryCatch();
2812
2813 /**
2814 * Unregisters and deletes this try/catch block.
2815 */
2816 ~TryCatch();
2817
2818 /**
2819 * Returns true if an exception has been caught by this try/catch block.
2820 */
2821 bool HasCaught() const;
2822
2823 /**
2824 * For certain types of exceptions, it makes no sense to continue
2825 * execution.
2826 *
2827 * Currently, the only type of exception that can be caught by a
2828 * TryCatch handler and for which it does not make sense to continue
2829 * is termination exception. Such exceptions are thrown when the
2830 * TerminateExecution methods are called to terminate a long-running
2831 * script.
2832 *
2833 * If CanContinue returns false, the correct action is to perform
2834 * any C++ cleanup needed and then return.
2835 */
2836 bool CanContinue() const;
2837
2838 /**
Steve Blockd0582a62009-12-15 09:54:21 +00002839 * Throws the exception caught by this TryCatch in a way that avoids
2840 * it being caught again by this same TryCatch. As with ThrowException
2841 * it is illegal to execute any JavaScript operations after calling
2842 * ReThrow; the caller must return immediately to where the exception
2843 * is caught.
2844 */
2845 Handle<Value> ReThrow();
2846
2847 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002848 * Returns the exception caught by this try/catch block. If no exception has
2849 * been caught an empty handle is returned.
2850 *
2851 * The returned handle is valid until this TryCatch block has been destroyed.
2852 */
2853 Local<Value> Exception() const;
2854
2855 /**
2856 * Returns the .stack property of the thrown object. If no .stack
2857 * property is present an empty handle is returned.
2858 */
2859 Local<Value> StackTrace() const;
2860
2861 /**
2862 * Returns the message associated with this exception. If there is
2863 * no message associated an empty handle is returned.
2864 *
2865 * The returned handle is valid until this TryCatch block has been
2866 * destroyed.
2867 */
2868 Local<v8::Message> Message() const;
2869
2870 /**
2871 * Clears any exceptions that may have been caught by this try/catch block.
2872 * After this method has been called, HasCaught() will return false.
2873 *
2874 * It is not necessary to clear a try/catch block before using it again; if
2875 * another exception is thrown the previously caught exception will just be
2876 * overwritten. However, it is often a good idea since it makes it easier
2877 * to determine which operation threw a given exception.
2878 */
2879 void Reset();
2880
2881 /**
2882 * Set verbosity of the external exception handler.
2883 *
2884 * By default, exceptions that are caught by an external exception
2885 * handler are not reported. Call SetVerbose with true on an
2886 * external exception handler to have exceptions caught by the
2887 * handler reported as if they were not caught.
2888 */
2889 void SetVerbose(bool value);
2890
2891 /**
2892 * Set whether or not this TryCatch should capture a Message object
2893 * which holds source information about where the exception
2894 * occurred. True by default.
2895 */
2896 void SetCaptureMessage(bool value);
2897
Steve Blockd0582a62009-12-15 09:54:21 +00002898 private:
2899 void* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +00002900 void* exception_;
2901 void* message_;
Steve Blockd0582a62009-12-15 09:54:21 +00002902 bool is_verbose_ : 1;
2903 bool can_continue_ : 1;
2904 bool capture_message_ : 1;
2905 bool rethrow_ : 1;
2906
2907 friend class v8::internal::Top;
Steve Blocka7e24c12009-10-30 11:49:00 +00002908};
2909
2910
2911// --- C o n t e x t ---
2912
2913
2914/**
2915 * Ignore
2916 */
2917class V8EXPORT ExtensionConfiguration {
2918 public:
2919 ExtensionConfiguration(int name_count, const char* names[])
2920 : name_count_(name_count), names_(names) { }
2921 private:
2922 friend class ImplementationUtilities;
2923 int name_count_;
2924 const char** names_;
2925};
2926
2927
2928/**
2929 * A sandboxed execution context with its own set of built-in objects
2930 * and functions.
2931 */
2932class V8EXPORT Context {
2933 public:
2934 /** Returns the global object of the context. */
2935 Local<Object> Global();
2936
2937 /**
2938 * Detaches the global object from its context before
2939 * the global object can be reused to create a new context.
2940 */
2941 void DetachGlobal();
2942
Andrei Popescu74b3c142010-03-29 12:03:09 +01002943 /**
2944 * Reattaches a global object to a context. This can be used to
2945 * restore the connection between a global object and a context
2946 * after DetachGlobal has been called.
2947 *
2948 * \param global_object The global object to reattach to the
2949 * context. For this to work, the global object must be the global
2950 * object that was associated with this context before a call to
2951 * DetachGlobal.
2952 */
2953 void ReattachGlobal(Handle<Object> global_object);
2954
Leon Clarkef7060e22010-06-03 12:02:55 +01002955 /** Creates a new context.
2956 *
2957 * Returns a persistent handle to the newly allocated context. This
2958 * persistent handle has to be disposed when the context is no
2959 * longer used so the context can be garbage collected.
2960 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002961 static Persistent<Context> New(
Andrei Popescu31002712010-02-23 13:46:05 +00002962 ExtensionConfiguration* extensions = NULL,
Steve Blocka7e24c12009-10-30 11:49:00 +00002963 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2964 Handle<Value> global_object = Handle<Value>());
2965
2966 /** Returns the last entered context. */
2967 static Local<Context> GetEntered();
2968
2969 /** Returns the context that is on the top of the stack. */
2970 static Local<Context> GetCurrent();
2971
2972 /**
2973 * Returns the context of the calling JavaScript code. That is the
2974 * context of the top-most JavaScript frame. If there are no
2975 * JavaScript frames an empty handle is returned.
2976 */
2977 static Local<Context> GetCalling();
2978
2979 /**
2980 * Sets the security token for the context. To access an object in
2981 * another context, the security tokens must match.
2982 */
2983 void SetSecurityToken(Handle<Value> token);
2984
2985 /** Restores the security token to the default value. */
2986 void UseDefaultSecurityToken();
2987
2988 /** Returns the security token of this context.*/
2989 Handle<Value> GetSecurityToken();
2990
2991 /**
2992 * Enter this context. After entering a context, all code compiled
2993 * and run is compiled and run in this context. If another context
2994 * is already entered, this old context is saved so it can be
2995 * restored when the new context is exited.
2996 */
2997 void Enter();
2998
2999 /**
3000 * Exit this context. Exiting the current context restores the
3001 * context that was in place when entering the current context.
3002 */
3003 void Exit();
3004
3005 /** Returns true if the context has experienced an out of memory situation. */
3006 bool HasOutOfMemoryException();
3007
3008 /** Returns true if V8 has a current context. */
3009 static bool InContext();
3010
3011 /**
3012 * Associate an additional data object with the context. This is mainly used
3013 * with the debugger to provide additional information on the context through
3014 * the debugger API.
3015 */
Steve Blockd0582a62009-12-15 09:54:21 +00003016 void SetData(Handle<String> data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003017 Local<Value> GetData();
3018
3019 /**
3020 * Stack-allocated class which sets the execution context for all
3021 * operations executed within a local scope.
3022 */
Steve Block8defd9f2010-07-08 12:39:36 +01003023 class Scope {
Steve Blocka7e24c12009-10-30 11:49:00 +00003024 public:
3025 inline Scope(Handle<Context> context) : context_(context) {
3026 context_->Enter();
3027 }
3028 inline ~Scope() { context_->Exit(); }
3029 private:
3030 Handle<Context> context_;
3031 };
3032
3033 private:
3034 friend class Value;
3035 friend class Script;
3036 friend class Object;
3037 friend class Function;
3038};
3039
3040
3041/**
3042 * Multiple threads in V8 are allowed, but only one thread at a time
3043 * is allowed to use V8. The definition of 'using V8' includes
3044 * accessing handles or holding onto object pointers obtained from V8
3045 * handles. It is up to the user of V8 to ensure (perhaps with
3046 * locking) that this constraint is not violated.
3047 *
3048 * If you wish to start using V8 in a thread you can do this by constructing
3049 * a v8::Locker object. After the code using V8 has completed for the
3050 * current thread you can call the destructor. This can be combined
3051 * with C++ scope-based construction as follows:
3052 *
3053 * \code
3054 * ...
3055 * {
3056 * v8::Locker locker;
3057 * ...
3058 * // Code using V8 goes here.
3059 * ...
3060 * } // Destructor called here
3061 * \endcode
3062 *
3063 * If you wish to stop using V8 in a thread A you can do this by either
3064 * by destroying the v8::Locker object as above or by constructing a
3065 * v8::Unlocker object:
3066 *
3067 * \code
3068 * {
3069 * v8::Unlocker unlocker;
3070 * ...
3071 * // Code not using V8 goes here while V8 can run in another thread.
3072 * ...
3073 * } // Destructor called here.
3074 * \endcode
3075 *
3076 * The Unlocker object is intended for use in a long-running callback
3077 * from V8, where you want to release the V8 lock for other threads to
3078 * use.
3079 *
3080 * The v8::Locker is a recursive lock. That is, you can lock more than
3081 * once in a given thread. This can be useful if you have code that can
3082 * be called either from code that holds the lock or from code that does
3083 * not. The Unlocker is not recursive so you can not have several
3084 * Unlockers on the stack at once, and you can not use an Unlocker in a
3085 * thread that is not inside a Locker's scope.
3086 *
3087 * An unlocker will unlock several lockers if it has to and reinstate
3088 * the correct depth of locking on its destruction. eg.:
3089 *
3090 * \code
3091 * // V8 not locked.
3092 * {
3093 * v8::Locker locker;
3094 * // V8 locked.
3095 * {
3096 * v8::Locker another_locker;
3097 * // V8 still locked (2 levels).
3098 * {
3099 * v8::Unlocker unlocker;
3100 * // V8 not locked.
3101 * }
3102 * // V8 locked again (2 levels).
3103 * }
3104 * // V8 still locked (1 level).
3105 * }
3106 * // V8 Now no longer locked.
3107 * \endcode
3108 */
3109class V8EXPORT Unlocker {
3110 public:
3111 Unlocker();
3112 ~Unlocker();
3113};
3114
3115
3116class V8EXPORT Locker {
3117 public:
3118 Locker();
3119 ~Locker();
3120
3121 /**
3122 * Start preemption.
3123 *
3124 * When preemption is started, a timer is fired every n milli seconds
3125 * that will switch between multiple threads that are in contention
3126 * for the V8 lock.
3127 */
3128 static void StartPreemption(int every_n_ms);
3129
3130 /**
3131 * Stop preemption.
3132 */
3133 static void StopPreemption();
3134
3135 /**
3136 * Returns whether or not the locker is locked by the current thread.
3137 */
3138 static bool IsLocked();
3139
3140 /**
3141 * Returns whether v8::Locker is being used by this V8 instance.
3142 */
3143 static bool IsActive() { return active_; }
3144
3145 private:
3146 bool has_lock_;
3147 bool top_level_;
3148
3149 static bool active_;
3150
3151 // Disallow copying and assigning.
3152 Locker(const Locker&);
3153 void operator=(const Locker&);
3154};
3155
3156
3157
3158// --- I m p l e m e n t a t i o n ---
3159
3160
3161namespace internal {
3162
3163
3164// Tag information for HeapObject.
3165const int kHeapObjectTag = 1;
3166const int kHeapObjectTagSize = 2;
3167const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3168
Steve Blocka7e24c12009-10-30 11:49:00 +00003169// Tag information for Smi.
3170const int kSmiTag = 0;
3171const int kSmiTagSize = 1;
3172const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3173
Steve Block3ce2e202009-11-05 08:53:23 +00003174template <size_t ptr_size> struct SmiConstants;
3175
3176// Smi constants for 32-bit systems.
3177template <> struct SmiConstants<4> {
3178 static const int kSmiShiftSize = 0;
3179 static const int kSmiValueSize = 31;
3180 static inline int SmiToInt(internal::Object* value) {
3181 int shift_bits = kSmiTagSize + kSmiShiftSize;
3182 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3183 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3184 }
3185};
3186
3187// Smi constants for 64-bit systems.
3188template <> struct SmiConstants<8> {
3189 static const int kSmiShiftSize = 31;
3190 static const int kSmiValueSize = 32;
3191 static inline int SmiToInt(internal::Object* value) {
3192 int shift_bits = kSmiTagSize + kSmiShiftSize;
3193 // Shift down and throw away top 32 bits.
3194 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3195 }
3196};
3197
3198const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
3199const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003200
Steve Blockd0582a62009-12-15 09:54:21 +00003201template <size_t ptr_size> struct InternalConstants;
3202
3203// Internal constants for 32-bit systems.
3204template <> struct InternalConstants<4> {
3205 static const int kStringResourceOffset = 3 * sizeof(void*);
3206};
3207
3208// Internal constants for 64-bit systems.
3209template <> struct InternalConstants<8> {
Steve Block6ded16b2010-05-10 14:33:55 +01003210 static const int kStringResourceOffset = 3 * sizeof(void*);
Steve Blockd0582a62009-12-15 09:54:21 +00003211};
3212
Steve Blocka7e24c12009-10-30 11:49:00 +00003213/**
3214 * This class exports constants and functionality from within v8 that
3215 * is necessary to implement inline functions in the v8 api. Don't
3216 * depend on functions and constants defined here.
3217 */
3218class Internals {
3219 public:
3220
3221 // These values match non-compiler-dependent values defined within
3222 // the implementation of v8.
3223 static const int kHeapObjectMapOffset = 0;
3224 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
Steve Blockd0582a62009-12-15 09:54:21 +00003225 static const int kStringResourceOffset =
3226 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3227
Steve Blocka7e24c12009-10-30 11:49:00 +00003228 static const int kProxyProxyOffset = sizeof(void*);
3229 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3230 static const int kFullStringRepresentationMask = 0x07;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003231 static const int kExternalTwoByteRepresentationTag = 0x02;
Steve Blocka7e24c12009-10-30 11:49:00 +00003232
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003233 static const int kJSObjectType = 0x9f;
3234 static const int kFirstNonstringType = 0x80;
3235 static const int kProxyType = 0x85;
Steve Blocka7e24c12009-10-30 11:49:00 +00003236
3237 static inline bool HasHeapObjectTag(internal::Object* value) {
3238 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3239 kHeapObjectTag);
3240 }
3241
3242 static inline bool HasSmiTag(internal::Object* value) {
3243 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3244 }
3245
3246 static inline int SmiValue(internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00003247 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3248 }
3249
3250 static inline int GetInstanceType(internal::Object* obj) {
3251 typedef internal::Object O;
3252 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3253 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3254 }
3255
3256 static inline void* GetExternalPointer(internal::Object* obj) {
3257 if (HasSmiTag(obj)) {
3258 return obj;
3259 } else if (GetInstanceType(obj) == kProxyType) {
3260 return ReadField<void*>(obj, kProxyProxyOffset);
3261 } else {
3262 return NULL;
3263 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003264 }
3265
3266 static inline bool IsExternalTwoByteString(int instance_type) {
3267 int representation = (instance_type & kFullStringRepresentationMask);
3268 return representation == kExternalTwoByteRepresentationTag;
3269 }
3270
3271 template <typename T>
3272 static inline T ReadField(Object* ptr, int offset) {
3273 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3274 return *reinterpret_cast<T*>(addr);
3275 }
3276
3277};
3278
3279}
3280
3281
3282template <class T>
3283Handle<T>::Handle() : val_(0) { }
3284
3285
3286template <class T>
3287Local<T>::Local() : Handle<T>() { }
3288
3289
3290template <class T>
3291Local<T> Local<T>::New(Handle<T> that) {
3292 if (that.IsEmpty()) return Local<T>();
3293 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
3294 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3295}
3296
3297
3298template <class T>
3299Persistent<T> Persistent<T>::New(Handle<T> that) {
3300 if (that.IsEmpty()) return Persistent<T>();
3301 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
3302 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3303}
3304
3305
3306template <class T>
3307bool Persistent<T>::IsNearDeath() const {
3308 if (this->IsEmpty()) return false;
3309 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
3310}
3311
3312
3313template <class T>
3314bool Persistent<T>::IsWeak() const {
3315 if (this->IsEmpty()) return false;
3316 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
3317}
3318
3319
3320template <class T>
3321void Persistent<T>::Dispose() {
3322 if (this->IsEmpty()) return;
3323 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
3324}
3325
3326
3327template <class T>
3328Persistent<T>::Persistent() : Handle<T>() { }
3329
3330template <class T>
3331void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
3332 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3333 parameters,
3334 callback);
3335}
3336
3337template <class T>
3338void Persistent<T>::ClearWeak() {
3339 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
3340}
3341
Steve Block8defd9f2010-07-08 12:39:36 +01003342
3343Arguments::Arguments(v8::Local<v8::Value> data,
3344 v8::Local<v8::Object> holder,
3345 v8::Local<v8::Function> callee,
3346 bool is_construct_call,
3347 void** values, int length)
3348 : data_(data), holder_(holder), callee_(callee),
3349 is_construct_call_(is_construct_call),
3350 values_(values), length_(length) { }
3351
3352
Steve Blocka7e24c12009-10-30 11:49:00 +00003353Local<Value> Arguments::operator[](int i) const {
3354 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3355 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3356}
3357
3358
3359Local<Function> Arguments::Callee() const {
3360 return callee_;
3361}
3362
3363
3364Local<Object> Arguments::This() const {
3365 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3366}
3367
3368
3369Local<Object> Arguments::Holder() const {
3370 return holder_;
3371}
3372
3373
3374Local<Value> Arguments::Data() const {
3375 return data_;
3376}
3377
3378
3379bool Arguments::IsConstructCall() const {
3380 return is_construct_call_;
3381}
3382
3383
3384int Arguments::Length() const {
3385 return length_;
3386}
3387
3388
3389template <class T>
3390Local<T> HandleScope::Close(Handle<T> value) {
3391 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3392 internal::Object** after = RawClose(before);
3393 return Local<T>(reinterpret_cast<T*>(after));
3394}
3395
3396Handle<Value> ScriptOrigin::ResourceName() const {
3397 return resource_name_;
3398}
3399
3400
3401Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
3402 return resource_line_offset_;
3403}
3404
3405
3406Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
3407 return resource_column_offset_;
3408}
3409
3410
3411Handle<Boolean> Boolean::New(bool value) {
3412 return value ? True() : False();
3413}
3414
3415
3416void Template::Set(const char* name, v8::Handle<Data> value) {
3417 Set(v8::String::New(name), value);
3418}
3419
3420
3421Local<Value> Object::GetInternalField(int index) {
3422#ifndef V8_ENABLE_CHECKS
3423 Local<Value> quick_result = UncheckedGetInternalField(index);
3424 if (!quick_result.IsEmpty()) return quick_result;
3425#endif
3426 return CheckedGetInternalField(index);
3427}
3428
3429
3430Local<Value> Object::UncheckedGetInternalField(int index) {
3431 typedef internal::Object O;
3432 typedef internal::Internals I;
3433 O* obj = *reinterpret_cast<O**>(this);
Steve Block3ce2e202009-11-05 08:53:23 +00003434 if (I::GetInstanceType(obj) == I::kJSObjectType) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003435 // If the object is a plain JSObject, which is the common case,
3436 // we know where to find the internal fields and can return the
3437 // value directly.
3438 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3439 O* value = I::ReadField<O*>(obj, offset);
3440 O** result = HandleScope::CreateHandle(value);
3441 return Local<Value>(reinterpret_cast<Value*>(result));
3442 } else {
3443 return Local<Value>();
3444 }
3445}
3446
3447
3448void* External::Unwrap(Handle<v8::Value> obj) {
3449#ifdef V8_ENABLE_CHECKS
3450 return FullUnwrap(obj);
3451#else
3452 return QuickUnwrap(obj);
3453#endif
3454}
3455
3456
3457void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3458 typedef internal::Object O;
Steve Blocka7e24c12009-10-30 11:49:00 +00003459 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
Steve Block3ce2e202009-11-05 08:53:23 +00003460 return internal::Internals::GetExternalPointer(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003461}
3462
3463
3464void* Object::GetPointerFromInternalField(int index) {
Steve Block3ce2e202009-11-05 08:53:23 +00003465 typedef internal::Object O;
3466 typedef internal::Internals I;
3467
3468 O* obj = *reinterpret_cast<O**>(this);
3469
3470 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3471 // If the object is a plain JSObject, which is the common case,
3472 // we know where to find the internal fields and can return the
3473 // value directly.
3474 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3475 O* value = I::ReadField<O*>(obj, offset);
3476 return I::GetExternalPointer(value);
3477 }
3478
3479 return SlowGetPointerFromInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003480}
3481
3482
3483String* String::Cast(v8::Value* value) {
3484#ifdef V8_ENABLE_CHECKS
3485 CheckCast(value);
3486#endif
3487 return static_cast<String*>(value);
3488}
3489
3490
3491String::ExternalStringResource* String::GetExternalStringResource() const {
3492 typedef internal::Object O;
3493 typedef internal::Internals I;
3494 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00003495 String::ExternalStringResource* result;
Steve Block3ce2e202009-11-05 08:53:23 +00003496 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003497 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3498 result = reinterpret_cast<String::ExternalStringResource*>(value);
3499 } else {
3500 result = NULL;
3501 }
3502#ifdef V8_ENABLE_CHECKS
3503 VerifyExternalStringResource(result);
3504#endif
3505 return result;
3506}
3507
3508
3509bool Value::IsString() const {
3510#ifdef V8_ENABLE_CHECKS
3511 return FullIsString();
3512#else
3513 return QuickIsString();
3514#endif
3515}
3516
3517bool Value::QuickIsString() const {
3518 typedef internal::Object O;
3519 typedef internal::Internals I;
3520 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3521 if (!I::HasHeapObjectTag(obj)) return false;
Steve Block3ce2e202009-11-05 08:53:23 +00003522 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
Steve Blocka7e24c12009-10-30 11:49:00 +00003523}
3524
3525
3526Number* Number::Cast(v8::Value* value) {
3527#ifdef V8_ENABLE_CHECKS
3528 CheckCast(value);
3529#endif
3530 return static_cast<Number*>(value);
3531}
3532
3533
3534Integer* Integer::Cast(v8::Value* value) {
3535#ifdef V8_ENABLE_CHECKS
3536 CheckCast(value);
3537#endif
3538 return static_cast<Integer*>(value);
3539}
3540
3541
3542Date* Date::Cast(v8::Value* value) {
3543#ifdef V8_ENABLE_CHECKS
3544 CheckCast(value);
3545#endif
3546 return static_cast<Date*>(value);
3547}
3548
3549
3550Object* Object::Cast(v8::Value* value) {
3551#ifdef V8_ENABLE_CHECKS
3552 CheckCast(value);
3553#endif
3554 return static_cast<Object*>(value);
3555}
3556
3557
3558Array* Array::Cast(v8::Value* value) {
3559#ifdef V8_ENABLE_CHECKS
3560 CheckCast(value);
3561#endif
3562 return static_cast<Array*>(value);
3563}
3564
3565
3566Function* Function::Cast(v8::Value* value) {
3567#ifdef V8_ENABLE_CHECKS
3568 CheckCast(value);
3569#endif
3570 return static_cast<Function*>(value);
3571}
3572
3573
3574External* External::Cast(v8::Value* value) {
3575#ifdef V8_ENABLE_CHECKS
3576 CheckCast(value);
3577#endif
3578 return static_cast<External*>(value);
3579}
3580
3581
3582Local<Value> AccessorInfo::Data() const {
Steve Block6ded16b2010-05-10 14:33:55 +01003583 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
Steve Blocka7e24c12009-10-30 11:49:00 +00003584}
3585
3586
3587Local<Object> AccessorInfo::This() const {
3588 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3589}
3590
3591
3592Local<Object> AccessorInfo::Holder() const {
3593 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3594}
3595
3596
3597/**
3598 * \example shell.cc
3599 * A simple shell that takes a list of expressions on the
3600 * command-line and executes them.
3601 */
3602
3603
3604/**
3605 * \example process.cc
3606 */
3607
3608
3609} // namespace v8
3610
3611
3612#undef V8EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +00003613#undef TYPE_CHECK
3614
3615
3616#endif // V8_H_