blob: 70b1f526c3f34f182041477979f094ff9cbc9b92 [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.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 */
1305enum AccessControl {
1306 DEFAULT = 0,
1307 ALL_CAN_READ = 1,
1308 ALL_CAN_WRITE = 2
1309};
1310
1311
1312/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001313 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 */
1315enum AccessType {
1316 ACCESS_GET,
1317 ACCESS_SET,
1318 ACCESS_HAS,
1319 ACCESS_DELETE,
1320 ACCESS_KEYS
1321};
1322
v8.team.kasperl727e9952008-09-02 14:56:44 +00001323
1324/**
1325 * Returns true if cross-context access should be allowed to the named
1326 * property with the given key on the global object.
1327 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328typedef bool (*NamedSecurityCallback)(Local<Object> global,
1329 Local<Value> key,
1330 AccessType type,
1331 Local<Value> data);
1332
v8.team.kasperl727e9952008-09-02 14:56:44 +00001333
1334/**
1335 * Returns true if cross-context access should be allowed to the indexed
1336 * property with the given index on the global object.
1337 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1339 uint32_t index,
1340 AccessType type,
1341 Local<Value> data);
1342
1343
1344/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001345 * A FunctionTemplate is used to create functions at runtime. There
1346 * can only be one function created from a FunctionTemplate in a
1347 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 *
1349 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001350 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001352 * A FunctionTemplate has a corresponding instance template which is
1353 * used to create object instances when the function is used as a
1354 * constructor. Properties added to the instance template are added to
1355 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 *
1357 * A FunctionTemplate can have a prototype template. The prototype template
1358 * is used to create the prototype object of the function.
1359 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001360 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001362 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1364 * t->Set("func_property", v8::Number::New(1));
1365 *
1366 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1367 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1368 * proto_t->Set("proto_const", v8::Number::New(2));
1369 *
1370 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1371 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1372 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1373 * instance_t->Set("instance_property", Number::New(3));
1374 *
1375 * v8::Local<v8::Function> function = t->GetFunction();
1376 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001377 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 *
1379 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001380 * and "instance" for the instance object created above. The function
1381 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001383 * \code
1384 * func_property in function == true;
1385 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001387 * function.prototype.proto_method() invokes 'InvokeCallback'
1388 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * instance instanceof function == true;
1391 * instance.instance_accessor calls 'InstanceAccessorCallback'
1392 * instance.instance_property == 3;
1393 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001395 * A FunctionTemplate can inherit from another one by calling the
1396 * FunctionTemplate::Inherit method. The following graph illustrates
1397 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001399 * \code
1400 * FunctionTemplate Parent -> Parent() . prototype -> { }
1401 * ^ ^
1402 * | Inherit(Parent) | .__proto__
1403 * | |
1404 * FunctionTemplate Child -> Child() . prototype -> { }
1405 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001407 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1408 * object of the Child() function has __proto__ pointing to the
1409 * Parent() function's prototype object. An instance of the Child
1410 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001412 * Let Parent be the FunctionTemplate initialized in the previous
1413 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001415 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 * Local<FunctionTemplate> parent = t;
1417 * Local<FunctionTemplate> child = FunctionTemplate::New();
1418 * child->Inherit(parent);
1419 *
1420 * Local<Function> child_function = child->GetFunction();
1421 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001422 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001424 * The Child function and Child instance will have the following
1425 * properties:
1426 *
1427 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001429 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001431 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001433class EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 public:
1435 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001436 static Local<FunctionTemplate> New(
1437 InvocationCallback callback = 0,
1438 Handle<Value> data = Handle<Value>(),
1439 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 /** Returns the unique function instance in the current execution context.*/
1441 Local<Function> GetFunction();
1442
v8.team.kasperl727e9952008-09-02 14:56:44 +00001443 /**
1444 * Set the call-handler callback for a FunctionTemplate. This
1445 * callback is called whenever the function created from this
1446 * FunctionTemplate is called.
1447 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 void SetCallHandler(InvocationCallback callback,
1449 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450
v8.team.kasperl727e9952008-09-02 14:56:44 +00001451 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 Local<ObjectTemplate> InstanceTemplate();
1453
1454 /** Causes the function template to inherit from a parent function template.*/
1455 void Inherit(Handle<FunctionTemplate> parent);
1456
1457 /**
1458 * A PrototypeTemplate is the template used to create the prototype object
1459 * of the function created by this template.
1460 */
1461 Local<ObjectTemplate> PrototypeTemplate();
1462
v8.team.kasperl727e9952008-09-02 14:56:44 +00001463
1464 /**
1465 * Set the class name of the FunctionTemplate. This is used for
1466 * printing objects created with the function created from the
1467 * FunctionTemplate as its constructor.
1468 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 void SetClassName(Handle<String> name);
1470
1471 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001472 * Determines whether the __proto__ accessor ignores instances of
1473 * the function template. If instances of the function template are
1474 * ignored, __proto__ skips all instances and instead returns the
1475 * next object in the prototype chain.
1476 *
1477 * Call with a value of true to make the __proto__ accessor ignore
1478 * instances of the function template. Call with a value of false
1479 * to make the __proto__ accessor not ignore instances of the
1480 * function template. By default, instances of a function template
1481 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 */
1483 void SetHiddenPrototype(bool value);
1484
1485 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001486 * Returns true if the given object is an instance of this function
1487 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 */
1489 bool HasInstance(Handle<Value> object);
1490
1491 private:
1492 FunctionTemplate();
1493 void AddInstancePropertyAccessor(Handle<String> name,
1494 AccessorGetter getter,
1495 AccessorSetter setter,
1496 Handle<Value> data,
1497 AccessControl settings,
1498 PropertyAttribute attributes);
1499 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1500 NamedPropertySetter setter,
1501 NamedPropertyQuery query,
1502 NamedPropertyDeleter remover,
1503 NamedPropertyEnumerator enumerator,
1504 Handle<Value> data);
1505 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1506 IndexedPropertySetter setter,
1507 IndexedPropertyQuery query,
1508 IndexedPropertyDeleter remover,
1509 IndexedPropertyEnumerator enumerator,
1510 Handle<Value> data);
1511 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1512 Handle<Value> data);
1513
1514 friend class Context;
1515 friend class ObjectTemplate;
1516};
1517
1518
1519/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001520 * An ObjectTemplate is used to create objects at runtime.
1521 *
1522 * Properties added to an ObjectTemplate are added to each object
1523 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001525class EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001527 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001529
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 /** Creates a new instance of this template.*/
1531 Local<Object> NewInstance();
1532
1533 /**
1534 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001535 *
1536 * Whenever the property with the given name is accessed on objects
1537 * created from this ObjectTemplate the getter and setter callbacks
1538 * are called instead of getting and setting the property directly
1539 * on the JavaScript object.
1540 *
1541 * \param name The name of the property for which an accessor is added.
1542 * \param getter The callback to invoke when getting the property.
1543 * \param setter The callback to invoke when setting the property.
1544 * \param data A piece of data that will be passed to the getter and setter
1545 * callbacks whenever they are invoked.
1546 * \param settings Access control settings for the accessor. This is a bit
1547 * field consisting of one of more of
1548 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1549 * The default is to not allow cross-context access.
1550 * ALL_CAN_READ means that all cross-context reads are allowed.
1551 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1552 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1553 * cross-context access.
1554 * \param attribute The attributes of the property for which an accessor
1555 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 */
1557 void SetAccessor(Handle<String> name,
1558 AccessorGetter getter,
1559 AccessorSetter setter = 0,
1560 Handle<Value> data = Handle<Value>(),
1561 AccessControl settings = DEFAULT,
1562 PropertyAttribute attribute = None);
1563
1564 /**
1565 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001566 *
1567 * Whenever a named property is accessed on objects created from
1568 * this object template, the provided callback is invoked instead of
1569 * accessing the property directly on the JavaScript object.
1570 *
1571 * \param getter The callback to invoke when getting a property.
1572 * \param setter The callback to invoke when setting a property.
1573 * \param query The callback to invoke to check is an object has a property.
1574 * \param deleter The callback to invoke when deleting a property.
1575 * \param enumerator The callback to invoke to enumerate all the named
1576 * properties of an object.
1577 * \param data A piece of data that will be passed to the callbacks
1578 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 */
1580 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1581 NamedPropertySetter setter = 0,
1582 NamedPropertyQuery query = 0,
1583 NamedPropertyDeleter deleter = 0,
1584 NamedPropertyEnumerator enumerator = 0,
1585 Handle<Value> data = Handle<Value>());
1586
1587 /**
1588 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001589 *
1590 * Whenever an indexed property is accessed on objects created from
1591 * this object template, the provided callback is invoked instead of
1592 * accessing the property directly on the JavaScript object.
1593 *
1594 * \param getter The callback to invoke when getting a property.
1595 * \param setter The callback to invoke when setting a property.
1596 * \param query The callback to invoke to check is an object has a property.
1597 * \param deleter The callback to invoke when deleting a property.
1598 * \param enumerator The callback to invoke to enumerate all the indexed
1599 * properties of an object.
1600 * \param data A piece of data that will be passed to the callbacks
1601 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 */
1603 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1604 IndexedPropertySetter setter = 0,
1605 IndexedPropertyQuery query = 0,
1606 IndexedPropertyDeleter deleter = 0,
1607 IndexedPropertyEnumerator enumerator = 0,
1608 Handle<Value> data = Handle<Value>());
1609 /**
1610 * Sets the callback to be used when calling instances created from
1611 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001612 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 * function.
1614 */
1615 void SetCallAsFunctionHandler(InvocationCallback callback,
1616 Handle<Value> data = Handle<Value>());
1617
v8.team.kasperl727e9952008-09-02 14:56:44 +00001618 /**
1619 * Mark object instances of the template as undetectable.
1620 *
1621 * In many ways, undetectable objects behave as though they are not
1622 * there. They behave like 'undefined' in conditionals and when
1623 * printed. However, properties can be accessed and called as on
1624 * normal objects.
1625 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626 void MarkAsUndetectable();
1627
v8.team.kasperl727e9952008-09-02 14:56:44 +00001628 /**
1629 * Sets access check callbacks on the object template.
1630 *
1631 * When accessing properties on instances of this object template,
1632 * the access check callback will be called to determine whether or
1633 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001634 * The last parameter specifies whether access checks are turned
1635 * on by default on instances. If access checks are off by default,
1636 * they can be turned on on individual instances by calling
1637 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001638 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1640 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001641 Handle<Value> data = Handle<Value>(),
1642 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643
kasper.lund212ac232008-07-16 07:07:30 +00001644 /**
1645 * Gets the number of internal fields for objects generated from
1646 * this template.
1647 */
1648 int InternalFieldCount();
1649
1650 /**
1651 * Sets the number of internal fields for objects generated from
1652 * this template.
1653 */
1654 void SetInternalFieldCount(int value);
1655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 private:
1657 ObjectTemplate();
1658 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1659 friend class FunctionTemplate;
1660};
1661
1662
1663/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001664 * A Signature specifies which receivers and arguments a function can
1665 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001667class EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 public:
1669 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1670 Handle<FunctionTemplate>(),
1671 int argc = 0,
1672 Handle<FunctionTemplate> argv[] = 0);
1673 private:
1674 Signature();
1675};
1676
1677
1678/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001679 * A utility for determining the type of objects based on the template
1680 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001682class EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 public:
1684 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1685 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1686 int match(Handle<Value> value);
1687 private:
1688 TypeSwitch();
1689};
1690
1691
1692// --- E x t e n s i o n s ---
1693
1694
1695/**
1696 * Ignore
1697 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001698class EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 public:
1700 Extension(const char* name,
1701 const char* source = 0,
1702 int dep_count = 0,
1703 const char** deps = 0);
1704 virtual ~Extension() { }
1705 virtual v8::Handle<v8::FunctionTemplate>
1706 GetNativeFunction(v8::Handle<v8::String> name) {
1707 return v8::Handle<v8::FunctionTemplate>();
1708 }
1709
1710 const char* name() { return name_; }
1711 const char* source() { return source_; }
1712 int dependency_count() { return dep_count_; }
1713 const char** dependencies() { return deps_; }
1714 void set_auto_enable(bool value) { auto_enable_ = value; }
1715 bool auto_enable() { return auto_enable_; }
1716
1717 private:
1718 const char* name_;
1719 const char* source_;
1720 int dep_count_;
1721 const char** deps_;
1722 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001723
1724 // Disallow copying and assigning.
1725 Extension(const Extension&);
1726 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727};
1728
1729
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001730void EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731
1732
1733/**
1734 * Ignore
1735 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001736class EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 public:
1738 inline DeclareExtension(Extension* extension) {
1739 RegisterExtension(extension);
1740 }
1741};
1742
1743
1744// --- S t a t i c s ---
1745
1746
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001747Handle<Primitive> EXPORT Undefined();
1748Handle<Primitive> EXPORT Null();
1749Handle<Boolean> EXPORT True();
1750Handle<Boolean> EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751
1752
1753/**
1754 * A set of constraints that specifies the limits of the runtime's
1755 * memory use.
1756 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001757class EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 public:
1759 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001760 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001762 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001764 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1766 private:
1767 int max_young_space_size_;
1768 int max_old_space_size_;
1769 uint32_t* stack_limit_;
1770};
1771
1772
1773bool SetResourceConstraints(ResourceConstraints* constraints);
1774
1775
1776// --- E x c e p t i o n s ---
1777
1778
1779typedef void (*FatalErrorCallback)(const char* location, const char* message);
1780
1781
1782typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1783
1784
1785/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001786 * Schedules an exception to be thrown when returning to JavaScript. When an
1787 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001789 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001791Handle<Value> EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
1793/**
1794 * Create new error objects by calling the corresponding error object
1795 * constructor with the message.
1796 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001797class EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 public:
1799 static Local<Value> RangeError(Handle<String> message);
1800 static Local<Value> ReferenceError(Handle<String> message);
1801 static Local<Value> SyntaxError(Handle<String> message);
1802 static Local<Value> TypeError(Handle<String> message);
1803 static Local<Value> Error(Handle<String> message);
1804};
1805
1806
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807// --- C o u n t e r s C a l l b a c k s
1808
1809typedef int* (*CounterLookupCallback)(const wchar_t* name);
1810
1811// --- 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 ---
1812typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1813 AccessType type,
1814 Local<Value> data);
1815
1816// --- 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
1817
1818/**
1819 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001820 * before and after a major garbage collection. Allocations are not
1821 * allowed in the callback function, you therefore cannot manipulate
1822 * objects (set or delete properties for example) since it is possible
1823 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 */
1825typedef void (*GCCallback)();
1826
1827
1828// --- C o n t e x t G e n e r a t o r
1829
1830/**
1831 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001832 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834typedef Persistent<Context> (*ContextGenerator)();
1835
1836
1837/**
1838 * Container class for static utility functions.
1839 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001840class EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001842 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 static void SetFatalErrorHandler(FatalErrorCallback that);
1844
v8.team.kasperl727e9952008-09-02 14:56:44 +00001845 /**
1846 * Ignore out-of-memory exceptions.
1847 *
1848 * V8 running out of memory is treated as a fatal error by default.
1849 * This means that the fatal error handler is called and that V8 is
1850 * terminated.
1851 *
1852 * IgnoreOutOfMemoryException can be used to not treat a
1853 * out-of-memory situation as a fatal error. This way, the contexts
1854 * that did not cause the out of memory problem might be able to
1855 * continue execution.
1856 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 static void IgnoreOutOfMemoryException();
1858
v8.team.kasperl727e9952008-09-02 14:56:44 +00001859 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001860 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001861 * fatal errors such as out-of-memory situations.
1862 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 static bool IsDead();
1864
1865 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001866 * Adds a message listener.
1867 *
1868 * The same message listener can be added more than once and it that
1869 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 */
1871 static bool AddMessageListener(MessageCallback that,
1872 Handle<Value> data = Handle<Value>());
1873
1874 /**
1875 * Remove all message listeners from the specified callback function.
1876 */
1877 static void RemoveMessageListeners(MessageCallback that);
1878
1879 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001880 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 */
1882 static void SetFlagsFromString(const char* str, int length);
1883
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001884 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001885 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001886 */
1887 static void SetFlagsFromCommandLine(int* argc,
1888 char** argv,
1889 bool remove_flags);
1890
kasper.lund7276f142008-07-30 08:49:36 +00001891 /** Get the version string. */
1892 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893
1894 /**
1895 * Enables the host application to provide a mechanism for recording
1896 * statistics counters.
1897 */
1898 static void SetCounterFunction(CounterLookupCallback);
1899
1900 /**
1901 * Enables the computation of a sliding window of states. The sliding
1902 * window information is recorded in statistics counters.
1903 */
1904 static void EnableSlidingStateWindow();
1905
1906 /** Callback function for reporting failed access checks.*/
1907 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
1908
1909 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001910 * Enables the host application to receive a notification before a
1911 * major garbage colletion. Allocations are not allowed in the
1912 * callback function, you therefore cannot manipulate objects (set
1913 * or delete properties for example) since it is possible such
1914 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 */
1916 static void SetGlobalGCPrologueCallback(GCCallback);
1917
1918 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001919 * Enables the host application to receive a notification after a
1920 * major garbage collection. Allocations are not allowed in the
1921 * callback function, you therefore cannot manipulate objects (set
1922 * or delete properties for example) since it is possible such
1923 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 */
1925 static void SetGlobalGCEpilogueCallback(GCCallback);
1926
1927 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001928 * Allows the host application to group objects together. If one
1929 * object in the group is alive, all objects in the group are alive.
1930 * After each garbage collection, object groups are removed. It is
1931 * intended to be used in the before-garbage-collection callback
1932 * function for istance to simulate DOM tree connections among JS
1933 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 */
1935 static void AddObjectToGroup(void* id, Persistent<Object> obj);
1936
1937 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001938 * Initializes from snapshot if possible. Otherwise, attempts to
1939 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 */
1941 static bool Initialize();
1942
kasper.lund7276f142008-07-30 08:49:36 +00001943 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001944 * Adjusts the amount of registered external memory. Used to give
1945 * V8 an indication of the amount of externally allocated memory
1946 * that is kept alive by JavaScript objects. V8 uses this to decide
1947 * when to perform global garbage collections. Registering
1948 * externally allocated memory will trigger global garbage
1949 * collections more often than otherwise in an attempt to garbage
1950 * collect the JavaScript objects keeping the externally allocated
1951 * memory alive.
1952 *
1953 * \param change_in_bytes the change in externally allocated memory
1954 * that is kept alive by JavaScript objects.
1955 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00001956 */
1957 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
1958
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 private:
1960 V8();
1961
1962 static void** GlobalizeReference(void** handle);
1963 static void DisposeGlobal(void** global_handle);
1964 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
1965 static void ClearWeak(void** global_handle);
1966 static bool IsGlobalNearDeath(void** global_handle);
1967 static bool IsGlobalWeak(void** global_handle);
1968
1969 template <class T> friend class Handle;
1970 template <class T> friend class Local;
1971 template <class T> friend class Persistent;
1972 friend class Context;
1973};
1974
1975
1976/**
1977 * An external exception handler.
1978 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001979class EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 public:
1981
1982 /**
1983 * Creates a new try/catch block and registers it with v8.
1984 */
1985 TryCatch();
1986
1987 /**
1988 * Unregisters and deletes this try/catch block.
1989 */
1990 ~TryCatch();
1991
1992 /**
1993 * Returns true if an exception has been caught by this try/catch block.
1994 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001995 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996
1997 /**
1998 * Returns the exception caught by this try/catch block. If no exception has
1999 * been caught an empty handle is returned.
2000 *
2001 * The returned handle is valid until this TryCatch block has been destroyed.
2002 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002003 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004
2005 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002006 * Returns the message associated with this exception. If there is
2007 * no message associated an empty handle is returned.
2008 *
2009 * The returned handle is valid until this TryCatch block has been
2010 * destroyed.
2011 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002012 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002013
2014 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 * Clears any exceptions that may have been caught by this try/catch block.
2016 * After this method has been called, HasCaught() will return false.
2017 *
2018 * It is not necessary to clear a try/catch block before using it again; if
2019 * another exception is thrown the previously caught exception will just be
2020 * overwritten. However, it is often a good idea since it makes it easier
2021 * to determine which operation threw a given exception.
2022 */
2023 void Reset();
2024
v8.team.kasperl727e9952008-09-02 14:56:44 +00002025 /**
2026 * Set verbosity of the external exception handler.
2027 *
2028 * By default, exceptions that are caught by an external exception
2029 * handler are not reported. Call SetVerbose with true on an
2030 * external exception handler to have exceptions caught by the
2031 * handler reported as if they were not caught.
2032 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 void SetVerbose(bool value);
2034
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002035 /**
2036 * Set whether or not this TryCatch should capture a Message object
2037 * which holds source information about where the exception
2038 * occurred. True by default.
2039 */
2040 void SetCaptureMessage(bool value);
2041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 public:
2043 TryCatch* next_;
2044 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002045 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002047 bool capture_message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048};
2049
2050
2051// --- C o n t e x t ---
2052
2053
2054/**
2055 * Ignore
2056 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002057class EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 public:
2059 ExtensionConfiguration(int name_count, const char* names[])
2060 : name_count_(name_count), names_(names) { }
2061 private:
2062 friend class ImplementationUtilities;
2063 int name_count_;
2064 const char** names_;
2065};
2066
2067
2068/**
2069 * A sandboxed execution context with its own set of built-in objects
2070 * and functions.
2071 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002072class EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002074 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 Local<Object> Global();
2076
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002077 /**
2078 * Detaches the global object from its context before
2079 * the global object can be reused to create a new context.
2080 */
2081 void DetachGlobal();
2082
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083 /** Creates a new context. */
2084 static Persistent<Context> New(
2085 ExtensionConfiguration* extensions = 0,
2086 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2087 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088
kasper.lund44510672008-07-25 07:37:58 +00002089 /** Returns the last entered context. */
2090 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091
kasper.lund44510672008-07-25 07:37:58 +00002092 /** Returns the context that is on the top of the stack. */
2093 static Local<Context> GetCurrent();
2094
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 /**
2096 * Sets the security token for the context. To access an object in
2097 * another context, the security tokens must match.
2098 */
2099 void SetSecurityToken(Handle<Value> token);
2100
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002101 /** Restores the security token to the default value. */
2102 void UseDefaultSecurityToken();
2103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 /** Returns the security token of this context.*/
2105 Handle<Value> GetSecurityToken();
2106
v8.team.kasperl727e9952008-09-02 14:56:44 +00002107 /**
2108 * Enter this context. After entering a context, all code compiled
2109 * and run is compiled and run in this context. If another context
2110 * is already entered, this old context is saved so it can be
2111 * restored when the new context is exited.
2112 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002114
2115 /**
2116 * Exit this context. Exiting the current context restores the
2117 * context that was in place when entering the current context.
2118 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 void Exit();
2120
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 bool HasOutOfMemoryException();
2123
v8.team.kasperl727e9952008-09-02 14:56:44 +00002124 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 static bool InContext();
2126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 /**
2128 * Stack-allocated class which sets the execution context for all
2129 * operations executed within a local scope.
2130 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002131 class EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 public:
2133 inline Scope(Handle<Context> context) : context_(context) {
2134 context_->Enter();
2135 }
2136 inline ~Scope() { context_->Exit(); }
2137 private:
2138 Handle<Context> context_;
2139 };
2140
2141 private:
2142 friend class Value;
2143 friend class Script;
2144 friend class Object;
2145 friend class Function;
2146};
2147
2148
2149/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002150 * Multiple threads in V8 are allowed, but only one thread at a time
2151 * is allowed to use V8. The definition of 'using V8' includes
2152 * accessing handles or holding onto object pointers obtained from V8
2153 * handles. It is up to the user of V8 to ensure (perhaps with
2154 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 *
2156 * If you wish to start using V8 in a thread you can do this by constructing
2157 * a v8::Locker object. After the code using V8 has completed for the
2158 * current thread you can call the destructor. This can be combined
2159 * with C++ scope-based construction as follows:
2160 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002161 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 * ...
2163 * {
2164 * v8::Locker locker;
2165 * ...
2166 * // Code using V8 goes here.
2167 * ...
2168 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002169 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 *
2171 * If you wish to stop using V8 in a thread A you can do this by either
2172 * by destroying the v8::Locker object as above or by constructing a
2173 * v8::Unlocker object:
2174 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 * {
2177 * v8::Unlocker unlocker;
2178 * ...
2179 * // Code not using V8 goes here while V8 can run in another thread.
2180 * ...
2181 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 *
2184 * The Unlocker object is intended for use in a long-running callback
2185 * from V8, where you want to release the V8 lock for other threads to
2186 * use.
2187 *
2188 * The v8::Locker is a recursive lock. That is, you can lock more than
2189 * once in a given thread. This can be useful if you have code that can
2190 * be called either from code that holds the lock or from code that does
2191 * not. The Unlocker is not recursive so you can not have several
2192 * Unlockers on the stack at once, and you can not use an Unlocker in a
2193 * thread that is not inside a Locker's scope.
2194 *
2195 * An unlocker will unlock several lockers if it has to and reinstate
2196 * the correct depth of locking on its destruction. eg.:
2197 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 * // V8 not locked.
2200 * {
2201 * v8::Locker locker;
2202 * // V8 locked.
2203 * {
2204 * v8::Locker another_locker;
2205 * // V8 still locked (2 levels).
2206 * {
2207 * v8::Unlocker unlocker;
2208 * // V8 not locked.
2209 * }
2210 * // V8 locked again (2 levels).
2211 * }
2212 * // V8 still locked (1 level).
2213 * }
2214 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002215 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002217class EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 public:
2219 Unlocker();
2220 ~Unlocker();
2221};
2222
2223
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002224class EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 public:
2226 Locker();
2227 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002228
2229 /**
2230 * Start preemption.
2231 *
2232 * When preemption is started, a timer is fired every n milli seconds
2233 * that will switch between multiple threads that are in contention
2234 * for the V8 lock.
2235 */
2236 static void StartPreemption(int every_n_ms);
2237
2238 /**
2239 * Stop preemption.
2240 */
2241 static void StopPreemption();
2242
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002243 /**
2244 * Returns whether or not the locker is locked by the current thread.
2245 */
2246 static bool IsLocked();
2247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 private:
2249 bool has_lock_;
2250 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002251
2252 // Disallow copying and assigning.
2253 Locker(const Locker&);
2254 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255};
2256
2257
2258
2259// --- I m p l e m e n t a t i o n ---
2260
2261template <class T>
2262Handle<T>::Handle() : val_(0) { }
2263
2264
2265template <class T>
2266Local<T>::Local() : Handle<T>() { }
2267
2268
2269template <class T>
2270Local<T> Local<T>::New(Handle<T> that) {
2271 if (that.IsEmpty()) return Local<T>();
2272 void** p = reinterpret_cast<void**>(*that);
2273 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2274}
2275
2276
2277template <class T>
2278Persistent<T> Persistent<T>::New(Handle<T> that) {
2279 if (that.IsEmpty()) return Persistent<T>();
2280 void** p = reinterpret_cast<void**>(*that);
2281 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2282}
2283
2284
2285template <class T>
2286bool Persistent<T>::IsNearDeath() {
2287 if (this->IsEmpty()) return false;
2288 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2289}
2290
2291
2292template <class T>
2293bool Persistent<T>::IsWeak() {
2294 if (this->IsEmpty()) return false;
2295 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2296}
2297
2298
2299template <class T>
2300void Persistent<T>::Dispose() {
2301 if (this->IsEmpty()) return;
2302 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2303}
2304
2305
2306template <class T>
2307Persistent<T>::Persistent() : Handle<T>() { }
2308
2309template <class T>
2310void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2311 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2312}
2313
2314template <class T>
2315void Persistent<T>::ClearWeak() {
2316 V8::ClearWeak(reinterpret_cast<void**>(**this));
2317}
2318
2319template <class T>
2320T* Handle<T>::operator->() {
2321 return val_;
2322}
2323
2324
2325template <class T>
2326T* Handle<T>::operator*() {
2327 return val_;
2328}
2329
2330
2331Local<Value> Arguments::operator[](int i) const {
2332 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2333 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2334}
2335
2336
2337Local<Function> Arguments::Callee() const {
2338 return callee_;
2339}
2340
2341
2342Local<Object> Arguments::This() const {
2343 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2344}
2345
2346
2347Local<Object> Arguments::Holder() const {
2348 return holder_;
2349}
2350
2351
2352Local<Value> Arguments::Data() const {
2353 return data_;
2354}
2355
2356
2357bool Arguments::IsConstructCall() const {
2358 return is_construct_call_;
2359}
2360
2361
2362int Arguments::Length() const {
2363 return length_;
2364}
2365
2366
2367Local<Value> AccessorInfo::Data() const {
2368 return data_;
2369}
2370
2371
2372Local<Object> AccessorInfo::This() const {
2373 return self_;
2374}
2375
2376
2377Local<Object> AccessorInfo::Holder() const {
2378 return holder_;
2379}
2380
2381
2382template <class T>
2383Local<T> HandleScope::Close(Handle<T> value) {
2384 void** after = RawClose(reinterpret_cast<void**>(*value));
2385 return Local<T>(reinterpret_cast<T*>(after));
2386}
2387
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002388Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 return resource_name_;
2390}
2391
2392
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002393Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394 return resource_line_offset_;
2395}
2396
2397
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002398Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399 return resource_column_offset_;
2400}
2401
2402
2403Handle<Boolean> Boolean::New(bool value) {
2404 return value ? True() : False();
2405}
2406
2407
2408void Template::Set(const char* name, v8::Handle<Data> value) {
2409 Set(v8::String::New(name), value);
2410}
2411
2412
2413/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002414 * \example shell.cc
2415 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002416 * command-line and executes them.
2417 */
2418
2419
2420/**
2421 * \example process.cc
2422 */
2423
2424
2425} // namespace v8
2426
2427
2428#undef EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002429#undef EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430#undef TYPE_CHECK
2431
2432
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002433#endif // V8_H_