blob: ab886cda36763a51c4bac392607d40f909530048 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2007-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41#include <stdio.h>
42
43#ifdef _WIN32
44typedef int int32_t;
45typedef unsigned int uint32_t;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000046typedef unsigned short uint16_t; // NOLINT
47typedef long long int64_t; // NOLINT
48
49// Setup for Windows DLL export/import. When building the V8 DLL the
50// BUILDING_V8_SHARED needs to be defined. When building a program which uses
51// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
52// static library or building a program which uses the V8 static library neither
53// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
54// The reason for having both EXPORT and EXPORT_INLINE is that classes which
55// have their code inside this header file needs to have __declspec(dllexport)
56// when building the DLL but cannot have __declspec(dllimport) when building
57// a program which uses the DLL.
58#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
59#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
60 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061#endif
62
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000063#ifdef BUILDING_V8_SHARED
64#define EXPORT __declspec(dllexport)
65#define EXPORT_INLINE __declspec(dllexport)
66#elif USING_V8_SHARED
67#define EXPORT __declspec(dllimport)
68#define EXPORT_INLINE
69#else
70#define EXPORT
71#define EXPORT_INLINE
72#endif // BUILDING_V8_SHARED
73
74#else // _WIN32
75
76#include <stdint.h>
77
78// Setup for Linux shared library export. There is no need to destinguish
79// neither between building or using the V8 shared library nor between using
80// the shared or static V8 library as there is on Windows. Therefore there is
ager@chromium.org7c537e22008-10-16 08:43:32 +000081// no checking of BUILDING_V8_SHARED and USING_V8_SHARED.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#if defined(__GNUC__) && (__GNUC__ >= 4)
83#define EXPORT __attribute__ ((visibility("default")))
84#define EXPORT_INLINE __attribute__ ((visibility("default")))
85#else // defined(__GNUC__) && (__GNUC__ >= 4)
86#define EXPORT
87#define EXPORT_INLINE
88#endif // defined(__GNUC__) && (__GNUC__ >= 4)
89
90#endif // _WIN32
91
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000093 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 */
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;
120
121
122// --- W e a k H a n d l e s
123
124
125/**
126 * A weak reference callback function.
127 *
128 * \param object the weak global object to be reclaimed by the garbage collector
129 * \param parameter the value passed in when making the weak global object
130 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000131typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132 void* parameter);
133
134
135// --- H a n d l e s ---
136
137#define TYPE_CHECK(T, S) \
138 while (false) { \
139 *(static_cast<T**>(0)) = static_cast<S*>(0); \
140 }
141
142/**
143 * An object reference managed by the v8 garbage collector.
144 *
145 * All objects returned from v8 have to be tracked by the garbage
146 * collector so that it knows that the objects are still alive. Also,
147 * because the garbage collector may move objects, it is unsafe to
148 * point directly to an object. Instead, all objects are stored in
149 * handles which are known by the garbage collector and updated
150 * whenever an object moves. Handles should always be passed by value
151 * (except in cases like out-parameters) and they should never be
152 * allocated on the heap.
153 *
154 * There are two types of handles: local and persistent handles.
155 * Local handles are light-weight and transient and typically used in
156 * local operations. They are managed by HandleScopes. Persistent
157 * handles can be used when storing objects across several independent
158 * operations and have to be explicitly deallocated when they're no
159 * longer used.
160 *
161 * It is safe to extract the object stored in the handle by
162 * dereferencing the handle (for instance, to extract the Object* from
163 * an Handle<Object>); the value will still be governed by a handle
164 * behind the scenes and the same rules apply to these values as to
165 * their handles.
166 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000167template <class T> class EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 public:
169
170 /**
171 * Creates an empty handle.
172 */
173 Handle();
174
175 /**
176 * Creates a new handle for the specified value.
177 */
178 explicit Handle(T* val) : val_(val) { }
179
180 /**
181 * Creates a handle for the contents of the specified handle. This
182 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000183 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 * incompatible handles, for instance from a Handle<String> to a
185 * Handle<Number> it will cause a compiletime error. Assigning
186 * between compatible handles, for instance assigning a
187 * Handle<String> to a variable declared as Handle<Value>, is legal
188 * because String is a subclass of Value.
189 */
190 template <class S> inline Handle(Handle<S> that)
191 : val_(reinterpret_cast<T*>(*that)) {
192 /**
193 * This check fails when trying to convert between incompatible
194 * handles. For example, converting from a Handle<String> to a
195 * Handle<Number>.
196 */
197 TYPE_CHECK(T, S);
198 }
199
200 /**
201 * Returns true if the handle is empty.
202 */
ager@chromium.org32912102009-01-16 10:38:43 +0000203 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
ager@chromium.org32912102009-01-16 10:38:43 +0000205 T* operator->() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
ager@chromium.org32912102009-01-16 10:38:43 +0000207 T* operator*() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208
209 /**
210 * Sets the handle to be empty. IsEmpty() will then return true.
211 */
212 void Clear() { this->val_ = 0; }
213
214 /**
215 * Checks whether two handles are the same.
216 * Returns true if both are empty, or if the objects
217 * to which they refer are identical.
218 * The handles' references are not checked.
219 */
ager@chromium.org32912102009-01-16 10:38:43 +0000220 template <class S> bool operator==(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 void** a = reinterpret_cast<void**>(**this);
222 void** b = reinterpret_cast<void**>(*that);
223 if (a == 0) return b == 0;
224 if (b == 0) return false;
225 return *a == *b;
226 }
227
228 /**
229 * Checks whether two handles are different.
230 * Returns true if only one of the handles is empty, or if
231 * the objects to which they refer are different.
232 * The handles' references are not checked.
233 */
ager@chromium.org32912102009-01-16 10:38:43 +0000234 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 return !operator==(that);
236 }
237
238 template <class S> static inline Handle<T> Cast(Handle<S> that) {
239 if (that.IsEmpty()) return Handle<T>();
240 return Handle<T>(T::Cast(*that));
241 }
242
243 private:
244 T* val_;
245};
246
247
248/**
249 * A light-weight stack-allocated object handle. All operations
250 * that return objects from within v8 return them in local handles. They
251 * are created within HandleScopes, and all local handles allocated within a
252 * handle scope are destroyed when the handle scope is destroyed. Hence it
253 * is not necessary to explicitly deallocate local handles.
254 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000255template <class T> class EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 public:
257 Local();
258 template <class S> inline Local(Local<S> that)
259 : Handle<T>(reinterpret_cast<T*>(*that)) {
260 /**
261 * This check fails when trying to convert between incompatible
262 * handles. For example, converting from a Handle<String> to a
263 * Handle<Number>.
264 */
265 TYPE_CHECK(T, S);
266 }
267 template <class S> inline Local(S* that) : Handle<T>(that) { }
268 template <class S> static inline Local<T> Cast(Local<S> that) {
269 if (that.IsEmpty()) return Local<T>();
270 return Local<T>(T::Cast(*that));
271 }
272
273 /** Create a local handle for the content of another handle.
274 * The referee is kept alive by the local handle even when
275 * the original handle is destroyed/disposed.
276 */
277 static Local<T> New(Handle<T> that);
278};
279
280
281/**
282 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000283 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 * allocated, a Persistent handle remains valid until it is explicitly
285 * disposed.
286 *
287 * A persistent handle contains a reference to a storage cell within
288 * the v8 engine which holds an object value and which is updated by
289 * the garbage collector whenever the object is moved. A new storage
290 * cell can be created using Persistent::New and existing handles can
291 * be disposed using Persistent::Dispose. Since persistent handles
292 * are passed by value you may have many persistent handle objects
293 * that point to the same storage cell. For instance, if you pass a
294 * persistent handle as an argument to a function you will not get two
295 * different storage cells but rather two references to the same
296 * storage cell.
297 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000298template <class T> class EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 public:
300
301 /**
302 * Creates an empty persistent handle that doesn't point to any
303 * storage cell.
304 */
305 Persistent();
306
307 /**
308 * Creates a persistent handle for the same storage cell as the
309 * specified handle. This constructor allows you to pass persistent
310 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000311 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000313 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 * between compatible persistent handles, for instance assigning a
315 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000316 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 */
318 template <class S> inline Persistent(Persistent<S> that)
319 : Handle<T>(reinterpret_cast<T*>(*that)) {
320 /**
321 * This check fails when trying to convert between incompatible
322 * handles. For example, converting from a Handle<String> to a
323 * Handle<Number>.
324 */
325 TYPE_CHECK(T, S);
326 }
327
328 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
329
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000330 /**
331 * "Casts" a plain handle which is known to be a persistent handle
332 * to a persistent handle.
333 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 template <class S> explicit inline Persistent(Handle<S> that)
335 : Handle<T>(*that) { }
336
337 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
338 if (that.IsEmpty()) return Persistent<T>();
339 return Persistent<T>(T::Cast(*that));
340 }
341
342 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000343 * Creates a new persistent handle for an existing local or
344 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 */
346 static Persistent<T> New(Handle<T> that);
347
348 /**
349 * Releases the storage cell referenced by this persistent handle.
350 * Does not remove the reference to the cell from any handles.
351 * This handle's reference, and any any other references to the storage
352 * cell remain and IsEmpty will still return false.
353 */
354 void Dispose();
355
356 /**
357 * Make the reference to this object weak. When only weak handles
358 * refer to the object, the garbage collector will perform a
359 * callback to the given V8::WeakReferenceCallback function, passing
360 * it the object reference and the given parameters.
361 */
362 void MakeWeak(void* parameters, WeakReferenceCallback callback);
363
364 /** Clears the weak reference to this object.*/
365 void ClearWeak();
366
367 /**
368 *Checks if the handle holds the only reference to an object.
369 */
ager@chromium.org32912102009-01-16 10:38:43 +0000370 bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371
372 /**
373 * Returns true if the handle's reference is weak.
374 */
ager@chromium.org32912102009-01-16 10:38:43 +0000375 bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 private:
378 friend class ImplementationUtilities;
379 friend class ObjectTemplate;
380};
381
382
v8.team.kasperl727e9952008-09-02 14:56:44 +0000383 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 * A stack-allocated class that governs a number of local handles.
385 * After a handle scope has been created, all local handles will be
386 * allocated within that handle scope until either the handle scope is
387 * deleted or another handle scope is created. If there is already a
388 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000389 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 * new handles will again be allocated in the original handle scope.
391 *
392 * After the handle scope of a local handle has been deleted the
393 * garbage collector will no longer track the object stored in the
394 * handle and may deallocate it. The behavior of accessing a handle
395 * for which the handle scope has been deleted is undefined.
396 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000397class EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000399 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000401 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402
403 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 * Closes the handle scope and returns the value as a handle in the
405 * previous scope, which is the new current scope after the call.
406 */
407 template <class T> Local<T> Close(Handle<T> value);
408
409 /**
410 * Counts the number of allocated handles.
411 */
412 static int NumberOfHandles();
413
414 /**
415 * Creates a new handle with the given value.
416 */
417 static void** CreateHandle(void* value);
418
419 private:
420 // Make it impossible to create heap-allocated or illegal handle
421 // scopes by disallowing certain operations.
422 HandleScope(const HandleScope&);
423 void operator=(const HandleScope&);
424 void* operator new(size_t size);
425 void operator delete(void*, size_t);
426
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000427 // This Data class is accessible internally through a typedef in the
428 // ImplementationUtilities class.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000429 class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 public:
431 int extensions;
432 void** next;
433 void** limit;
434 inline void Initialize() {
435 extensions = -1;
436 next = limit = NULL;
437 }
438 };
439
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000440 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000442 // Allow for the active closing of HandleScopes which allows to pass a handle
443 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444 bool is_closed_;
445 void** RawClose(void** value);
446
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 friend class ImplementationUtilities;
448};
449
450
451// --- S p e c i a l o b j e c t s ---
452
453
454/**
455 * The superclass of values and API object templates.
456 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000457class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 private:
459 Data();
460};
461
462
463/**
464 * Pre-compilation data that can be associated with a script. This
465 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000466 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000467 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000469class EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 public:
471 virtual ~ScriptData() { }
472 static ScriptData* PreCompile(const char* input, int length);
473 static ScriptData* New(unsigned* data, int length);
474
475 virtual int Length() = 0;
476 virtual unsigned* Data() = 0;
477};
478
479
480/**
481 * The origin, within a file, of a script.
482 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000483class EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000485 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 Handle<Integer> resource_line_offset = Handle<Integer>(),
487 Handle<Integer> resource_column_offset = Handle<Integer>())
488 : resource_name_(resource_name),
489 resource_line_offset_(resource_line_offset),
490 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000491 inline Handle<Value> ResourceName() const;
492 inline Handle<Integer> ResourceLineOffset() const;
493 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000495 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 Handle<Integer> resource_line_offset_;
497 Handle<Integer> resource_column_offset_;
498};
499
500
501/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000502 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000504class EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 public:
506
507 /**
508 * Compiles the specified script. The ScriptOrigin* and ScriptData*
509 * parameters are owned by the caller of Script::Compile. No
510 * references to these objects are kept after compilation finishes.
511 */
512 static Local<Script> Compile(Handle<String> source,
513 ScriptOrigin* origin = NULL,
514 ScriptData* pre_data = NULL);
515
mads.s.agercbaa0602008-08-14 13:41:48 +0000516 /**
517 * Compiles the specified script using the specified file name
518 * object (typically a string) as the script's origin.
519 */
520 static Local<Script> Compile(Handle<String> source,
521 Handle<Value> file_name);
522
v8.team.kasperl727e9952008-09-02 14:56:44 +0000523 /**
524 * Runs the script returning the resulting value.
525 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527
528 /**
529 * Returns the script id value.
530 */
531 Local<Value> Id();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532};
533
534
535/**
536 * An error message.
537 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000538class EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000540 Local<String> Get() const;
541 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542
ager@chromium.org32912102009-01-16 10:38:43 +0000543 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000545 /**
546 * Returns the number, 1-based, of the line where the error occurred.
547 */
ager@chromium.org32912102009-01-16 10:38:43 +0000548 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000550 /**
551 * Returns the index within the script of the first character where
552 * the error occurred.
553 */
ager@chromium.org32912102009-01-16 10:38:43 +0000554 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000555
556 /**
557 * Returns the index within the script of the last character where
558 * the error occurred.
559 */
ager@chromium.org32912102009-01-16 10:38:43 +0000560 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000561
562 /**
563 * Returns the index within the line of the first character where
564 * the error occurred.
565 */
ager@chromium.org32912102009-01-16 10:38:43 +0000566 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000567
568 /**
569 * Returns the index within the line of the last character where
570 * the error occurred.
571 */
ager@chromium.org32912102009-01-16 10:38:43 +0000572 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000573
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 // TODO(1245381): Print to a string instead of on a FILE.
575 static void PrintCurrentStackTrace(FILE* out);
576};
577
578
579// --- V a l u e ---
580
581
582/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000583 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000585class EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 public:
587
588 /**
589 * Returns true if this value is the undefined value. See ECMA-262
590 * 4.3.10.
591 */
ager@chromium.org32912102009-01-16 10:38:43 +0000592 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593
594 /**
595 * Returns true if this value is the null value. See ECMA-262
596 * 4.3.11.
597 */
ager@chromium.org32912102009-01-16 10:38:43 +0000598 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599
600 /**
601 * Returns true if this value is true.
602 */
ager@chromium.org32912102009-01-16 10:38:43 +0000603 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604
605 /**
606 * Returns true if this value is false.
607 */
ager@chromium.org32912102009-01-16 10:38:43 +0000608 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
610 /**
611 * Returns true if this value is an instance of the String type.
612 * See ECMA-262 8.4.
613 */
ager@chromium.org32912102009-01-16 10:38:43 +0000614 bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
616 /**
617 * Returns true if this value is a function.
618 */
ager@chromium.org32912102009-01-16 10:38:43 +0000619 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620
621 /**
622 * Returns true if this value is an array.
623 */
ager@chromium.org32912102009-01-16 10:38:43 +0000624 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
v8.team.kasperl727e9952008-09-02 14:56:44 +0000626 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 * Returns true if this value is an object.
628 */
ager@chromium.org32912102009-01-16 10:38:43 +0000629 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
v8.team.kasperl727e9952008-09-02 14:56:44 +0000631 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 * Returns true if this value is boolean.
633 */
ager@chromium.org32912102009-01-16 10:38:43 +0000634 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000635
v8.team.kasperl727e9952008-09-02 14:56:44 +0000636 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 * Returns true if this value is a number.
638 */
ager@chromium.org32912102009-01-16 10:38:43 +0000639 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640
v8.team.kasperl727e9952008-09-02 14:56:44 +0000641 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642 * Returns true if this value is external.
643 */
ager@chromium.org32912102009-01-16 10:38:43 +0000644 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645
v8.team.kasperl727e9952008-09-02 14:56:44 +0000646 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 * Returns true if this value is a 32-bit signed integer.
648 */
ager@chromium.org32912102009-01-16 10:38:43 +0000649 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000651 /**
652 * Returns true if this value is a Date.
653 */
ager@chromium.org32912102009-01-16 10:38:43 +0000654 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000655
ager@chromium.org32912102009-01-16 10:38:43 +0000656 Local<Boolean> ToBoolean() const;
657 Local<Number> ToNumber() const;
658 Local<String> ToString() const;
659 Local<String> ToDetailString() const;
660 Local<Object> ToObject() const;
661 Local<Integer> ToInteger() const;
662 Local<Uint32> ToUint32() const;
663 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664
665 /**
666 * Attempts to convert a string to an array index.
667 * Returns an empty handle if the conversion fails.
668 */
ager@chromium.org32912102009-01-16 10:38:43 +0000669 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670
ager@chromium.org32912102009-01-16 10:38:43 +0000671 bool BooleanValue() const;
672 double NumberValue() const;
673 int64_t IntegerValue() const;
674 uint32_t Uint32Value() const;
675 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676
677 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000678 bool Equals(Handle<Value> that) const;
679 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680};
681
682
683/**
684 * The superclass of primitive values. See ECMA-262 4.3.2.
685 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000686class EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687
688
689/**
690 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
691 * or false value.
692 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000693class EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000695 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 static inline Handle<Boolean> New(bool value);
697};
698
699
700/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000701 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000703class EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000705
706 /**
707 * Returns the number of characters in this string.
708 */
ager@chromium.org32912102009-01-16 10:38:43 +0000709 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
v8.team.kasperl727e9952008-09-02 14:56:44 +0000711 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000712 * Returns the number of bytes in the UTF-8 encoded
713 * representation of this string.
714 */
ager@chromium.org32912102009-01-16 10:38:43 +0000715 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000716
717 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000718 * Write the contents of the string to an external buffer.
719 * If no arguments are given, expects the buffer to be large
720 * enough to hold the entire string and NULL terminator. Copies
721 * the contents of the string and the NULL terminator into the
722 * buffer.
723 *
724 * Copies up to length characters into the output buffer.
725 * Only null-terminates if there is enough space in the buffer.
726 *
727 * \param buffer The buffer into which the string will be copied.
728 * \param start The starting position within the string at which
729 * copying begins.
730 * \param length The number of bytes to copy from the string.
731 * \return The number of characters copied to the buffer
732 * excluding the NULL terminator.
733 */
ager@chromium.org32912102009-01-16 10:38:43 +0000734 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
735 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
736 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737
v8.team.kasperl727e9952008-09-02 14:56:44 +0000738 /**
739 * Returns true if the string is external
740 */
ager@chromium.org32912102009-01-16 10:38:43 +0000741 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
v8.team.kasperl727e9952008-09-02 14:56:44 +0000743 /**
744 * Returns true if the string is both external and ascii
745 */
ager@chromium.org32912102009-01-16 10:38:43 +0000746 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000747 /**
748 * An ExternalStringResource is a wrapper around a two-byte string
749 * buffer that resides outside V8's heap. Implement an
750 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000751 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000752 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000753 class EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 public:
755 /**
756 * Override the destructor to manage the life cycle of the underlying
757 * buffer.
758 */
759 virtual ~ExternalStringResource() {}
760 /** The string data from the underlying buffer.*/
761 virtual const uint16_t* data() const = 0;
762 /** The length of the string. That is, the number of two-byte characters.*/
763 virtual size_t length() const = 0;
764 protected:
765 ExternalStringResource() {}
766 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000767 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768 ExternalStringResource(const ExternalStringResource&);
769 void operator=(const ExternalStringResource&);
770 };
771
772 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000773 * An ExternalAsciiStringResource is a wrapper around an ascii
774 * string buffer that resides outside V8's heap. Implement an
775 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000776 * underlying buffer. Note that the string data must be immutable
777 * and that the data must be strict 7-bit ASCII, not Latin1 or
778 * UTF-8, which would require special treatment internally in the
779 * engine and, in the case of UTF-8, do not allow efficient indexing.
780 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000781 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000783 class EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 public:
785 /**
786 * Override the destructor to manage the life cycle of the underlying
787 * buffer.
788 */
789 virtual ~ExternalAsciiStringResource() {}
790 /** The string data from the underlying buffer.*/
791 virtual const char* data() const = 0;
792 /** The number of ascii characters in the string.*/
793 virtual size_t length() const = 0;
794 protected:
795 ExternalAsciiStringResource() {}
796 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000797 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
799 void operator=(const ExternalAsciiStringResource&);
800 };
801
802 /**
803 * Get the ExternalStringResource for an external string. Only
804 * valid if IsExternal() returns true.
805 */
ager@chromium.org32912102009-01-16 10:38:43 +0000806 ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807
808 /**
809 * Get the ExternalAsciiStringResource for an external ascii string.
810 * Only valid if IsExternalAscii() returns true.
811 */
ager@chromium.org32912102009-01-16 10:38:43 +0000812 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813
814 static String* Cast(v8::Value* obj);
815
816 /**
817 * Allocates a new string from either utf-8 encoded or ascii data.
818 * The second parameter 'length' gives the buffer length.
819 * If the data is utf-8 encoded, the caller must
820 * be careful to supply the length parameter.
821 * If it is not given, the function calls
822 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000823 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000824 */
825 static Local<String> New(const char* data, int length = -1);
826
827 /** Allocates a new string from utf16 data.*/
828 static Local<String> New(const uint16_t* data, int length = -1);
829
830 /** Creates a symbol. Returns one if it exists already.*/
831 static Local<String> NewSymbol(const char* data, int length = -1);
832
v8.team.kasperl727e9952008-09-02 14:56:44 +0000833 /**
834 * Creates a new external string using the data defined in the given
835 * resource. The resource is deleted when the external string is no
836 * longer live on V8's heap. The caller of this function should not
837 * delete or modify the resource. Neither should the underlying buffer be
838 * deallocated or modified except through the destructor of the
839 * external string resource.
840 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000842
843 /**
844 * Associate an external string resource with this string by transforming it
845 * in place so that existing references to this string in the JavaScript heap
846 * will use the external string resource. The external string resource's
847 * character contents needs to be equivalent to this string.
848 * Returns true if the string has been changed to be an external string.
849 * The string is not modified if the operation fails.
850 */
851 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852
v8.team.kasperl727e9952008-09-02 14:56:44 +0000853 /**
854 * Creates a new external string using the ascii data defined in the given
855 * resource. The resource is deleted when the external string is no
856 * longer live on V8's heap. The caller of this function should not
857 * delete or modify the resource. Neither should the underlying buffer be
858 * deallocated or modified except through the destructor of the
859 * external string resource.
860 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000862
863 /**
864 * Associate an external string resource with this string by transforming it
865 * in place so that existing references to this string in the JavaScript heap
866 * will use the external string resource. The external string resource's
867 * character contents needs to be equivalent to this string.
868 * Returns true if the string has been changed to be an external string.
869 * The string is not modified if the operation fails.
870 */
871 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
kasper.lund7276f142008-07-30 08:49:36 +0000873 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 static Local<String> NewUndetectable(const char* data, int length = -1);
875
kasper.lund7276f142008-07-30 08:49:36 +0000876 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
878
879 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000880 * Converts an object to a utf8-encoded character array. Useful if
881 * you want to print the object.
882 */
883 class EXPORT Utf8Value {
884 public:
885 explicit Utf8Value(Handle<v8::Value> obj);
886 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000887 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000888 int length() { return length_; }
889 private:
890 char* str_;
891 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000892
893 // Disallow copying and assigning.
894 Utf8Value(const Utf8Value&);
895 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000896 };
897
898 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 * Converts an object to an ascii string.
900 * Useful if you want to print the object.
901 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000902 class EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 public:
904 explicit AsciiValue(Handle<v8::Value> obj);
905 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000906 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000907 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 private:
909 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000910 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000911
912 // Disallow copying and assigning.
913 AsciiValue(const AsciiValue&);
914 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 };
916
917 /**
918 * Converts an object to a two-byte string.
919 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000920 class EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 public:
922 explicit Value(Handle<v8::Value> obj);
923 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000924 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000925 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 private:
927 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000928 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000929
930 // Disallow copying and assigning.
931 Value(const Value&);
932 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933 };
934};
935
936
937/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000938 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000940class EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000942 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943 static Local<Number> New(double value);
944 static Number* Cast(v8::Value* obj);
945 private:
946 Number();
947};
948
949
950/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000951 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000953class EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954 public:
955 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +0000956 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 static Integer* Cast(v8::Value* obj);
958 private:
959 Integer();
960};
961
962
963/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000964 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000966class EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000968 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 private:
970 Int32();
971};
972
973
974/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000975 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000977class EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000979 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 private:
981 Uint32();
982};
983
984
985/**
986 * An instance of the built-in Date constructor (ECMA-262, 15.9).
987 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000988class EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989 public:
990 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000991
992 /**
993 * A specialization of Value::NumberValue that is more efficient
994 * because we know the structure of this object.
995 */
ager@chromium.org32912102009-01-16 10:38:43 +0000996 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000997
998 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999};
1000
1001
1002enum PropertyAttribute {
1003 None = 0,
1004 ReadOnly = 1 << 0,
1005 DontEnum = 1 << 1,
1006 DontDelete = 1 << 2
1007};
1008
1009/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001010 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001012class EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 public:
1014 bool Set(Handle<Value> key,
1015 Handle<Value> value,
1016 PropertyAttribute attribs = None);
1017 Local<Value> Get(Handle<Value> key);
1018
1019 // TODO(1245389): Replace the type-specific versions of these
1020 // functions with generic ones that accept a Handle<Value> key.
1021 bool Has(Handle<String> key);
1022 bool Delete(Handle<String> key);
1023 bool Has(uint32_t index);
1024 bool Delete(uint32_t index);
1025
1026 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001027 * Returns an array containing the names of the enumerable properties
1028 * of this object, including properties from prototype objects. The
1029 * array returned by this method contains the same values as would
1030 * be enumerated by a for-in statement over this object.
1031 */
1032 Local<Array> GetPropertyNames();
1033
1034 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035 * Get the prototype object. This does not skip objects marked to
1036 * be skipped by __proto__ and it does not consult the security
1037 * handler.
1038 */
1039 Local<Value> GetPrototype();
1040
1041 /**
1042 * Call builtin Object.prototype.toString on this object.
1043 * This is different from Value::ToString() that may call
1044 * user-defined toString function. This one does not.
1045 */
1046 Local<String> ObjectProtoToString();
1047
kasper.lund212ac232008-07-16 07:07:30 +00001048 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001050 /** Gets the value in an internal field. */
1051 Local<Value> GetInternalField(int index);
1052 /** Sets the value in an internal field. */
1053 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054
1055 // Testers for local properties.
1056 bool HasRealNamedProperty(Handle<String> key);
1057 bool HasRealIndexedProperty(uint32_t index);
1058 bool HasRealNamedCallbackProperty(Handle<String> key);
1059
1060 /**
1061 * If result.IsEmpty() no real property was located in the prototype chain.
1062 * This means interceptors in the prototype chain are not called.
1063 */
1064 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1065
1066 /** Tests for a named lookup interceptor.*/
1067 bool HasNamedLookupInterceptor();
1068
kasper.lund212ac232008-07-16 07:07:30 +00001069 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 bool HasIndexedLookupInterceptor();
1071
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001072 /**
1073 * Turns on access check on the object if the object is an instance of
1074 * a template that has access check callbacks. If an object has no
1075 * access check info, the object cannot be accessed by anyone.
1076 */
1077 void TurnOnAccessCheck();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001078
1079 /**
1080 * Returns the identity hash for this object. The current implemenation uses
1081 * a hidden property on the object to store the identity hash.
1082 */
1083 int GetIdentityHash();
1084
1085 /**
1086 * Access hidden properties on JavaScript objects. These properties are
1087 * hidden from the executing JavaScript and only accessible through the V8
1088 * C++ API. Hidden properties introduced by V8 internally (for example the
1089 * identity hash) are prefixed with "v8::".
1090 */
1091 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1092 Local<Value> GetHiddenValue(Handle<String> key);
1093 bool DeleteHiddenValue(Handle<String> key);
1094
1095 /**
1096 * Clone this object with a fast but shallow copy. Values will point
1097 * to the same values as the original object.
1098 */
1099 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
1101 static Local<Object> New();
1102 static Object* Cast(Value* obj);
1103 private:
1104 Object();
1105};
1106
1107
1108/**
1109 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1110 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001111class EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001113 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114
1115 static Local<Array> New(int length = 0);
1116 static Array* Cast(Value* obj);
1117 private:
1118 Array();
1119};
1120
1121
1122/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001123 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001125class EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001127 Local<Object> NewInstance() const;
1128 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1130 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001131 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 static Function* Cast(Value* obj);
1133 private:
1134 Function();
1135};
1136
1137
1138/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001139 * A JavaScript value that wraps a c++ void*. This type of value is
1140 * mainly used to associate c++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 * objects.
1142 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001143class EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 public:
1145 static Local<External> New(void* value);
1146 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001147 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 private:
1149 External();
1150};
1151
1152
1153// --- T e m p l a t e s ---
1154
1155
1156/**
1157 * The superclass of object and function templates.
1158 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001159class EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 public:
1161 /** Adds a property to each instance created by this template.*/
1162 void Set(Handle<String> name, Handle<Data> value,
1163 PropertyAttribute attributes = None);
1164 inline void Set(const char* name, Handle<Data> value);
1165 private:
1166 Template();
1167
1168 friend class ObjectTemplate;
1169 friend class FunctionTemplate;
1170};
1171
1172
1173/**
1174 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001175 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176 * including the receiver, the number and values of arguments, and
1177 * the holder of the function.
1178 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001179class EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 public:
1181 inline int Length() const;
1182 inline Local<Value> operator[](int i) const;
1183 inline Local<Function> Callee() const;
1184 inline Local<Object> This() const;
1185 inline Local<Object> Holder() const;
1186 inline bool IsConstructCall() const;
1187 inline Local<Value> Data() const;
1188 private:
1189 Arguments();
1190 friend class ImplementationUtilities;
1191 inline Arguments(Local<Value> data,
1192 Local<Object> holder,
1193 Local<Function> callee,
1194 bool is_construct_call,
1195 void** values, int length);
1196 Local<Value> data_;
1197 Local<Object> holder_;
1198 Local<Function> callee_;
1199 bool is_construct_call_;
1200 void** values_;
1201 int length_;
1202};
1203
1204
1205/**
1206 * The information passed to an accessor callback about the context
1207 * of the property access.
1208 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001209class EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 public:
1211 inline AccessorInfo(Local<Object> self,
1212 Local<Value> data,
1213 Local<Object> holder)
1214 : self_(self), data_(data), holder_(holder) { }
1215 inline Local<Value> Data() const;
1216 inline Local<Object> This() const;
1217 inline Local<Object> Holder() const;
1218 private:
1219 Local<Object> self_;
1220 Local<Value> data_;
1221 Local<Object> holder_;
1222};
1223
1224
1225typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1226
1227typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1228
1229/**
1230 * Accessor[Getter|Setter] are used as callback functions when
1231 * setting|getting a particular property. See objectTemplate::SetAccessor.
1232 */
1233typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1234 const AccessorInfo& info);
1235
1236
1237typedef void (*AccessorSetter)(Local<String> property,
1238 Local<Value> value,
1239 const AccessorInfo& info);
1240
1241
1242/**
1243 * NamedProperty[Getter|Setter] are used as interceptors on object.
1244 * See ObjectTemplate::SetNamedPropertyHandler.
1245 */
1246typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1247 const AccessorInfo& info);
1248
1249
1250/**
1251 * Returns the value if the setter intercepts the request.
1252 * Otherwise, returns an empty handle.
1253 */
1254typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1255 Local<Value> value,
1256 const AccessorInfo& info);
1257
1258
1259/**
1260 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001261 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 */
1263typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1264 const AccessorInfo& info);
1265
1266
1267/**
1268 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001269 * The return value is true if the property could be deleted and false
1270 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 */
1272typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1273 const AccessorInfo& info);
1274
1275/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001276 * Returns an array containing the names of the properties the named
1277 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 */
1279typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1280
v8.team.kasperl727e9952008-09-02 14:56:44 +00001281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001283 * Returns the value of the property if the getter intercepts the
1284 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 */
1286typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1287 const AccessorInfo& info);
1288
1289
1290/**
1291 * Returns the value if the setter intercepts the request.
1292 * Otherwise, returns an empty handle.
1293 */
1294typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1295 Local<Value> value,
1296 const AccessorInfo& info);
1297
1298
1299/**
1300 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001301 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 */
1303typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1304 const AccessorInfo& info);
1305
1306/**
1307 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001308 * The return value is true if the property could be deleted and false
1309 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 */
1311typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1312 const AccessorInfo& info);
1313
v8.team.kasperl727e9952008-09-02 14:56:44 +00001314/**
1315 * Returns an array containing the indices of the properties the
1316 * indexed property getter intercepts.
1317 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1319
1320
1321/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001322 * Access control specifications.
1323 *
1324 * Some accessors should be accessible across contexts. These
1325 * accessors have an explicit access control parameter which specifies
1326 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001327 *
1328 * Additionally, for security, accessors can prohibit overwriting by
1329 * accessors defined in JavaScript. For objects that have such
1330 * accessors either locally or in their prototype chain it is not
1331 * possible to overwrite the accessor by using __defineGetter__ or
1332 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 */
1334enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001335 DEFAULT = 0,
1336 ALL_CAN_READ = 1,
1337 ALL_CAN_WRITE = 1 << 1,
1338 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339};
1340
1341
1342/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001343 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 */
1345enum AccessType {
1346 ACCESS_GET,
1347 ACCESS_SET,
1348 ACCESS_HAS,
1349 ACCESS_DELETE,
1350 ACCESS_KEYS
1351};
1352
v8.team.kasperl727e9952008-09-02 14:56:44 +00001353
1354/**
1355 * Returns true if cross-context access should be allowed to the named
1356 * property with the given key on the global object.
1357 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358typedef bool (*NamedSecurityCallback)(Local<Object> global,
1359 Local<Value> key,
1360 AccessType type,
1361 Local<Value> data);
1362
v8.team.kasperl727e9952008-09-02 14:56:44 +00001363
1364/**
1365 * Returns true if cross-context access should be allowed to the indexed
1366 * property with the given index on the global object.
1367 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1369 uint32_t index,
1370 AccessType type,
1371 Local<Value> data);
1372
1373
1374/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001375 * A FunctionTemplate is used to create functions at runtime. There
1376 * can only be one function created from a FunctionTemplate in a
1377 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 *
1379 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001380 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001382 * A FunctionTemplate has a corresponding instance template which is
1383 * used to create object instances when the function is used as a
1384 * constructor. Properties added to the instance template are added to
1385 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 *
1387 * A FunctionTemplate can have a prototype template. The prototype template
1388 * is used to create the prototype object of the function.
1389 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001392 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1394 * t->Set("func_property", v8::Number::New(1));
1395 *
1396 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1397 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1398 * proto_t->Set("proto_const", v8::Number::New(2));
1399 *
1400 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1401 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1402 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1403 * instance_t->Set("instance_property", Number::New(3));
1404 *
1405 * v8::Local<v8::Function> function = t->GetFunction();
1406 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001407 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 *
1409 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001410 * and "instance" for the instance object created above. The function
1411 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001413 * \code
1414 * func_property in function == true;
1415 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001417 * function.prototype.proto_method() invokes 'InvokeCallback'
1418 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001420 * instance instanceof function == true;
1421 * instance.instance_accessor calls 'InstanceAccessorCallback'
1422 * instance.instance_property == 3;
1423 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001425 * A FunctionTemplate can inherit from another one by calling the
1426 * FunctionTemplate::Inherit method. The following graph illustrates
1427 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001429 * \code
1430 * FunctionTemplate Parent -> Parent() . prototype -> { }
1431 * ^ ^
1432 * | Inherit(Parent) | .__proto__
1433 * | |
1434 * FunctionTemplate Child -> Child() . prototype -> { }
1435 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001437 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1438 * object of the Child() function has __proto__ pointing to the
1439 * Parent() function's prototype object. An instance of the Child
1440 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001442 * Let Parent be the FunctionTemplate initialized in the previous
1443 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001445 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 * Local<FunctionTemplate> parent = t;
1447 * Local<FunctionTemplate> child = FunctionTemplate::New();
1448 * child->Inherit(parent);
1449 *
1450 * Local<Function> child_function = child->GetFunction();
1451 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001452 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001454 * The Child function and Child instance will have the following
1455 * properties:
1456 *
1457 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001459 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001461 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001463class EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 public:
1465 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001466 static Local<FunctionTemplate> New(
1467 InvocationCallback callback = 0,
1468 Handle<Value> data = Handle<Value>(),
1469 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470 /** Returns the unique function instance in the current execution context.*/
1471 Local<Function> GetFunction();
1472
v8.team.kasperl727e9952008-09-02 14:56:44 +00001473 /**
1474 * Set the call-handler callback for a FunctionTemplate. This
1475 * callback is called whenever the function created from this
1476 * FunctionTemplate is called.
1477 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 void SetCallHandler(InvocationCallback callback,
1479 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480
v8.team.kasperl727e9952008-09-02 14:56:44 +00001481 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 Local<ObjectTemplate> InstanceTemplate();
1483
1484 /** Causes the function template to inherit from a parent function template.*/
1485 void Inherit(Handle<FunctionTemplate> parent);
1486
1487 /**
1488 * A PrototypeTemplate is the template used to create the prototype object
1489 * of the function created by this template.
1490 */
1491 Local<ObjectTemplate> PrototypeTemplate();
1492
v8.team.kasperl727e9952008-09-02 14:56:44 +00001493
1494 /**
1495 * Set the class name of the FunctionTemplate. This is used for
1496 * printing objects created with the function created from the
1497 * FunctionTemplate as its constructor.
1498 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 void SetClassName(Handle<String> name);
1500
1501 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001502 * Determines whether the __proto__ accessor ignores instances of
1503 * the function template. If instances of the function template are
1504 * ignored, __proto__ skips all instances and instead returns the
1505 * next object in the prototype chain.
1506 *
1507 * Call with a value of true to make the __proto__ accessor ignore
1508 * instances of the function template. Call with a value of false
1509 * to make the __proto__ accessor not ignore instances of the
1510 * function template. By default, instances of a function template
1511 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 */
1513 void SetHiddenPrototype(bool value);
1514
1515 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001516 * Returns true if the given object is an instance of this function
1517 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 */
1519 bool HasInstance(Handle<Value> object);
1520
1521 private:
1522 FunctionTemplate();
1523 void AddInstancePropertyAccessor(Handle<String> name,
1524 AccessorGetter getter,
1525 AccessorSetter setter,
1526 Handle<Value> data,
1527 AccessControl settings,
1528 PropertyAttribute attributes);
1529 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1530 NamedPropertySetter setter,
1531 NamedPropertyQuery query,
1532 NamedPropertyDeleter remover,
1533 NamedPropertyEnumerator enumerator,
1534 Handle<Value> data);
1535 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1536 IndexedPropertySetter setter,
1537 IndexedPropertyQuery query,
1538 IndexedPropertyDeleter remover,
1539 IndexedPropertyEnumerator enumerator,
1540 Handle<Value> data);
1541 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1542 Handle<Value> data);
1543
1544 friend class Context;
1545 friend class ObjectTemplate;
1546};
1547
1548
1549/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001550 * An ObjectTemplate is used to create objects at runtime.
1551 *
1552 * Properties added to an ObjectTemplate are added to each object
1553 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001555class EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001557 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 /** Creates a new instance of this template.*/
1561 Local<Object> NewInstance();
1562
1563 /**
1564 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001565 *
1566 * Whenever the property with the given name is accessed on objects
1567 * created from this ObjectTemplate the getter and setter callbacks
1568 * are called instead of getting and setting the property directly
1569 * on the JavaScript object.
1570 *
1571 * \param name The name of the property for which an accessor is added.
1572 * \param getter The callback to invoke when getting the property.
1573 * \param setter The callback to invoke when setting the property.
1574 * \param data A piece of data that will be passed to the getter and setter
1575 * callbacks whenever they are invoked.
1576 * \param settings Access control settings for the accessor. This is a bit
1577 * field consisting of one of more of
1578 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1579 * The default is to not allow cross-context access.
1580 * ALL_CAN_READ means that all cross-context reads are allowed.
1581 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1582 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1583 * cross-context access.
1584 * \param attribute The attributes of the property for which an accessor
1585 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 */
1587 void SetAccessor(Handle<String> name,
1588 AccessorGetter getter,
1589 AccessorSetter setter = 0,
1590 Handle<Value> data = Handle<Value>(),
1591 AccessControl settings = DEFAULT,
1592 PropertyAttribute attribute = None);
1593
1594 /**
1595 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001596 *
1597 * Whenever a named property is accessed on objects created from
1598 * this object template, the provided callback is invoked instead of
1599 * accessing the property directly on the JavaScript object.
1600 *
1601 * \param getter The callback to invoke when getting a property.
1602 * \param setter The callback to invoke when setting a property.
1603 * \param query The callback to invoke to check is an object has a property.
1604 * \param deleter The callback to invoke when deleting a property.
1605 * \param enumerator The callback to invoke to enumerate all the named
1606 * properties of an object.
1607 * \param data A piece of data that will be passed to the callbacks
1608 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 */
1610 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1611 NamedPropertySetter setter = 0,
1612 NamedPropertyQuery query = 0,
1613 NamedPropertyDeleter deleter = 0,
1614 NamedPropertyEnumerator enumerator = 0,
1615 Handle<Value> data = Handle<Value>());
1616
1617 /**
1618 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001619 *
1620 * Whenever an indexed property is accessed on objects created from
1621 * this object template, the provided callback is invoked instead of
1622 * accessing the property directly on the JavaScript object.
1623 *
1624 * \param getter The callback to invoke when getting a property.
1625 * \param setter The callback to invoke when setting a property.
1626 * \param query The callback to invoke to check is an object has a property.
1627 * \param deleter The callback to invoke when deleting a property.
1628 * \param enumerator The callback to invoke to enumerate all the indexed
1629 * properties of an object.
1630 * \param data A piece of data that will be passed to the callbacks
1631 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632 */
1633 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1634 IndexedPropertySetter setter = 0,
1635 IndexedPropertyQuery query = 0,
1636 IndexedPropertyDeleter deleter = 0,
1637 IndexedPropertyEnumerator enumerator = 0,
1638 Handle<Value> data = Handle<Value>());
1639 /**
1640 * Sets the callback to be used when calling instances created from
1641 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001642 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 * function.
1644 */
1645 void SetCallAsFunctionHandler(InvocationCallback callback,
1646 Handle<Value> data = Handle<Value>());
1647
v8.team.kasperl727e9952008-09-02 14:56:44 +00001648 /**
1649 * Mark object instances of the template as undetectable.
1650 *
1651 * In many ways, undetectable objects behave as though they are not
1652 * there. They behave like 'undefined' in conditionals and when
1653 * printed. However, properties can be accessed and called as on
1654 * normal objects.
1655 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 void MarkAsUndetectable();
1657
v8.team.kasperl727e9952008-09-02 14:56:44 +00001658 /**
1659 * Sets access check callbacks on the object template.
1660 *
1661 * When accessing properties on instances of this object template,
1662 * the access check callback will be called to determine whether or
1663 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001664 * The last parameter specifies whether access checks are turned
1665 * on by default on instances. If access checks are off by default,
1666 * they can be turned on on individual instances by calling
1667 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001668 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1670 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001671 Handle<Value> data = Handle<Value>(),
1672 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673
kasper.lund212ac232008-07-16 07:07:30 +00001674 /**
1675 * Gets the number of internal fields for objects generated from
1676 * this template.
1677 */
1678 int InternalFieldCount();
1679
1680 /**
1681 * Sets the number of internal fields for objects generated from
1682 * this template.
1683 */
1684 void SetInternalFieldCount(int value);
1685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 private:
1687 ObjectTemplate();
1688 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1689 friend class FunctionTemplate;
1690};
1691
1692
1693/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001694 * A Signature specifies which receivers and arguments a function can
1695 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001697class EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 public:
1699 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1700 Handle<FunctionTemplate>(),
1701 int argc = 0,
1702 Handle<FunctionTemplate> argv[] = 0);
1703 private:
1704 Signature();
1705};
1706
1707
1708/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001709 * A utility for determining the type of objects based on the template
1710 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001712class EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 public:
1714 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1715 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1716 int match(Handle<Value> value);
1717 private:
1718 TypeSwitch();
1719};
1720
1721
1722// --- E x t e n s i o n s ---
1723
1724
1725/**
1726 * Ignore
1727 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001728class EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 public:
1730 Extension(const char* name,
1731 const char* source = 0,
1732 int dep_count = 0,
1733 const char** deps = 0);
1734 virtual ~Extension() { }
1735 virtual v8::Handle<v8::FunctionTemplate>
1736 GetNativeFunction(v8::Handle<v8::String> name) {
1737 return v8::Handle<v8::FunctionTemplate>();
1738 }
1739
1740 const char* name() { return name_; }
1741 const char* source() { return source_; }
1742 int dependency_count() { return dep_count_; }
1743 const char** dependencies() { return deps_; }
1744 void set_auto_enable(bool value) { auto_enable_ = value; }
1745 bool auto_enable() { return auto_enable_; }
1746
1747 private:
1748 const char* name_;
1749 const char* source_;
1750 int dep_count_;
1751 const char** deps_;
1752 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001753
1754 // Disallow copying and assigning.
1755 Extension(const Extension&);
1756 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757};
1758
1759
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001760void EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761
1762
1763/**
1764 * Ignore
1765 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001766class EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 public:
1768 inline DeclareExtension(Extension* extension) {
1769 RegisterExtension(extension);
1770 }
1771};
1772
1773
1774// --- S t a t i c s ---
1775
1776
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001777Handle<Primitive> EXPORT Undefined();
1778Handle<Primitive> EXPORT Null();
1779Handle<Boolean> EXPORT True();
1780Handle<Boolean> EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781
1782
1783/**
1784 * A set of constraints that specifies the limits of the runtime's
1785 * memory use.
1786 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001787class EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 public:
1789 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001790 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001792 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001794 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1796 private:
1797 int max_young_space_size_;
1798 int max_old_space_size_;
1799 uint32_t* stack_limit_;
1800};
1801
1802
1803bool SetResourceConstraints(ResourceConstraints* constraints);
1804
1805
1806// --- E x c e p t i o n s ---
1807
1808
1809typedef void (*FatalErrorCallback)(const char* location, const char* message);
1810
1811
1812typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1813
1814
1815/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001816 * Schedules an exception to be thrown when returning to JavaScript. When an
1817 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001819 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001821Handle<Value> EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
1823/**
1824 * Create new error objects by calling the corresponding error object
1825 * constructor with the message.
1826 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001827class EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 public:
1829 static Local<Value> RangeError(Handle<String> message);
1830 static Local<Value> ReferenceError(Handle<String> message);
1831 static Local<Value> SyntaxError(Handle<String> message);
1832 static Local<Value> TypeError(Handle<String> message);
1833 static Local<Value> Error(Handle<String> message);
1834};
1835
1836
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001837// --- C o u n t e r s C a l l b a c k s ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001839typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840
1841// --- 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 ---
1842typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1843 AccessType type,
1844 Local<Value> data);
1845
1846// --- 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
1847
1848/**
1849 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001850 * before and after a major garbage collection. Allocations are not
1851 * allowed in the callback function, you therefore cannot manipulate
1852 * objects (set or delete properties for example) since it is possible
1853 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 */
1855typedef void (*GCCallback)();
1856
1857
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001858// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859
1860/**
1861 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001862 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864typedef Persistent<Context> (*ContextGenerator)();
1865
1866
1867/**
1868 * Container class for static utility functions.
1869 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001870class EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001872 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 static void SetFatalErrorHandler(FatalErrorCallback that);
1874
v8.team.kasperl727e9952008-09-02 14:56:44 +00001875 /**
1876 * Ignore out-of-memory exceptions.
1877 *
1878 * V8 running out of memory is treated as a fatal error by default.
1879 * This means that the fatal error handler is called and that V8 is
1880 * terminated.
1881 *
1882 * IgnoreOutOfMemoryException can be used to not treat a
1883 * out-of-memory situation as a fatal error. This way, the contexts
1884 * that did not cause the out of memory problem might be able to
1885 * continue execution.
1886 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 static void IgnoreOutOfMemoryException();
1888
v8.team.kasperl727e9952008-09-02 14:56:44 +00001889 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001890 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001891 * fatal errors such as out-of-memory situations.
1892 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 static bool IsDead();
1894
1895 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001896 * Adds a message listener.
1897 *
1898 * The same message listener can be added more than once and it that
1899 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 */
1901 static bool AddMessageListener(MessageCallback that,
1902 Handle<Value> data = Handle<Value>());
1903
1904 /**
1905 * Remove all message listeners from the specified callback function.
1906 */
1907 static void RemoveMessageListeners(MessageCallback that);
1908
1909 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001910 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 */
1912 static void SetFlagsFromString(const char* str, int length);
1913
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001914 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001915 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001916 */
1917 static void SetFlagsFromCommandLine(int* argc,
1918 char** argv,
1919 bool remove_flags);
1920
kasper.lund7276f142008-07-30 08:49:36 +00001921 /** Get the version string. */
1922 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923
1924 /**
1925 * Enables the host application to provide a mechanism for recording
1926 * statistics counters.
1927 */
1928 static void SetCounterFunction(CounterLookupCallback);
1929
1930 /**
1931 * Enables the computation of a sliding window of states. The sliding
1932 * window information is recorded in statistics counters.
1933 */
1934 static void EnableSlidingStateWindow();
1935
1936 /** Callback function for reporting failed access checks.*/
1937 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
1938
1939 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001940 * Enables the host application to receive a notification before a
1941 * major garbage colletion. Allocations are not allowed in the
1942 * callback function, you therefore cannot manipulate objects (set
1943 * or delete properties for example) since it is possible such
1944 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 */
1946 static void SetGlobalGCPrologueCallback(GCCallback);
1947
1948 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001949 * Enables the host application to receive a notification after a
1950 * major garbage collection. Allocations are not allowed in the
1951 * callback function, you therefore cannot manipulate objects (set
1952 * or delete properties for example) since it is possible such
1953 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 */
1955 static void SetGlobalGCEpilogueCallback(GCCallback);
1956
1957 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001958 * Allows the host application to group objects together. If one
1959 * object in the group is alive, all objects in the group are alive.
1960 * After each garbage collection, object groups are removed. It is
1961 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00001962 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00001963 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00001965 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966
1967 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001968 * Initializes from snapshot if possible. Otherwise, attempts to
1969 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 */
1971 static bool Initialize();
1972
kasper.lund7276f142008-07-30 08:49:36 +00001973 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001974 * Adjusts the amount of registered external memory. Used to give
1975 * V8 an indication of the amount of externally allocated memory
1976 * that is kept alive by JavaScript objects. V8 uses this to decide
1977 * when to perform global garbage collections. Registering
1978 * externally allocated memory will trigger global garbage
1979 * collections more often than otherwise in an attempt to garbage
1980 * collect the JavaScript objects keeping the externally allocated
1981 * memory alive.
1982 *
1983 * \param change_in_bytes the change in externally allocated memory
1984 * that is kept alive by JavaScript objects.
1985 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00001986 */
1987 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
1988
iposva@chromium.org245aa852009-02-10 00:49:54 +00001989 /**
1990 * Suspends recording of tick samples in the profiler.
1991 * When the V8 profiling mode is enabled (usually via command line
1992 * switches) this function suspends recording of tick samples.
1993 * Profiling ticks are discarded until ResumeProfiler() is called.
1994 *
1995 * See also the --prof and --prof_auto command line switches to
1996 * enable V8 profiling.
1997 */
1998 static void PauseProfiler();
1999
2000 /**
2001 * Resumes recording of tick samples in the profiler.
2002 * See also PauseProfiler().
2003 */
2004 static void ResumeProfiler();
2005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 private:
2007 V8();
2008
2009 static void** GlobalizeReference(void** handle);
2010 static void DisposeGlobal(void** global_handle);
2011 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
2012 static void ClearWeak(void** global_handle);
2013 static bool IsGlobalNearDeath(void** global_handle);
2014 static bool IsGlobalWeak(void** global_handle);
2015
2016 template <class T> friend class Handle;
2017 template <class T> friend class Local;
2018 template <class T> friend class Persistent;
2019 friend class Context;
2020};
2021
2022
2023/**
2024 * An external exception handler.
2025 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002026class EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 public:
2028
2029 /**
2030 * Creates a new try/catch block and registers it with v8.
2031 */
2032 TryCatch();
2033
2034 /**
2035 * Unregisters and deletes this try/catch block.
2036 */
2037 ~TryCatch();
2038
2039 /**
2040 * Returns true if an exception has been caught by this try/catch block.
2041 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002042 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043
2044 /**
2045 * Returns the exception caught by this try/catch block. If no exception has
2046 * been caught an empty handle is returned.
2047 *
2048 * The returned handle is valid until this TryCatch block has been destroyed.
2049 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002050 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
2052 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002053 * Returns the message associated with this exception. If there is
2054 * no message associated an empty handle is returned.
2055 *
2056 * The returned handle is valid until this TryCatch block has been
2057 * destroyed.
2058 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002059 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002060
2061 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 * Clears any exceptions that may have been caught by this try/catch block.
2063 * After this method has been called, HasCaught() will return false.
2064 *
2065 * It is not necessary to clear a try/catch block before using it again; if
2066 * another exception is thrown the previously caught exception will just be
2067 * overwritten. However, it is often a good idea since it makes it easier
2068 * to determine which operation threw a given exception.
2069 */
2070 void Reset();
2071
v8.team.kasperl727e9952008-09-02 14:56:44 +00002072 /**
2073 * Set verbosity of the external exception handler.
2074 *
2075 * By default, exceptions that are caught by an external exception
2076 * handler are not reported. Call SetVerbose with true on an
2077 * external exception handler to have exceptions caught by the
2078 * handler reported as if they were not caught.
2079 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 void SetVerbose(bool value);
2081
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002082 /**
2083 * Set whether or not this TryCatch should capture a Message object
2084 * which holds source information about where the exception
2085 * occurred. True by default.
2086 */
2087 void SetCaptureMessage(bool value);
2088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 public:
2090 TryCatch* next_;
2091 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002092 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002094 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002095 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096};
2097
2098
2099// --- C o n t e x t ---
2100
2101
2102/**
2103 * Ignore
2104 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002105class EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 public:
2107 ExtensionConfiguration(int name_count, const char* names[])
2108 : name_count_(name_count), names_(names) { }
2109 private:
2110 friend class ImplementationUtilities;
2111 int name_count_;
2112 const char** names_;
2113};
2114
2115
2116/**
2117 * A sandboxed execution context with its own set of built-in objects
2118 * and functions.
2119 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002120class EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002122 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 Local<Object> Global();
2124
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002125 /**
2126 * Detaches the global object from its context before
2127 * the global object can be reused to create a new context.
2128 */
2129 void DetachGlobal();
2130
v8.team.kasperl727e9952008-09-02 14:56:44 +00002131 /** Creates a new context. */
2132 static Persistent<Context> New(
2133 ExtensionConfiguration* extensions = 0,
2134 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2135 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136
kasper.lund44510672008-07-25 07:37:58 +00002137 /** Returns the last entered context. */
2138 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139
kasper.lund44510672008-07-25 07:37:58 +00002140 /** Returns the context that is on the top of the stack. */
2141 static Local<Context> GetCurrent();
2142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 /**
2144 * Sets the security token for the context. To access an object in
2145 * another context, the security tokens must match.
2146 */
2147 void SetSecurityToken(Handle<Value> token);
2148
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002149 /** Restores the security token to the default value. */
2150 void UseDefaultSecurityToken();
2151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 /** Returns the security token of this context.*/
2153 Handle<Value> GetSecurityToken();
2154
v8.team.kasperl727e9952008-09-02 14:56:44 +00002155 /**
2156 * Enter this context. After entering a context, all code compiled
2157 * and run is compiled and run in this context. If another context
2158 * is already entered, this old context is saved so it can be
2159 * restored when the new context is exited.
2160 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002162
2163 /**
2164 * Exit this context. Exiting the current context restores the
2165 * context that was in place when entering the current context.
2166 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 void Exit();
2168
v8.team.kasperl727e9952008-09-02 14:56:44 +00002169 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 bool HasOutOfMemoryException();
2171
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 static bool InContext();
2174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 /**
2176 * Stack-allocated class which sets the execution context for all
2177 * operations executed within a local scope.
2178 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002179 class EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 public:
2181 inline Scope(Handle<Context> context) : context_(context) {
2182 context_->Enter();
2183 }
2184 inline ~Scope() { context_->Exit(); }
2185 private:
2186 Handle<Context> context_;
2187 };
2188
2189 private:
2190 friend class Value;
2191 friend class Script;
2192 friend class Object;
2193 friend class Function;
2194};
2195
2196
2197/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * Multiple threads in V8 are allowed, but only one thread at a time
2199 * is allowed to use V8. The definition of 'using V8' includes
2200 * accessing handles or holding onto object pointers obtained from V8
2201 * handles. It is up to the user of V8 to ensure (perhaps with
2202 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 *
2204 * If you wish to start using V8 in a thread you can do this by constructing
2205 * a v8::Locker object. After the code using V8 has completed for the
2206 * current thread you can call the destructor. This can be combined
2207 * with C++ scope-based construction as follows:
2208 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002209 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 * ...
2211 * {
2212 * v8::Locker locker;
2213 * ...
2214 * // Code using V8 goes here.
2215 * ...
2216 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002217 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 *
2219 * If you wish to stop using V8 in a thread A you can do this by either
2220 * by destroying the v8::Locker object as above or by constructing a
2221 * v8::Unlocker object:
2222 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002223 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 * {
2225 * v8::Unlocker unlocker;
2226 * ...
2227 * // Code not using V8 goes here while V8 can run in another thread.
2228 * ...
2229 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002230 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 *
2232 * The Unlocker object is intended for use in a long-running callback
2233 * from V8, where you want to release the V8 lock for other threads to
2234 * use.
2235 *
2236 * The v8::Locker is a recursive lock. That is, you can lock more than
2237 * once in a given thread. This can be useful if you have code that can
2238 * be called either from code that holds the lock or from code that does
2239 * not. The Unlocker is not recursive so you can not have several
2240 * Unlockers on the stack at once, and you can not use an Unlocker in a
2241 * thread that is not inside a Locker's scope.
2242 *
2243 * An unlocker will unlock several lockers if it has to and reinstate
2244 * the correct depth of locking on its destruction. eg.:
2245 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002246 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 * // V8 not locked.
2248 * {
2249 * v8::Locker locker;
2250 * // V8 locked.
2251 * {
2252 * v8::Locker another_locker;
2253 * // V8 still locked (2 levels).
2254 * {
2255 * v8::Unlocker unlocker;
2256 * // V8 not locked.
2257 * }
2258 * // V8 locked again (2 levels).
2259 * }
2260 * // V8 still locked (1 level).
2261 * }
2262 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002265class EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 public:
2267 Unlocker();
2268 ~Unlocker();
2269};
2270
2271
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002272class EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 public:
2274 Locker();
2275 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002276
2277 /**
2278 * Start preemption.
2279 *
2280 * When preemption is started, a timer is fired every n milli seconds
2281 * that will switch between multiple threads that are in contention
2282 * for the V8 lock.
2283 */
2284 static void StartPreemption(int every_n_ms);
2285
2286 /**
2287 * Stop preemption.
2288 */
2289 static void StopPreemption();
2290
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002291 /**
2292 * Returns whether or not the locker is locked by the current thread.
2293 */
2294 static bool IsLocked();
2295
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002296 /**
2297 * Returns whether v8::Locker is being used by this V8 instance.
2298 */
2299 static bool IsActive() { return active_; }
2300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 private:
2302 bool has_lock_;
2303 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002304
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002305 static bool active_;
2306
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002307 // Disallow copying and assigning.
2308 Locker(const Locker&);
2309 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310};
2311
2312
2313
2314// --- I m p l e m e n t a t i o n ---
2315
2316template <class T>
2317Handle<T>::Handle() : val_(0) { }
2318
2319
2320template <class T>
2321Local<T>::Local() : Handle<T>() { }
2322
2323
2324template <class T>
2325Local<T> Local<T>::New(Handle<T> that) {
2326 if (that.IsEmpty()) return Local<T>();
2327 void** p = reinterpret_cast<void**>(*that);
2328 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2329}
2330
2331
2332template <class T>
2333Persistent<T> Persistent<T>::New(Handle<T> that) {
2334 if (that.IsEmpty()) return Persistent<T>();
2335 void** p = reinterpret_cast<void**>(*that);
2336 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2337}
2338
2339
2340template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002341bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 if (this->IsEmpty()) return false;
2343 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2344}
2345
2346
2347template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002348bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 if (this->IsEmpty()) return false;
2350 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2351}
2352
2353
2354template <class T>
2355void Persistent<T>::Dispose() {
2356 if (this->IsEmpty()) return;
2357 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2358}
2359
2360
2361template <class T>
2362Persistent<T>::Persistent() : Handle<T>() { }
2363
2364template <class T>
2365void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2366 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2367}
2368
2369template <class T>
2370void Persistent<T>::ClearWeak() {
2371 V8::ClearWeak(reinterpret_cast<void**>(**this));
2372}
2373
2374template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002375T* Handle<T>::operator->() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376 return val_;
2377}
2378
2379
2380template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002381T* Handle<T>::operator*() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 return val_;
2383}
2384
2385
2386Local<Value> Arguments::operator[](int i) const {
2387 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2388 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2389}
2390
2391
2392Local<Function> Arguments::Callee() const {
2393 return callee_;
2394}
2395
2396
2397Local<Object> Arguments::This() const {
2398 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2399}
2400
2401
2402Local<Object> Arguments::Holder() const {
2403 return holder_;
2404}
2405
2406
2407Local<Value> Arguments::Data() const {
2408 return data_;
2409}
2410
2411
2412bool Arguments::IsConstructCall() const {
2413 return is_construct_call_;
2414}
2415
2416
2417int Arguments::Length() const {
2418 return length_;
2419}
2420
2421
2422Local<Value> AccessorInfo::Data() const {
2423 return data_;
2424}
2425
2426
2427Local<Object> AccessorInfo::This() const {
2428 return self_;
2429}
2430
2431
2432Local<Object> AccessorInfo::Holder() const {
2433 return holder_;
2434}
2435
2436
2437template <class T>
2438Local<T> HandleScope::Close(Handle<T> value) {
2439 void** after = RawClose(reinterpret_cast<void**>(*value));
2440 return Local<T>(reinterpret_cast<T*>(after));
2441}
2442
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002443Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 return resource_name_;
2445}
2446
2447
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002448Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 return resource_line_offset_;
2450}
2451
2452
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002453Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454 return resource_column_offset_;
2455}
2456
2457
2458Handle<Boolean> Boolean::New(bool value) {
2459 return value ? True() : False();
2460}
2461
2462
2463void Template::Set(const char* name, v8::Handle<Data> value) {
2464 Set(v8::String::New(name), value);
2465}
2466
2467
2468/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002469 * \example shell.cc
2470 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471 * command-line and executes them.
2472 */
2473
2474
2475/**
2476 * \example process.cc
2477 */
2478
2479
2480} // namespace v8
2481
2482
2483#undef EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002484#undef EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485#undef TYPE_CHECK
2486
2487
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002488#endif // V8_H_