blob: fc8b47a4f491f39e6427f1375d522b9618f9ebe8 [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();
527};
528
529
530/**
531 * An error message.
532 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000533class EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000535 Local<String> Get() const;
536 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537
ager@chromium.org32912102009-01-16 10:38:43 +0000538 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000540 /**
541 * Returns the number, 1-based, of the line where the error occurred.
542 */
ager@chromium.org32912102009-01-16 10:38:43 +0000543 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000545 /**
546 * Returns the index within the script of the first character where
547 * the error occurred.
548 */
ager@chromium.org32912102009-01-16 10:38:43 +0000549 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000550
551 /**
552 * Returns the index within the script of the last character where
553 * the error occurred.
554 */
ager@chromium.org32912102009-01-16 10:38:43 +0000555 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000556
557 /**
558 * Returns the index within the line of the first character where
559 * the error occurred.
560 */
ager@chromium.org32912102009-01-16 10:38:43 +0000561 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000562
563 /**
564 * Returns the index within the line of the last character where
565 * the error occurred.
566 */
ager@chromium.org32912102009-01-16 10:38:43 +0000567 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 // TODO(1245381): Print to a string instead of on a FILE.
570 static void PrintCurrentStackTrace(FILE* out);
571};
572
573
574// --- V a l u e ---
575
576
577/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000578 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000580class EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 public:
582
583 /**
584 * Returns true if this value is the undefined value. See ECMA-262
585 * 4.3.10.
586 */
ager@chromium.org32912102009-01-16 10:38:43 +0000587 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588
589 /**
590 * Returns true if this value is the null value. See ECMA-262
591 * 4.3.11.
592 */
ager@chromium.org32912102009-01-16 10:38:43 +0000593 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
595 /**
596 * Returns true if this value is true.
597 */
ager@chromium.org32912102009-01-16 10:38:43 +0000598 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599
600 /**
601 * Returns true if this value is false.
602 */
ager@chromium.org32912102009-01-16 10:38:43 +0000603 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604
605 /**
606 * Returns true if this value is an instance of the String type.
607 * See ECMA-262 8.4.
608 */
ager@chromium.org32912102009-01-16 10:38:43 +0000609 bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
611 /**
612 * Returns true if this value is a function.
613 */
ager@chromium.org32912102009-01-16 10:38:43 +0000614 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
616 /**
617 * Returns true if this value is an array.
618 */
ager@chromium.org32912102009-01-16 10:38:43 +0000619 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000620
v8.team.kasperl727e9952008-09-02 14:56:44 +0000621 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 * Returns true if this value is an object.
623 */
ager@chromium.org32912102009-01-16 10:38:43 +0000624 bool IsObject() 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 boolean.
628 */
ager@chromium.org32912102009-01-16 10:38:43 +0000629 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +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 a number.
633 */
ager@chromium.org32912102009-01-16 10:38:43 +0000634 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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 external.
638 */
ager@chromium.org32912102009-01-16 10:38:43 +0000639 bool IsExternal() 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 a 32-bit signed integer.
643 */
ager@chromium.org32912102009-01-16 10:38:43 +0000644 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000646 /**
647 * Returns true if this value is a Date.
648 */
ager@chromium.org32912102009-01-16 10:38:43 +0000649 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000650
ager@chromium.org32912102009-01-16 10:38:43 +0000651 Local<Boolean> ToBoolean() const;
652 Local<Number> ToNumber() const;
653 Local<String> ToString() const;
654 Local<String> ToDetailString() const;
655 Local<Object> ToObject() const;
656 Local<Integer> ToInteger() const;
657 Local<Uint32> ToUint32() const;
658 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659
660 /**
661 * Attempts to convert a string to an array index.
662 * Returns an empty handle if the conversion fails.
663 */
ager@chromium.org32912102009-01-16 10:38:43 +0000664 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
ager@chromium.org32912102009-01-16 10:38:43 +0000666 bool BooleanValue() const;
667 double NumberValue() const;
668 int64_t IntegerValue() const;
669 uint32_t Uint32Value() const;
670 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
672 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000673 bool Equals(Handle<Value> that) const;
674 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675};
676
677
678/**
679 * The superclass of primitive values. See ECMA-262 4.3.2.
680 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000681class EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682
683
684/**
685 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
686 * or false value.
687 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000688class EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000690 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691 static inline Handle<Boolean> New(bool value);
692};
693
694
695/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000696 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000698class EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000700
701 /**
702 * Returns the number of characters in this string.
703 */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
v8.team.kasperl727e9952008-09-02 14:56:44 +0000706 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000707 * Returns the number of bytes in the UTF-8 encoded
708 * representation of this string.
709 */
ager@chromium.org32912102009-01-16 10:38:43 +0000710 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000711
712 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000713 * Write the contents of the string to an external buffer.
714 * If no arguments are given, expects the buffer to be large
715 * enough to hold the entire string and NULL terminator. Copies
716 * the contents of the string and the NULL terminator into the
717 * buffer.
718 *
719 * Copies up to length characters into the output buffer.
720 * Only null-terminates if there is enough space in the buffer.
721 *
722 * \param buffer The buffer into which the string will be copied.
723 * \param start The starting position within the string at which
724 * copying begins.
725 * \param length The number of bytes to copy from the string.
726 * \return The number of characters copied to the buffer
727 * excluding the NULL terminator.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
730 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
731 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732
v8.team.kasperl727e9952008-09-02 14:56:44 +0000733 /**
734 * Returns true if the string is external
735 */
ager@chromium.org32912102009-01-16 10:38:43 +0000736 bool IsExternal() const;
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 both external and ascii
740 */
ager@chromium.org32912102009-01-16 10:38:43 +0000741 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000742 /**
743 * An ExternalStringResource is a wrapper around a two-byte string
744 * buffer that resides outside V8's heap. Implement an
745 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000746 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000747 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000748 class EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 public:
750 /**
751 * Override the destructor to manage the life cycle of the underlying
752 * buffer.
753 */
754 virtual ~ExternalStringResource() {}
755 /** The string data from the underlying buffer.*/
756 virtual const uint16_t* data() const = 0;
757 /** The length of the string. That is, the number of two-byte characters.*/
758 virtual size_t length() const = 0;
759 protected:
760 ExternalStringResource() {}
761 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000762 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763 ExternalStringResource(const ExternalStringResource&);
764 void operator=(const ExternalStringResource&);
765 };
766
767 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000768 * An ExternalAsciiStringResource is a wrapper around an ascii
769 * string buffer that resides outside V8's heap. Implement an
770 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000771 * underlying buffer. Note that the string data must be immutable
772 * and that the data must be strict 7-bit ASCII, not Latin1 or
773 * UTF-8, which would require special treatment internally in the
774 * engine and, in the case of UTF-8, do not allow efficient indexing.
775 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000776 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000778 class EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 public:
780 /**
781 * Override the destructor to manage the life cycle of the underlying
782 * buffer.
783 */
784 virtual ~ExternalAsciiStringResource() {}
785 /** The string data from the underlying buffer.*/
786 virtual const char* data() const = 0;
787 /** The number of ascii characters in the string.*/
788 virtual size_t length() const = 0;
789 protected:
790 ExternalAsciiStringResource() {}
791 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000792 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
794 void operator=(const ExternalAsciiStringResource&);
795 };
796
797 /**
798 * Get the ExternalStringResource for an external string. Only
799 * valid if IsExternal() returns true.
800 */
ager@chromium.org32912102009-01-16 10:38:43 +0000801 ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000802
803 /**
804 * Get the ExternalAsciiStringResource for an external ascii string.
805 * Only valid if IsExternalAscii() returns true.
806 */
ager@chromium.org32912102009-01-16 10:38:43 +0000807 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808
809 static String* Cast(v8::Value* obj);
810
811 /**
812 * Allocates a new string from either utf-8 encoded or ascii data.
813 * The second parameter 'length' gives the buffer length.
814 * If the data is utf-8 encoded, the caller must
815 * be careful to supply the length parameter.
816 * If it is not given, the function calls
817 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000818 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819 */
820 static Local<String> New(const char* data, int length = -1);
821
822 /** Allocates a new string from utf16 data.*/
823 static Local<String> New(const uint16_t* data, int length = -1);
824
825 /** Creates a symbol. Returns one if it exists already.*/
826 static Local<String> NewSymbol(const char* data, int length = -1);
827
v8.team.kasperl727e9952008-09-02 14:56:44 +0000828 /**
829 * Creates a new external string using the data defined in the given
830 * resource. The resource is deleted when the external string is no
831 * longer live on V8's heap. The caller of this function should not
832 * delete or modify the resource. Neither should the underlying buffer be
833 * deallocated or modified except through the destructor of the
834 * external string resource.
835 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 static Local<String> NewExternal(ExternalStringResource* resource);
837
v8.team.kasperl727e9952008-09-02 14:56:44 +0000838 /**
839 * Creates a new external string using the ascii data defined in the given
840 * resource. The resource is deleted when the external string is no
841 * longer live on V8's heap. The caller of this function should not
842 * delete or modify the resource. Neither should the underlying buffer be
843 * deallocated or modified except through the destructor of the
844 * external string resource.
845 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
847
kasper.lund7276f142008-07-30 08:49:36 +0000848 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849 static Local<String> NewUndetectable(const char* data, int length = -1);
850
kasper.lund7276f142008-07-30 08:49:36 +0000851 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
853
854 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000855 * Converts an object to a utf8-encoded character array. Useful if
856 * you want to print the object.
857 */
858 class EXPORT Utf8Value {
859 public:
860 explicit Utf8Value(Handle<v8::Value> obj);
861 ~Utf8Value();
862 char* operator*() { return str_; }
863 int length() { return length_; }
864 private:
865 char* str_;
866 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000867
868 // Disallow copying and assigning.
869 Utf8Value(const Utf8Value&);
870 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000871 };
872
873 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 * Converts an object to an ascii string.
875 * Useful if you want to print the object.
876 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000877 class EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 public:
879 explicit AsciiValue(Handle<v8::Value> obj);
880 ~AsciiValue();
881 char* operator*() { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000882 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 private:
884 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000885 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000886
887 // Disallow copying and assigning.
888 AsciiValue(const AsciiValue&);
889 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 };
891
892 /**
893 * Converts an object to a two-byte string.
894 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000895 class EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 public:
897 explicit Value(Handle<v8::Value> obj);
898 ~Value();
899 uint16_t* operator*() { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000900 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 private:
902 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000903 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000904
905 // Disallow copying and assigning.
906 Value(const Value&);
907 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 };
909};
910
911
912/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000913 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000915class EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000917 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918 static Local<Number> New(double value);
919 static Number* Cast(v8::Value* obj);
920 private:
921 Number();
922};
923
924
925/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000926 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000928class EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 public:
930 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +0000931 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932 static Integer* Cast(v8::Value* obj);
933 private:
934 Integer();
935};
936
937
938/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000939 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000941class EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000943 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944 private:
945 Int32();
946};
947
948
949/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000950 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000952class EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000954 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 private:
956 Uint32();
957};
958
959
960/**
961 * An instance of the built-in Date constructor (ECMA-262, 15.9).
962 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000963class EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 public:
965 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000966
967 /**
968 * A specialization of Value::NumberValue that is more efficient
969 * because we know the structure of this object.
970 */
ager@chromium.org32912102009-01-16 10:38:43 +0000971 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000972
973 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974};
975
976
977enum PropertyAttribute {
978 None = 0,
979 ReadOnly = 1 << 0,
980 DontEnum = 1 << 1,
981 DontDelete = 1 << 2
982};
983
984/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000985 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000987class EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 public:
989 bool Set(Handle<Value> key,
990 Handle<Value> value,
991 PropertyAttribute attribs = None);
992 Local<Value> Get(Handle<Value> key);
993
994 // TODO(1245389): Replace the type-specific versions of these
995 // functions with generic ones that accept a Handle<Value> key.
996 bool Has(Handle<String> key);
997 bool Delete(Handle<String> key);
998 bool Has(uint32_t index);
999 bool Delete(uint32_t index);
1000
1001 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001002 * Returns an array containing the names of the enumerable properties
1003 * of this object, including properties from prototype objects. The
1004 * array returned by this method contains the same values as would
1005 * be enumerated by a for-in statement over this object.
1006 */
1007 Local<Array> GetPropertyNames();
1008
1009 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 * Get the prototype object. This does not skip objects marked to
1011 * be skipped by __proto__ and it does not consult the security
1012 * handler.
1013 */
1014 Local<Value> GetPrototype();
1015
1016 /**
1017 * Call builtin Object.prototype.toString on this object.
1018 * This is different from Value::ToString() that may call
1019 * user-defined toString function. This one does not.
1020 */
1021 Local<String> ObjectProtoToString();
1022
kasper.lund212ac232008-07-16 07:07:30 +00001023 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001025 /** Gets the value in an internal field. */
1026 Local<Value> GetInternalField(int index);
1027 /** Sets the value in an internal field. */
1028 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029
1030 // Testers for local properties.
1031 bool HasRealNamedProperty(Handle<String> key);
1032 bool HasRealIndexedProperty(uint32_t index);
1033 bool HasRealNamedCallbackProperty(Handle<String> key);
1034
1035 /**
1036 * If result.IsEmpty() no real property was located in the prototype chain.
1037 * This means interceptors in the prototype chain are not called.
1038 */
1039 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1040
1041 /** Tests for a named lookup interceptor.*/
1042 bool HasNamedLookupInterceptor();
1043
kasper.lund212ac232008-07-16 07:07:30 +00001044 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 bool HasIndexedLookupInterceptor();
1046
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001047 /**
1048 * Turns on access check on the object if the object is an instance of
1049 * a template that has access check callbacks. If an object has no
1050 * access check info, the object cannot be accessed by anyone.
1051 */
1052 void TurnOnAccessCheck();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
1054 static Local<Object> New();
1055 static Object* Cast(Value* obj);
1056 private:
1057 Object();
1058};
1059
1060
1061/**
1062 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1063 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001064class EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001066 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067
1068 static Local<Array> New(int length = 0);
1069 static Array* Cast(Value* obj);
1070 private:
1071 Array();
1072};
1073
1074
1075/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001076 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001078class EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001080 Local<Object> NewInstance() const;
1081 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1083 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001084 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 static Function* Cast(Value* obj);
1086 private:
1087 Function();
1088};
1089
1090
1091/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001092 * A JavaScript value that wraps a c++ void*. This type of value is
1093 * mainly used to associate c++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 * objects.
1095 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001096class EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 public:
1098 static Local<External> New(void* value);
1099 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001100 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 private:
1102 External();
1103};
1104
1105
1106// --- T e m p l a t e s ---
1107
1108
1109/**
1110 * The superclass of object and function templates.
1111 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001112class EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 public:
1114 /** Adds a property to each instance created by this template.*/
1115 void Set(Handle<String> name, Handle<Data> value,
1116 PropertyAttribute attributes = None);
1117 inline void Set(const char* name, Handle<Data> value);
1118 private:
1119 Template();
1120
1121 friend class ObjectTemplate;
1122 friend class FunctionTemplate;
1123};
1124
1125
1126/**
1127 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001128 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 * including the receiver, the number and values of arguments, and
1130 * the holder of the function.
1131 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001132class EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 public:
1134 inline int Length() const;
1135 inline Local<Value> operator[](int i) const;
1136 inline Local<Function> Callee() const;
1137 inline Local<Object> This() const;
1138 inline Local<Object> Holder() const;
1139 inline bool IsConstructCall() const;
1140 inline Local<Value> Data() const;
1141 private:
1142 Arguments();
1143 friend class ImplementationUtilities;
1144 inline Arguments(Local<Value> data,
1145 Local<Object> holder,
1146 Local<Function> callee,
1147 bool is_construct_call,
1148 void** values, int length);
1149 Local<Value> data_;
1150 Local<Object> holder_;
1151 Local<Function> callee_;
1152 bool is_construct_call_;
1153 void** values_;
1154 int length_;
1155};
1156
1157
1158/**
1159 * The information passed to an accessor callback about the context
1160 * of the property access.
1161 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001162class EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 public:
1164 inline AccessorInfo(Local<Object> self,
1165 Local<Value> data,
1166 Local<Object> holder)
1167 : self_(self), data_(data), holder_(holder) { }
1168 inline Local<Value> Data() const;
1169 inline Local<Object> This() const;
1170 inline Local<Object> Holder() const;
1171 private:
1172 Local<Object> self_;
1173 Local<Value> data_;
1174 Local<Object> holder_;
1175};
1176
1177
1178typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1179
1180typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1181
1182/**
1183 * Accessor[Getter|Setter] are used as callback functions when
1184 * setting|getting a particular property. See objectTemplate::SetAccessor.
1185 */
1186typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1187 const AccessorInfo& info);
1188
1189
1190typedef void (*AccessorSetter)(Local<String> property,
1191 Local<Value> value,
1192 const AccessorInfo& info);
1193
1194
1195/**
1196 * NamedProperty[Getter|Setter] are used as interceptors on object.
1197 * See ObjectTemplate::SetNamedPropertyHandler.
1198 */
1199typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1200 const AccessorInfo& info);
1201
1202
1203/**
1204 * Returns the value if the setter intercepts the request.
1205 * Otherwise, returns an empty handle.
1206 */
1207typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1208 Local<Value> value,
1209 const AccessorInfo& info);
1210
1211
1212/**
1213 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001214 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215 */
1216typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1217 const AccessorInfo& info);
1218
1219
1220/**
1221 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001222 * The return value is true if the property could be deleted and false
1223 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 */
1225typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1226 const AccessorInfo& info);
1227
1228/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001229 * Returns an array containing the names of the properties the named
1230 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 */
1232typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1233
v8.team.kasperl727e9952008-09-02 14:56:44 +00001234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001236 * Returns the value of the property if the getter intercepts the
1237 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 */
1239typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1240 const AccessorInfo& info);
1241
1242
1243/**
1244 * Returns the value if the setter intercepts the request.
1245 * Otherwise, returns an empty handle.
1246 */
1247typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1248 Local<Value> value,
1249 const AccessorInfo& info);
1250
1251
1252/**
1253 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001254 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 */
1256typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1257 const AccessorInfo& info);
1258
1259/**
1260 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001261 * The return value is true if the property could be deleted and false
1262 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 */
1264typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1265 const AccessorInfo& info);
1266
v8.team.kasperl727e9952008-09-02 14:56:44 +00001267/**
1268 * Returns an array containing the indices of the properties the
1269 * indexed property getter intercepts.
1270 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1272
1273
1274/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001275 * Access control specifications.
1276 *
1277 * Some accessors should be accessible across contexts. These
1278 * accessors have an explicit access control parameter which specifies
1279 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001280 *
1281 * Additionally, for security, accessors can prohibit overwriting by
1282 * accessors defined in JavaScript. For objects that have such
1283 * accessors either locally or in their prototype chain it is not
1284 * possible to overwrite the accessor by using __defineGetter__ or
1285 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286 */
1287enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001288 DEFAULT = 0,
1289 ALL_CAN_READ = 1,
1290 ALL_CAN_WRITE = 1 << 1,
1291 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292};
1293
1294
1295/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001296 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 */
1298enum AccessType {
1299 ACCESS_GET,
1300 ACCESS_SET,
1301 ACCESS_HAS,
1302 ACCESS_DELETE,
1303 ACCESS_KEYS
1304};
1305
v8.team.kasperl727e9952008-09-02 14:56:44 +00001306
1307/**
1308 * Returns true if cross-context access should be allowed to the named
1309 * property with the given key on the global object.
1310 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311typedef bool (*NamedSecurityCallback)(Local<Object> global,
1312 Local<Value> key,
1313 AccessType type,
1314 Local<Value> data);
1315
v8.team.kasperl727e9952008-09-02 14:56:44 +00001316
1317/**
1318 * Returns true if cross-context access should be allowed to the indexed
1319 * property with the given index on the global object.
1320 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1322 uint32_t index,
1323 AccessType type,
1324 Local<Value> data);
1325
1326
1327/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001328 * A FunctionTemplate is used to create functions at runtime. There
1329 * can only be one function created from a FunctionTemplate in a
1330 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 *
1332 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001333 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001335 * A FunctionTemplate has a corresponding instance template which is
1336 * used to create object instances when the function is used as a
1337 * constructor. Properties added to the instance template are added to
1338 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 *
1340 * A FunctionTemplate can have a prototype template. The prototype template
1341 * is used to create the prototype object of the function.
1342 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001343 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001345 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1347 * t->Set("func_property", v8::Number::New(1));
1348 *
1349 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1350 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1351 * proto_t->Set("proto_const", v8::Number::New(2));
1352 *
1353 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1354 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1355 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1356 * instance_t->Set("instance_property", Number::New(3));
1357 *
1358 * v8::Local<v8::Function> function = t->GetFunction();
1359 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001360 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 *
1362 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001363 * and "instance" for the instance object created above. The function
1364 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001366 * \code
1367 * func_property in function == true;
1368 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001370 * function.prototype.proto_method() invokes 'InvokeCallback'
1371 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001373 * instance instanceof function == true;
1374 * instance.instance_accessor calls 'InstanceAccessorCallback'
1375 * instance.instance_property == 3;
1376 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001378 * A FunctionTemplate can inherit from another one by calling the
1379 * FunctionTemplate::Inherit method. The following graph illustrates
1380 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001382 * \code
1383 * FunctionTemplate Parent -> Parent() . prototype -> { }
1384 * ^ ^
1385 * | Inherit(Parent) | .__proto__
1386 * | |
1387 * FunctionTemplate Child -> Child() . prototype -> { }
1388 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1391 * object of the Child() function has __proto__ pointing to the
1392 * Parent() function's prototype object. An instance of the Child
1393 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001395 * Let Parent be the FunctionTemplate initialized in the previous
1396 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001398 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 * Local<FunctionTemplate> parent = t;
1400 * Local<FunctionTemplate> child = FunctionTemplate::New();
1401 * child->Inherit(parent);
1402 *
1403 * Local<Function> child_function = child->GetFunction();
1404 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001405 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001407 * The Child function and Child instance will have the following
1408 * properties:
1409 *
1410 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001412 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001414 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001416class EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 public:
1418 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001419 static Local<FunctionTemplate> New(
1420 InvocationCallback callback = 0,
1421 Handle<Value> data = Handle<Value>(),
1422 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 /** Returns the unique function instance in the current execution context.*/
1424 Local<Function> GetFunction();
1425
v8.team.kasperl727e9952008-09-02 14:56:44 +00001426 /**
1427 * Set the call-handler callback for a FunctionTemplate. This
1428 * callback is called whenever the function created from this
1429 * FunctionTemplate is called.
1430 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 void SetCallHandler(InvocationCallback callback,
1432 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433
v8.team.kasperl727e9952008-09-02 14:56:44 +00001434 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 Local<ObjectTemplate> InstanceTemplate();
1436
1437 /** Causes the function template to inherit from a parent function template.*/
1438 void Inherit(Handle<FunctionTemplate> parent);
1439
1440 /**
1441 * A PrototypeTemplate is the template used to create the prototype object
1442 * of the function created by this template.
1443 */
1444 Local<ObjectTemplate> PrototypeTemplate();
1445
v8.team.kasperl727e9952008-09-02 14:56:44 +00001446
1447 /**
1448 * Set the class name of the FunctionTemplate. This is used for
1449 * printing objects created with the function created from the
1450 * FunctionTemplate as its constructor.
1451 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 void SetClassName(Handle<String> name);
1453
1454 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001455 * Determines whether the __proto__ accessor ignores instances of
1456 * the function template. If instances of the function template are
1457 * ignored, __proto__ skips all instances and instead returns the
1458 * next object in the prototype chain.
1459 *
1460 * Call with a value of true to make the __proto__ accessor ignore
1461 * instances of the function template. Call with a value of false
1462 * to make the __proto__ accessor not ignore instances of the
1463 * function template. By default, instances of a function template
1464 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 */
1466 void SetHiddenPrototype(bool value);
1467
1468 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001469 * Returns true if the given object is an instance of this function
1470 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 */
1472 bool HasInstance(Handle<Value> object);
1473
1474 private:
1475 FunctionTemplate();
1476 void AddInstancePropertyAccessor(Handle<String> name,
1477 AccessorGetter getter,
1478 AccessorSetter setter,
1479 Handle<Value> data,
1480 AccessControl settings,
1481 PropertyAttribute attributes);
1482 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1483 NamedPropertySetter setter,
1484 NamedPropertyQuery query,
1485 NamedPropertyDeleter remover,
1486 NamedPropertyEnumerator enumerator,
1487 Handle<Value> data);
1488 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1489 IndexedPropertySetter setter,
1490 IndexedPropertyQuery query,
1491 IndexedPropertyDeleter remover,
1492 IndexedPropertyEnumerator enumerator,
1493 Handle<Value> data);
1494 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1495 Handle<Value> data);
1496
1497 friend class Context;
1498 friend class ObjectTemplate;
1499};
1500
1501
1502/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001503 * An ObjectTemplate is used to create objects at runtime.
1504 *
1505 * Properties added to an ObjectTemplate are added to each object
1506 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001508class EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001510 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001512
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 /** Creates a new instance of this template.*/
1514 Local<Object> NewInstance();
1515
1516 /**
1517 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001518 *
1519 * Whenever the property with the given name is accessed on objects
1520 * created from this ObjectTemplate the getter and setter callbacks
1521 * are called instead of getting and setting the property directly
1522 * on the JavaScript object.
1523 *
1524 * \param name The name of the property for which an accessor is added.
1525 * \param getter The callback to invoke when getting the property.
1526 * \param setter The callback to invoke when setting the property.
1527 * \param data A piece of data that will be passed to the getter and setter
1528 * callbacks whenever they are invoked.
1529 * \param settings Access control settings for the accessor. This is a bit
1530 * field consisting of one of more of
1531 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1532 * The default is to not allow cross-context access.
1533 * ALL_CAN_READ means that all cross-context reads are allowed.
1534 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1535 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1536 * cross-context access.
1537 * \param attribute The attributes of the property for which an accessor
1538 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539 */
1540 void SetAccessor(Handle<String> name,
1541 AccessorGetter getter,
1542 AccessorSetter setter = 0,
1543 Handle<Value> data = Handle<Value>(),
1544 AccessControl settings = DEFAULT,
1545 PropertyAttribute attribute = None);
1546
1547 /**
1548 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001549 *
1550 * Whenever a named property is accessed on objects created from
1551 * this object template, the provided callback is invoked instead of
1552 * accessing the property directly on the JavaScript object.
1553 *
1554 * \param getter The callback to invoke when getting a property.
1555 * \param setter The callback to invoke when setting a property.
1556 * \param query The callback to invoke to check is an object has a property.
1557 * \param deleter The callback to invoke when deleting a property.
1558 * \param enumerator The callback to invoke to enumerate all the named
1559 * properties of an object.
1560 * \param data A piece of data that will be passed to the callbacks
1561 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 */
1563 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1564 NamedPropertySetter setter = 0,
1565 NamedPropertyQuery query = 0,
1566 NamedPropertyDeleter deleter = 0,
1567 NamedPropertyEnumerator enumerator = 0,
1568 Handle<Value> data = Handle<Value>());
1569
1570 /**
1571 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001572 *
1573 * Whenever an indexed property is accessed on objects created from
1574 * this object template, the provided callback is invoked instead of
1575 * accessing the property directly on the JavaScript object.
1576 *
1577 * \param getter The callback to invoke when getting a property.
1578 * \param setter The callback to invoke when setting a property.
1579 * \param query The callback to invoke to check is an object has a property.
1580 * \param deleter The callback to invoke when deleting a property.
1581 * \param enumerator The callback to invoke to enumerate all the indexed
1582 * properties of an object.
1583 * \param data A piece of data that will be passed to the callbacks
1584 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 */
1586 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1587 IndexedPropertySetter setter = 0,
1588 IndexedPropertyQuery query = 0,
1589 IndexedPropertyDeleter deleter = 0,
1590 IndexedPropertyEnumerator enumerator = 0,
1591 Handle<Value> data = Handle<Value>());
1592 /**
1593 * Sets the callback to be used when calling instances created from
1594 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001595 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 * function.
1597 */
1598 void SetCallAsFunctionHandler(InvocationCallback callback,
1599 Handle<Value> data = Handle<Value>());
1600
v8.team.kasperl727e9952008-09-02 14:56:44 +00001601 /**
1602 * Mark object instances of the template as undetectable.
1603 *
1604 * In many ways, undetectable objects behave as though they are not
1605 * there. They behave like 'undefined' in conditionals and when
1606 * printed. However, properties can be accessed and called as on
1607 * normal objects.
1608 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 void MarkAsUndetectable();
1610
v8.team.kasperl727e9952008-09-02 14:56:44 +00001611 /**
1612 * Sets access check callbacks on the object template.
1613 *
1614 * When accessing properties on instances of this object template,
1615 * the access check callback will be called to determine whether or
1616 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001617 * The last parameter specifies whether access checks are turned
1618 * on by default on instances. If access checks are off by default,
1619 * they can be turned on on individual instances by calling
1620 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001621 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1623 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001624 Handle<Value> data = Handle<Value>(),
1625 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626
kasper.lund212ac232008-07-16 07:07:30 +00001627 /**
1628 * Gets the number of internal fields for objects generated from
1629 * this template.
1630 */
1631 int InternalFieldCount();
1632
1633 /**
1634 * Sets the number of internal fields for objects generated from
1635 * this template.
1636 */
1637 void SetInternalFieldCount(int value);
1638
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 private:
1640 ObjectTemplate();
1641 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1642 friend class FunctionTemplate;
1643};
1644
1645
1646/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001647 * A Signature specifies which receivers and arguments a function can
1648 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001650class EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 public:
1652 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1653 Handle<FunctionTemplate>(),
1654 int argc = 0,
1655 Handle<FunctionTemplate> argv[] = 0);
1656 private:
1657 Signature();
1658};
1659
1660
1661/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001662 * A utility for determining the type of objects based on the template
1663 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001665class EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 public:
1667 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1668 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1669 int match(Handle<Value> value);
1670 private:
1671 TypeSwitch();
1672};
1673
1674
1675// --- E x t e n s i o n s ---
1676
1677
1678/**
1679 * Ignore
1680 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001681class EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 public:
1683 Extension(const char* name,
1684 const char* source = 0,
1685 int dep_count = 0,
1686 const char** deps = 0);
1687 virtual ~Extension() { }
1688 virtual v8::Handle<v8::FunctionTemplate>
1689 GetNativeFunction(v8::Handle<v8::String> name) {
1690 return v8::Handle<v8::FunctionTemplate>();
1691 }
1692
1693 const char* name() { return name_; }
1694 const char* source() { return source_; }
1695 int dependency_count() { return dep_count_; }
1696 const char** dependencies() { return deps_; }
1697 void set_auto_enable(bool value) { auto_enable_ = value; }
1698 bool auto_enable() { return auto_enable_; }
1699
1700 private:
1701 const char* name_;
1702 const char* source_;
1703 int dep_count_;
1704 const char** deps_;
1705 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001706
1707 // Disallow copying and assigning.
1708 Extension(const Extension&);
1709 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710};
1711
1712
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001713void EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714
1715
1716/**
1717 * Ignore
1718 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001719class EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 public:
1721 inline DeclareExtension(Extension* extension) {
1722 RegisterExtension(extension);
1723 }
1724};
1725
1726
1727// --- S t a t i c s ---
1728
1729
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001730Handle<Primitive> EXPORT Undefined();
1731Handle<Primitive> EXPORT Null();
1732Handle<Boolean> EXPORT True();
1733Handle<Boolean> EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734
1735
1736/**
1737 * A set of constraints that specifies the limits of the runtime's
1738 * memory use.
1739 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001740class EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 public:
1742 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001743 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001745 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001747 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1749 private:
1750 int max_young_space_size_;
1751 int max_old_space_size_;
1752 uint32_t* stack_limit_;
1753};
1754
1755
1756bool SetResourceConstraints(ResourceConstraints* constraints);
1757
1758
1759// --- E x c e p t i o n s ---
1760
1761
1762typedef void (*FatalErrorCallback)(const char* location, const char* message);
1763
1764
1765typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1766
1767
1768/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001769 * Schedules an exception to be thrown when returning to JavaScript. When an
1770 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001772 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001774Handle<Value> EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775
1776/**
1777 * Create new error objects by calling the corresponding error object
1778 * constructor with the message.
1779 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001780class EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 public:
1782 static Local<Value> RangeError(Handle<String> message);
1783 static Local<Value> ReferenceError(Handle<String> message);
1784 static Local<Value> SyntaxError(Handle<String> message);
1785 static Local<Value> TypeError(Handle<String> message);
1786 static Local<Value> Error(Handle<String> message);
1787};
1788
1789
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001790// --- 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 +00001791
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001792typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793
1794// --- 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 ---
1795typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1796 AccessType type,
1797 Local<Value> data);
1798
1799// --- 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
1800
1801/**
1802 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001803 * before and after a major garbage collection. Allocations are not
1804 * allowed in the callback function, you therefore cannot manipulate
1805 * objects (set or delete properties for example) since it is possible
1806 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807 */
1808typedef void (*GCCallback)();
1809
1810
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001811// --- E x t e r n a l S y m b o l C a l l b a c k ---
1812
1813/**
1814 * Callback used to allocate certain V8 symbols as external strings.
1815 *
1816 * The data passed to the callback is utf8 encoded.
1817 *
1818 * Allocations are not allowed in the callback function, you therefore
1819 * cannot manipulate objects (set or delete properties for example)
1820 * since it is possible such operations will result in the allocation
1821 * of objects.
1822 */
1823typedef String::ExternalStringResource* (*ExternalSymbolCallback)(
1824 const char* utf8,
1825 size_t length);
1826
1827
1828// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829
1830/**
1831 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001832 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834typedef Persistent<Context> (*ContextGenerator)();
1835
1836
1837/**
1838 * Container class for static utility functions.
1839 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001840class EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001842 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 static void SetFatalErrorHandler(FatalErrorCallback that);
1844
v8.team.kasperl727e9952008-09-02 14:56:44 +00001845 /**
1846 * Ignore out-of-memory exceptions.
1847 *
1848 * V8 running out of memory is treated as a fatal error by default.
1849 * This means that the fatal error handler is called and that V8 is
1850 * terminated.
1851 *
1852 * IgnoreOutOfMemoryException can be used to not treat a
1853 * out-of-memory situation as a fatal error. This way, the contexts
1854 * that did not cause the out of memory problem might be able to
1855 * continue execution.
1856 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 static void IgnoreOutOfMemoryException();
1858
v8.team.kasperl727e9952008-09-02 14:56:44 +00001859 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001860 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001861 * fatal errors such as out-of-memory situations.
1862 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 static bool IsDead();
1864
1865 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001866 * Adds a message listener.
1867 *
1868 * The same message listener can be added more than once and it that
1869 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 */
1871 static bool AddMessageListener(MessageCallback that,
1872 Handle<Value> data = Handle<Value>());
1873
1874 /**
1875 * Remove all message listeners from the specified callback function.
1876 */
1877 static void RemoveMessageListeners(MessageCallback that);
1878
1879 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001880 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 */
1882 static void SetFlagsFromString(const char* str, int length);
1883
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001884 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001885 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001886 */
1887 static void SetFlagsFromCommandLine(int* argc,
1888 char** argv,
1889 bool remove_flags);
1890
kasper.lund7276f142008-07-30 08:49:36 +00001891 /** Get the version string. */
1892 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893
1894 /**
1895 * Enables the host application to provide a mechanism for recording
1896 * statistics counters.
1897 */
1898 static void SetCounterFunction(CounterLookupCallback);
1899
1900 /**
1901 * Enables the computation of a sliding window of states. The sliding
1902 * window information is recorded in statistics counters.
1903 */
1904 static void EnableSlidingStateWindow();
1905
1906 /** Callback function for reporting failed access checks.*/
1907 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
1908
1909 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001910 * Enables the host application to receive a notification before a
1911 * major garbage colletion. Allocations are not allowed in the
1912 * callback function, you therefore cannot manipulate objects (set
1913 * or delete properties for example) since it is possible such
1914 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 */
1916 static void SetGlobalGCPrologueCallback(GCCallback);
1917
1918 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001919 * Enables the host application to receive a notification after a
1920 * major garbage collection. Allocations are not allowed in the
1921 * callback function, you therefore cannot manipulate objects (set
1922 * or delete properties for example) since it is possible such
1923 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 */
1925 static void SetGlobalGCEpilogueCallback(GCCallback);
1926
1927 /**
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001928 * Applications can register a callback that will be used when
1929 * allocating most of the V8 symbols. The callback must return an
1930 * external string resource that represents the symbols.
1931 *
1932 * Most often when performing a property lookup the key will be a
1933 * symbol. Allocating symbols as external strings can reduce the
1934 * amount of string conversions needed when using interceptors and
1935 * accessors.
1936 *
1937 * \note This is an experimental feature and it might be removed.
1938 */
1939 static void SetExternalSymbolCallback(ExternalSymbolCallback);
1940
1941 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001942 * Allows the host application to group objects together. If one
1943 * object in the group is alive, all objects in the group are alive.
1944 * After each garbage collection, object groups are removed. It is
1945 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00001946 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00001947 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00001949 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950
1951 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001952 * Initializes from snapshot if possible. Otherwise, attempts to
1953 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 */
1955 static bool Initialize();
1956
kasper.lund7276f142008-07-30 08:49:36 +00001957 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001958 * Adjusts the amount of registered external memory. Used to give
1959 * V8 an indication of the amount of externally allocated memory
1960 * that is kept alive by JavaScript objects. V8 uses this to decide
1961 * when to perform global garbage collections. Registering
1962 * externally allocated memory will trigger global garbage
1963 * collections more often than otherwise in an attempt to garbage
1964 * collect the JavaScript objects keeping the externally allocated
1965 * memory alive.
1966 *
1967 * \param change_in_bytes the change in externally allocated memory
1968 * that is kept alive by JavaScript objects.
1969 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00001970 */
1971 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
1972
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 private:
1974 V8();
1975
1976 static void** GlobalizeReference(void** handle);
1977 static void DisposeGlobal(void** global_handle);
1978 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
1979 static void ClearWeak(void** global_handle);
1980 static bool IsGlobalNearDeath(void** global_handle);
1981 static bool IsGlobalWeak(void** global_handle);
1982
1983 template <class T> friend class Handle;
1984 template <class T> friend class Local;
1985 template <class T> friend class Persistent;
1986 friend class Context;
1987};
1988
1989
1990/**
1991 * An external exception handler.
1992 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001993class EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 public:
1995
1996 /**
1997 * Creates a new try/catch block and registers it with v8.
1998 */
1999 TryCatch();
2000
2001 /**
2002 * Unregisters and deletes this try/catch block.
2003 */
2004 ~TryCatch();
2005
2006 /**
2007 * Returns true if an exception has been caught by this try/catch block.
2008 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002009 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
2011 /**
2012 * Returns the exception caught by this try/catch block. If no exception has
2013 * been caught an empty handle is returned.
2014 *
2015 * The returned handle is valid until this TryCatch block has been destroyed.
2016 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002017 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018
2019 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002020 * Returns the message associated with this exception. If there is
2021 * no message associated an empty handle is returned.
2022 *
2023 * The returned handle is valid until this TryCatch block has been
2024 * destroyed.
2025 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002026 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002027
2028 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 * Clears any exceptions that may have been caught by this try/catch block.
2030 * After this method has been called, HasCaught() will return false.
2031 *
2032 * It is not necessary to clear a try/catch block before using it again; if
2033 * another exception is thrown the previously caught exception will just be
2034 * overwritten. However, it is often a good idea since it makes it easier
2035 * to determine which operation threw a given exception.
2036 */
2037 void Reset();
2038
v8.team.kasperl727e9952008-09-02 14:56:44 +00002039 /**
2040 * Set verbosity of the external exception handler.
2041 *
2042 * By default, exceptions that are caught by an external exception
2043 * handler are not reported. Call SetVerbose with true on an
2044 * external exception handler to have exceptions caught by the
2045 * handler reported as if they were not caught.
2046 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 void SetVerbose(bool value);
2048
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002049 /**
2050 * Set whether or not this TryCatch should capture a Message object
2051 * which holds source information about where the exception
2052 * occurred. True by default.
2053 */
2054 void SetCaptureMessage(bool value);
2055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 public:
2057 TryCatch* next_;
2058 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002059 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002061 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002062 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063};
2064
2065
2066// --- C o n t e x t ---
2067
2068
2069/**
2070 * Ignore
2071 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002072class EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 public:
2074 ExtensionConfiguration(int name_count, const char* names[])
2075 : name_count_(name_count), names_(names) { }
2076 private:
2077 friend class ImplementationUtilities;
2078 int name_count_;
2079 const char** names_;
2080};
2081
2082
2083/**
2084 * A sandboxed execution context with its own set of built-in objects
2085 * and functions.
2086 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002087class EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002089 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 Local<Object> Global();
2091
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002092 /**
2093 * Detaches the global object from its context before
2094 * the global object can be reused to create a new context.
2095 */
2096 void DetachGlobal();
2097
v8.team.kasperl727e9952008-09-02 14:56:44 +00002098 /** Creates a new context. */
2099 static Persistent<Context> New(
2100 ExtensionConfiguration* extensions = 0,
2101 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2102 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103
kasper.lund44510672008-07-25 07:37:58 +00002104 /** Returns the last entered context. */
2105 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106
kasper.lund44510672008-07-25 07:37:58 +00002107 /** Returns the context that is on the top of the stack. */
2108 static Local<Context> GetCurrent();
2109
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 /**
2111 * Sets the security token for the context. To access an object in
2112 * another context, the security tokens must match.
2113 */
2114 void SetSecurityToken(Handle<Value> token);
2115
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002116 /** Restores the security token to the default value. */
2117 void UseDefaultSecurityToken();
2118
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 /** Returns the security token of this context.*/
2120 Handle<Value> GetSecurityToken();
2121
v8.team.kasperl727e9952008-09-02 14:56:44 +00002122 /**
2123 * Enter this context. After entering a context, all code compiled
2124 * and run is compiled and run in this context. If another context
2125 * is already entered, this old context is saved so it can be
2126 * restored when the new context is exited.
2127 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002129
2130 /**
2131 * Exit this context. Exiting the current context restores the
2132 * context that was in place when entering the current context.
2133 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 void Exit();
2135
v8.team.kasperl727e9952008-09-02 14:56:44 +00002136 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 bool HasOutOfMemoryException();
2138
v8.team.kasperl727e9952008-09-02 14:56:44 +00002139 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 static bool InContext();
2141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 /**
2143 * Stack-allocated class which sets the execution context for all
2144 * operations executed within a local scope.
2145 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002146 class EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 public:
2148 inline Scope(Handle<Context> context) : context_(context) {
2149 context_->Enter();
2150 }
2151 inline ~Scope() { context_->Exit(); }
2152 private:
2153 Handle<Context> context_;
2154 };
2155
2156 private:
2157 friend class Value;
2158 friend class Script;
2159 friend class Object;
2160 friend class Function;
2161};
2162
2163
2164/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002165 * Multiple threads in V8 are allowed, but only one thread at a time
2166 * is allowed to use V8. The definition of 'using V8' includes
2167 * accessing handles or holding onto object pointers obtained from V8
2168 * handles. It is up to the user of V8 to ensure (perhaps with
2169 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 *
2171 * If you wish to start using V8 in a thread you can do this by constructing
2172 * a v8::Locker object. After the code using V8 has completed for the
2173 * current thread you can call the destructor. This can be combined
2174 * with C++ scope-based construction as follows:
2175 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002176 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 * ...
2178 * {
2179 * v8::Locker locker;
2180 * ...
2181 * // Code using V8 goes here.
2182 * ...
2183 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 *
2186 * If you wish to stop using V8 in a thread A you can do this by either
2187 * by destroying the v8::Locker object as above or by constructing a
2188 * v8::Unlocker object:
2189 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 * {
2192 * v8::Unlocker unlocker;
2193 * ...
2194 * // Code not using V8 goes here while V8 can run in another thread.
2195 * ...
2196 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 *
2199 * The Unlocker object is intended for use in a long-running callback
2200 * from V8, where you want to release the V8 lock for other threads to
2201 * use.
2202 *
2203 * The v8::Locker is a recursive lock. That is, you can lock more than
2204 * once in a given thread. This can be useful if you have code that can
2205 * be called either from code that holds the lock or from code that does
2206 * not. The Unlocker is not recursive so you can not have several
2207 * Unlockers on the stack at once, and you can not use an Unlocker in a
2208 * thread that is not inside a Locker's scope.
2209 *
2210 * An unlocker will unlock several lockers if it has to and reinstate
2211 * the correct depth of locking on its destruction. eg.:
2212 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002213 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 * // V8 not locked.
2215 * {
2216 * v8::Locker locker;
2217 * // V8 locked.
2218 * {
2219 * v8::Locker another_locker;
2220 * // V8 still locked (2 levels).
2221 * {
2222 * v8::Unlocker unlocker;
2223 * // V8 not locked.
2224 * }
2225 * // V8 locked again (2 levels).
2226 * }
2227 * // V8 still locked (1 level).
2228 * }
2229 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002230 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002232class EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 public:
2234 Unlocker();
2235 ~Unlocker();
2236};
2237
2238
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002239class EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 public:
2241 Locker();
2242 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243
2244 /**
2245 * Start preemption.
2246 *
2247 * When preemption is started, a timer is fired every n milli seconds
2248 * that will switch between multiple threads that are in contention
2249 * for the V8 lock.
2250 */
2251 static void StartPreemption(int every_n_ms);
2252
2253 /**
2254 * Stop preemption.
2255 */
2256 static void StopPreemption();
2257
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002258 /**
2259 * Returns whether or not the locker is locked by the current thread.
2260 */
2261 static bool IsLocked();
2262
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002263 /**
2264 * Returns whether v8::Locker is being used by this V8 instance.
2265 */
2266 static bool IsActive() { return active_; }
2267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 private:
2269 bool has_lock_;
2270 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002271
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002272 static bool active_;
2273
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002274 // Disallow copying and assigning.
2275 Locker(const Locker&);
2276 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277};
2278
2279
2280
2281// --- I m p l e m e n t a t i o n ---
2282
2283template <class T>
2284Handle<T>::Handle() : val_(0) { }
2285
2286
2287template <class T>
2288Local<T>::Local() : Handle<T>() { }
2289
2290
2291template <class T>
2292Local<T> Local<T>::New(Handle<T> that) {
2293 if (that.IsEmpty()) return Local<T>();
2294 void** p = reinterpret_cast<void**>(*that);
2295 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2296}
2297
2298
2299template <class T>
2300Persistent<T> Persistent<T>::New(Handle<T> that) {
2301 if (that.IsEmpty()) return Persistent<T>();
2302 void** p = reinterpret_cast<void**>(*that);
2303 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2304}
2305
2306
2307template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002308bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309 if (this->IsEmpty()) return false;
2310 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2311}
2312
2313
2314template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002315bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 if (this->IsEmpty()) return false;
2317 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2318}
2319
2320
2321template <class T>
2322void Persistent<T>::Dispose() {
2323 if (this->IsEmpty()) return;
2324 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2325}
2326
2327
2328template <class T>
2329Persistent<T>::Persistent() : Handle<T>() { }
2330
2331template <class T>
2332void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2333 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2334}
2335
2336template <class T>
2337void Persistent<T>::ClearWeak() {
2338 V8::ClearWeak(reinterpret_cast<void**>(**this));
2339}
2340
2341template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002342T* Handle<T>::operator->() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 return val_;
2344}
2345
2346
2347template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002348T* Handle<T>::operator*() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 return val_;
2350}
2351
2352
2353Local<Value> Arguments::operator[](int i) const {
2354 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2355 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2356}
2357
2358
2359Local<Function> Arguments::Callee() const {
2360 return callee_;
2361}
2362
2363
2364Local<Object> Arguments::This() const {
2365 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2366}
2367
2368
2369Local<Object> Arguments::Holder() const {
2370 return holder_;
2371}
2372
2373
2374Local<Value> Arguments::Data() const {
2375 return data_;
2376}
2377
2378
2379bool Arguments::IsConstructCall() const {
2380 return is_construct_call_;
2381}
2382
2383
2384int Arguments::Length() const {
2385 return length_;
2386}
2387
2388
2389Local<Value> AccessorInfo::Data() const {
2390 return data_;
2391}
2392
2393
2394Local<Object> AccessorInfo::This() const {
2395 return self_;
2396}
2397
2398
2399Local<Object> AccessorInfo::Holder() const {
2400 return holder_;
2401}
2402
2403
2404template <class T>
2405Local<T> HandleScope::Close(Handle<T> value) {
2406 void** after = RawClose(reinterpret_cast<void**>(*value));
2407 return Local<T>(reinterpret_cast<T*>(after));
2408}
2409
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002410Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411 return resource_name_;
2412}
2413
2414
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002415Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 return resource_line_offset_;
2417}
2418
2419
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002420Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 return resource_column_offset_;
2422}
2423
2424
2425Handle<Boolean> Boolean::New(bool value) {
2426 return value ? True() : False();
2427}
2428
2429
2430void Template::Set(const char* name, v8::Handle<Data> value) {
2431 Set(v8::String::New(name), value);
2432}
2433
2434
2435/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002436 * \example shell.cc
2437 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 * command-line and executes them.
2439 */
2440
2441
2442/**
2443 * \example process.cc
2444 */
2445
2446
2447} // namespace v8
2448
2449
2450#undef EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002451#undef EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452#undef TYPE_CHECK
2453
2454
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002455#endif // V8_H_