blob: accffc0893d9c9c0e7a15402d09a2d621cd09e5e [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 */
203 bool IsEmpty() { return val_ == 0; }
204
205 T* operator->();
206
207 T* operator*();
208
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 */
220 template <class S> bool operator==(Handle<S> that) {
221 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 */
234 template <class S> bool operator!=(Handle<S> that) {
235 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 */
370 bool IsNearDeath();
371
372 /**
373 * Returns true if the handle's reference is weak.
374 */
375 bool IsWeak();
376
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:
399 HandleScope() : previous_(current_), is_closed_(false) {
400 current_.extensions = 0;
401 }
402
403 ~HandleScope() {
404 // TODO(1245391): In a perfect world, there would be a way of not
v8.team.kasperl727e9952008-09-02 14:56:44 +0000405 // having to check for explicitly closed scopes maybe through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 // subclassing HandleScope?
407 if (!is_closed_) RestorePreviousState();
408 }
409
410 /**
411 * TODO(1245391): Consider introducing a subclass for this.
412 * Closes the handle scope and returns the value as a handle in the
413 * previous scope, which is the new current scope after the call.
414 */
415 template <class T> Local<T> Close(Handle<T> value);
416
417 /**
418 * Counts the number of allocated handles.
419 */
420 static int NumberOfHandles();
421
422 /**
423 * Creates a new handle with the given value.
424 */
425 static void** CreateHandle(void* value);
426
427 private:
428 // Make it impossible to create heap-allocated or illegal handle
429 // scopes by disallowing certain operations.
430 HandleScope(const HandleScope&);
431 void operator=(const HandleScope&);
432 void* operator new(size_t size);
433 void operator delete(void*, size_t);
434
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000435 class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 public:
437 int extensions;
438 void** next;
439 void** limit;
440 inline void Initialize() {
441 extensions = -1;
442 next = limit = NULL;
443 }
444 };
445
446 static Data current_;
447 const Data previous_;
448
449 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000450 * Re-establishes the previous scope state. Should be called only
451 * once, and only for the current scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 */
453 void RestorePreviousState() {
454 if (current_.extensions > 0) DeleteExtensions();
455 current_ = previous_;
456#ifdef DEBUG
457 ZapRange(current_.next, current_.limit);
458#endif
459 }
460
461 // TODO(1245391): Consider creating a subclass for this.
462 bool is_closed_;
463 void** RawClose(void** value);
464
465 /** Deallocates any extensions used by the current scope.*/
466 static void DeleteExtensions();
467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 // Zaps the handles in the half-open interval [start, end).
469 static void ZapRange(void** start, void** end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
471 friend class ImplementationUtilities;
472};
473
474
475// --- S p e c i a l o b j e c t s ---
476
477
478/**
479 * The superclass of values and API object templates.
480 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000481class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482 private:
483 Data();
484};
485
486
487/**
488 * Pre-compilation data that can be associated with a script. This
489 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000490 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000491 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000493class EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494 public:
495 virtual ~ScriptData() { }
496 static ScriptData* PreCompile(const char* input, int length);
497 static ScriptData* New(unsigned* data, int length);
498
499 virtual int Length() = 0;
500 virtual unsigned* Data() = 0;
501};
502
503
504/**
505 * The origin, within a file, of a script.
506 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000507class EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000509 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 Handle<Integer> resource_line_offset = Handle<Integer>(),
511 Handle<Integer> resource_column_offset = Handle<Integer>())
512 : resource_name_(resource_name),
513 resource_line_offset_(resource_line_offset),
514 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000515 inline Handle<Value> ResourceName() const;
516 inline Handle<Integer> ResourceLineOffset() const;
517 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000519 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 Handle<Integer> resource_line_offset_;
521 Handle<Integer> resource_column_offset_;
522};
523
524
525/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000526 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000528class EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529 public:
530
531 /**
532 * Compiles the specified script. The ScriptOrigin* and ScriptData*
533 * parameters are owned by the caller of Script::Compile. No
534 * references to these objects are kept after compilation finishes.
535 */
536 static Local<Script> Compile(Handle<String> source,
537 ScriptOrigin* origin = NULL,
538 ScriptData* pre_data = NULL);
539
mads.s.agercbaa0602008-08-14 13:41:48 +0000540 /**
541 * Compiles the specified script using the specified file name
542 * object (typically a string) as the script's origin.
543 */
544 static Local<Script> Compile(Handle<String> source,
545 Handle<Value> file_name);
546
v8.team.kasperl727e9952008-09-02 14:56:44 +0000547 /**
548 * Runs the script returning the resulting value.
549 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 Local<Value> Run();
551};
552
553
554/**
555 * An error message.
556 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000557class EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 public:
559 Local<String> Get();
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000560 Local<String> GetSourceLine();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000562 Handle<Value> GetScriptResourceName();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000564 /**
565 * Returns the number, 1-based, of the line where the error occurred.
566 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 int GetLineNumber();
568
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000569 /**
570 * Returns the index within the script of the first character where
571 * the error occurred.
572 */
573 int GetStartPosition();
574
575 /**
576 * Returns the index within the script of the last character where
577 * the error occurred.
578 */
579 int GetEndPosition();
580
581 /**
582 * Returns the index within the line of the first character where
583 * the error occurred.
584 */
585 int GetStartColumn();
586
587 /**
588 * Returns the index within the line of the last character where
589 * the error occurred.
590 */
591 int GetEndColumn();
592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 // TODO(1245381): Print to a string instead of on a FILE.
594 static void PrintCurrentStackTrace(FILE* out);
595};
596
597
598// --- V a l u e ---
599
600
601/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000602 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000604class EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605 public:
606
607 /**
608 * Returns true if this value is the undefined value. See ECMA-262
609 * 4.3.10.
610 */
611 bool IsUndefined();
612
613 /**
614 * Returns true if this value is the null value. See ECMA-262
615 * 4.3.11.
616 */
617 bool IsNull();
618
619 /**
620 * Returns true if this value is true.
621 */
622 bool IsTrue();
623
624 /**
625 * Returns true if this value is false.
626 */
627 bool IsFalse();
628
629 /**
630 * Returns true if this value is an instance of the String type.
631 * See ECMA-262 8.4.
632 */
633 bool IsString();
634
635 /**
636 * Returns true if this value is a function.
637 */
638 bool IsFunction();
639
640 /**
641 * Returns true if this value is an array.
642 */
643 bool IsArray();
644
v8.team.kasperl727e9952008-09-02 14:56:44 +0000645 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 * Returns true if this value is an object.
647 */
648 bool IsObject();
649
v8.team.kasperl727e9952008-09-02 14:56:44 +0000650 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 * Returns true if this value is boolean.
652 */
653 bool IsBoolean();
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000654
v8.team.kasperl727e9952008-09-02 14:56:44 +0000655 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 * Returns true if this value is a number.
657 */
658 bool IsNumber();
659
v8.team.kasperl727e9952008-09-02 14:56:44 +0000660 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 * Returns true if this value is external.
662 */
663 bool IsExternal();
664
v8.team.kasperl727e9952008-09-02 14:56:44 +0000665 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 * Returns true if this value is a 32-bit signed integer.
667 */
668 bool IsInt32();
669
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000670 /**
671 * Returns true if this value is a Date.
672 */
673 bool IsDate();
674
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 Local<Boolean> ToBoolean();
676 Local<Number> ToNumber();
677 Local<String> ToString();
678 Local<String> ToDetailString();
679 Local<Object> ToObject();
680 Local<Integer> ToInteger();
681 Local<Uint32> ToUint32();
682 Local<Int32> ToInt32();
683
684 /**
685 * Attempts to convert a string to an array index.
686 * Returns an empty handle if the conversion fails.
687 */
688 Local<Uint32> ToArrayIndex();
689
690 bool BooleanValue();
691 double NumberValue();
692 int64_t IntegerValue();
693 uint32_t Uint32Value();
694 int32_t Int32Value();
695
696 /** JS == */
697 bool Equals(Handle<Value> that);
698 bool StrictEquals(Handle<Value> that);
699};
700
701
702/**
703 * The superclass of primitive values. See ECMA-262 4.3.2.
704 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000705class EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
707
708/**
709 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
710 * or false value.
711 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000712class EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 public:
714 bool Value();
715 static inline Handle<Boolean> New(bool value);
716};
717
718
719/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000720 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000722class EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000724
725 /**
726 * Returns the number of characters in this string.
727 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728 int Length();
729
v8.team.kasperl727e9952008-09-02 14:56:44 +0000730 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000731 * Returns the number of bytes in the UTF-8 encoded
732 * representation of this string.
733 */
734 int Utf8Length();
735
736 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000737 * Write the contents of the string to an external buffer.
738 * If no arguments are given, expects the buffer to be large
739 * enough to hold the entire string and NULL terminator. Copies
740 * the contents of the string and the NULL terminator into the
741 * buffer.
742 *
743 * Copies up to length characters into the output buffer.
744 * Only null-terminates if there is enough space in the buffer.
745 *
746 * \param buffer The buffer into which the string will be copied.
747 * \param start The starting position within the string at which
748 * copying begins.
749 * \param length The number of bytes to copy from the string.
750 * \return The number of characters copied to the buffer
751 * excluding the NULL terminator.
752 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 int Write(uint16_t* buffer, int start = 0, int length = -1); // UTF-16
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000754 int WriteAscii(char* buffer, int start = 0, int length = -1); // ASCII
755 int WriteUtf8(char* buffer, int length = -1); // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
v8.team.kasperl727e9952008-09-02 14:56:44 +0000757 /**
758 * Returns true if the string is external
759 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 bool IsExternal();
761
v8.team.kasperl727e9952008-09-02 14:56:44 +0000762 /**
763 * Returns true if the string is both external and ascii
764 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 bool IsExternalAscii();
v8.team.kasperl727e9952008-09-02 14:56:44 +0000766 /**
767 * An ExternalStringResource is a wrapper around a two-byte string
768 * buffer that resides outside V8's heap. Implement an
769 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000770 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000771 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000772 class EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773 public:
774 /**
775 * Override the destructor to manage the life cycle of the underlying
776 * buffer.
777 */
778 virtual ~ExternalStringResource() {}
779 /** The string data from the underlying buffer.*/
780 virtual const uint16_t* data() const = 0;
781 /** The length of the string. That is, the number of two-byte characters.*/
782 virtual size_t length() const = 0;
783 protected:
784 ExternalStringResource() {}
785 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000786 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 ExternalStringResource(const ExternalStringResource&);
788 void operator=(const ExternalStringResource&);
789 };
790
791 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000792 * An ExternalAsciiStringResource is a wrapper around an ascii
793 * string buffer that resides outside V8's heap. Implement an
794 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000795 * underlying buffer. Note that the string data must be immutable
796 * and that the data must be strict 7-bit ASCII, not Latin1 or
797 * UTF-8, which would require special treatment internally in the
798 * engine and, in the case of UTF-8, do not allow efficient indexing.
799 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000800 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000802 class EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 public:
804 /**
805 * Override the destructor to manage the life cycle of the underlying
806 * buffer.
807 */
808 virtual ~ExternalAsciiStringResource() {}
809 /** The string data from the underlying buffer.*/
810 virtual const char* data() const = 0;
811 /** The number of ascii characters in the string.*/
812 virtual size_t length() const = 0;
813 protected:
814 ExternalAsciiStringResource() {}
815 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000816 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
818 void operator=(const ExternalAsciiStringResource&);
819 };
820
821 /**
822 * Get the ExternalStringResource for an external string. Only
823 * valid if IsExternal() returns true.
824 */
825 ExternalStringResource* GetExternalStringResource();
826
827 /**
828 * Get the ExternalAsciiStringResource for an external ascii string.
829 * Only valid if IsExternalAscii() returns true.
830 */
831 ExternalAsciiStringResource* GetExternalAsciiStringResource();
832
833 static String* Cast(v8::Value* obj);
834
835 /**
836 * Allocates a new string from either utf-8 encoded or ascii data.
837 * The second parameter 'length' gives the buffer length.
838 * If the data is utf-8 encoded, the caller must
839 * be careful to supply the length parameter.
840 * If it is not given, the function calls
841 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000842 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 */
844 static Local<String> New(const char* data, int length = -1);
845
846 /** Allocates a new string from utf16 data.*/
847 static Local<String> New(const uint16_t* data, int length = -1);
848
849 /** Creates a symbol. Returns one if it exists already.*/
850 static Local<String> NewSymbol(const char* data, int length = -1);
851
v8.team.kasperl727e9952008-09-02 14:56:44 +0000852 /**
853 * Creates a new external string using the data defined in the given
854 * resource. The resource is deleted when the external string is no
855 * longer live on V8's heap. The caller of this function should not
856 * delete or modify the resource. Neither should the underlying buffer be
857 * deallocated or modified except through the destructor of the
858 * external string resource.
859 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 static Local<String> NewExternal(ExternalStringResource* resource);
861
v8.team.kasperl727e9952008-09-02 14:56:44 +0000862 /**
863 * Creates a new external string using the ascii data defined in the given
864 * resource. The resource is deleted when the external string is no
865 * longer live on V8's heap. The caller of this function should not
866 * delete or modify the resource. Neither should the underlying buffer be
867 * deallocated or modified except through the destructor of the
868 * external string resource.
869 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
871
kasper.lund7276f142008-07-30 08:49:36 +0000872 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 static Local<String> NewUndetectable(const char* data, int length = -1);
874
kasper.lund7276f142008-07-30 08:49:36 +0000875 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
877
878 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000879 * Converts an object to a utf8-encoded character array. Useful if
880 * you want to print the object.
881 */
882 class EXPORT Utf8Value {
883 public:
884 explicit Utf8Value(Handle<v8::Value> obj);
885 ~Utf8Value();
886 char* operator*() { return str_; }
887 int length() { return length_; }
888 private:
889 char* str_;
890 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000891
892 // Disallow copying and assigning.
893 Utf8Value(const Utf8Value&);
894 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000895 };
896
897 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 * Converts an object to an ascii string.
899 * Useful if you want to print the object.
900 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000901 class EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 public:
903 explicit AsciiValue(Handle<v8::Value> obj);
904 ~AsciiValue();
905 char* operator*() { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000906 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 private:
908 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000909 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000910
911 // Disallow copying and assigning.
912 AsciiValue(const AsciiValue&);
913 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 };
915
916 /**
917 * Converts an object to a two-byte string.
918 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000919 class EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 public:
921 explicit Value(Handle<v8::Value> obj);
922 ~Value();
923 uint16_t* operator*() { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000924 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 private:
926 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000927 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000928
929 // Disallow copying and assigning.
930 Value(const Value&);
931 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932 };
933};
934
935
936/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000937 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000939class EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 public:
941 double Value();
942 static Local<Number> New(double value);
943 static Number* Cast(v8::Value* obj);
944 private:
945 Number();
946};
947
948
949/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000950 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000952class EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 public:
954 static Local<Integer> New(int32_t value);
955 int64_t Value();
956 static Integer* Cast(v8::Value* obj);
957 private:
958 Integer();
959};
960
961
962/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000963 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000965class EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 public:
967 int32_t Value();
968 private:
969 Int32();
970};
971
972
973/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000974 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000976class EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 public:
978 uint32_t Value();
979 private:
980 Uint32();
981};
982
983
984/**
985 * An instance of the built-in Date constructor (ECMA-262, 15.9).
986 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000987class EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 public:
989 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000990
991 /**
992 * A specialization of Value::NumberValue that is more efficient
993 * because we know the structure of this object.
994 */
995 double NumberValue();
996
997 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998};
999
1000
1001enum PropertyAttribute {
1002 None = 0,
1003 ReadOnly = 1 << 0,
1004 DontEnum = 1 << 1,
1005 DontDelete = 1 << 2
1006};
1007
1008/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001009 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001011class EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 public:
1013 bool Set(Handle<Value> key,
1014 Handle<Value> value,
1015 PropertyAttribute attribs = None);
1016 Local<Value> Get(Handle<Value> key);
1017
1018 // TODO(1245389): Replace the type-specific versions of these
1019 // functions with generic ones that accept a Handle<Value> key.
1020 bool Has(Handle<String> key);
1021 bool Delete(Handle<String> key);
1022 bool Has(uint32_t index);
1023 bool Delete(uint32_t index);
1024
1025 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001026 * Returns an array containing the names of the enumerable properties
1027 * of this object, including properties from prototype objects. The
1028 * array returned by this method contains the same values as would
1029 * be enumerated by a for-in statement over this object.
1030 */
1031 Local<Array> GetPropertyNames();
1032
1033 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 * Get the prototype object. This does not skip objects marked to
1035 * be skipped by __proto__ and it does not consult the security
1036 * handler.
1037 */
1038 Local<Value> GetPrototype();
1039
1040 /**
1041 * Call builtin Object.prototype.toString on this object.
1042 * This is different from Value::ToString() that may call
1043 * user-defined toString function. This one does not.
1044 */
1045 Local<String> ObjectProtoToString();
1046
kasper.lund212ac232008-07-16 07:07:30 +00001047 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001049 /** Gets the value in an internal field. */
1050 Local<Value> GetInternalField(int index);
1051 /** Sets the value in an internal field. */
1052 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053
1054 // Testers for local properties.
1055 bool HasRealNamedProperty(Handle<String> key);
1056 bool HasRealIndexedProperty(uint32_t index);
1057 bool HasRealNamedCallbackProperty(Handle<String> key);
1058
1059 /**
1060 * If result.IsEmpty() no real property was located in the prototype chain.
1061 * This means interceptors in the prototype chain are not called.
1062 */
1063 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1064
1065 /** Tests for a named lookup interceptor.*/
1066 bool HasNamedLookupInterceptor();
1067
kasper.lund212ac232008-07-16 07:07:30 +00001068 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069 bool HasIndexedLookupInterceptor();
1070
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001071 /**
1072 * Turns on access check on the object if the object is an instance of
1073 * a template that has access check callbacks. If an object has no
1074 * access check info, the object cannot be accessed by anyone.
1075 */
1076 void TurnOnAccessCheck();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077
1078 static Local<Object> New();
1079 static Object* Cast(Value* obj);
1080 private:
1081 Object();
1082};
1083
1084
1085/**
1086 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1087 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001088class EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
1090 uint32_t Length();
1091
1092 static Local<Array> New(int length = 0);
1093 static Array* Cast(Value* obj);
1094 private:
1095 Array();
1096};
1097
1098
1099/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001100 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001102class EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 public:
1104 Local<Object> NewInstance();
1105 Local<Object> NewInstance(int argc, Handle<Value> argv[]);
1106 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1107 void SetName(Handle<String> name);
1108 Handle<Value> GetName();
1109 static Function* Cast(Value* obj);
1110 private:
1111 Function();
1112};
1113
1114
1115/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001116 * A JavaScript value that wraps a c++ void*. This type of value is
1117 * mainly used to associate c++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 * objects.
1119 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001120class EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 public:
1122 static Local<External> New(void* value);
1123 static External* Cast(Value* obj);
1124 void* Value();
1125 private:
1126 External();
1127};
1128
1129
1130// --- T e m p l a t e s ---
1131
1132
1133/**
1134 * The superclass of object and function templates.
1135 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001136class EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001137 public:
1138 /** Adds a property to each instance created by this template.*/
1139 void Set(Handle<String> name, Handle<Data> value,
1140 PropertyAttribute attributes = None);
1141 inline void Set(const char* name, Handle<Data> value);
1142 private:
1143 Template();
1144
1145 friend class ObjectTemplate;
1146 friend class FunctionTemplate;
1147};
1148
1149
1150/**
1151 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001152 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 * including the receiver, the number and values of arguments, and
1154 * the holder of the function.
1155 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001156class EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 public:
1158 inline int Length() const;
1159 inline Local<Value> operator[](int i) const;
1160 inline Local<Function> Callee() const;
1161 inline Local<Object> This() const;
1162 inline Local<Object> Holder() const;
1163 inline bool IsConstructCall() const;
1164 inline Local<Value> Data() const;
1165 private:
1166 Arguments();
1167 friend class ImplementationUtilities;
1168 inline Arguments(Local<Value> data,
1169 Local<Object> holder,
1170 Local<Function> callee,
1171 bool is_construct_call,
1172 void** values, int length);
1173 Local<Value> data_;
1174 Local<Object> holder_;
1175 Local<Function> callee_;
1176 bool is_construct_call_;
1177 void** values_;
1178 int length_;
1179};
1180
1181
1182/**
1183 * The information passed to an accessor callback about the context
1184 * of the property access.
1185 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001186class EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 public:
1188 inline AccessorInfo(Local<Object> self,
1189 Local<Value> data,
1190 Local<Object> holder)
1191 : self_(self), data_(data), holder_(holder) { }
1192 inline Local<Value> Data() const;
1193 inline Local<Object> This() const;
1194 inline Local<Object> Holder() const;
1195 private:
1196 Local<Object> self_;
1197 Local<Value> data_;
1198 Local<Object> holder_;
1199};
1200
1201
1202typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1203
1204typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1205
1206/**
1207 * Accessor[Getter|Setter] are used as callback functions when
1208 * setting|getting a particular property. See objectTemplate::SetAccessor.
1209 */
1210typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1211 const AccessorInfo& info);
1212
1213
1214typedef void (*AccessorSetter)(Local<String> property,
1215 Local<Value> value,
1216 const AccessorInfo& info);
1217
1218
1219/**
1220 * NamedProperty[Getter|Setter] are used as interceptors on object.
1221 * See ObjectTemplate::SetNamedPropertyHandler.
1222 */
1223typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1224 const AccessorInfo& info);
1225
1226
1227/**
1228 * Returns the value if the setter intercepts the request.
1229 * Otherwise, returns an empty handle.
1230 */
1231typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1232 Local<Value> value,
1233 const AccessorInfo& info);
1234
1235
1236/**
1237 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001238 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 */
1240typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1241 const AccessorInfo& info);
1242
1243
1244/**
1245 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001246 * The return value is true if the property could be deleted and false
1247 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 */
1249typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1250 const AccessorInfo& info);
1251
1252/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001253 * Returns an array containing the names of the properties the named
1254 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 */
1256typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1257
v8.team.kasperl727e9952008-09-02 14:56:44 +00001258
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001260 * Returns the value of the property if the getter intercepts the
1261 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 */
1263typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1264 const AccessorInfo& info);
1265
1266
1267/**
1268 * Returns the value if the setter intercepts the request.
1269 * Otherwise, returns an empty handle.
1270 */
1271typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1272 Local<Value> value,
1273 const AccessorInfo& info);
1274
1275
1276/**
1277 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001278 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 */
1280typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1281 const AccessorInfo& info);
1282
1283/**
1284 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001285 * The return value is true if the property could be deleted and false
1286 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 */
1288typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1289 const AccessorInfo& info);
1290
v8.team.kasperl727e9952008-09-02 14:56:44 +00001291/**
1292 * Returns an array containing the indices of the properties the
1293 * indexed property getter intercepts.
1294 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1296
1297
1298/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001299 * Access control specifications.
1300 *
1301 * Some accessors should be accessible across contexts. These
1302 * accessors have an explicit access control parameter which specifies
1303 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001304 *
1305 * Additionally, for security, accessors can prohibit overwriting by
1306 * accessors defined in JavaScript. For objects that have such
1307 * accessors either locally or in their prototype chain it is not
1308 * possible to overwrite the accessor by using __defineGetter__ or
1309 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 */
1311enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001312 DEFAULT = 0,
1313 ALL_CAN_READ = 1,
1314 ALL_CAN_WRITE = 1 << 1,
1315 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316};
1317
1318
1319/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001320 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 */
1322enum AccessType {
1323 ACCESS_GET,
1324 ACCESS_SET,
1325 ACCESS_HAS,
1326 ACCESS_DELETE,
1327 ACCESS_KEYS
1328};
1329
v8.team.kasperl727e9952008-09-02 14:56:44 +00001330
1331/**
1332 * Returns true if cross-context access should be allowed to the named
1333 * property with the given key on the global object.
1334 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335typedef bool (*NamedSecurityCallback)(Local<Object> global,
1336 Local<Value> key,
1337 AccessType type,
1338 Local<Value> data);
1339
v8.team.kasperl727e9952008-09-02 14:56:44 +00001340
1341/**
1342 * Returns true if cross-context access should be allowed to the indexed
1343 * property with the given index on the global object.
1344 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1346 uint32_t index,
1347 AccessType type,
1348 Local<Value> data);
1349
1350
1351/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001352 * A FunctionTemplate is used to create functions at runtime. There
1353 * can only be one function created from a FunctionTemplate in a
1354 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 *
1356 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001357 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001359 * A FunctionTemplate has a corresponding instance template which is
1360 * used to create object instances when the function is used as a
1361 * constructor. Properties added to the instance template are added to
1362 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 *
1364 * A FunctionTemplate can have a prototype template. The prototype template
1365 * is used to create the prototype object of the function.
1366 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001367 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001369 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1371 * t->Set("func_property", v8::Number::New(1));
1372 *
1373 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1374 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1375 * proto_t->Set("proto_const", v8::Number::New(2));
1376 *
1377 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1378 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1379 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1380 * instance_t->Set("instance_property", Number::New(3));
1381 *
1382 * v8::Local<v8::Function> function = t->GetFunction();
1383 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001384 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 *
1386 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001387 * and "instance" for the instance object created above. The function
1388 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * \code
1391 * func_property in function == true;
1392 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001394 * function.prototype.proto_method() invokes 'InvokeCallback'
1395 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001397 * instance instanceof function == true;
1398 * instance.instance_accessor calls 'InstanceAccessorCallback'
1399 * instance.instance_property == 3;
1400 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001402 * A FunctionTemplate can inherit from another one by calling the
1403 * FunctionTemplate::Inherit method. The following graph illustrates
1404 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001406 * \code
1407 * FunctionTemplate Parent -> Parent() . prototype -> { }
1408 * ^ ^
1409 * | Inherit(Parent) | .__proto__
1410 * | |
1411 * FunctionTemplate Child -> Child() . prototype -> { }
1412 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001414 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1415 * object of the Child() function has __proto__ pointing to the
1416 * Parent() function's prototype object. An instance of the Child
1417 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001419 * Let Parent be the FunctionTemplate initialized in the previous
1420 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001422 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 * Local<FunctionTemplate> parent = t;
1424 * Local<FunctionTemplate> child = FunctionTemplate::New();
1425 * child->Inherit(parent);
1426 *
1427 * Local<Function> child_function = child->GetFunction();
1428 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001429 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001431 * The Child function and Child instance will have the following
1432 * properties:
1433 *
1434 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001436 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001438 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001440class EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 public:
1442 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001443 static Local<FunctionTemplate> New(
1444 InvocationCallback callback = 0,
1445 Handle<Value> data = Handle<Value>(),
1446 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447 /** Returns the unique function instance in the current execution context.*/
1448 Local<Function> GetFunction();
1449
v8.team.kasperl727e9952008-09-02 14:56:44 +00001450 /**
1451 * Set the call-handler callback for a FunctionTemplate. This
1452 * callback is called whenever the function created from this
1453 * FunctionTemplate is called.
1454 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 void SetCallHandler(InvocationCallback callback,
1456 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457
v8.team.kasperl727e9952008-09-02 14:56:44 +00001458 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 Local<ObjectTemplate> InstanceTemplate();
1460
1461 /** Causes the function template to inherit from a parent function template.*/
1462 void Inherit(Handle<FunctionTemplate> parent);
1463
1464 /**
1465 * A PrototypeTemplate is the template used to create the prototype object
1466 * of the function created by this template.
1467 */
1468 Local<ObjectTemplate> PrototypeTemplate();
1469
v8.team.kasperl727e9952008-09-02 14:56:44 +00001470
1471 /**
1472 * Set the class name of the FunctionTemplate. This is used for
1473 * printing objects created with the function created from the
1474 * FunctionTemplate as its constructor.
1475 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 void SetClassName(Handle<String> name);
1477
1478 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001479 * Determines whether the __proto__ accessor ignores instances of
1480 * the function template. If instances of the function template are
1481 * ignored, __proto__ skips all instances and instead returns the
1482 * next object in the prototype chain.
1483 *
1484 * Call with a value of true to make the __proto__ accessor ignore
1485 * instances of the function template. Call with a value of false
1486 * to make the __proto__ accessor not ignore instances of the
1487 * function template. By default, instances of a function template
1488 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489 */
1490 void SetHiddenPrototype(bool value);
1491
1492 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001493 * Returns true if the given object is an instance of this function
1494 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 */
1496 bool HasInstance(Handle<Value> object);
1497
1498 private:
1499 FunctionTemplate();
1500 void AddInstancePropertyAccessor(Handle<String> name,
1501 AccessorGetter getter,
1502 AccessorSetter setter,
1503 Handle<Value> data,
1504 AccessControl settings,
1505 PropertyAttribute attributes);
1506 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1507 NamedPropertySetter setter,
1508 NamedPropertyQuery query,
1509 NamedPropertyDeleter remover,
1510 NamedPropertyEnumerator enumerator,
1511 Handle<Value> data);
1512 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1513 IndexedPropertySetter setter,
1514 IndexedPropertyQuery query,
1515 IndexedPropertyDeleter remover,
1516 IndexedPropertyEnumerator enumerator,
1517 Handle<Value> data);
1518 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1519 Handle<Value> data);
1520
1521 friend class Context;
1522 friend class ObjectTemplate;
1523};
1524
1525
1526/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001527 * An ObjectTemplate is used to create objects at runtime.
1528 *
1529 * Properties added to an ObjectTemplate are added to each object
1530 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001532class EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001534 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001536
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 /** Creates a new instance of this template.*/
1538 Local<Object> NewInstance();
1539
1540 /**
1541 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001542 *
1543 * Whenever the property with the given name is accessed on objects
1544 * created from this ObjectTemplate the getter and setter callbacks
1545 * are called instead of getting and setting the property directly
1546 * on the JavaScript object.
1547 *
1548 * \param name The name of the property for which an accessor is added.
1549 * \param getter The callback to invoke when getting the property.
1550 * \param setter The callback to invoke when setting the property.
1551 * \param data A piece of data that will be passed to the getter and setter
1552 * callbacks whenever they are invoked.
1553 * \param settings Access control settings for the accessor. This is a bit
1554 * field consisting of one of more of
1555 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1556 * The default is to not allow cross-context access.
1557 * ALL_CAN_READ means that all cross-context reads are allowed.
1558 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1559 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1560 * cross-context access.
1561 * \param attribute The attributes of the property for which an accessor
1562 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 */
1564 void SetAccessor(Handle<String> name,
1565 AccessorGetter getter,
1566 AccessorSetter setter = 0,
1567 Handle<Value> data = Handle<Value>(),
1568 AccessControl settings = DEFAULT,
1569 PropertyAttribute attribute = None);
1570
1571 /**
1572 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001573 *
1574 * Whenever a named property is accessed on objects created from
1575 * this object template, the provided callback is invoked instead of
1576 * accessing the property directly on the JavaScript object.
1577 *
1578 * \param getter The callback to invoke when getting a property.
1579 * \param setter The callback to invoke when setting a property.
1580 * \param query The callback to invoke to check is an object has a property.
1581 * \param deleter The callback to invoke when deleting a property.
1582 * \param enumerator The callback to invoke to enumerate all the named
1583 * properties of an object.
1584 * \param data A piece of data that will be passed to the callbacks
1585 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 */
1587 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1588 NamedPropertySetter setter = 0,
1589 NamedPropertyQuery query = 0,
1590 NamedPropertyDeleter deleter = 0,
1591 NamedPropertyEnumerator enumerator = 0,
1592 Handle<Value> data = Handle<Value>());
1593
1594 /**
1595 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001596 *
1597 * Whenever an indexed property is accessed on objects created from
1598 * this object template, the provided callback is invoked instead of
1599 * accessing the property directly on the JavaScript object.
1600 *
1601 * \param getter The callback to invoke when getting a property.
1602 * \param setter The callback to invoke when setting a property.
1603 * \param query The callback to invoke to check is an object has a property.
1604 * \param deleter The callback to invoke when deleting a property.
1605 * \param enumerator The callback to invoke to enumerate all the indexed
1606 * properties of an object.
1607 * \param data A piece of data that will be passed to the callbacks
1608 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 */
1610 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1611 IndexedPropertySetter setter = 0,
1612 IndexedPropertyQuery query = 0,
1613 IndexedPropertyDeleter deleter = 0,
1614 IndexedPropertyEnumerator enumerator = 0,
1615 Handle<Value> data = Handle<Value>());
1616 /**
1617 * Sets the callback to be used when calling instances created from
1618 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001619 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 * function.
1621 */
1622 void SetCallAsFunctionHandler(InvocationCallback callback,
1623 Handle<Value> data = Handle<Value>());
1624
v8.team.kasperl727e9952008-09-02 14:56:44 +00001625 /**
1626 * Mark object instances of the template as undetectable.
1627 *
1628 * In many ways, undetectable objects behave as though they are not
1629 * there. They behave like 'undefined' in conditionals and when
1630 * printed. However, properties can be accessed and called as on
1631 * normal objects.
1632 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 void MarkAsUndetectable();
1634
v8.team.kasperl727e9952008-09-02 14:56:44 +00001635 /**
1636 * Sets access check callbacks on the object template.
1637 *
1638 * When accessing properties on instances of this object template,
1639 * the access check callback will be called to determine whether or
1640 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001641 * The last parameter specifies whether access checks are turned
1642 * on by default on instances. If access checks are off by default,
1643 * they can be turned on on individual instances by calling
1644 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001645 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1647 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001648 Handle<Value> data = Handle<Value>(),
1649 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650
kasper.lund212ac232008-07-16 07:07:30 +00001651 /**
1652 * Gets the number of internal fields for objects generated from
1653 * this template.
1654 */
1655 int InternalFieldCount();
1656
1657 /**
1658 * Sets the number of internal fields for objects generated from
1659 * this template.
1660 */
1661 void SetInternalFieldCount(int value);
1662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 private:
1664 ObjectTemplate();
1665 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1666 friend class FunctionTemplate;
1667};
1668
1669
1670/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001671 * A Signature specifies which receivers and arguments a function can
1672 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001674class EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 public:
1676 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1677 Handle<FunctionTemplate>(),
1678 int argc = 0,
1679 Handle<FunctionTemplate> argv[] = 0);
1680 private:
1681 Signature();
1682};
1683
1684
1685/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001686 * A utility for determining the type of objects based on the template
1687 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001689class EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 public:
1691 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1692 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1693 int match(Handle<Value> value);
1694 private:
1695 TypeSwitch();
1696};
1697
1698
1699// --- E x t e n s i o n s ---
1700
1701
1702/**
1703 * Ignore
1704 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001705class EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 public:
1707 Extension(const char* name,
1708 const char* source = 0,
1709 int dep_count = 0,
1710 const char** deps = 0);
1711 virtual ~Extension() { }
1712 virtual v8::Handle<v8::FunctionTemplate>
1713 GetNativeFunction(v8::Handle<v8::String> name) {
1714 return v8::Handle<v8::FunctionTemplate>();
1715 }
1716
1717 const char* name() { return name_; }
1718 const char* source() { return source_; }
1719 int dependency_count() { return dep_count_; }
1720 const char** dependencies() { return deps_; }
1721 void set_auto_enable(bool value) { auto_enable_ = value; }
1722 bool auto_enable() { return auto_enable_; }
1723
1724 private:
1725 const char* name_;
1726 const char* source_;
1727 int dep_count_;
1728 const char** deps_;
1729 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001730
1731 // Disallow copying and assigning.
1732 Extension(const Extension&);
1733 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734};
1735
1736
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001737void EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738
1739
1740/**
1741 * Ignore
1742 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001743class EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 public:
1745 inline DeclareExtension(Extension* extension) {
1746 RegisterExtension(extension);
1747 }
1748};
1749
1750
1751// --- S t a t i c s ---
1752
1753
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001754Handle<Primitive> EXPORT Undefined();
1755Handle<Primitive> EXPORT Null();
1756Handle<Boolean> EXPORT True();
1757Handle<Boolean> EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758
1759
1760/**
1761 * A set of constraints that specifies the limits of the runtime's
1762 * memory use.
1763 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001764class EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 public:
1766 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001767 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001769 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001771 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1773 private:
1774 int max_young_space_size_;
1775 int max_old_space_size_;
1776 uint32_t* stack_limit_;
1777};
1778
1779
1780bool SetResourceConstraints(ResourceConstraints* constraints);
1781
1782
1783// --- E x c e p t i o n s ---
1784
1785
1786typedef void (*FatalErrorCallback)(const char* location, const char* message);
1787
1788
1789typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1790
1791
1792/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001793 * Schedules an exception to be thrown when returning to JavaScript. When an
1794 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001796 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001798Handle<Value> EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799
1800/**
1801 * Create new error objects by calling the corresponding error object
1802 * constructor with the message.
1803 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001804class EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 public:
1806 static Local<Value> RangeError(Handle<String> message);
1807 static Local<Value> ReferenceError(Handle<String> message);
1808 static Local<Value> SyntaxError(Handle<String> message);
1809 static Local<Value> TypeError(Handle<String> message);
1810 static Local<Value> Error(Handle<String> message);
1811};
1812
1813
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001814// --- 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 +00001815
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001816typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817
1818// --- 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 ---
1819typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1820 AccessType type,
1821 Local<Value> data);
1822
1823// --- 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
1824
1825/**
1826 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001827 * before and after a major garbage collection. Allocations are not
1828 * allowed in the callback function, you therefore cannot manipulate
1829 * objects (set or delete properties for example) since it is possible
1830 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831 */
1832typedef void (*GCCallback)();
1833
1834
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001835// --- E x t e r n a l S y m b o l C a l l b a c k ---
1836
1837/**
1838 * Callback used to allocate certain V8 symbols as external strings.
1839 *
1840 * The data passed to the callback is utf8 encoded.
1841 *
1842 * Allocations are not allowed in the callback function, you therefore
1843 * cannot manipulate objects (set or delete properties for example)
1844 * since it is possible such operations will result in the allocation
1845 * of objects.
1846 */
1847typedef String::ExternalStringResource* (*ExternalSymbolCallback)(
1848 const char* utf8,
1849 size_t length);
1850
1851
1852// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853
1854/**
1855 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001856 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858typedef Persistent<Context> (*ContextGenerator)();
1859
1860
1861/**
1862 * Container class for static utility functions.
1863 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001864class EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001866 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 static void SetFatalErrorHandler(FatalErrorCallback that);
1868
v8.team.kasperl727e9952008-09-02 14:56:44 +00001869 /**
1870 * Ignore out-of-memory exceptions.
1871 *
1872 * V8 running out of memory is treated as a fatal error by default.
1873 * This means that the fatal error handler is called and that V8 is
1874 * terminated.
1875 *
1876 * IgnoreOutOfMemoryException can be used to not treat a
1877 * out-of-memory situation as a fatal error. This way, the contexts
1878 * that did not cause the out of memory problem might be able to
1879 * continue execution.
1880 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 static void IgnoreOutOfMemoryException();
1882
v8.team.kasperl727e9952008-09-02 14:56:44 +00001883 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001884 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001885 * fatal errors such as out-of-memory situations.
1886 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 static bool IsDead();
1888
1889 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001890 * Adds a message listener.
1891 *
1892 * The same message listener can be added more than once and it that
1893 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894 */
1895 static bool AddMessageListener(MessageCallback that,
1896 Handle<Value> data = Handle<Value>());
1897
1898 /**
1899 * Remove all message listeners from the specified callback function.
1900 */
1901 static void RemoveMessageListeners(MessageCallback that);
1902
1903 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001904 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 */
1906 static void SetFlagsFromString(const char* str, int length);
1907
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001908 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001909 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001910 */
1911 static void SetFlagsFromCommandLine(int* argc,
1912 char** argv,
1913 bool remove_flags);
1914
kasper.lund7276f142008-07-30 08:49:36 +00001915 /** Get the version string. */
1916 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917
1918 /**
1919 * Enables the host application to provide a mechanism for recording
1920 * statistics counters.
1921 */
1922 static void SetCounterFunction(CounterLookupCallback);
1923
1924 /**
1925 * Enables the computation of a sliding window of states. The sliding
1926 * window information is recorded in statistics counters.
1927 */
1928 static void EnableSlidingStateWindow();
1929
1930 /** Callback function for reporting failed access checks.*/
1931 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
1932
1933 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001934 * Enables the host application to receive a notification before a
1935 * major garbage colletion. Allocations are not allowed in the
1936 * callback function, you therefore cannot manipulate objects (set
1937 * or delete properties for example) since it is possible such
1938 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 */
1940 static void SetGlobalGCPrologueCallback(GCCallback);
1941
1942 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001943 * Enables the host application to receive a notification after a
1944 * major garbage collection. Allocations are not allowed in the
1945 * callback function, you therefore cannot manipulate objects (set
1946 * or delete properties for example) since it is possible such
1947 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 */
1949 static void SetGlobalGCEpilogueCallback(GCCallback);
1950
1951 /**
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001952 * Applications can register a callback that will be used when
1953 * allocating most of the V8 symbols. The callback must return an
1954 * external string resource that represents the symbols.
1955 *
1956 * Most often when performing a property lookup the key will be a
1957 * symbol. Allocating symbols as external strings can reduce the
1958 * amount of string conversions needed when using interceptors and
1959 * accessors.
1960 *
1961 * \note This is an experimental feature and it might be removed.
1962 */
1963 static void SetExternalSymbolCallback(ExternalSymbolCallback);
1964
1965 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001966 * Allows the host application to group objects together. If one
1967 * object in the group is alive, all objects in the group are alive.
1968 * After each garbage collection, object groups are removed. It is
1969 * intended to be used in the before-garbage-collection callback
1970 * function for istance to simulate DOM tree connections among JS
1971 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972 */
1973 static void AddObjectToGroup(void* id, Persistent<Object> obj);
1974
1975 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001976 * Initializes from snapshot if possible. Otherwise, attempts to
1977 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 */
1979 static bool Initialize();
1980
kasper.lund7276f142008-07-30 08:49:36 +00001981 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001982 * Adjusts the amount of registered external memory. Used to give
1983 * V8 an indication of the amount of externally allocated memory
1984 * that is kept alive by JavaScript objects. V8 uses this to decide
1985 * when to perform global garbage collections. Registering
1986 * externally allocated memory will trigger global garbage
1987 * collections more often than otherwise in an attempt to garbage
1988 * collect the JavaScript objects keeping the externally allocated
1989 * memory alive.
1990 *
1991 * \param change_in_bytes the change in externally allocated memory
1992 * that is kept alive by JavaScript objects.
1993 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00001994 */
1995 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
1996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 private:
1998 V8();
1999
2000 static void** GlobalizeReference(void** handle);
2001 static void DisposeGlobal(void** global_handle);
2002 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
2003 static void ClearWeak(void** global_handle);
2004 static bool IsGlobalNearDeath(void** global_handle);
2005 static bool IsGlobalWeak(void** global_handle);
2006
2007 template <class T> friend class Handle;
2008 template <class T> friend class Local;
2009 template <class T> friend class Persistent;
2010 friend class Context;
2011};
2012
2013
2014/**
2015 * An external exception handler.
2016 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002017class EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 public:
2019
2020 /**
2021 * Creates a new try/catch block and registers it with v8.
2022 */
2023 TryCatch();
2024
2025 /**
2026 * Unregisters and deletes this try/catch block.
2027 */
2028 ~TryCatch();
2029
2030 /**
2031 * Returns true if an exception has been caught by this try/catch block.
2032 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002033 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034
2035 /**
2036 * Returns the exception caught by this try/catch block. If no exception has
2037 * been caught an empty handle is returned.
2038 *
2039 * The returned handle is valid until this TryCatch block has been destroyed.
2040 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002041 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042
2043 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002044 * Returns the message associated with this exception. If there is
2045 * no message associated an empty handle is returned.
2046 *
2047 * The returned handle is valid until this TryCatch block has been
2048 * destroyed.
2049 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002050 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002051
2052 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 * Clears any exceptions that may have been caught by this try/catch block.
2054 * After this method has been called, HasCaught() will return false.
2055 *
2056 * It is not necessary to clear a try/catch block before using it again; if
2057 * another exception is thrown the previously caught exception will just be
2058 * overwritten. However, it is often a good idea since it makes it easier
2059 * to determine which operation threw a given exception.
2060 */
2061 void Reset();
2062
v8.team.kasperl727e9952008-09-02 14:56:44 +00002063 /**
2064 * Set verbosity of the external exception handler.
2065 *
2066 * By default, exceptions that are caught by an external exception
2067 * handler are not reported. Call SetVerbose with true on an
2068 * external exception handler to have exceptions caught by the
2069 * handler reported as if they were not caught.
2070 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 void SetVerbose(bool value);
2072
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002073 /**
2074 * Set whether or not this TryCatch should capture a Message object
2075 * which holds source information about where the exception
2076 * occurred. True by default.
2077 */
2078 void SetCaptureMessage(bool value);
2079
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 public:
2081 TryCatch* next_;
2082 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002083 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002085 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002086 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087};
2088
2089
2090// --- C o n t e x t ---
2091
2092
2093/**
2094 * Ignore
2095 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002096class EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 public:
2098 ExtensionConfiguration(int name_count, const char* names[])
2099 : name_count_(name_count), names_(names) { }
2100 private:
2101 friend class ImplementationUtilities;
2102 int name_count_;
2103 const char** names_;
2104};
2105
2106
2107/**
2108 * A sandboxed execution context with its own set of built-in objects
2109 * and functions.
2110 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002111class EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002113 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 Local<Object> Global();
2115
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002116 /**
2117 * Detaches the global object from its context before
2118 * the global object can be reused to create a new context.
2119 */
2120 void DetachGlobal();
2121
v8.team.kasperl727e9952008-09-02 14:56:44 +00002122 /** Creates a new context. */
2123 static Persistent<Context> New(
2124 ExtensionConfiguration* extensions = 0,
2125 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2126 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127
kasper.lund44510672008-07-25 07:37:58 +00002128 /** Returns the last entered context. */
2129 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
kasper.lund44510672008-07-25 07:37:58 +00002131 /** Returns the context that is on the top of the stack. */
2132 static Local<Context> GetCurrent();
2133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 /**
2135 * Sets the security token for the context. To access an object in
2136 * another context, the security tokens must match.
2137 */
2138 void SetSecurityToken(Handle<Value> token);
2139
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002140 /** Restores the security token to the default value. */
2141 void UseDefaultSecurityToken();
2142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 /** Returns the security token of this context.*/
2144 Handle<Value> GetSecurityToken();
2145
v8.team.kasperl727e9952008-09-02 14:56:44 +00002146 /**
2147 * Enter this context. After entering a context, all code compiled
2148 * and run is compiled and run in this context. If another context
2149 * is already entered, this old context is saved so it can be
2150 * restored when the new context is exited.
2151 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002153
2154 /**
2155 * Exit this context. Exiting the current context restores the
2156 * context that was in place when entering the current context.
2157 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 void Exit();
2159
v8.team.kasperl727e9952008-09-02 14:56:44 +00002160 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 bool HasOutOfMemoryException();
2162
v8.team.kasperl727e9952008-09-02 14:56:44 +00002163 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 static bool InContext();
2165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 /**
2167 * Stack-allocated class which sets the execution context for all
2168 * operations executed within a local scope.
2169 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002170 class EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171 public:
2172 inline Scope(Handle<Context> context) : context_(context) {
2173 context_->Enter();
2174 }
2175 inline ~Scope() { context_->Exit(); }
2176 private:
2177 Handle<Context> context_;
2178 };
2179
2180 private:
2181 friend class Value;
2182 friend class Script;
2183 friend class Object;
2184 friend class Function;
2185};
2186
2187
2188/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002189 * Multiple threads in V8 are allowed, but only one thread at a time
2190 * is allowed to use V8. The definition of 'using V8' includes
2191 * accessing handles or holding onto object pointers obtained from V8
2192 * handles. It is up to the user of V8 to ensure (perhaps with
2193 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 *
2195 * If you wish to start using V8 in a thread you can do this by constructing
2196 * a v8::Locker object. After the code using V8 has completed for the
2197 * current thread you can call the destructor. This can be combined
2198 * with C++ scope-based construction as follows:
2199 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002200 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 * ...
2202 * {
2203 * v8::Locker locker;
2204 * ...
2205 * // Code using V8 goes here.
2206 * ...
2207 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002208 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 *
2210 * If you wish to stop using V8 in a thread A you can do this by either
2211 * by destroying the v8::Locker object as above or by constructing a
2212 * v8::Unlocker object:
2213 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002214 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002215 * {
2216 * v8::Unlocker unlocker;
2217 * ...
2218 * // Code not using V8 goes here while V8 can run in another thread.
2219 * ...
2220 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002221 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 *
2223 * The Unlocker object is intended for use in a long-running callback
2224 * from V8, where you want to release the V8 lock for other threads to
2225 * use.
2226 *
2227 * The v8::Locker is a recursive lock. That is, you can lock more than
2228 * once in a given thread. This can be useful if you have code that can
2229 * be called either from code that holds the lock or from code that does
2230 * not. The Unlocker is not recursive so you can not have several
2231 * Unlockers on the stack at once, and you can not use an Unlocker in a
2232 * thread that is not inside a Locker's scope.
2233 *
2234 * An unlocker will unlock several lockers if it has to and reinstate
2235 * the correct depth of locking on its destruction. eg.:
2236 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002237 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 * // V8 not locked.
2239 * {
2240 * v8::Locker locker;
2241 * // V8 locked.
2242 * {
2243 * v8::Locker another_locker;
2244 * // V8 still locked (2 levels).
2245 * {
2246 * v8::Unlocker unlocker;
2247 * // V8 not locked.
2248 * }
2249 * // V8 locked again (2 levels).
2250 * }
2251 * // V8 still locked (1 level).
2252 * }
2253 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002254 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002256class EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 public:
2258 Unlocker();
2259 ~Unlocker();
2260};
2261
2262
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002263class EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 public:
2265 Locker();
2266 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002267
2268 /**
2269 * Start preemption.
2270 *
2271 * When preemption is started, a timer is fired every n milli seconds
2272 * that will switch between multiple threads that are in contention
2273 * for the V8 lock.
2274 */
2275 static void StartPreemption(int every_n_ms);
2276
2277 /**
2278 * Stop preemption.
2279 */
2280 static void StopPreemption();
2281
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002282 /**
2283 * Returns whether or not the locker is locked by the current thread.
2284 */
2285 static bool IsLocked();
2286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287 private:
2288 bool has_lock_;
2289 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002290
2291 // Disallow copying and assigning.
2292 Locker(const Locker&);
2293 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002294};
2295
2296
2297
2298// --- I m p l e m e n t a t i o n ---
2299
2300template <class T>
2301Handle<T>::Handle() : val_(0) { }
2302
2303
2304template <class T>
2305Local<T>::Local() : Handle<T>() { }
2306
2307
2308template <class T>
2309Local<T> Local<T>::New(Handle<T> that) {
2310 if (that.IsEmpty()) return Local<T>();
2311 void** p = reinterpret_cast<void**>(*that);
2312 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2313}
2314
2315
2316template <class T>
2317Persistent<T> Persistent<T>::New(Handle<T> that) {
2318 if (that.IsEmpty()) return Persistent<T>();
2319 void** p = reinterpret_cast<void**>(*that);
2320 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2321}
2322
2323
2324template <class T>
2325bool Persistent<T>::IsNearDeath() {
2326 if (this->IsEmpty()) return false;
2327 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2328}
2329
2330
2331template <class T>
2332bool Persistent<T>::IsWeak() {
2333 if (this->IsEmpty()) return false;
2334 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2335}
2336
2337
2338template <class T>
2339void Persistent<T>::Dispose() {
2340 if (this->IsEmpty()) return;
2341 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2342}
2343
2344
2345template <class T>
2346Persistent<T>::Persistent() : Handle<T>() { }
2347
2348template <class T>
2349void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2350 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2351}
2352
2353template <class T>
2354void Persistent<T>::ClearWeak() {
2355 V8::ClearWeak(reinterpret_cast<void**>(**this));
2356}
2357
2358template <class T>
2359T* Handle<T>::operator->() {
2360 return val_;
2361}
2362
2363
2364template <class T>
2365T* Handle<T>::operator*() {
2366 return val_;
2367}
2368
2369
2370Local<Value> Arguments::operator[](int i) const {
2371 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2372 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2373}
2374
2375
2376Local<Function> Arguments::Callee() const {
2377 return callee_;
2378}
2379
2380
2381Local<Object> Arguments::This() const {
2382 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2383}
2384
2385
2386Local<Object> Arguments::Holder() const {
2387 return holder_;
2388}
2389
2390
2391Local<Value> Arguments::Data() const {
2392 return data_;
2393}
2394
2395
2396bool Arguments::IsConstructCall() const {
2397 return is_construct_call_;
2398}
2399
2400
2401int Arguments::Length() const {
2402 return length_;
2403}
2404
2405
2406Local<Value> AccessorInfo::Data() const {
2407 return data_;
2408}
2409
2410
2411Local<Object> AccessorInfo::This() const {
2412 return self_;
2413}
2414
2415
2416Local<Object> AccessorInfo::Holder() const {
2417 return holder_;
2418}
2419
2420
2421template <class T>
2422Local<T> HandleScope::Close(Handle<T> value) {
2423 void** after = RawClose(reinterpret_cast<void**>(*value));
2424 return Local<T>(reinterpret_cast<T*>(after));
2425}
2426
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002427Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 return resource_name_;
2429}
2430
2431
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002432Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 return resource_line_offset_;
2434}
2435
2436
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002437Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 return resource_column_offset_;
2439}
2440
2441
2442Handle<Boolean> Boolean::New(bool value) {
2443 return value ? True() : False();
2444}
2445
2446
2447void Template::Set(const char* name, v8::Handle<Data> value) {
2448 Set(v8::String::New(name), value);
2449}
2450
2451
2452/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002453 * \example shell.cc
2454 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 * command-line and executes them.
2456 */
2457
2458
2459/**
2460 * \example process.cc
2461 */
2462
2463
2464} // namespace v8
2465
2466
2467#undef EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002468#undef EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469#undef TYPE_CHECK
2470
2471
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002472#endif // V8_H_