blob: 9e4cebb73a175a1893a68778d0de19996080c0d8 [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 *
140 * \param object the weak global object to be reclaimed by the garbage collector
141 * \param parameter the value passed in when making the weak global object
142 */
143typedef void (*WeakReferenceCallback)(Persistent<Value> object,
144 void* parameter);
145
146
147// --- H a n d l e s ---
148
149#define TYPE_CHECK(T, S) \
150 while (false) { \
151 *(static_cast<T**>(0)) = static_cast<S*>(0); \
152 }
153
154/**
155 * An object reference managed by the v8 garbage collector.
156 *
157 * All objects returned from v8 have to be tracked by the garbage
158 * collector so that it knows that the objects are still alive. Also,
159 * because the garbage collector may move objects, it is unsafe to
160 * point directly to an object. Instead, all objects are stored in
161 * handles which are known by the garbage collector and updated
162 * whenever an object moves. Handles should always be passed by value
163 * (except in cases like out-parameters) and they should never be
164 * allocated on the heap.
165 *
166 * There are two types of handles: local and persistent handles.
167 * Local handles are light-weight and transient and typically used in
168 * local operations. They are managed by HandleScopes. Persistent
169 * handles can be used when storing objects across several independent
170 * operations and have to be explicitly deallocated when they're no
171 * longer used.
172 *
173 * It is safe to extract the object stored in the handle by
174 * dereferencing the handle (for instance, to extract the Object* from
175 * an Handle<Object>); the value will still be governed by a handle
176 * behind the scenes and the same rules apply to these values as to
177 * their handles.
178 */
Steve Block8defd9f2010-07-08 12:39:36 +0100179template <class T> class Handle {
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 public:
181
182 /**
183 * Creates an empty handle.
184 */
185 inline Handle();
186
187 /**
188 * Creates a new handle for the specified value.
189 */
Steve Block8defd9f2010-07-08 12:39:36 +0100190 inline explicit Handle(T* val) : val_(val) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000191
192 /**
193 * Creates a handle for the contents of the specified handle. This
194 * constructor allows you to pass handles as arguments by value and
195 * to assign between handles. However, if you try to assign between
196 * incompatible handles, for instance from a Handle<String> to a
197 * Handle<Number> it will cause a compiletime error. Assigning
198 * between compatible handles, for instance assigning a
199 * Handle<String> to a variable declared as Handle<Value>, is legal
200 * because String is a subclass of Value.
201 */
202 template <class S> inline Handle(Handle<S> that)
203 : val_(reinterpret_cast<T*>(*that)) {
204 /**
205 * This check fails when trying to convert between incompatible
206 * handles. For example, converting from a Handle<String> to a
207 * Handle<Number>.
208 */
209 TYPE_CHECK(T, S);
210 }
211
212 /**
213 * Returns true if the handle is empty.
214 */
Steve Block8defd9f2010-07-08 12:39:36 +0100215 inline bool IsEmpty() const { return val_ == 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000216
Steve Block8defd9f2010-07-08 12:39:36 +0100217 inline T* operator->() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000218
Steve Block8defd9f2010-07-08 12:39:36 +0100219 inline T* operator*() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000220
221 /**
222 * Sets the handle to be empty. IsEmpty() will then return true.
223 */
Steve Block8defd9f2010-07-08 12:39:36 +0100224 inline void Clear() { this->val_ = 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000225
226 /**
227 * Checks whether two handles are the same.
228 * Returns true if both are empty, or if the objects
229 * to which they refer are identical.
230 * The handles' references are not checked.
231 */
Steve Block8defd9f2010-07-08 12:39:36 +0100232 template <class S> inline bool operator==(Handle<S> that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
234 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
235 if (a == 0) return b == 0;
236 if (b == 0) return false;
237 return *a == *b;
238 }
239
240 /**
241 * Checks whether two handles are different.
242 * Returns true if only one of the handles is empty, or if
243 * the objects to which they refer are different.
244 * The handles' references are not checked.
245 */
Steve Block8defd9f2010-07-08 12:39:36 +0100246 template <class S> inline bool operator!=(Handle<S> that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000247 return !operator==(that);
248 }
249
250 template <class S> static inline Handle<T> Cast(Handle<S> that) {
251#ifdef V8_ENABLE_CHECKS
252 // If we're going to perform the type check then we have to check
253 // that the handle isn't empty before doing the checked cast.
254 if (that.IsEmpty()) return Handle<T>();
255#endif
256 return Handle<T>(T::Cast(*that));
257 }
258
Steve Block6ded16b2010-05-10 14:33:55 +0100259 template <class S> inline Handle<S> As() {
260 return Handle<S>::Cast(*this);
261 }
262
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 private:
264 T* val_;
265};
266
267
268/**
269 * A light-weight stack-allocated object handle. All operations
270 * that return objects from within v8 return them in local handles. They
271 * are created within HandleScopes, and all local handles allocated within a
272 * handle scope are destroyed when the handle scope is destroyed. Hence it
273 * is not necessary to explicitly deallocate local handles.
274 */
Steve Block8defd9f2010-07-08 12:39:36 +0100275template <class T> class Local : public Handle<T> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 public:
277 inline Local();
278 template <class S> inline Local(Local<S> that)
279 : Handle<T>(reinterpret_cast<T*>(*that)) {
280 /**
281 * This check fails when trying to convert between incompatible
282 * handles. For example, converting from a Handle<String> to a
283 * Handle<Number>.
284 */
285 TYPE_CHECK(T, S);
286 }
287 template <class S> inline Local(S* that) : Handle<T>(that) { }
288 template <class S> static inline Local<T> Cast(Local<S> that) {
289#ifdef V8_ENABLE_CHECKS
290 // If we're going to perform the type check then we have to check
291 // that the handle isn't empty before doing the checked cast.
292 if (that.IsEmpty()) return Local<T>();
293#endif
294 return Local<T>(T::Cast(*that));
295 }
296
Steve Block6ded16b2010-05-10 14:33:55 +0100297 template <class S> inline Local<S> As() {
298 return Local<S>::Cast(*this);
299 }
300
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 /** Create a local handle for the content of another handle.
302 * The referee is kept alive by the local handle even when
303 * the original handle is destroyed/disposed.
304 */
305 inline static Local<T> New(Handle<T> that);
306};
307
308
309/**
310 * An object reference that is independent of any handle scope. Where
311 * a Local handle only lives as long as the HandleScope in which it was
312 * allocated, a Persistent handle remains valid until it is explicitly
313 * disposed.
314 *
315 * A persistent handle contains a reference to a storage cell within
316 * the v8 engine which holds an object value and which is updated by
317 * the garbage collector whenever the object is moved. A new storage
318 * cell can be created using Persistent::New and existing handles can
319 * be disposed using Persistent::Dispose. Since persistent handles
320 * are passed by value you may have many persistent handle objects
321 * that point to the same storage cell. For instance, if you pass a
322 * persistent handle as an argument to a function you will not get two
323 * different storage cells but rather two references to the same
324 * storage cell.
325 */
Steve Block8defd9f2010-07-08 12:39:36 +0100326template <class T> class Persistent : public Handle<T> {
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 public:
328
329 /**
330 * Creates an empty persistent handle that doesn't point to any
331 * storage cell.
332 */
333 inline Persistent();
334
335 /**
336 * Creates a persistent handle for the same storage cell as the
337 * specified handle. This constructor allows you to pass persistent
338 * handles as arguments by value and to assign between persistent
339 * handles. However, attempting to assign between incompatible
340 * persistent handles, for instance from a Persistent<String> to a
341 * Persistent<Number> will cause a compiletime error. Assigning
342 * between compatible persistent handles, for instance assigning a
343 * Persistent<String> to a variable declared as Persistent<Value>,
344 * is allowed as String is a subclass of Value.
345 */
346 template <class S> inline Persistent(Persistent<S> that)
347 : Handle<T>(reinterpret_cast<T*>(*that)) {
348 /**
349 * This check fails when trying to convert between incompatible
350 * handles. For example, converting from a Handle<String> to a
351 * Handle<Number>.
352 */
353 TYPE_CHECK(T, S);
354 }
355
356 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
357
358 /**
359 * "Casts" a plain handle which is known to be a persistent handle
360 * to a persistent handle.
361 */
362 template <class S> explicit inline Persistent(Handle<S> that)
363 : Handle<T>(*that) { }
364
365 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
366#ifdef V8_ENABLE_CHECKS
367 // If we're going to perform the type check then we have to check
368 // that the handle isn't empty before doing the checked cast.
369 if (that.IsEmpty()) return Persistent<T>();
370#endif
371 return Persistent<T>(T::Cast(*that));
372 }
373
Steve Block6ded16b2010-05-10 14:33:55 +0100374 template <class S> inline Persistent<S> As() {
375 return Persistent<S>::Cast(*this);
376 }
377
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 /**
379 * Creates a new persistent handle for an existing local or
380 * persistent handle.
381 */
382 inline static Persistent<T> New(Handle<T> that);
383
384 /**
385 * Releases the storage cell referenced by this persistent handle.
386 * Does not remove the reference to the cell from any handles.
387 * This handle's reference, and any any other references to the storage
388 * cell remain and IsEmpty will still return false.
389 */
390 inline void Dispose();
391
392 /**
393 * Make the reference to this object weak. When only weak handles
394 * refer to the object, the garbage collector will perform a
395 * callback to the given V8::WeakReferenceCallback function, passing
396 * it the object reference and the given parameters.
397 */
398 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
399
400 /** Clears the weak reference to this object.*/
401 inline void ClearWeak();
402
403 /**
404 *Checks if the handle holds the only reference to an object.
405 */
406 inline bool IsNearDeath() const;
407
408 /**
409 * Returns true if the handle's reference is weak.
410 */
411 inline bool IsWeak() const;
412
413 private:
414 friend class ImplementationUtilities;
415 friend class ObjectTemplate;
416};
417
418
419 /**
420 * A stack-allocated class that governs a number of local handles.
421 * After a handle scope has been created, all local handles will be
422 * allocated within that handle scope until either the handle scope is
423 * deleted or another handle scope is created. If there is already a
424 * handle scope and a new one is created, all allocations will take
425 * place in the new handle scope until it is deleted. After that,
426 * new handles will again be allocated in the original handle scope.
427 *
428 * After the handle scope of a local handle has been deleted the
429 * garbage collector will no longer track the object stored in the
430 * handle and may deallocate it. The behavior of accessing a handle
431 * for which the handle scope has been deleted is undefined.
432 */
433class V8EXPORT HandleScope {
434 public:
435 HandleScope();
436
437 ~HandleScope();
438
439 /**
440 * Closes the handle scope and returns the value as a handle in the
441 * previous scope, which is the new current scope after the call.
442 */
443 template <class T> Local<T> Close(Handle<T> value);
444
445 /**
446 * Counts the number of allocated handles.
447 */
448 static int NumberOfHandles();
449
450 /**
451 * Creates a new handle with the given value.
452 */
453 static internal::Object** CreateHandle(internal::Object* value);
454
455 private:
456 // Make it impossible to create heap-allocated or illegal handle
457 // scopes by disallowing certain operations.
458 HandleScope(const HandleScope&);
459 void operator=(const HandleScope&);
460 void* operator new(size_t size);
461 void operator delete(void*, size_t);
462
Steve Blockd0582a62009-12-15 09:54:21 +0000463 // This Data class is accessible internally as HandleScopeData through a
464 // typedef in the ImplementationUtilities class.
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 class V8EXPORT Data {
466 public:
467 int extensions;
468 internal::Object** next;
469 internal::Object** limit;
470 inline void Initialize() {
471 extensions = -1;
472 next = limit = NULL;
473 }
474 };
475
476 Data previous_;
477
478 // Allow for the active closing of HandleScopes which allows to pass a handle
479 // from the HandleScope being closed to the next top most HandleScope.
480 bool is_closed_;
481 internal::Object** RawClose(internal::Object** value);
482
483 friend class ImplementationUtilities;
484};
485
486
487// --- S p e c i a l o b j e c t s ---
488
489
490/**
491 * The superclass of values and API object templates.
492 */
493class V8EXPORT Data {
494 private:
495 Data();
496};
497
498
499/**
500 * Pre-compilation data that can be associated with a script. This
501 * data can be calculated for a script in advance of actually
502 * compiling it, and can be stored between compilations. When script
503 * data is given to the compile method compilation will be faster.
504 */
505class V8EXPORT ScriptData { // NOLINT
506 public:
507 virtual ~ScriptData() { }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100508
Leon Clarkef7060e22010-06-03 12:02:55 +0100509 /**
510 * Pre-compiles the specified script (context-independent).
511 *
512 * \param input Pointer to UTF-8 script source code.
513 * \param length Length of UTF-8 script source code.
514 */
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 static ScriptData* PreCompile(const char* input, int length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000516
Leon Clarkef7060e22010-06-03 12:02:55 +0100517 /**
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100518 * Pre-compiles the specified script (context-independent).
519 *
520 * NOTE: Pre-compilation using this method cannot happen on another thread
521 * without using Lockers.
522 *
523 * \param source Script source code.
524 */
525 static ScriptData* PreCompile(Handle<String> source);
526
527 /**
Leon Clarkef7060e22010-06-03 12:02:55 +0100528 * Load previous pre-compilation data.
529 *
530 * \param data Pointer to data returned by a call to Data() of a previous
531 * ScriptData. Ownership is not transferred.
532 * \param length Length of data.
533 */
534 static ScriptData* New(const char* data, int length);
535
536 /**
537 * Returns the length of Data().
538 */
Steve Blocka7e24c12009-10-30 11:49:00 +0000539 virtual int Length() = 0;
Leon Clarkef7060e22010-06-03 12:02:55 +0100540
541 /**
542 * Returns a serialized representation of this ScriptData that can later be
543 * passed to New(). NOTE: Serialized data is platform-dependent.
544 */
545 virtual const char* Data() = 0;
546
547 /**
548 * Returns true if the source code could not be parsed.
549 */
Leon Clarkee46be812010-01-19 14:06:41 +0000550 virtual bool HasError() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000551};
552
553
554/**
555 * The origin, within a file, of a script.
556 */
Steve Block8defd9f2010-07-08 12:39:36 +0100557class ScriptOrigin {
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 public:
Steve Block8defd9f2010-07-08 12:39:36 +0100559 inline ScriptOrigin(
560 Handle<Value> resource_name,
561 Handle<Integer> resource_line_offset = Handle<Integer>(),
562 Handle<Integer> resource_column_offset = Handle<Integer>())
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 : resource_name_(resource_name),
564 resource_line_offset_(resource_line_offset),
565 resource_column_offset_(resource_column_offset) { }
566 inline Handle<Value> ResourceName() const;
567 inline Handle<Integer> ResourceLineOffset() const;
568 inline Handle<Integer> ResourceColumnOffset() const;
569 private:
570 Handle<Value> resource_name_;
571 Handle<Integer> resource_line_offset_;
572 Handle<Integer> resource_column_offset_;
573};
574
575
576/**
577 * A compiled JavaScript script.
578 */
579class V8EXPORT Script {
580 public:
581
Steve Blocka7e24c12009-10-30 11:49:00 +0000582 /**
Andrei Popescu402d9372010-02-26 13:31:12 +0000583 * Compiles the specified script (context-independent).
Steve Blocka7e24c12009-10-30 11:49:00 +0000584 *
Andrei Popescu402d9372010-02-26 13:31:12 +0000585 * \param source Script source code.
Steve Block6ded16b2010-05-10 14:33:55 +0100586 * \param origin Script origin, owned by caller, no references are kept
Andrei Popescu402d9372010-02-26 13:31:12 +0000587 * when New() returns
588 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
589 * using pre_data speeds compilation if it's done multiple times.
590 * Owned by caller, no references are kept when New() returns.
591 * \param script_data Arbitrary data associated with script. Using
Steve Block6ded16b2010-05-10 14:33:55 +0100592 * this has same effect as calling SetData(), but allows data to be
Andrei Popescu402d9372010-02-26 13:31:12 +0000593 * available to compile event handlers.
594 * \return Compiled script object (context independent; when run it
595 * will use the currently entered context).
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 */
Andrei Popescu402d9372010-02-26 13:31:12 +0000597 static Local<Script> New(Handle<String> source,
598 ScriptOrigin* origin = NULL,
599 ScriptData* pre_data = NULL,
600 Handle<String> script_data = Handle<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000601
602 /**
603 * Compiles the specified script using the specified file name
604 * object (typically a string) as the script's origin.
605 *
Andrei Popescu402d9372010-02-26 13:31:12 +0000606 * \param source Script source code.
Steve Block6ded16b2010-05-10 14:33:55 +0100607 * \param file_name file name object (typically a string) to be used
Andrei Popescu402d9372010-02-26 13:31:12 +0000608 * as the script's origin.
609 * \return Compiled script object (context independent; when run it
610 * will use the currently entered context).
611 */
612 static Local<Script> New(Handle<String> source,
613 Handle<Value> file_name);
614
615 /**
616 * Compiles the specified script (bound to current context).
617 *
618 * \param source Script source code.
Steve Block6ded16b2010-05-10 14:33:55 +0100619 * \param origin Script origin, owned by caller, no references are kept
Andrei Popescu402d9372010-02-26 13:31:12 +0000620 * when Compile() returns
621 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
622 * using pre_data speeds compilation if it's done multiple times.
623 * Owned by caller, no references are kept when Compile() returns.
624 * \param script_data Arbitrary data associated with script. Using
625 * this has same effect as calling SetData(), but makes data available
626 * earlier (i.e. to compile event handlers).
627 * \return Compiled script object, bound to the context that was active
628 * when this function was called. When run it will always use this
629 * context.
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 */
631 static Local<Script> Compile(Handle<String> source,
Andrei Popescu402d9372010-02-26 13:31:12 +0000632 ScriptOrigin* origin = NULL,
633 ScriptData* pre_data = NULL,
634 Handle<String> script_data = Handle<String>());
635
636 /**
637 * Compiles the specified script using the specified file name
638 * object (typically a string) as the script's origin.
639 *
640 * \param source Script source code.
641 * \param file_name File name to use as script's origin
642 * \param script_data Arbitrary data associated with script. Using
643 * this has same effect as calling SetData(), but makes data available
644 * earlier (i.e. to compile event handlers).
645 * \return Compiled script object, bound to the context that was active
646 * when this function was called. When run it will always use this
647 * context.
648 */
649 static Local<Script> Compile(Handle<String> source,
650 Handle<Value> file_name,
651 Handle<String> script_data = Handle<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000652
653 /**
654 * Runs the script returning the resulting value. If the script is
655 * context independent (created using ::New) it will be run in the
656 * currently entered context. If it is context specific (created
657 * using ::Compile) it will be run in the context in which it was
658 * compiled.
659 */
660 Local<Value> Run();
661
662 /**
663 * Returns the script id value.
664 */
665 Local<Value> Id();
666
667 /**
668 * Associate an additional data object with the script. This is mainly used
669 * with the debugger as this data object is only available through the
670 * debugger API.
671 */
Steve Blockd0582a62009-12-15 09:54:21 +0000672 void SetData(Handle<String> data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000673};
674
675
676/**
677 * An error message.
678 */
679class V8EXPORT Message {
680 public:
681 Local<String> Get() const;
682 Local<String> GetSourceLine() const;
683
684 /**
685 * Returns the resource name for the script from where the function causing
686 * the error originates.
687 */
688 Handle<Value> GetScriptResourceName() const;
689
690 /**
691 * Returns the resource data for the script from where the function causing
692 * the error originates.
693 */
694 Handle<Value> GetScriptData() const;
695
696 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100697 * Exception stack trace. By default stack traces are not captured for
698 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
699 * to change this option.
700 */
701 Handle<StackTrace> GetStackTrace() const;
702
703 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000704 * Returns the number, 1-based, of the line where the error occurred.
705 */
706 int GetLineNumber() const;
707
708 /**
709 * Returns the index within the script of the first character where
710 * the error occurred.
711 */
712 int GetStartPosition() const;
713
714 /**
715 * Returns the index within the script of the last character where
716 * the error occurred.
717 */
718 int GetEndPosition() const;
719
720 /**
721 * Returns the index within the line of the first character where
722 * the error occurred.
723 */
724 int GetStartColumn() const;
725
726 /**
727 * Returns the index within the line of the last character where
728 * the error occurred.
729 */
730 int GetEndColumn() const;
731
732 // TODO(1245381): Print to a string instead of on a FILE.
733 static void PrintCurrentStackTrace(FILE* out);
Kristian Monsen25f61362010-05-21 11:50:48 +0100734
735 static const int kNoLineNumberInfo = 0;
736 static const int kNoColumnInfo = 0;
737};
738
739
740/**
741 * Representation of a JavaScript stack trace. The information collected is a
742 * snapshot of the execution stack and the information remains valid after
743 * execution continues.
744 */
745class V8EXPORT StackTrace {
746 public:
747 /**
748 * Flags that determine what information is placed captured for each
749 * StackFrame when grabbing the current stack trace.
750 */
751 enum StackTraceOptions {
752 kLineNumber = 1,
753 kColumnOffset = 1 << 1 | kLineNumber,
754 kScriptName = 1 << 2,
755 kFunctionName = 1 << 3,
756 kIsEval = 1 << 4,
757 kIsConstructor = 1 << 5,
758 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
759 kDetailed = kOverview | kIsEval | kIsConstructor
760 };
761
762 /**
763 * Returns a StackFrame at a particular index.
764 */
765 Local<StackFrame> GetFrame(uint32_t index) const;
766
767 /**
768 * Returns the number of StackFrames.
769 */
770 int GetFrameCount() const;
771
772 /**
773 * Returns StackTrace as a v8::Array that contains StackFrame objects.
774 */
775 Local<Array> AsArray();
776
777 /**
778 * Grab a snapshot of the the current JavaScript execution stack.
779 *
780 * \param frame_limit The maximum number of stack frames we want to capture.
781 * \param options Enumerates the set of things we will capture for each
782 * StackFrame.
783 */
784 static Local<StackTrace> CurrentStackTrace(
785 int frame_limit,
786 StackTraceOptions options = kOverview);
787};
788
789
790/**
791 * A single JavaScript stack frame.
792 */
793class V8EXPORT StackFrame {
794 public:
795 /**
796 * Returns the number, 1-based, of the line for the associate function call.
797 * This method will return Message::kNoLineNumberInfo if it is unable to
798 * retrieve the line number, or if kLineNumber was not passed as an option
799 * when capturing the StackTrace.
800 */
801 int GetLineNumber() const;
802
803 /**
804 * Returns the 1-based column offset on the line for the associated function
805 * call.
806 * This method will return Message::kNoColumnInfo if it is unable to retrieve
807 * the column number, or if kColumnOffset was not passed as an option when
808 * capturing the StackTrace.
809 */
810 int GetColumn() const;
811
812 /**
813 * Returns the name of the resource that contains the script for the
814 * function for this StackFrame.
815 */
816 Local<String> GetScriptName() const;
817
818 /**
819 * Returns the name of the function associated with this stack frame.
820 */
821 Local<String> GetFunctionName() const;
822
823 /**
824 * Returns whether or not the associated function is compiled via a call to
825 * eval().
826 */
827 bool IsEval() const;
828
829 /**
830 * Returns whther or not the associated function is called as a
831 * constructor via "new".
832 */
833 bool IsConstructor() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000834};
835
836
837// --- V a l u e ---
838
839
840/**
841 * The superclass of all JavaScript values and objects.
842 */
Steve Block8defd9f2010-07-08 12:39:36 +0100843class Value : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 public:
845
846 /**
847 * Returns true if this value is the undefined value. See ECMA-262
848 * 4.3.10.
849 */
Steve Block8defd9f2010-07-08 12:39:36 +0100850 V8EXPORT bool IsUndefined() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000851
852 /**
853 * Returns true if this value is the null value. See ECMA-262
854 * 4.3.11.
855 */
Steve Block8defd9f2010-07-08 12:39:36 +0100856 V8EXPORT bool IsNull() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000857
858 /**
859 * Returns true if this value is true.
860 */
Steve Block8defd9f2010-07-08 12:39:36 +0100861 V8EXPORT bool IsTrue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000862
863 /**
864 * Returns true if this value is false.
865 */
Steve Block8defd9f2010-07-08 12:39:36 +0100866 V8EXPORT bool IsFalse() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000867
868 /**
869 * Returns true if this value is an instance of the String type.
870 * See ECMA-262 8.4.
871 */
872 inline bool IsString() const;
873
874 /**
875 * Returns true if this value is a function.
876 */
Steve Block8defd9f2010-07-08 12:39:36 +0100877 V8EXPORT bool IsFunction() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000878
879 /**
880 * Returns true if this value is an array.
881 */
Steve Block8defd9f2010-07-08 12:39:36 +0100882 V8EXPORT bool IsArray() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000883
884 /**
885 * Returns true if this value is an object.
886 */
Steve Block8defd9f2010-07-08 12:39:36 +0100887 V8EXPORT bool IsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000888
889 /**
890 * Returns true if this value is boolean.
891 */
Steve Block8defd9f2010-07-08 12:39:36 +0100892 V8EXPORT bool IsBoolean() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000893
894 /**
895 * Returns true if this value is a number.
896 */
Steve Block8defd9f2010-07-08 12:39:36 +0100897 V8EXPORT bool IsNumber() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000898
899 /**
900 * Returns true if this value is external.
901 */
Steve Block8defd9f2010-07-08 12:39:36 +0100902 V8EXPORT bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000903
904 /**
905 * Returns true if this value is a 32-bit signed integer.
906 */
Steve Block8defd9f2010-07-08 12:39:36 +0100907 V8EXPORT bool IsInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000908
909 /**
Steve Block6ded16b2010-05-10 14:33:55 +0100910 * Returns true if this value is a 32-bit unsigned integer.
911 */
Steve Block8defd9f2010-07-08 12:39:36 +0100912 V8EXPORT bool IsUint32() const;
Steve Block6ded16b2010-05-10 14:33:55 +0100913
914 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000915 * Returns true if this value is a Date.
916 */
Steve Block8defd9f2010-07-08 12:39:36 +0100917 V8EXPORT bool IsDate() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000918
Steve Block8defd9f2010-07-08 12:39:36 +0100919 V8EXPORT Local<Boolean> ToBoolean() const;
920 V8EXPORT Local<Number> ToNumber() const;
921 V8EXPORT Local<String> ToString() const;
922 V8EXPORT Local<String> ToDetailString() const;
923 V8EXPORT Local<Object> ToObject() const;
924 V8EXPORT Local<Integer> ToInteger() const;
925 V8EXPORT Local<Uint32> ToUint32() const;
926 V8EXPORT Local<Int32> ToInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000927
928 /**
929 * Attempts to convert a string to an array index.
930 * Returns an empty handle if the conversion fails.
931 */
Steve Block8defd9f2010-07-08 12:39:36 +0100932 V8EXPORT Local<Uint32> ToArrayIndex() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
Steve Block8defd9f2010-07-08 12:39:36 +0100934 V8EXPORT bool BooleanValue() const;
935 V8EXPORT double NumberValue() const;
936 V8EXPORT int64_t IntegerValue() const;
937 V8EXPORT uint32_t Uint32Value() const;
938 V8EXPORT int32_t Int32Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000939
940 /** JS == */
Steve Block8defd9f2010-07-08 12:39:36 +0100941 V8EXPORT bool Equals(Handle<Value> that) const;
942 V8EXPORT bool StrictEquals(Handle<Value> that) const;
Steve Block3ce2e202009-11-05 08:53:23 +0000943
Steve Blocka7e24c12009-10-30 11:49:00 +0000944 private:
945 inline bool QuickIsString() const;
Steve Block8defd9f2010-07-08 12:39:36 +0100946 V8EXPORT bool FullIsString() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000947};
948
949
950/**
951 * The superclass of primitive values. See ECMA-262 4.3.2.
952 */
Steve Block8defd9f2010-07-08 12:39:36 +0100953class Primitive : public Value { };
Steve Blocka7e24c12009-10-30 11:49:00 +0000954
955
956/**
957 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
958 * or false value.
959 */
Steve Block8defd9f2010-07-08 12:39:36 +0100960class Boolean : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +0000961 public:
Steve Block8defd9f2010-07-08 12:39:36 +0100962 V8EXPORT bool Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000963 static inline Handle<Boolean> New(bool value);
964};
965
966
967/**
968 * A JavaScript string value (ECMA-262, 4.3.17).
969 */
Steve Block8defd9f2010-07-08 12:39:36 +0100970class String : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +0000971 public:
972
973 /**
974 * Returns the number of characters in this string.
975 */
Steve Block8defd9f2010-07-08 12:39:36 +0100976 V8EXPORT int Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000977
978 /**
979 * Returns the number of bytes in the UTF-8 encoded
980 * representation of this string.
981 */
Steve Block8defd9f2010-07-08 12:39:36 +0100982 V8EXPORT int Utf8Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +0000983
984 /**
985 * Write the contents of the string to an external buffer.
986 * If no arguments are given, expects the buffer to be large
987 * enough to hold the entire string and NULL terminator. Copies
988 * the contents of the string and the NULL terminator into the
989 * buffer.
990 *
991 * Copies up to length characters into the output buffer.
992 * Only null-terminates if there is enough space in the buffer.
993 *
994 * \param buffer The buffer into which the string will be copied.
995 * \param start The starting position within the string at which
996 * copying begins.
997 * \param length The number of bytes to copy from the string.
Steve Block6ded16b2010-05-10 14:33:55 +0100998 * \param nchars_ref The number of characters written, can be NULL.
999 * \param hints Various hints that might affect performance of this or
1000 * subsequent operations.
1001 * \return The number of bytes copied to the buffer
Steve Blocka7e24c12009-10-30 11:49:00 +00001002 * excluding the NULL terminator.
1003 */
Steve Block6ded16b2010-05-10 14:33:55 +01001004 enum WriteHints {
1005 NO_HINTS = 0,
1006 HINT_MANY_WRITES_EXPECTED = 1
1007 };
1008
Steve Block8defd9f2010-07-08 12:39:36 +01001009 V8EXPORT int Write(uint16_t* buffer,
1010 int start = 0,
1011 int length = -1,
1012 WriteHints hints = NO_HINTS) const; // UTF-16
1013 V8EXPORT int WriteAscii(char* buffer,
1014 int start = 0,
1015 int length = -1,
1016 WriteHints hints = NO_HINTS) const; // ASCII
1017 V8EXPORT int WriteUtf8(char* buffer,
1018 int length = -1,
1019 int* nchars_ref = NULL,
1020 WriteHints hints = NO_HINTS) const; // UTF-8
Steve Blocka7e24c12009-10-30 11:49:00 +00001021
1022 /**
1023 * A zero length string.
1024 */
Steve Block8defd9f2010-07-08 12:39:36 +01001025 V8EXPORT static v8::Local<v8::String> Empty();
Steve Blocka7e24c12009-10-30 11:49:00 +00001026
1027 /**
1028 * Returns true if the string is external
1029 */
Steve Block8defd9f2010-07-08 12:39:36 +01001030 V8EXPORT bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001031
1032 /**
1033 * Returns true if the string is both external and ascii
1034 */
Steve Block8defd9f2010-07-08 12:39:36 +01001035 V8EXPORT bool IsExternalAscii() const;
Leon Clarkee46be812010-01-19 14:06:41 +00001036
1037 class V8EXPORT ExternalStringResourceBase {
1038 public:
1039 virtual ~ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001040
Leon Clarkee46be812010-01-19 14:06:41 +00001041 protected:
1042 ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001043
1044 /**
1045 * Internally V8 will call this Dispose method when the external string
1046 * resource is no longer needed. The default implementation will use the
1047 * delete operator. This method can be overridden in subclasses to
1048 * control how allocated external string resources are disposed.
1049 */
1050 virtual void Dispose() { delete this; }
1051
Leon Clarkee46be812010-01-19 14:06:41 +00001052 private:
1053 // Disallow copying and assigning.
1054 ExternalStringResourceBase(const ExternalStringResourceBase&);
1055 void operator=(const ExternalStringResourceBase&);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001056
1057 friend class v8::internal::Heap;
Leon Clarkee46be812010-01-19 14:06:41 +00001058 };
1059
Steve Blocka7e24c12009-10-30 11:49:00 +00001060 /**
1061 * An ExternalStringResource is a wrapper around a two-byte string
1062 * buffer that resides outside V8's heap. Implement an
1063 * ExternalStringResource to manage the life cycle of the underlying
1064 * buffer. Note that the string data must be immutable.
1065 */
Leon Clarkee46be812010-01-19 14:06:41 +00001066 class V8EXPORT ExternalStringResource
1067 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 public:
1069 /**
1070 * Override the destructor to manage the life cycle of the underlying
1071 * buffer.
1072 */
1073 virtual ~ExternalStringResource() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001074
1075 /**
1076 * The string data from the underlying buffer.
1077 */
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 virtual const uint16_t* data() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001079
1080 /**
1081 * The length of the string. That is, the number of two-byte characters.
1082 */
Steve Blocka7e24c12009-10-30 11:49:00 +00001083 virtual size_t length() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001084
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 protected:
1086 ExternalStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001087 };
1088
1089 /**
1090 * An ExternalAsciiStringResource is a wrapper around an ascii
1091 * string buffer that resides outside V8's heap. Implement an
1092 * ExternalAsciiStringResource to manage the life cycle of the
1093 * underlying buffer. Note that the string data must be immutable
1094 * and that the data must be strict 7-bit ASCII, not Latin1 or
1095 * UTF-8, which would require special treatment internally in the
1096 * engine and, in the case of UTF-8, do not allow efficient indexing.
1097 * Use String::New or convert to 16 bit data for non-ASCII.
1098 */
1099
Leon Clarkee46be812010-01-19 14:06:41 +00001100 class V8EXPORT ExternalAsciiStringResource
1101 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00001102 public:
1103 /**
1104 * Override the destructor to manage the life cycle of the underlying
1105 * buffer.
1106 */
1107 virtual ~ExternalAsciiStringResource() {}
1108 /** The string data from the underlying buffer.*/
1109 virtual const char* data() const = 0;
1110 /** The number of ascii characters in the string.*/
1111 virtual size_t length() const = 0;
1112 protected:
1113 ExternalAsciiStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001114 };
1115
1116 /**
1117 * Get the ExternalStringResource for an external string. Returns
1118 * NULL if IsExternal() doesn't return true.
1119 */
1120 inline ExternalStringResource* GetExternalStringResource() const;
1121
1122 /**
1123 * Get the ExternalAsciiStringResource for an external ascii string.
1124 * Returns NULL if IsExternalAscii() doesn't return true.
1125 */
Steve Block8defd9f2010-07-08 12:39:36 +01001126 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001127
1128 static inline String* Cast(v8::Value* obj);
1129
1130 /**
1131 * Allocates a new string from either utf-8 encoded or ascii data.
1132 * The second parameter 'length' gives the buffer length.
1133 * If the data is utf-8 encoded, the caller must
1134 * be careful to supply the length parameter.
1135 * If it is not given, the function calls
1136 * 'strlen' to determine the buffer length, it might be
1137 * wrong if 'data' contains a null character.
1138 */
Steve Block8defd9f2010-07-08 12:39:36 +01001139 V8EXPORT static Local<String> New(const char* data, int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001140
1141 /** Allocates a new string from utf16 data.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001142 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001143
1144 /** Creates a symbol. Returns one if it exists already.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001145 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001146
1147 /**
Steve Block3ce2e202009-11-05 08:53:23 +00001148 * Creates a new string by concatenating the left and the right strings
1149 * passed in as parameters.
1150 */
Steve Block8defd9f2010-07-08 12:39:36 +01001151 V8EXPORT static Local<String> Concat(Handle<String> left,
1152 Handle<String>right);
Steve Block3ce2e202009-11-05 08:53:23 +00001153
1154 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001155 * Creates a new external string using the data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001156 * resource. When the external string is no longer live on V8's heap the
1157 * resource will be disposed by calling its Dispose method. The caller of
1158 * this function should not otherwise delete or modify the resource. Neither
1159 * should the underlying buffer be deallocated or modified except through the
1160 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001161 */
Steve Block8defd9f2010-07-08 12:39:36 +01001162 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001163
1164 /**
1165 * Associate an external string resource with this string by transforming it
1166 * in place so that existing references to this string in the JavaScript heap
1167 * will use the external string resource. The external string resource's
1168 * character contents needs to be equivalent to this string.
1169 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001170 * The string is not modified if the operation fails. See NewExternal for
1171 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001172 */
Steve Block8defd9f2010-07-08 12:39:36 +01001173 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001174
1175 /**
1176 * Creates a new external string using the ascii data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001177 * resource. When the external string is no longer live on V8's heap the
1178 * resource will be disposed by calling its Dispose method. The caller of
1179 * this function should not otherwise delete or modify the resource. Neither
1180 * should the underlying buffer be deallocated or modified except through the
1181 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001182 */
Steve Block8defd9f2010-07-08 12:39:36 +01001183 V8EXPORT static Local<String> NewExternal(
1184 ExternalAsciiStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001185
1186 /**
1187 * Associate an external string resource with this string by transforming it
1188 * in place so that existing references to this string in the JavaScript heap
1189 * will use the external string resource. The external string resource's
1190 * character contents needs to be equivalent to this string.
1191 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001192 * The string is not modified if the operation fails. See NewExternal for
1193 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00001194 */
Steve Block8defd9f2010-07-08 12:39:36 +01001195 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00001196
1197 /**
1198 * Returns true if this string can be made external.
1199 */
Steve Block8defd9f2010-07-08 12:39:36 +01001200 V8EXPORT bool CanMakeExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00001201
1202 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001203 V8EXPORT static Local<String> NewUndetectable(const char* data,
1204 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001205
1206 /** Creates an undetectable string from the supplied utf-16 data.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001207 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1208 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001209
1210 /**
1211 * Converts an object to a utf8-encoded character array. Useful if
1212 * you want to print the object. If conversion to a string fails
1213 * (eg. due to an exception in the toString() method of the object)
1214 * then the length() method returns 0 and the * operator returns
1215 * NULL.
1216 */
1217 class V8EXPORT Utf8Value {
1218 public:
1219 explicit Utf8Value(Handle<v8::Value> obj);
1220 ~Utf8Value();
1221 char* operator*() { return str_; }
1222 const char* operator*() const { return str_; }
1223 int length() const { return length_; }
1224 private:
1225 char* str_;
1226 int length_;
1227
1228 // Disallow copying and assigning.
1229 Utf8Value(const Utf8Value&);
1230 void operator=(const Utf8Value&);
1231 };
1232
1233 /**
1234 * Converts an object to an ascii string.
1235 * Useful if you want to print the object.
1236 * If conversion to a string fails (eg. due to an exception in the toString()
1237 * method of the object) then the length() method returns 0 and the * operator
1238 * returns NULL.
1239 */
1240 class V8EXPORT AsciiValue {
1241 public:
1242 explicit AsciiValue(Handle<v8::Value> obj);
1243 ~AsciiValue();
1244 char* operator*() { return str_; }
1245 const char* operator*() const { return str_; }
1246 int length() const { return length_; }
1247 private:
1248 char* str_;
1249 int length_;
1250
1251 // Disallow copying and assigning.
1252 AsciiValue(const AsciiValue&);
1253 void operator=(const AsciiValue&);
1254 };
1255
1256 /**
1257 * Converts an object to a two-byte string.
1258 * If conversion to a string fails (eg. due to an exception in the toString()
1259 * method of the object) then the length() method returns 0 and the * operator
1260 * returns NULL.
1261 */
1262 class V8EXPORT Value {
1263 public:
1264 explicit Value(Handle<v8::Value> obj);
1265 ~Value();
1266 uint16_t* operator*() { return str_; }
1267 const uint16_t* operator*() const { return str_; }
1268 int length() const { return length_; }
1269 private:
1270 uint16_t* str_;
1271 int length_;
1272
1273 // Disallow copying and assigning.
1274 Value(const Value&);
1275 void operator=(const Value&);
1276 };
Steve Block3ce2e202009-11-05 08:53:23 +00001277
Steve Blocka7e24c12009-10-30 11:49:00 +00001278 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001279 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1280 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001281};
1282
1283
1284/**
1285 * A JavaScript number value (ECMA-262, 4.3.20)
1286 */
Steve Block8defd9f2010-07-08 12:39:36 +01001287class Number : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00001288 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001289 V8EXPORT double Value() const;
1290 V8EXPORT static Local<Number> New(double value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001291 static inline Number* Cast(v8::Value* obj);
1292 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001293 V8EXPORT Number();
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 static void CheckCast(v8::Value* obj);
1295};
1296
1297
1298/**
1299 * A JavaScript value representing a signed integer.
1300 */
Steve Block8defd9f2010-07-08 12:39:36 +01001301class Integer : public Number {
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001303 V8EXPORT static Local<Integer> New(int32_t value);
1304 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1305 V8EXPORT int64_t Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 static inline Integer* Cast(v8::Value* obj);
1307 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001308 V8EXPORT Integer();
1309 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001310};
1311
1312
1313/**
1314 * A JavaScript value representing a 32-bit signed integer.
1315 */
Steve Block8defd9f2010-07-08 12:39:36 +01001316class Int32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00001317 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001318 V8EXPORT int32_t Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001319 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001320 V8EXPORT Int32();
Steve Blocka7e24c12009-10-30 11:49:00 +00001321};
1322
1323
1324/**
1325 * A JavaScript value representing a 32-bit unsigned integer.
1326 */
Steve Block8defd9f2010-07-08 12:39:36 +01001327class Uint32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001329 V8EXPORT uint32_t Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001331 V8EXPORT Uint32();
Steve Blocka7e24c12009-10-30 11:49:00 +00001332};
1333
1334
1335/**
1336 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1337 */
Steve Block8defd9f2010-07-08 12:39:36 +01001338class Date : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001340 V8EXPORT static Local<Value> New(double time);
Steve Blocka7e24c12009-10-30 11:49:00 +00001341
1342 /**
1343 * A specialization of Value::NumberValue that is more efficient
1344 * because we know the structure of this object.
1345 */
Steve Block8defd9f2010-07-08 12:39:36 +01001346 V8EXPORT double NumberValue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001347
1348 static inline Date* Cast(v8::Value* obj);
1349 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001350 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001351};
1352
1353
1354enum PropertyAttribute {
1355 None = 0,
1356 ReadOnly = 1 << 0,
1357 DontEnum = 1 << 1,
1358 DontDelete = 1 << 2
1359};
1360
Steve Block3ce2e202009-11-05 08:53:23 +00001361enum ExternalArrayType {
1362 kExternalByteArray = 1,
1363 kExternalUnsignedByteArray,
1364 kExternalShortArray,
1365 kExternalUnsignedShortArray,
1366 kExternalIntArray,
1367 kExternalUnsignedIntArray,
1368 kExternalFloatArray
1369};
1370
Steve Blocka7e24c12009-10-30 11:49:00 +00001371/**
Leon Clarkef7060e22010-06-03 12:02:55 +01001372 * Accessor[Getter|Setter] are used as callback functions when
1373 * setting|getting a particular property. See Object and ObjectTemplate's
1374 * method SetAccessor.
1375 */
1376typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1377 const AccessorInfo& info);
1378
1379
1380typedef void (*AccessorSetter)(Local<String> property,
1381 Local<Value> value,
1382 const AccessorInfo& info);
1383
1384
1385/**
1386 * Access control specifications.
1387 *
1388 * Some accessors should be accessible across contexts. These
1389 * accessors have an explicit access control parameter which specifies
1390 * the kind of cross-context access that should be allowed.
1391 *
1392 * Additionally, for security, accessors can prohibit overwriting by
1393 * accessors defined in JavaScript. For objects that have such
1394 * accessors either locally or in their prototype chain it is not
1395 * possible to overwrite the accessor by using __defineGetter__ or
1396 * __defineSetter__ from JavaScript code.
1397 */
1398enum AccessControl {
1399 DEFAULT = 0,
1400 ALL_CAN_READ = 1,
1401 ALL_CAN_WRITE = 1 << 1,
1402 PROHIBITS_OVERWRITING = 1 << 2
1403};
1404
1405
1406/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 * A JavaScript object (ECMA-262, 4.3.3)
1408 */
Steve Block8defd9f2010-07-08 12:39:36 +01001409class Object : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00001410 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001411 V8EXPORT bool Set(Handle<Value> key,
1412 Handle<Value> value,
1413 PropertyAttribute attribs = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00001414
Steve Block8defd9f2010-07-08 12:39:36 +01001415 V8EXPORT bool Set(uint32_t index,
1416 Handle<Value> value);
Steve Block6ded16b2010-05-10 14:33:55 +01001417
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 // Sets a local property on this object bypassing interceptors and
1419 // overriding accessors or read-only properties.
1420 //
1421 // Note that if the object has an interceptor the property will be set
1422 // locally, but since the interceptor takes precedence the local property
1423 // will only be returned if the interceptor doesn't return a value.
1424 //
1425 // Note also that this only works for named properties.
Steve Block8defd9f2010-07-08 12:39:36 +01001426 V8EXPORT bool ForceSet(Handle<Value> key,
1427 Handle<Value> value,
1428 PropertyAttribute attribs = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00001429
Steve Block8defd9f2010-07-08 12:39:36 +01001430 V8EXPORT Local<Value> Get(Handle<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001431
Steve Block8defd9f2010-07-08 12:39:36 +01001432 V8EXPORT Local<Value> Get(uint32_t index);
Steve Block6ded16b2010-05-10 14:33:55 +01001433
Steve Blocka7e24c12009-10-30 11:49:00 +00001434 // TODO(1245389): Replace the type-specific versions of these
1435 // functions with generic ones that accept a Handle<Value> key.
Steve Block8defd9f2010-07-08 12:39:36 +01001436 V8EXPORT bool Has(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001437
Steve Block8defd9f2010-07-08 12:39:36 +01001438 V8EXPORT bool Delete(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439
1440 // Delete a property on this object bypassing interceptors and
1441 // ignoring dont-delete attributes.
Steve Block8defd9f2010-07-08 12:39:36 +01001442 V8EXPORT bool ForceDelete(Handle<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001443
Steve Block8defd9f2010-07-08 12:39:36 +01001444 V8EXPORT bool Has(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001445
Steve Block8defd9f2010-07-08 12:39:36 +01001446 V8EXPORT bool Delete(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001447
Steve Block8defd9f2010-07-08 12:39:36 +01001448 V8EXPORT bool SetAccessor(Handle<String> name,
1449 AccessorGetter getter,
1450 AccessorSetter setter = 0,
1451 Handle<Value> data = Handle<Value>(),
1452 AccessControl settings = DEFAULT,
1453 PropertyAttribute attribute = None);
Leon Clarkef7060e22010-06-03 12:02:55 +01001454
Steve Blocka7e24c12009-10-30 11:49:00 +00001455 /**
1456 * Returns an array containing the names of the enumerable properties
1457 * of this object, including properties from prototype objects. The
1458 * array returned by this method contains the same values as would
1459 * be enumerated by a for-in statement over this object.
1460 */
Steve Block8defd9f2010-07-08 12:39:36 +01001461 V8EXPORT Local<Array> GetPropertyNames();
Steve Blocka7e24c12009-10-30 11:49:00 +00001462
1463 /**
1464 * Get the prototype object. This does not skip objects marked to
1465 * be skipped by __proto__ and it does not consult the security
1466 * handler.
1467 */
Steve Block8defd9f2010-07-08 12:39:36 +01001468 V8EXPORT Local<Value> GetPrototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00001469
1470 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00001471 * Set the prototype object. This does not skip objects marked to
1472 * be skipped by __proto__ and it does not consult the security
1473 * handler.
1474 */
Steve Block8defd9f2010-07-08 12:39:36 +01001475 V8EXPORT bool SetPrototype(Handle<Value> prototype);
Andrei Popescu402d9372010-02-26 13:31:12 +00001476
1477 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001478 * Finds an instance of the given function template in the prototype
1479 * chain.
1480 */
Steve Block8defd9f2010-07-08 12:39:36 +01001481 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1482 Handle<FunctionTemplate> tmpl);
Steve Blocka7e24c12009-10-30 11:49:00 +00001483
1484 /**
1485 * Call builtin Object.prototype.toString on this object.
1486 * This is different from Value::ToString() that may call
1487 * user-defined toString function. This one does not.
1488 */
Steve Block8defd9f2010-07-08 12:39:36 +01001489 V8EXPORT Local<String> ObjectProtoToString();
Steve Blocka7e24c12009-10-30 11:49:00 +00001490
1491 /** Gets the number of internal fields for this Object. */
Steve Block8defd9f2010-07-08 12:39:36 +01001492 V8EXPORT int InternalFieldCount();
Steve Blocka7e24c12009-10-30 11:49:00 +00001493 /** Gets the value in an internal field. */
1494 inline Local<Value> GetInternalField(int index);
1495 /** Sets the value in an internal field. */
Steve Block8defd9f2010-07-08 12:39:36 +01001496 V8EXPORT void SetInternalField(int index, Handle<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001497
1498 /** Gets a native pointer from an internal field. */
1499 inline void* GetPointerFromInternalField(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00001500
Steve Blocka7e24c12009-10-30 11:49:00 +00001501 /** Sets a native pointer in an internal field. */
Steve Block8defd9f2010-07-08 12:39:36 +01001502 V8EXPORT void SetPointerInInternalField(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001503
1504 // Testers for local properties.
Steve Block8defd9f2010-07-08 12:39:36 +01001505 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1506 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1507 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001508
1509 /**
1510 * If result.IsEmpty() no real property was located in the prototype chain.
1511 * This means interceptors in the prototype chain are not called.
1512 */
Steve Block8defd9f2010-07-08 12:39:36 +01001513 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1514 Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001515
1516 /**
1517 * If result.IsEmpty() no real property was located on the object or
1518 * in the prototype chain.
1519 * This means interceptors in the prototype chain are not called.
1520 */
Steve Block8defd9f2010-07-08 12:39:36 +01001521 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001522
1523 /** Tests for a named lookup interceptor.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001524 V8EXPORT bool HasNamedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001525
1526 /** Tests for an index lookup interceptor.*/
Steve Block8defd9f2010-07-08 12:39:36 +01001527 V8EXPORT bool HasIndexedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001528
1529 /**
1530 * Turns on access check on the object if the object is an instance of
1531 * a template that has access check callbacks. If an object has no
1532 * access check info, the object cannot be accessed by anyone.
1533 */
Steve Block8defd9f2010-07-08 12:39:36 +01001534 V8EXPORT void TurnOnAccessCheck();
Steve Blocka7e24c12009-10-30 11:49:00 +00001535
1536 /**
1537 * Returns the identity hash for this object. The current implemenation uses
1538 * a hidden property on the object to store the identity hash.
1539 *
1540 * The return value will never be 0. Also, it is not guaranteed to be
1541 * unique.
1542 */
Steve Block8defd9f2010-07-08 12:39:36 +01001543 V8EXPORT int GetIdentityHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00001544
1545 /**
1546 * Access hidden properties on JavaScript objects. These properties are
1547 * hidden from the executing JavaScript and only accessible through the V8
1548 * C++ API. Hidden properties introduced by V8 internally (for example the
1549 * identity hash) are prefixed with "v8::".
1550 */
Steve Block8defd9f2010-07-08 12:39:36 +01001551 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1552 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1553 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
Steve Block3ce2e202009-11-05 08:53:23 +00001554
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 /**
1556 * Returns true if this is an instance of an api function (one
1557 * created from a function created from a function template) and has
1558 * been modified since it was created. Note that this method is
1559 * conservative and may return true for objects that haven't actually
1560 * been modified.
1561 */
Steve Block8defd9f2010-07-08 12:39:36 +01001562 V8EXPORT bool IsDirty();
Steve Blocka7e24c12009-10-30 11:49:00 +00001563
1564 /**
1565 * Clone this object with a fast but shallow copy. Values will point
1566 * to the same values as the original object.
1567 */
Steve Block8defd9f2010-07-08 12:39:36 +01001568 V8EXPORT Local<Object> Clone();
Steve Blocka7e24c12009-10-30 11:49:00 +00001569
1570 /**
1571 * Set the backing store of the indexed properties to be managed by the
1572 * embedding layer. Access to the indexed properties will follow the rules
1573 * spelled out in CanvasPixelArray.
1574 * Note: The embedding program still owns the data and needs to ensure that
1575 * the backing store is preserved while V8 has a reference.
1576 */
Steve Block8defd9f2010-07-08 12:39:36 +01001577 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001578 bool HasIndexedPropertiesInPixelData();
1579 uint8_t* GetIndexedPropertiesPixelData();
1580 int GetIndexedPropertiesPixelDataLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001581
Steve Block3ce2e202009-11-05 08:53:23 +00001582 /**
1583 * Set the backing store of the indexed properties to be managed by the
1584 * embedding layer. Access to the indexed properties will follow the rules
1585 * spelled out for the CanvasArray subtypes in the WebGL specification.
1586 * Note: The embedding program still owns the data and needs to ensure that
1587 * the backing store is preserved while V8 has a reference.
1588 */
Steve Block8defd9f2010-07-08 12:39:36 +01001589 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1590 void* data,
1591 ExternalArrayType array_type,
1592 int number_of_elements);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001593 bool HasIndexedPropertiesInExternalArrayData();
1594 void* GetIndexedPropertiesExternalArrayData();
1595 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1596 int GetIndexedPropertiesExternalArrayDataLength();
Steve Block3ce2e202009-11-05 08:53:23 +00001597
Steve Block8defd9f2010-07-08 12:39:36 +01001598 V8EXPORT static Local<Object> New();
Steve Blocka7e24c12009-10-30 11:49:00 +00001599 static inline Object* Cast(Value* obj);
1600 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001601 V8EXPORT Object();
1602 V8EXPORT static void CheckCast(Value* obj);
1603 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1604 V8EXPORT void* SlowGetPointerFromInternalField(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001605
1606 /**
1607 * If quick access to the internal field is possible this method
Steve Block3ce2e202009-11-05 08:53:23 +00001608 * returns the value. Otherwise an empty handle is returned.
Steve Blocka7e24c12009-10-30 11:49:00 +00001609 */
1610 inline Local<Value> UncheckedGetInternalField(int index);
1611};
1612
1613
1614/**
1615 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1616 */
Steve Block8defd9f2010-07-08 12:39:36 +01001617class Array : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00001618 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001619 V8EXPORT uint32_t Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001620
1621 /**
1622 * Clones an element at index |index|. Returns an empty
1623 * handle if cloning fails (for any reason).
1624 */
Steve Block8defd9f2010-07-08 12:39:36 +01001625 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001626
Steve Block8defd9f2010-07-08 12:39:36 +01001627 V8EXPORT static Local<Array> New(int length = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 static inline Array* Cast(Value* obj);
1629 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001630 V8EXPORT Array();
Steve Blocka7e24c12009-10-30 11:49:00 +00001631 static void CheckCast(Value* obj);
1632};
1633
1634
1635/**
1636 * A JavaScript function object (ECMA-262, 15.3).
1637 */
Steve Block8defd9f2010-07-08 12:39:36 +01001638class Function : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00001639 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001640 V8EXPORT Local<Object> NewInstance() const;
1641 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1642 V8EXPORT Local<Value> Call(Handle<Object> recv,
1643 int argc,
1644 Handle<Value> argv[]);
1645 V8EXPORT void SetName(Handle<String> name);
1646 V8EXPORT Handle<Value> GetName() const;
Andrei Popescu402d9372010-02-26 13:31:12 +00001647
1648 /**
1649 * Returns zero based line number of function body and
1650 * kLineOffsetNotFound if no information available.
1651 */
Steve Block8defd9f2010-07-08 12:39:36 +01001652 V8EXPORT int GetScriptLineNumber() const;
1653 V8EXPORT ScriptOrigin GetScriptOrigin() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001654 static inline Function* Cast(Value* obj);
Steve Block8defd9f2010-07-08 12:39:36 +01001655 V8EXPORT static const int kLineOffsetNotFound;
Steve Blocka7e24c12009-10-30 11:49:00 +00001656 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001657 V8EXPORT Function();
1658 V8EXPORT static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001659};
1660
1661
1662/**
1663 * A JavaScript value that wraps a C++ void*. This type of value is
1664 * mainly used to associate C++ data structures with JavaScript
1665 * objects.
1666 *
1667 * The Wrap function V8 will return the most optimal Value object wrapping the
1668 * C++ void*. The type of the value is not guaranteed to be an External object
1669 * and no assumptions about its type should be made. To access the wrapped
1670 * value Unwrap should be used, all other operations on that object will lead
1671 * to unpredictable results.
1672 */
Steve Block8defd9f2010-07-08 12:39:36 +01001673class External : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00001674 public:
Steve Block8defd9f2010-07-08 12:39:36 +01001675 V8EXPORT static Local<Value> Wrap(void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001676 static inline void* Unwrap(Handle<Value> obj);
1677
Steve Block8defd9f2010-07-08 12:39:36 +01001678 V8EXPORT static Local<External> New(void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 static inline External* Cast(Value* obj);
Steve Block8defd9f2010-07-08 12:39:36 +01001680 V8EXPORT void* Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001681 private:
Steve Block8defd9f2010-07-08 12:39:36 +01001682 V8EXPORT External();
1683 V8EXPORT static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001684 static inline void* QuickUnwrap(Handle<v8::Value> obj);
Steve Block8defd9f2010-07-08 12:39:36 +01001685 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00001686};
1687
1688
1689// --- T e m p l a t e s ---
1690
1691
1692/**
1693 * The superclass of object and function templates.
1694 */
1695class V8EXPORT Template : public Data {
1696 public:
1697 /** Adds a property to each instance created by this template.*/
1698 void Set(Handle<String> name, Handle<Data> value,
1699 PropertyAttribute attributes = None);
1700 inline void Set(const char* name, Handle<Data> value);
1701 private:
1702 Template();
1703
1704 friend class ObjectTemplate;
1705 friend class FunctionTemplate;
1706};
1707
1708
1709/**
1710 * The argument information given to function call callbacks. This
1711 * class provides access to information about the context of the call,
1712 * including the receiver, the number and values of arguments, and
1713 * the holder of the function.
1714 */
Steve Block8defd9f2010-07-08 12:39:36 +01001715class Arguments {
Steve Blocka7e24c12009-10-30 11:49:00 +00001716 public:
1717 inline int Length() const;
1718 inline Local<Value> operator[](int i) const;
1719 inline Local<Function> Callee() const;
1720 inline Local<Object> This() const;
1721 inline Local<Object> Holder() const;
1722 inline bool IsConstructCall() const;
1723 inline Local<Value> Data() const;
1724 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00001725 friend class ImplementationUtilities;
1726 inline Arguments(Local<Value> data,
1727 Local<Object> holder,
1728 Local<Function> callee,
1729 bool is_construct_call,
1730 void** values, int length);
1731 Local<Value> data_;
1732 Local<Object> holder_;
1733 Local<Function> callee_;
1734 bool is_construct_call_;
1735 void** values_;
1736 int length_;
1737};
1738
1739
1740/**
1741 * The information passed to an accessor callback about the context
1742 * of the property access.
1743 */
1744class V8EXPORT AccessorInfo {
1745 public:
1746 inline AccessorInfo(internal::Object** args)
1747 : args_(args) { }
1748 inline Local<Value> Data() const;
1749 inline Local<Object> This() const;
1750 inline Local<Object> Holder() const;
1751 private:
1752 internal::Object** args_;
1753};
1754
1755
1756typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1757
1758typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1759
1760/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001761 * NamedProperty[Getter|Setter] are used as interceptors on object.
1762 * See ObjectTemplate::SetNamedPropertyHandler.
1763 */
1764typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1765 const AccessorInfo& info);
1766
1767
1768/**
1769 * Returns the value if the setter intercepts the request.
1770 * Otherwise, returns an empty handle.
1771 */
1772typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1773 Local<Value> value,
1774 const AccessorInfo& info);
1775
Steve Blocka7e24c12009-10-30 11:49:00 +00001776/**
1777 * Returns a non-empty handle if the interceptor intercepts the request.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001778 * The result is an integer encoding property attributes (like v8::None,
1779 * v8::DontEnum, etc.)
Steve Blocka7e24c12009-10-30 11:49:00 +00001780 */
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001781typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1782 const AccessorInfo& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00001783
1784
1785/**
1786 * Returns a non-empty handle if the deleter intercepts the request.
1787 * The return value is true if the property could be deleted and false
1788 * otherwise.
1789 */
1790typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1791 const AccessorInfo& info);
1792
1793/**
1794 * Returns an array containing the names of the properties the named
1795 * property getter intercepts.
1796 */
1797typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1798
1799
1800/**
1801 * Returns the value of the property if the getter intercepts the
1802 * request. Otherwise, returns an empty handle.
1803 */
1804typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1805 const AccessorInfo& info);
1806
1807
1808/**
1809 * Returns the value if the setter intercepts the request.
1810 * Otherwise, returns an empty handle.
1811 */
1812typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1813 Local<Value> value,
1814 const AccessorInfo& info);
1815
1816
1817/**
1818 * Returns a non-empty handle if the interceptor intercepts the request.
1819 * The result is true if the property exists and false otherwise.
1820 */
1821typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1822 const AccessorInfo& info);
1823
1824/**
1825 * Returns a non-empty handle if the deleter intercepts the request.
1826 * The return value is true if the property could be deleted and false
1827 * otherwise.
1828 */
1829typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1830 const AccessorInfo& info);
1831
1832/**
1833 * Returns an array containing the indices of the properties the
1834 * indexed property getter intercepts.
1835 */
1836typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1837
1838
1839/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001840 * Access type specification.
1841 */
1842enum AccessType {
1843 ACCESS_GET,
1844 ACCESS_SET,
1845 ACCESS_HAS,
1846 ACCESS_DELETE,
1847 ACCESS_KEYS
1848};
1849
1850
1851/**
1852 * Returns true if cross-context access should be allowed to the named
1853 * property with the given key on the host object.
1854 */
1855typedef bool (*NamedSecurityCallback)(Local<Object> host,
1856 Local<Value> key,
1857 AccessType type,
1858 Local<Value> data);
1859
1860
1861/**
1862 * Returns true if cross-context access should be allowed to the indexed
1863 * property with the given index on the host object.
1864 */
1865typedef bool (*IndexedSecurityCallback)(Local<Object> host,
1866 uint32_t index,
1867 AccessType type,
1868 Local<Value> data);
1869
1870
1871/**
1872 * A FunctionTemplate is used to create functions at runtime. There
1873 * can only be one function created from a FunctionTemplate in a
1874 * context. The lifetime of the created function is equal to the
1875 * lifetime of the context. So in case the embedder needs to create
1876 * temporary functions that can be collected using Scripts is
1877 * preferred.
1878 *
1879 * A FunctionTemplate can have properties, these properties are added to the
1880 * function object when it is created.
1881 *
1882 * A FunctionTemplate has a corresponding instance template which is
1883 * used to create object instances when the function is used as a
1884 * constructor. Properties added to the instance template are added to
1885 * each object instance.
1886 *
1887 * A FunctionTemplate can have a prototype template. The prototype template
1888 * is used to create the prototype object of the function.
1889 *
1890 * The following example shows how to use a FunctionTemplate:
1891 *
1892 * \code
1893 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1894 * t->Set("func_property", v8::Number::New(1));
1895 *
1896 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1897 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1898 * proto_t->Set("proto_const", v8::Number::New(2));
1899 *
1900 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1901 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1902 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1903 * instance_t->Set("instance_property", Number::New(3));
1904 *
1905 * v8::Local<v8::Function> function = t->GetFunction();
1906 * v8::Local<v8::Object> instance = function->NewInstance();
1907 * \endcode
1908 *
1909 * Let's use "function" as the JS variable name of the function object
1910 * and "instance" for the instance object created above. The function
1911 * and the instance will have the following properties:
1912 *
1913 * \code
1914 * func_property in function == true;
1915 * function.func_property == 1;
1916 *
1917 * function.prototype.proto_method() invokes 'InvokeCallback'
1918 * function.prototype.proto_const == 2;
1919 *
1920 * instance instanceof function == true;
1921 * instance.instance_accessor calls 'InstanceAccessorCallback'
1922 * instance.instance_property == 3;
1923 * \endcode
1924 *
1925 * A FunctionTemplate can inherit from another one by calling the
1926 * FunctionTemplate::Inherit method. The following graph illustrates
1927 * the semantics of inheritance:
1928 *
1929 * \code
1930 * FunctionTemplate Parent -> Parent() . prototype -> { }
1931 * ^ ^
1932 * | Inherit(Parent) | .__proto__
1933 * | |
1934 * FunctionTemplate Child -> Child() . prototype -> { }
1935 * \endcode
1936 *
1937 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1938 * object of the Child() function has __proto__ pointing to the
1939 * Parent() function's prototype object. An instance of the Child
1940 * function has all properties on Parent's instance templates.
1941 *
1942 * Let Parent be the FunctionTemplate initialized in the previous
1943 * section and create a Child FunctionTemplate by:
1944 *
1945 * \code
1946 * Local<FunctionTemplate> parent = t;
1947 * Local<FunctionTemplate> child = FunctionTemplate::New();
1948 * child->Inherit(parent);
1949 *
1950 * Local<Function> child_function = child->GetFunction();
1951 * Local<Object> child_instance = child_function->NewInstance();
1952 * \endcode
1953 *
1954 * The Child function and Child instance will have the following
1955 * properties:
1956 *
1957 * \code
1958 * child_func.prototype.__proto__ == function.prototype;
1959 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
1960 * child_instance.instance_property == 3;
1961 * \endcode
1962 */
1963class V8EXPORT FunctionTemplate : public Template {
1964 public:
1965 /** Creates a function template.*/
1966 static Local<FunctionTemplate> New(
1967 InvocationCallback callback = 0,
1968 Handle<Value> data = Handle<Value>(),
1969 Handle<Signature> signature = Handle<Signature>());
1970 /** Returns the unique function instance in the current execution context.*/
1971 Local<Function> GetFunction();
1972
1973 /**
1974 * Set the call-handler callback for a FunctionTemplate. This
1975 * callback is called whenever the function created from this
1976 * FunctionTemplate is called.
1977 */
1978 void SetCallHandler(InvocationCallback callback,
1979 Handle<Value> data = Handle<Value>());
1980
1981 /** Get the InstanceTemplate. */
1982 Local<ObjectTemplate> InstanceTemplate();
1983
1984 /** Causes the function template to inherit from a parent function template.*/
1985 void Inherit(Handle<FunctionTemplate> parent);
1986
1987 /**
1988 * A PrototypeTemplate is the template used to create the prototype object
1989 * of the function created by this template.
1990 */
1991 Local<ObjectTemplate> PrototypeTemplate();
1992
1993
1994 /**
1995 * Set the class name of the FunctionTemplate. This is used for
1996 * printing objects created with the function created from the
1997 * FunctionTemplate as its constructor.
1998 */
1999 void SetClassName(Handle<String> name);
2000
2001 /**
2002 * Determines whether the __proto__ accessor ignores instances of
2003 * the function template. If instances of the function template are
2004 * ignored, __proto__ skips all instances and instead returns the
2005 * next object in the prototype chain.
2006 *
2007 * Call with a value of true to make the __proto__ accessor ignore
2008 * instances of the function template. Call with a value of false
2009 * to make the __proto__ accessor not ignore instances of the
2010 * function template. By default, instances of a function template
2011 * are not ignored.
2012 */
2013 void SetHiddenPrototype(bool value);
2014
2015 /**
2016 * Returns true if the given object is an instance of this function
2017 * template.
2018 */
2019 bool HasInstance(Handle<Value> object);
2020
2021 private:
2022 FunctionTemplate();
2023 void AddInstancePropertyAccessor(Handle<String> name,
2024 AccessorGetter getter,
2025 AccessorSetter setter,
2026 Handle<Value> data,
2027 AccessControl settings,
2028 PropertyAttribute attributes);
2029 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2030 NamedPropertySetter setter,
2031 NamedPropertyQuery query,
2032 NamedPropertyDeleter remover,
2033 NamedPropertyEnumerator enumerator,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002034 Handle<Value> data);
Steve Blocka7e24c12009-10-30 11:49:00 +00002035 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2036 IndexedPropertySetter setter,
2037 IndexedPropertyQuery query,
2038 IndexedPropertyDeleter remover,
2039 IndexedPropertyEnumerator enumerator,
2040 Handle<Value> data);
2041 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2042 Handle<Value> data);
2043
2044 friend class Context;
2045 friend class ObjectTemplate;
2046};
2047
2048
2049/**
2050 * An ObjectTemplate is used to create objects at runtime.
2051 *
2052 * Properties added to an ObjectTemplate are added to each object
2053 * created from the ObjectTemplate.
2054 */
2055class V8EXPORT ObjectTemplate : public Template {
2056 public:
2057 /** Creates an ObjectTemplate. */
2058 static Local<ObjectTemplate> New();
2059
2060 /** Creates a new instance of this template.*/
2061 Local<Object> NewInstance();
2062
2063 /**
2064 * Sets an accessor on the object template.
2065 *
2066 * Whenever the property with the given name is accessed on objects
2067 * created from this ObjectTemplate the getter and setter callbacks
2068 * are called instead of getting and setting the property directly
2069 * on the JavaScript object.
2070 *
2071 * \param name The name of the property for which an accessor is added.
2072 * \param getter The callback to invoke when getting the property.
2073 * \param setter The callback to invoke when setting the property.
2074 * \param data A piece of data that will be passed to the getter and setter
2075 * callbacks whenever they are invoked.
2076 * \param settings Access control settings for the accessor. This is a bit
2077 * field consisting of one of more of
2078 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2079 * The default is to not allow cross-context access.
2080 * ALL_CAN_READ means that all cross-context reads are allowed.
2081 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2082 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2083 * cross-context access.
2084 * \param attribute The attributes of the property for which an accessor
2085 * is added.
2086 */
2087 void SetAccessor(Handle<String> name,
2088 AccessorGetter getter,
2089 AccessorSetter setter = 0,
2090 Handle<Value> data = Handle<Value>(),
2091 AccessControl settings = DEFAULT,
2092 PropertyAttribute attribute = None);
2093
2094 /**
2095 * Sets a named property handler on the object template.
2096 *
2097 * Whenever a named property is accessed on objects created from
2098 * this object template, the provided callback is invoked instead of
2099 * accessing the property directly on the JavaScript object.
2100 *
2101 * \param getter The callback to invoke when getting a property.
2102 * \param setter The callback to invoke when setting a property.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002103 * \param query The callback to invoke to check if a property is present,
2104 * and if present, get its attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00002105 * \param deleter The callback to invoke when deleting a property.
2106 * \param enumerator The callback to invoke to enumerate all the named
2107 * properties of an object.
2108 * \param data A piece of data that will be passed to the callbacks
2109 * whenever they are invoked.
2110 */
2111 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2112 NamedPropertySetter setter = 0,
2113 NamedPropertyQuery query = 0,
2114 NamedPropertyDeleter deleter = 0,
2115 NamedPropertyEnumerator enumerator = 0,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002116 Handle<Value> data = Handle<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00002117
2118 /**
2119 * Sets an indexed property handler on the object template.
2120 *
2121 * Whenever an indexed property is accessed on objects created from
2122 * this object template, the provided callback is invoked instead of
2123 * accessing the property directly on the JavaScript object.
2124 *
2125 * \param getter The callback to invoke when getting a property.
2126 * \param setter The callback to invoke when setting a property.
2127 * \param query The callback to invoke to check is an object has a property.
2128 * \param deleter The callback to invoke when deleting a property.
2129 * \param enumerator The callback to invoke to enumerate all the indexed
2130 * properties of an object.
2131 * \param data A piece of data that will be passed to the callbacks
2132 * whenever they are invoked.
2133 */
2134 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2135 IndexedPropertySetter setter = 0,
2136 IndexedPropertyQuery query = 0,
2137 IndexedPropertyDeleter deleter = 0,
2138 IndexedPropertyEnumerator enumerator = 0,
2139 Handle<Value> data = Handle<Value>());
2140 /**
2141 * Sets the callback to be used when calling instances created from
2142 * this template as a function. If no callback is set, instances
2143 * behave like normal JavaScript objects that cannot be called as a
2144 * function.
2145 */
2146 void SetCallAsFunctionHandler(InvocationCallback callback,
2147 Handle<Value> data = Handle<Value>());
2148
2149 /**
2150 * Mark object instances of the template as undetectable.
2151 *
2152 * In many ways, undetectable objects behave as though they are not
2153 * there. They behave like 'undefined' in conditionals and when
2154 * printed. However, properties can be accessed and called as on
2155 * normal objects.
2156 */
2157 void MarkAsUndetectable();
2158
2159 /**
2160 * Sets access check callbacks on the object template.
2161 *
2162 * When accessing properties on instances of this object template,
2163 * the access check callback will be called to determine whether or
2164 * not to allow cross-context access to the properties.
2165 * The last parameter specifies whether access checks are turned
2166 * on by default on instances. If access checks are off by default,
2167 * they can be turned on on individual instances by calling
2168 * Object::TurnOnAccessCheck().
2169 */
2170 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2171 IndexedSecurityCallback indexed_handler,
2172 Handle<Value> data = Handle<Value>(),
2173 bool turned_on_by_default = true);
2174
2175 /**
2176 * Gets the number of internal fields for objects generated from
2177 * this template.
2178 */
2179 int InternalFieldCount();
2180
2181 /**
2182 * Sets the number of internal fields for objects generated from
2183 * this template.
2184 */
2185 void SetInternalFieldCount(int value);
2186
2187 private:
2188 ObjectTemplate();
2189 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2190 friend class FunctionTemplate;
2191};
2192
2193
2194/**
2195 * A Signature specifies which receivers and arguments a function can
2196 * legally be called with.
2197 */
2198class V8EXPORT Signature : public Data {
2199 public:
2200 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2201 Handle<FunctionTemplate>(),
2202 int argc = 0,
2203 Handle<FunctionTemplate> argv[] = 0);
2204 private:
2205 Signature();
2206};
2207
2208
2209/**
2210 * A utility for determining the type of objects based on the template
2211 * they were constructed from.
2212 */
2213class V8EXPORT TypeSwitch : public Data {
2214 public:
2215 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2216 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2217 int match(Handle<Value> value);
2218 private:
2219 TypeSwitch();
2220};
2221
2222
2223// --- E x t e n s i o n s ---
2224
2225
2226/**
2227 * Ignore
2228 */
2229class V8EXPORT Extension { // NOLINT
2230 public:
2231 Extension(const char* name,
2232 const char* source = 0,
2233 int dep_count = 0,
2234 const char** deps = 0);
2235 virtual ~Extension() { }
2236 virtual v8::Handle<v8::FunctionTemplate>
2237 GetNativeFunction(v8::Handle<v8::String> name) {
2238 return v8::Handle<v8::FunctionTemplate>();
2239 }
2240
2241 const char* name() { return name_; }
2242 const char* source() { return source_; }
2243 int dependency_count() { return dep_count_; }
2244 const char** dependencies() { return deps_; }
2245 void set_auto_enable(bool value) { auto_enable_ = value; }
2246 bool auto_enable() { return auto_enable_; }
2247
2248 private:
2249 const char* name_;
2250 const char* source_;
2251 int dep_count_;
2252 const char** deps_;
2253 bool auto_enable_;
2254
2255 // Disallow copying and assigning.
2256 Extension(const Extension&);
2257 void operator=(const Extension&);
2258};
2259
2260
2261void V8EXPORT RegisterExtension(Extension* extension);
2262
2263
2264/**
2265 * Ignore
2266 */
2267class V8EXPORT DeclareExtension {
2268 public:
2269 inline DeclareExtension(Extension* extension) {
2270 RegisterExtension(extension);
2271 }
2272};
2273
2274
2275// --- S t a t i c s ---
2276
2277
2278Handle<Primitive> V8EXPORT Undefined();
2279Handle<Primitive> V8EXPORT Null();
2280Handle<Boolean> V8EXPORT True();
2281Handle<Boolean> V8EXPORT False();
2282
2283
2284/**
2285 * A set of constraints that specifies the limits of the runtime's memory use.
2286 * You must set the heap size before initializing the VM - the size cannot be
2287 * adjusted after the VM is initialized.
2288 *
2289 * If you are using threads then you should hold the V8::Locker lock while
2290 * setting the stack limit and you must set a non-default stack limit separately
2291 * for each thread.
2292 */
2293class V8EXPORT ResourceConstraints {
2294 public:
2295 ResourceConstraints();
2296 int max_young_space_size() const { return max_young_space_size_; }
2297 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
2298 int max_old_space_size() const { return max_old_space_size_; }
2299 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
2300 uint32_t* stack_limit() const { return stack_limit_; }
2301 // Sets an address beyond which the VM's stack may not grow.
2302 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2303 private:
2304 int max_young_space_size_;
2305 int max_old_space_size_;
2306 uint32_t* stack_limit_;
2307};
2308
2309
Kristian Monsen25f61362010-05-21 11:50:48 +01002310bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
Steve Blocka7e24c12009-10-30 11:49:00 +00002311
2312
2313// --- E x c e p t i o n s ---
2314
2315
2316typedef void (*FatalErrorCallback)(const char* location, const char* message);
2317
2318
2319typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2320
2321
2322/**
2323 * Schedules an exception to be thrown when returning to JavaScript. When an
2324 * exception has been scheduled it is illegal to invoke any JavaScript
2325 * operation; the caller must return immediately and only after the exception
2326 * has been handled does it become legal to invoke JavaScript operations.
2327 */
2328Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
2329
2330/**
2331 * Create new error objects by calling the corresponding error object
2332 * constructor with the message.
2333 */
2334class V8EXPORT Exception {
2335 public:
2336 static Local<Value> RangeError(Handle<String> message);
2337 static Local<Value> ReferenceError(Handle<String> message);
2338 static Local<Value> SyntaxError(Handle<String> message);
2339 static Local<Value> TypeError(Handle<String> message);
2340 static Local<Value> Error(Handle<String> message);
2341};
2342
2343
2344// --- C o u n t e r s C a l l b a c k s ---
2345
2346typedef int* (*CounterLookupCallback)(const char* name);
2347
2348typedef void* (*CreateHistogramCallback)(const char* name,
2349 int min,
2350 int max,
2351 size_t buckets);
2352
2353typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2354
2355// --- 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 ---
2356typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2357 AccessType type,
2358 Local<Value> data);
2359
2360// --- 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
2361
2362/**
Steve Block6ded16b2010-05-10 14:33:55 +01002363 * Applications can register callback functions which will be called
2364 * before and after a garbage collection. Allocations are not
2365 * allowed in the callback functions, you therefore cannot manipulate
Steve Blocka7e24c12009-10-30 11:49:00 +00002366 * objects (set or delete properties for example) since it is possible
2367 * such operations will result in the allocation of objects.
2368 */
Steve Block6ded16b2010-05-10 14:33:55 +01002369enum GCType {
2370 kGCTypeScavenge = 1 << 0,
2371 kGCTypeMarkSweepCompact = 1 << 1,
2372 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2373};
2374
2375enum GCCallbackFlags {
2376 kNoGCCallbackFlags = 0,
2377 kGCCallbackFlagCompacted = 1 << 0
2378};
2379
2380typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2381typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2382
Steve Blocka7e24c12009-10-30 11:49:00 +00002383typedef void (*GCCallback)();
2384
2385
Steve Blocka7e24c12009-10-30 11:49:00 +00002386/**
2387 * Profiler modules.
2388 *
2389 * In V8, profiler consists of several modules: CPU profiler, and different
2390 * kinds of heap profiling. Each can be turned on / off independently.
2391 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2392 * modules are enabled only temporarily for making a snapshot of the heap.
2393 */
2394enum ProfilerModules {
2395 PROFILER_MODULE_NONE = 0,
2396 PROFILER_MODULE_CPU = 1,
2397 PROFILER_MODULE_HEAP_STATS = 1 << 1,
2398 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2399 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
2400};
2401
2402
2403/**
Steve Block3ce2e202009-11-05 08:53:23 +00002404 * Collection of V8 heap information.
2405 *
2406 * Instances of this class can be passed to v8::V8::HeapStatistics to
2407 * get heap statistics from V8.
2408 */
2409class V8EXPORT HeapStatistics {
2410 public:
2411 HeapStatistics();
2412 size_t total_heap_size() { return total_heap_size_; }
2413 size_t used_heap_size() { return used_heap_size_; }
2414
2415 private:
2416 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2417 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2418
2419 size_t total_heap_size_;
2420 size_t used_heap_size_;
2421
2422 friend class V8;
2423};
2424
2425
2426/**
Steve Blocka7e24c12009-10-30 11:49:00 +00002427 * Container class for static utility functions.
2428 */
2429class V8EXPORT V8 {
2430 public:
2431 /** Set the callback to invoke in case of fatal errors. */
2432 static void SetFatalErrorHandler(FatalErrorCallback that);
2433
2434 /**
2435 * Ignore out-of-memory exceptions.
2436 *
2437 * V8 running out of memory is treated as a fatal error by default.
2438 * This means that the fatal error handler is called and that V8 is
2439 * terminated.
2440 *
2441 * IgnoreOutOfMemoryException can be used to not treat a
2442 * out-of-memory situation as a fatal error. This way, the contexts
2443 * that did not cause the out of memory problem might be able to
2444 * continue execution.
2445 */
2446 static void IgnoreOutOfMemoryException();
2447
2448 /**
2449 * Check if V8 is dead and therefore unusable. This is the case after
2450 * fatal errors such as out-of-memory situations.
2451 */
2452 static bool IsDead();
2453
2454 /**
2455 * Adds a message listener.
2456 *
2457 * The same message listener can be added more than once and it that
2458 * case it will be called more than once for each message.
2459 */
2460 static bool AddMessageListener(MessageCallback that,
2461 Handle<Value> data = Handle<Value>());
2462
2463 /**
2464 * Remove all message listeners from the specified callback function.
2465 */
2466 static void RemoveMessageListeners(MessageCallback that);
2467
2468 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002469 * Tells V8 to capture current stack trace when uncaught exception occurs
2470 * and report it to the message listeners. The option is off by default.
2471 */
2472 static void SetCaptureStackTraceForUncaughtExceptions(
2473 bool capture,
2474 int frame_limit = 10,
2475 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2476
2477 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002478 * Sets V8 flags from a string.
2479 */
2480 static void SetFlagsFromString(const char* str, int length);
2481
2482 /**
2483 * Sets V8 flags from the command line.
2484 */
2485 static void SetFlagsFromCommandLine(int* argc,
2486 char** argv,
2487 bool remove_flags);
2488
2489 /** Get the version string. */
2490 static const char* GetVersion();
2491
2492 /**
2493 * Enables the host application to provide a mechanism for recording
2494 * statistics counters.
2495 */
2496 static void SetCounterFunction(CounterLookupCallback);
2497
2498 /**
2499 * Enables the host application to provide a mechanism for recording
2500 * histograms. The CreateHistogram function returns a
2501 * histogram which will later be passed to the AddHistogramSample
2502 * function.
2503 */
2504 static void SetCreateHistogramFunction(CreateHistogramCallback);
2505 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2506
2507 /**
2508 * Enables the computation of a sliding window of states. The sliding
2509 * window information is recorded in statistics counters.
2510 */
2511 static void EnableSlidingStateWindow();
2512
2513 /** Callback function for reporting failed access checks.*/
2514 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2515
2516 /**
2517 * Enables the host application to receive a notification before a
Steve Block6ded16b2010-05-10 14:33:55 +01002518 * garbage collection. Allocations are not allowed in the
2519 * callback function, you therefore cannot manipulate objects (set
2520 * or delete properties for example) since it is possible such
2521 * operations will result in the allocation of objects. It is possible
2522 * to specify the GCType filter for your callback. But it is not possible to
2523 * register the same callback function two times with different
2524 * GCType filters.
2525 */
2526 static void AddGCPrologueCallback(
2527 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2528
2529 /**
2530 * This function removes callback which was installed by
2531 * AddGCPrologueCallback function.
2532 */
2533 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2534
2535 /**
2536 * The function is deprecated. Please use AddGCPrologueCallback instead.
2537 * Enables the host application to receive a notification before a
2538 * garbage collection. Allocations are not allowed in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002539 * callback function, you therefore cannot manipulate objects (set
2540 * or delete properties for example) since it is possible such
2541 * operations will result in the allocation of objects.
2542 */
2543 static void SetGlobalGCPrologueCallback(GCCallback);
2544
2545 /**
2546 * Enables the host application to receive a notification after a
Steve Block6ded16b2010-05-10 14:33:55 +01002547 * garbage collection. Allocations are not allowed in the
2548 * callback function, you therefore cannot manipulate objects (set
2549 * or delete properties for example) since it is possible such
2550 * operations will result in the allocation of objects. It is possible
2551 * to specify the GCType filter for your callback. But it is not possible to
2552 * register the same callback function two times with different
2553 * GCType filters.
2554 */
2555 static void AddGCEpilogueCallback(
2556 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2557
2558 /**
2559 * This function removes callback which was installed by
2560 * AddGCEpilogueCallback function.
2561 */
2562 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2563
2564 /**
2565 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2566 * Enables the host application to receive a notification after a
Steve Blocka7e24c12009-10-30 11:49:00 +00002567 * major garbage collection. Allocations are not allowed in the
2568 * callback function, you therefore cannot manipulate objects (set
2569 * or delete properties for example) since it is possible such
2570 * operations will result in the allocation of objects.
2571 */
2572 static void SetGlobalGCEpilogueCallback(GCCallback);
2573
2574 /**
2575 * Allows the host application to group objects together. If one
2576 * object in the group is alive, all objects in the group are alive.
2577 * After each garbage collection, object groups are removed. It is
2578 * intended to be used in the before-garbage-collection callback
2579 * function, for instance to simulate DOM tree connections among JS
2580 * wrapper objects.
2581 */
2582 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
2583
2584 /**
2585 * Initializes from snapshot if possible. Otherwise, attempts to
2586 * initialize from scratch. This function is called implicitly if
2587 * you use the API without calling it first.
2588 */
2589 static bool Initialize();
2590
2591 /**
2592 * Adjusts the amount of registered external memory. Used to give
2593 * V8 an indication of the amount of externally allocated memory
2594 * that is kept alive by JavaScript objects. V8 uses this to decide
2595 * when to perform global garbage collections. Registering
2596 * externally allocated memory will trigger global garbage
2597 * collections more often than otherwise in an attempt to garbage
2598 * collect the JavaScript objects keeping the externally allocated
2599 * memory alive.
2600 *
2601 * \param change_in_bytes the change in externally allocated memory
2602 * that is kept alive by JavaScript objects.
2603 * \returns the adjusted value.
2604 */
2605 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2606
2607 /**
2608 * Suspends recording of tick samples in the profiler.
2609 * When the V8 profiling mode is enabled (usually via command line
2610 * switches) this function suspends recording of tick samples.
2611 * Profiling ticks are discarded until ResumeProfiler() is called.
2612 *
2613 * See also the --prof and --prof_auto command line switches to
2614 * enable V8 profiling.
2615 */
2616 static void PauseProfiler();
2617
2618 /**
2619 * Resumes recording of tick samples in the profiler.
2620 * See also PauseProfiler().
2621 */
2622 static void ResumeProfiler();
2623
2624 /**
2625 * Return whether profiler is currently paused.
2626 */
2627 static bool IsProfilerPaused();
2628
2629 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002630 * Resumes specified profiler modules. Can be called several times to
2631 * mark the opening of a profiler events block with the given tag.
2632 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002633 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2634 * See ProfilerModules enum.
2635 *
2636 * \param flags Flags specifying profiler modules.
Andrei Popescu402d9372010-02-26 13:31:12 +00002637 * \param tag Profile tag.
Steve Blocka7e24c12009-10-30 11:49:00 +00002638 */
Andrei Popescu402d9372010-02-26 13:31:12 +00002639 static void ResumeProfilerEx(int flags, int tag = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002640
2641 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002642 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2643 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2644 * same tag value. There is no need for blocks to be properly nested.
2645 * The profiler is paused when the last opened block is closed.
2646 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002647 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2648 * See ProfilerModules enum.
2649 *
2650 * \param flags Flags specifying profiler modules.
Andrei Popescu402d9372010-02-26 13:31:12 +00002651 * \param tag Profile tag.
Steve Blocka7e24c12009-10-30 11:49:00 +00002652 */
Andrei Popescu402d9372010-02-26 13:31:12 +00002653 static void PauseProfilerEx(int flags, int tag = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002654
2655 /**
2656 * Returns active (resumed) profiler modules.
2657 * See ProfilerModules enum.
2658 *
2659 * \returns active profiler modules.
2660 */
2661 static int GetActiveProfilerModules();
2662
2663 /**
2664 * If logging is performed into a memory buffer (via --logfile=*), allows to
2665 * retrieve previously written messages. This can be used for retrieving
2666 * profiler log data in the application. This function is thread-safe.
2667 *
2668 * Caller provides a destination buffer that must exist during GetLogLines
2669 * call. Only whole log lines are copied into the buffer.
2670 *
2671 * \param from_pos specified a point in a buffer to read from, 0 is the
2672 * beginning of a buffer. It is assumed that caller updates its current
2673 * position using returned size value from the previous call.
2674 * \param dest_buf destination buffer for log data.
2675 * \param max_size size of the destination buffer.
2676 * \returns actual size of log data copied into buffer.
2677 */
2678 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2679
2680 /**
Steve Block6ded16b2010-05-10 14:33:55 +01002681 * The minimum allowed size for a log lines buffer. If the size of
2682 * the buffer given will not be enough to hold a line of the maximum
2683 * length, an attempt to find a log line end in GetLogLines will
2684 * fail, and an empty result will be returned.
2685 */
2686 static const int kMinimumSizeForLogLinesBuffer = 2048;
2687
2688 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002689 * Retrieve the V8 thread id of the calling thread.
2690 *
2691 * The thread id for a thread should only be retrieved after the V8
2692 * lock has been acquired with a Locker object with that thread.
2693 */
2694 static int GetCurrentThreadId();
2695
2696 /**
2697 * Forcefully terminate execution of a JavaScript thread. This can
2698 * be used to terminate long-running scripts.
2699 *
2700 * TerminateExecution should only be called when then V8 lock has
2701 * been acquired with a Locker object. Therefore, in order to be
2702 * able to terminate long-running threads, preemption must be
2703 * enabled to allow the user of TerminateExecution to acquire the
2704 * lock.
2705 *
2706 * The termination is achieved by throwing an exception that is
2707 * uncatchable by JavaScript exception handlers. Termination
2708 * exceptions act as if they were caught by a C++ TryCatch exception
2709 * handlers. If forceful termination is used, any C++ TryCatch
2710 * exception handler that catches an exception should check if that
2711 * exception is a termination exception and immediately return if
2712 * that is the case. Returning immediately in that case will
2713 * continue the propagation of the termination exception if needed.
2714 *
2715 * The thread id passed to TerminateExecution must have been
2716 * obtained by calling GetCurrentThreadId on the thread in question.
2717 *
2718 * \param thread_id The thread id of the thread to terminate.
2719 */
2720 static void TerminateExecution(int thread_id);
2721
2722 /**
2723 * Forcefully terminate the current thread of JavaScript execution.
2724 *
2725 * This method can be used by any thread even if that thread has not
2726 * acquired the V8 lock with a Locker object.
2727 */
2728 static void TerminateExecution();
2729
2730 /**
Steve Block6ded16b2010-05-10 14:33:55 +01002731 * Is V8 terminating JavaScript execution.
2732 *
2733 * Returns true if JavaScript execution is currently terminating
2734 * because of a call to TerminateExecution. In that case there are
2735 * still JavaScript frames on the stack and the termination
2736 * exception is still active.
2737 */
2738 static bool IsExecutionTerminating();
2739
2740 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002741 * Releases any resources used by v8 and stops any utility threads
2742 * that may be running. Note that disposing v8 is permanent, it
2743 * cannot be reinitialized.
2744 *
2745 * It should generally not be necessary to dispose v8 before exiting
2746 * a process, this should happen automatically. It is only necessary
2747 * to use if the process needs the resources taken up by v8.
2748 */
2749 static bool Dispose();
2750
Steve Block3ce2e202009-11-05 08:53:23 +00002751 /**
2752 * Get statistics about the heap memory usage.
2753 */
2754 static void GetHeapStatistics(HeapStatistics* heap_statistics);
Steve Blocka7e24c12009-10-30 11:49:00 +00002755
2756 /**
2757 * Optional notification that the embedder is idle.
2758 * V8 uses the notification to reduce memory footprint.
2759 * This call can be used repeatedly if the embedder remains idle.
Steve Blocka7e24c12009-10-30 11:49:00 +00002760 * Returns true if the embedder should stop calling IdleNotification
2761 * until real work has been done. This indicates that V8 has done
2762 * as much cleanup as it will be able to do.
2763 */
Steve Block3ce2e202009-11-05 08:53:23 +00002764 static bool IdleNotification();
Steve Blocka7e24c12009-10-30 11:49:00 +00002765
2766 /**
2767 * Optional notification that the system is running low on memory.
2768 * V8 uses these notifications to attempt to free memory.
2769 */
2770 static void LowMemoryNotification();
2771
Steve Block6ded16b2010-05-10 14:33:55 +01002772 /**
2773 * Optional notification that a context has been disposed. V8 uses
2774 * these notifications to guide the GC heuristic. Returns the number
2775 * of context disposals - including this one - since the last time
2776 * V8 had a chance to clean up.
2777 */
2778 static int ContextDisposedNotification();
2779
Steve Blocka7e24c12009-10-30 11:49:00 +00002780 private:
2781 V8();
2782
2783 static internal::Object** GlobalizeReference(internal::Object** handle);
2784 static void DisposeGlobal(internal::Object** global_handle);
2785 static void MakeWeak(internal::Object** global_handle,
2786 void* data,
2787 WeakReferenceCallback);
2788 static void ClearWeak(internal::Object** global_handle);
2789 static bool IsGlobalNearDeath(internal::Object** global_handle);
2790 static bool IsGlobalWeak(internal::Object** global_handle);
2791
2792 template <class T> friend class Handle;
2793 template <class T> friend class Local;
2794 template <class T> friend class Persistent;
2795 friend class Context;
2796};
2797
2798
2799/**
2800 * An external exception handler.
2801 */
2802class V8EXPORT TryCatch {
2803 public:
2804
2805 /**
2806 * Creates a new try/catch block and registers it with v8.
2807 */
2808 TryCatch();
2809
2810 /**
2811 * Unregisters and deletes this try/catch block.
2812 */
2813 ~TryCatch();
2814
2815 /**
2816 * Returns true if an exception has been caught by this try/catch block.
2817 */
2818 bool HasCaught() const;
2819
2820 /**
2821 * For certain types of exceptions, it makes no sense to continue
2822 * execution.
2823 *
2824 * Currently, the only type of exception that can be caught by a
2825 * TryCatch handler and for which it does not make sense to continue
2826 * is termination exception. Such exceptions are thrown when the
2827 * TerminateExecution methods are called to terminate a long-running
2828 * script.
2829 *
2830 * If CanContinue returns false, the correct action is to perform
2831 * any C++ cleanup needed and then return.
2832 */
2833 bool CanContinue() const;
2834
2835 /**
Steve Blockd0582a62009-12-15 09:54:21 +00002836 * Throws the exception caught by this TryCatch in a way that avoids
2837 * it being caught again by this same TryCatch. As with ThrowException
2838 * it is illegal to execute any JavaScript operations after calling
2839 * ReThrow; the caller must return immediately to where the exception
2840 * is caught.
2841 */
2842 Handle<Value> ReThrow();
2843
2844 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002845 * Returns the exception caught by this try/catch block. If no exception has
2846 * been caught an empty handle is returned.
2847 *
2848 * The returned handle is valid until this TryCatch block has been destroyed.
2849 */
2850 Local<Value> Exception() const;
2851
2852 /**
2853 * Returns the .stack property of the thrown object. If no .stack
2854 * property is present an empty handle is returned.
2855 */
2856 Local<Value> StackTrace() const;
2857
2858 /**
2859 * Returns the message associated with this exception. If there is
2860 * no message associated an empty handle is returned.
2861 *
2862 * The returned handle is valid until this TryCatch block has been
2863 * destroyed.
2864 */
2865 Local<v8::Message> Message() const;
2866
2867 /**
2868 * Clears any exceptions that may have been caught by this try/catch block.
2869 * After this method has been called, HasCaught() will return false.
2870 *
2871 * It is not necessary to clear a try/catch block before using it again; if
2872 * another exception is thrown the previously caught exception will just be
2873 * overwritten. However, it is often a good idea since it makes it easier
2874 * to determine which operation threw a given exception.
2875 */
2876 void Reset();
2877
2878 /**
2879 * Set verbosity of the external exception handler.
2880 *
2881 * By default, exceptions that are caught by an external exception
2882 * handler are not reported. Call SetVerbose with true on an
2883 * external exception handler to have exceptions caught by the
2884 * handler reported as if they were not caught.
2885 */
2886 void SetVerbose(bool value);
2887
2888 /**
2889 * Set whether or not this TryCatch should capture a Message object
2890 * which holds source information about where the exception
2891 * occurred. True by default.
2892 */
2893 void SetCaptureMessage(bool value);
2894
Steve Blockd0582a62009-12-15 09:54:21 +00002895 private:
2896 void* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +00002897 void* exception_;
2898 void* message_;
Steve Blockd0582a62009-12-15 09:54:21 +00002899 bool is_verbose_ : 1;
2900 bool can_continue_ : 1;
2901 bool capture_message_ : 1;
2902 bool rethrow_ : 1;
2903
2904 friend class v8::internal::Top;
Steve Blocka7e24c12009-10-30 11:49:00 +00002905};
2906
2907
2908// --- C o n t e x t ---
2909
2910
2911/**
2912 * Ignore
2913 */
2914class V8EXPORT ExtensionConfiguration {
2915 public:
2916 ExtensionConfiguration(int name_count, const char* names[])
2917 : name_count_(name_count), names_(names) { }
2918 private:
2919 friend class ImplementationUtilities;
2920 int name_count_;
2921 const char** names_;
2922};
2923
2924
2925/**
2926 * A sandboxed execution context with its own set of built-in objects
2927 * and functions.
2928 */
2929class V8EXPORT Context {
2930 public:
2931 /** Returns the global object of the context. */
2932 Local<Object> Global();
2933
2934 /**
2935 * Detaches the global object from its context before
2936 * the global object can be reused to create a new context.
2937 */
2938 void DetachGlobal();
2939
Andrei Popescu74b3c142010-03-29 12:03:09 +01002940 /**
2941 * Reattaches a global object to a context. This can be used to
2942 * restore the connection between a global object and a context
2943 * after DetachGlobal has been called.
2944 *
2945 * \param global_object The global object to reattach to the
2946 * context. For this to work, the global object must be the global
2947 * object that was associated with this context before a call to
2948 * DetachGlobal.
2949 */
2950 void ReattachGlobal(Handle<Object> global_object);
2951
Leon Clarkef7060e22010-06-03 12:02:55 +01002952 /** Creates a new context.
2953 *
2954 * Returns a persistent handle to the newly allocated context. This
2955 * persistent handle has to be disposed when the context is no
2956 * longer used so the context can be garbage collected.
2957 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002958 static Persistent<Context> New(
Andrei Popescu31002712010-02-23 13:46:05 +00002959 ExtensionConfiguration* extensions = NULL,
Steve Blocka7e24c12009-10-30 11:49:00 +00002960 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2961 Handle<Value> global_object = Handle<Value>());
2962
2963 /** Returns the last entered context. */
2964 static Local<Context> GetEntered();
2965
2966 /** Returns the context that is on the top of the stack. */
2967 static Local<Context> GetCurrent();
2968
2969 /**
2970 * Returns the context of the calling JavaScript code. That is the
2971 * context of the top-most JavaScript frame. If there are no
2972 * JavaScript frames an empty handle is returned.
2973 */
2974 static Local<Context> GetCalling();
2975
2976 /**
2977 * Sets the security token for the context. To access an object in
2978 * another context, the security tokens must match.
2979 */
2980 void SetSecurityToken(Handle<Value> token);
2981
2982 /** Restores the security token to the default value. */
2983 void UseDefaultSecurityToken();
2984
2985 /** Returns the security token of this context.*/
2986 Handle<Value> GetSecurityToken();
2987
2988 /**
2989 * Enter this context. After entering a context, all code compiled
2990 * and run is compiled and run in this context. If another context
2991 * is already entered, this old context is saved so it can be
2992 * restored when the new context is exited.
2993 */
2994 void Enter();
2995
2996 /**
2997 * Exit this context. Exiting the current context restores the
2998 * context that was in place when entering the current context.
2999 */
3000 void Exit();
3001
3002 /** Returns true if the context has experienced an out of memory situation. */
3003 bool HasOutOfMemoryException();
3004
3005 /** Returns true if V8 has a current context. */
3006 static bool InContext();
3007
3008 /**
3009 * Associate an additional data object with the context. This is mainly used
3010 * with the debugger to provide additional information on the context through
3011 * the debugger API.
3012 */
Steve Blockd0582a62009-12-15 09:54:21 +00003013 void SetData(Handle<String> data);
Steve Blocka7e24c12009-10-30 11:49:00 +00003014 Local<Value> GetData();
3015
3016 /**
3017 * Stack-allocated class which sets the execution context for all
3018 * operations executed within a local scope.
3019 */
Steve Block8defd9f2010-07-08 12:39:36 +01003020 class Scope {
Steve Blocka7e24c12009-10-30 11:49:00 +00003021 public:
3022 inline Scope(Handle<Context> context) : context_(context) {
3023 context_->Enter();
3024 }
3025 inline ~Scope() { context_->Exit(); }
3026 private:
3027 Handle<Context> context_;
3028 };
3029
3030 private:
3031 friend class Value;
3032 friend class Script;
3033 friend class Object;
3034 friend class Function;
3035};
3036
3037
3038/**
3039 * Multiple threads in V8 are allowed, but only one thread at a time
3040 * is allowed to use V8. The definition of 'using V8' includes
3041 * accessing handles or holding onto object pointers obtained from V8
3042 * handles. It is up to the user of V8 to ensure (perhaps with
3043 * locking) that this constraint is not violated.
3044 *
3045 * If you wish to start using V8 in a thread you can do this by constructing
3046 * a v8::Locker object. After the code using V8 has completed for the
3047 * current thread you can call the destructor. This can be combined
3048 * with C++ scope-based construction as follows:
3049 *
3050 * \code
3051 * ...
3052 * {
3053 * v8::Locker locker;
3054 * ...
3055 * // Code using V8 goes here.
3056 * ...
3057 * } // Destructor called here
3058 * \endcode
3059 *
3060 * If you wish to stop using V8 in a thread A you can do this by either
3061 * by destroying the v8::Locker object as above or by constructing a
3062 * v8::Unlocker object:
3063 *
3064 * \code
3065 * {
3066 * v8::Unlocker unlocker;
3067 * ...
3068 * // Code not using V8 goes here while V8 can run in another thread.
3069 * ...
3070 * } // Destructor called here.
3071 * \endcode
3072 *
3073 * The Unlocker object is intended for use in a long-running callback
3074 * from V8, where you want to release the V8 lock for other threads to
3075 * use.
3076 *
3077 * The v8::Locker is a recursive lock. That is, you can lock more than
3078 * once in a given thread. This can be useful if you have code that can
3079 * be called either from code that holds the lock or from code that does
3080 * not. The Unlocker is not recursive so you can not have several
3081 * Unlockers on the stack at once, and you can not use an Unlocker in a
3082 * thread that is not inside a Locker's scope.
3083 *
3084 * An unlocker will unlock several lockers if it has to and reinstate
3085 * the correct depth of locking on its destruction. eg.:
3086 *
3087 * \code
3088 * // V8 not locked.
3089 * {
3090 * v8::Locker locker;
3091 * // V8 locked.
3092 * {
3093 * v8::Locker another_locker;
3094 * // V8 still locked (2 levels).
3095 * {
3096 * v8::Unlocker unlocker;
3097 * // V8 not locked.
3098 * }
3099 * // V8 locked again (2 levels).
3100 * }
3101 * // V8 still locked (1 level).
3102 * }
3103 * // V8 Now no longer locked.
3104 * \endcode
3105 */
3106class V8EXPORT Unlocker {
3107 public:
3108 Unlocker();
3109 ~Unlocker();
3110};
3111
3112
3113class V8EXPORT Locker {
3114 public:
3115 Locker();
3116 ~Locker();
3117
3118 /**
3119 * Start preemption.
3120 *
3121 * When preemption is started, a timer is fired every n milli seconds
3122 * that will switch between multiple threads that are in contention
3123 * for the V8 lock.
3124 */
3125 static void StartPreemption(int every_n_ms);
3126
3127 /**
3128 * Stop preemption.
3129 */
3130 static void StopPreemption();
3131
3132 /**
3133 * Returns whether or not the locker is locked by the current thread.
3134 */
3135 static bool IsLocked();
3136
3137 /**
3138 * Returns whether v8::Locker is being used by this V8 instance.
3139 */
3140 static bool IsActive() { return active_; }
3141
3142 private:
3143 bool has_lock_;
3144 bool top_level_;
3145
3146 static bool active_;
3147
3148 // Disallow copying and assigning.
3149 Locker(const Locker&);
3150 void operator=(const Locker&);
3151};
3152
3153
3154
3155// --- I m p l e m e n t a t i o n ---
3156
3157
3158namespace internal {
3159
3160
3161// Tag information for HeapObject.
3162const int kHeapObjectTag = 1;
3163const int kHeapObjectTagSize = 2;
3164const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3165
Steve Blocka7e24c12009-10-30 11:49:00 +00003166// Tag information for Smi.
3167const int kSmiTag = 0;
3168const int kSmiTagSize = 1;
3169const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3170
Steve Block3ce2e202009-11-05 08:53:23 +00003171template <size_t ptr_size> struct SmiConstants;
3172
3173// Smi constants for 32-bit systems.
3174template <> struct SmiConstants<4> {
3175 static const int kSmiShiftSize = 0;
3176 static const int kSmiValueSize = 31;
3177 static inline int SmiToInt(internal::Object* value) {
3178 int shift_bits = kSmiTagSize + kSmiShiftSize;
3179 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3180 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3181 }
3182};
3183
3184// Smi constants for 64-bit systems.
3185template <> struct SmiConstants<8> {
3186 static const int kSmiShiftSize = 31;
3187 static const int kSmiValueSize = 32;
3188 static inline int SmiToInt(internal::Object* value) {
3189 int shift_bits = kSmiTagSize + kSmiShiftSize;
3190 // Shift down and throw away top 32 bits.
3191 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3192 }
3193};
3194
3195const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
3196const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003197
Steve Blockd0582a62009-12-15 09:54:21 +00003198template <size_t ptr_size> struct InternalConstants;
3199
3200// Internal constants for 32-bit systems.
3201template <> struct InternalConstants<4> {
3202 static const int kStringResourceOffset = 3 * sizeof(void*);
3203};
3204
3205// Internal constants for 64-bit systems.
3206template <> struct InternalConstants<8> {
Steve Block6ded16b2010-05-10 14:33:55 +01003207 static const int kStringResourceOffset = 3 * sizeof(void*);
Steve Blockd0582a62009-12-15 09:54:21 +00003208};
3209
Steve Blocka7e24c12009-10-30 11:49:00 +00003210/**
3211 * This class exports constants and functionality from within v8 that
3212 * is necessary to implement inline functions in the v8 api. Don't
3213 * depend on functions and constants defined here.
3214 */
3215class Internals {
3216 public:
3217
3218 // These values match non-compiler-dependent values defined within
3219 // the implementation of v8.
3220 static const int kHeapObjectMapOffset = 0;
3221 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
Steve Blockd0582a62009-12-15 09:54:21 +00003222 static const int kStringResourceOffset =
3223 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3224
Steve Blocka7e24c12009-10-30 11:49:00 +00003225 static const int kProxyProxyOffset = sizeof(void*);
3226 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3227 static const int kFullStringRepresentationMask = 0x07;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003228 static const int kExternalTwoByteRepresentationTag = 0x02;
Steve Blocka7e24c12009-10-30 11:49:00 +00003229
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003230 static const int kJSObjectType = 0x9f;
3231 static const int kFirstNonstringType = 0x80;
3232 static const int kProxyType = 0x85;
Steve Blocka7e24c12009-10-30 11:49:00 +00003233
3234 static inline bool HasHeapObjectTag(internal::Object* value) {
3235 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3236 kHeapObjectTag);
3237 }
3238
3239 static inline bool HasSmiTag(internal::Object* value) {
3240 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3241 }
3242
3243 static inline int SmiValue(internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00003244 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3245 }
3246
3247 static inline int GetInstanceType(internal::Object* obj) {
3248 typedef internal::Object O;
3249 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3250 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3251 }
3252
3253 static inline void* GetExternalPointer(internal::Object* obj) {
3254 if (HasSmiTag(obj)) {
3255 return obj;
3256 } else if (GetInstanceType(obj) == kProxyType) {
3257 return ReadField<void*>(obj, kProxyProxyOffset);
3258 } else {
3259 return NULL;
3260 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003261 }
3262
3263 static inline bool IsExternalTwoByteString(int instance_type) {
3264 int representation = (instance_type & kFullStringRepresentationMask);
3265 return representation == kExternalTwoByteRepresentationTag;
3266 }
3267
3268 template <typename T>
3269 static inline T ReadField(Object* ptr, int offset) {
3270 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3271 return *reinterpret_cast<T*>(addr);
3272 }
3273
3274};
3275
3276}
3277
3278
3279template <class T>
3280Handle<T>::Handle() : val_(0) { }
3281
3282
3283template <class T>
3284Local<T>::Local() : Handle<T>() { }
3285
3286
3287template <class T>
3288Local<T> Local<T>::New(Handle<T> that) {
3289 if (that.IsEmpty()) return Local<T>();
3290 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
3291 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3292}
3293
3294
3295template <class T>
3296Persistent<T> Persistent<T>::New(Handle<T> that) {
3297 if (that.IsEmpty()) return Persistent<T>();
3298 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
3299 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3300}
3301
3302
3303template <class T>
3304bool Persistent<T>::IsNearDeath() const {
3305 if (this->IsEmpty()) return false;
3306 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
3307}
3308
3309
3310template <class T>
3311bool Persistent<T>::IsWeak() const {
3312 if (this->IsEmpty()) return false;
3313 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
3314}
3315
3316
3317template <class T>
3318void Persistent<T>::Dispose() {
3319 if (this->IsEmpty()) return;
3320 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
3321}
3322
3323
3324template <class T>
3325Persistent<T>::Persistent() : Handle<T>() { }
3326
3327template <class T>
3328void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
3329 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3330 parameters,
3331 callback);
3332}
3333
3334template <class T>
3335void Persistent<T>::ClearWeak() {
3336 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
3337}
3338
Steve Block8defd9f2010-07-08 12:39:36 +01003339
3340Arguments::Arguments(v8::Local<v8::Value> data,
3341 v8::Local<v8::Object> holder,
3342 v8::Local<v8::Function> callee,
3343 bool is_construct_call,
3344 void** values, int length)
3345 : data_(data), holder_(holder), callee_(callee),
3346 is_construct_call_(is_construct_call),
3347 values_(values), length_(length) { }
3348
3349
Steve Blocka7e24c12009-10-30 11:49:00 +00003350Local<Value> Arguments::operator[](int i) const {
3351 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3352 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3353}
3354
3355
3356Local<Function> Arguments::Callee() const {
3357 return callee_;
3358}
3359
3360
3361Local<Object> Arguments::This() const {
3362 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3363}
3364
3365
3366Local<Object> Arguments::Holder() const {
3367 return holder_;
3368}
3369
3370
3371Local<Value> Arguments::Data() const {
3372 return data_;
3373}
3374
3375
3376bool Arguments::IsConstructCall() const {
3377 return is_construct_call_;
3378}
3379
3380
3381int Arguments::Length() const {
3382 return length_;
3383}
3384
3385
3386template <class T>
3387Local<T> HandleScope::Close(Handle<T> value) {
3388 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3389 internal::Object** after = RawClose(before);
3390 return Local<T>(reinterpret_cast<T*>(after));
3391}
3392
3393Handle<Value> ScriptOrigin::ResourceName() const {
3394 return resource_name_;
3395}
3396
3397
3398Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
3399 return resource_line_offset_;
3400}
3401
3402
3403Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
3404 return resource_column_offset_;
3405}
3406
3407
3408Handle<Boolean> Boolean::New(bool value) {
3409 return value ? True() : False();
3410}
3411
3412
3413void Template::Set(const char* name, v8::Handle<Data> value) {
3414 Set(v8::String::New(name), value);
3415}
3416
3417
3418Local<Value> Object::GetInternalField(int index) {
3419#ifndef V8_ENABLE_CHECKS
3420 Local<Value> quick_result = UncheckedGetInternalField(index);
3421 if (!quick_result.IsEmpty()) return quick_result;
3422#endif
3423 return CheckedGetInternalField(index);
3424}
3425
3426
3427Local<Value> Object::UncheckedGetInternalField(int index) {
3428 typedef internal::Object O;
3429 typedef internal::Internals I;
3430 O* obj = *reinterpret_cast<O**>(this);
Steve Block3ce2e202009-11-05 08:53:23 +00003431 if (I::GetInstanceType(obj) == I::kJSObjectType) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003432 // If the object is a plain JSObject, which is the common case,
3433 // we know where to find the internal fields and can return the
3434 // value directly.
3435 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3436 O* value = I::ReadField<O*>(obj, offset);
3437 O** result = HandleScope::CreateHandle(value);
3438 return Local<Value>(reinterpret_cast<Value*>(result));
3439 } else {
3440 return Local<Value>();
3441 }
3442}
3443
3444
3445void* External::Unwrap(Handle<v8::Value> obj) {
3446#ifdef V8_ENABLE_CHECKS
3447 return FullUnwrap(obj);
3448#else
3449 return QuickUnwrap(obj);
3450#endif
3451}
3452
3453
3454void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3455 typedef internal::Object O;
Steve Blocka7e24c12009-10-30 11:49:00 +00003456 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
Steve Block3ce2e202009-11-05 08:53:23 +00003457 return internal::Internals::GetExternalPointer(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003458}
3459
3460
3461void* Object::GetPointerFromInternalField(int index) {
Steve Block3ce2e202009-11-05 08:53:23 +00003462 typedef internal::Object O;
3463 typedef internal::Internals I;
3464
3465 O* obj = *reinterpret_cast<O**>(this);
3466
3467 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3468 // If the object is a plain JSObject, which is the common case,
3469 // we know where to find the internal fields and can return the
3470 // value directly.
3471 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3472 O* value = I::ReadField<O*>(obj, offset);
3473 return I::GetExternalPointer(value);
3474 }
3475
3476 return SlowGetPointerFromInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003477}
3478
3479
3480String* String::Cast(v8::Value* value) {
3481#ifdef V8_ENABLE_CHECKS
3482 CheckCast(value);
3483#endif
3484 return static_cast<String*>(value);
3485}
3486
3487
3488String::ExternalStringResource* String::GetExternalStringResource() const {
3489 typedef internal::Object O;
3490 typedef internal::Internals I;
3491 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
Steve Blocka7e24c12009-10-30 11:49:00 +00003492 String::ExternalStringResource* result;
Steve Block3ce2e202009-11-05 08:53:23 +00003493 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003494 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3495 result = reinterpret_cast<String::ExternalStringResource*>(value);
3496 } else {
3497 result = NULL;
3498 }
3499#ifdef V8_ENABLE_CHECKS
3500 VerifyExternalStringResource(result);
3501#endif
3502 return result;
3503}
3504
3505
3506bool Value::IsString() const {
3507#ifdef V8_ENABLE_CHECKS
3508 return FullIsString();
3509#else
3510 return QuickIsString();
3511#endif
3512}
3513
3514bool Value::QuickIsString() const {
3515 typedef internal::Object O;
3516 typedef internal::Internals I;
3517 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3518 if (!I::HasHeapObjectTag(obj)) return false;
Steve Block3ce2e202009-11-05 08:53:23 +00003519 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
Steve Blocka7e24c12009-10-30 11:49:00 +00003520}
3521
3522
3523Number* Number::Cast(v8::Value* value) {
3524#ifdef V8_ENABLE_CHECKS
3525 CheckCast(value);
3526#endif
3527 return static_cast<Number*>(value);
3528}
3529
3530
3531Integer* Integer::Cast(v8::Value* value) {
3532#ifdef V8_ENABLE_CHECKS
3533 CheckCast(value);
3534#endif
3535 return static_cast<Integer*>(value);
3536}
3537
3538
3539Date* Date::Cast(v8::Value* value) {
3540#ifdef V8_ENABLE_CHECKS
3541 CheckCast(value);
3542#endif
3543 return static_cast<Date*>(value);
3544}
3545
3546
3547Object* Object::Cast(v8::Value* value) {
3548#ifdef V8_ENABLE_CHECKS
3549 CheckCast(value);
3550#endif
3551 return static_cast<Object*>(value);
3552}
3553
3554
3555Array* Array::Cast(v8::Value* value) {
3556#ifdef V8_ENABLE_CHECKS
3557 CheckCast(value);
3558#endif
3559 return static_cast<Array*>(value);
3560}
3561
3562
3563Function* Function::Cast(v8::Value* value) {
3564#ifdef V8_ENABLE_CHECKS
3565 CheckCast(value);
3566#endif
3567 return static_cast<Function*>(value);
3568}
3569
3570
3571External* External::Cast(v8::Value* value) {
3572#ifdef V8_ENABLE_CHECKS
3573 CheckCast(value);
3574#endif
3575 return static_cast<External*>(value);
3576}
3577
3578
3579Local<Value> AccessorInfo::Data() const {
Steve Block6ded16b2010-05-10 14:33:55 +01003580 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
Steve Blocka7e24c12009-10-30 11:49:00 +00003581}
3582
3583
3584Local<Object> AccessorInfo::This() const {
3585 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3586}
3587
3588
3589Local<Object> AccessorInfo::Holder() const {
3590 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3591}
3592
3593
3594/**
3595 * \example shell.cc
3596 * A simple shell that takes a list of expressions on the
3597 * command-line and executes them.
3598 */
3599
3600
3601/**
3602 * \example process.cc
3603 */
3604
3605
3606} // namespace v8
3607
3608
3609#undef V8EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +00003610#undef TYPE_CHECK
3611
3612
3613#endif // V8_H_