blob: 2842c328dc53c6aef0f167dd58b18bee9a84f4bb [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
81// on checking of BUILDING_V8_SHARED and USING_V8_SHARED.
82#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 */
131typedef void (*WeakReferenceCallback)(Persistent<Object> object,
132 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
330 template <class S> explicit inline Persistent(Handle<S> that)
331 : Handle<T>(*that) { }
332
333 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
334 if (that.IsEmpty()) return Persistent<T>();
335 return Persistent<T>(T::Cast(*that));
336 }
337
338 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000339 * Creates a new persistent handle for an existing local or
340 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 */
342 static Persistent<T> New(Handle<T> that);
343
344 /**
345 * Releases the storage cell referenced by this persistent handle.
346 * Does not remove the reference to the cell from any handles.
347 * This handle's reference, and any any other references to the storage
348 * cell remain and IsEmpty will still return false.
349 */
350 void Dispose();
351
352 /**
353 * Make the reference to this object weak. When only weak handles
354 * refer to the object, the garbage collector will perform a
355 * callback to the given V8::WeakReferenceCallback function, passing
356 * it the object reference and the given parameters.
357 */
358 void MakeWeak(void* parameters, WeakReferenceCallback callback);
359
360 /** Clears the weak reference to this object.*/
361 void ClearWeak();
362
363 /**
364 *Checks if the handle holds the only reference to an object.
365 */
366 bool IsNearDeath();
367
368 /**
369 * Returns true if the handle's reference is weak.
370 */
371 bool IsWeak();
372
373 private:
374 friend class ImplementationUtilities;
375 friend class ObjectTemplate;
376};
377
378
v8.team.kasperl727e9952008-09-02 14:56:44 +0000379 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 * A stack-allocated class that governs a number of local handles.
381 * After a handle scope has been created, all local handles will be
382 * allocated within that handle scope until either the handle scope is
383 * deleted or another handle scope is created. If there is already a
384 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000385 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 * new handles will again be allocated in the original handle scope.
387 *
388 * After the handle scope of a local handle has been deleted the
389 * garbage collector will no longer track the object stored in the
390 * handle and may deallocate it. The behavior of accessing a handle
391 * for which the handle scope has been deleted is undefined.
392 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000393class EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 public:
395 HandleScope() : previous_(current_), is_closed_(false) {
396 current_.extensions = 0;
397 }
398
399 ~HandleScope() {
400 // TODO(1245391): In a perfect world, there would be a way of not
v8.team.kasperl727e9952008-09-02 14:56:44 +0000401 // having to check for explicitly closed scopes maybe through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 // subclassing HandleScope?
403 if (!is_closed_) RestorePreviousState();
404 }
405
406 /**
407 * TODO(1245391): Consider introducing a subclass for this.
408 * Closes the handle scope and returns the value as a handle in the
409 * previous scope, which is the new current scope after the call.
410 */
411 template <class T> Local<T> Close(Handle<T> value);
412
413 /**
414 * Counts the number of allocated handles.
415 */
416 static int NumberOfHandles();
417
418 /**
419 * Creates a new handle with the given value.
420 */
421 static void** CreateHandle(void* value);
422
423 private:
424 // Make it impossible to create heap-allocated or illegal handle
425 // scopes by disallowing certain operations.
426 HandleScope(const HandleScope&);
427 void operator=(const HandleScope&);
428 void* operator new(size_t size);
429 void operator delete(void*, size_t);
430
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000431 class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 public:
433 int extensions;
434 void** next;
435 void** limit;
436 inline void Initialize() {
437 extensions = -1;
438 next = limit = NULL;
439 }
440 };
441
442 static Data current_;
443 const Data previous_;
444
445 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000446 * Re-establishes the previous scope state. Should be called only
447 * once, and only for the current scope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 */
449 void RestorePreviousState() {
450 if (current_.extensions > 0) DeleteExtensions();
451 current_ = previous_;
452#ifdef DEBUG
453 ZapRange(current_.next, current_.limit);
454#endif
455 }
456
457 // TODO(1245391): Consider creating a subclass for this.
458 bool is_closed_;
459 void** RawClose(void** value);
460
461 /** Deallocates any extensions used by the current scope.*/
462 static void DeleteExtensions();
463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 // Zaps the handles in the half-open interval [start, end).
465 static void ZapRange(void** start, void** end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466
467 friend class ImplementationUtilities;
468};
469
470
471// --- S p e c i a l o b j e c t s ---
472
473
474/**
475 * The superclass of values and API object templates.
476 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000477class EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 private:
479 Data();
480};
481
482
483/**
484 * Pre-compilation data that can be associated with a script. This
485 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000486 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000487 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000489class EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 public:
491 virtual ~ScriptData() { }
492 static ScriptData* PreCompile(const char* input, int length);
493 static ScriptData* New(unsigned* data, int length);
494
495 virtual int Length() = 0;
496 virtual unsigned* Data() = 0;
497};
498
499
500/**
501 * The origin, within a file, of a script.
502 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000503class EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000505 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 Handle<Integer> resource_line_offset = Handle<Integer>(),
507 Handle<Integer> resource_column_offset = Handle<Integer>())
508 : resource_name_(resource_name),
509 resource_line_offset_(resource_line_offset),
510 resource_column_offset_(resource_column_offset) { }
mads.s.agercbaa0602008-08-14 13:41:48 +0000511 inline Handle<Value> ResourceName();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 inline Handle<Integer> ResourceLineOffset();
513 inline Handle<Integer> ResourceColumnOffset();
514 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000515 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 Handle<Integer> resource_line_offset_;
517 Handle<Integer> resource_column_offset_;
518};
519
520
521/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000522 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000524class EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 public:
526
527 /**
528 * Compiles the specified script. The ScriptOrigin* and ScriptData*
529 * parameters are owned by the caller of Script::Compile. No
530 * references to these objects are kept after compilation finishes.
531 */
532 static Local<Script> Compile(Handle<String> source,
533 ScriptOrigin* origin = NULL,
534 ScriptData* pre_data = NULL);
535
mads.s.agercbaa0602008-08-14 13:41:48 +0000536 /**
537 * Compiles the specified script using the specified file name
538 * object (typically a string) as the script's origin.
539 */
540 static Local<Script> Compile(Handle<String> source,
541 Handle<Value> file_name);
542
v8.team.kasperl727e9952008-09-02 14:56:44 +0000543 /**
544 * Runs the script returning the resulting value.
545 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 Local<Value> Run();
547};
548
549
550/**
551 * An error message.
552 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000553class EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 public:
555 Local<String> Get();
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000556 Local<String> GetSourceLine();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557
558 // TODO(1241256): Rewrite (or remove) this method. We don't want to
559 // deal with ownership of the returned string and we want to use
v8.team.kasperl727e9952008-09-02 14:56:44 +0000560 // JavaScript data structures exclusively.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 char* GetUnderline(char* source_line, char underline_char);
562
563 Handle<String> GetScriptResourceName();
564
565 // TODO(1240903): Remove this when no longer used in WebKit V8
566 // bindings.
567 Handle<Value> GetSourceData();
568
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000569 /**
570 * Returns the number, 1-based, of the line where the error occurred.
571 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 int GetLineNumber();
573
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000574 /**
575 * Returns the index within the script of the first character where
576 * the error occurred.
577 */
578 int GetStartPosition();
579
580 /**
581 * Returns the index within the script of the last character where
582 * the error occurred.
583 */
584 int GetEndPosition();
585
586 /**
587 * Returns the index within the line of the first character where
588 * the error occurred.
589 */
590 int GetStartColumn();
591
592 /**
593 * Returns the index within the line of the last character where
594 * the error occurred.
595 */
596 int GetEndColumn();
597
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 // TODO(1245381): Print to a string instead of on a FILE.
599 static void PrintCurrentStackTrace(FILE* out);
600};
601
602
603// --- V a l u e ---
604
605
606/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000607 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000609class EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 public:
611
612 /**
613 * Returns true if this value is the undefined value. See ECMA-262
614 * 4.3.10.
615 */
616 bool IsUndefined();
617
618 /**
619 * Returns true if this value is the null value. See ECMA-262
620 * 4.3.11.
621 */
622 bool IsNull();
623
624 /**
625 * Returns true if this value is true.
626 */
627 bool IsTrue();
628
629 /**
630 * Returns true if this value is false.
631 */
632 bool IsFalse();
633
634 /**
635 * Returns true if this value is an instance of the String type.
636 * See ECMA-262 8.4.
637 */
638 bool IsString();
639
640 /**
641 * Returns true if this value is a function.
642 */
643 bool IsFunction();
644
645 /**
646 * Returns true if this value is an array.
647 */
648 bool IsArray();
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 an object.
652 */
653 bool IsObject();
654
v8.team.kasperl727e9952008-09-02 14:56:44 +0000655 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 * Returns true if this value is boolean.
657 */
658 bool IsBoolean();
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000659
v8.team.kasperl727e9952008-09-02 14:56:44 +0000660 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 * Returns true if this value is a number.
662 */
663 bool IsNumber();
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 external.
667 */
668 bool IsExternal();
669
v8.team.kasperl727e9952008-09-02 14:56:44 +0000670 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 * Returns true if this value is a 32-bit signed integer.
672 */
673 bool IsInt32();
674
675 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);
990};
991
992
993enum PropertyAttribute {
994 None = 0,
995 ReadOnly = 1 << 0,
996 DontEnum = 1 << 1,
997 DontDelete = 1 << 2
998};
999
1000/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001001 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001003class EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004 public:
1005 bool Set(Handle<Value> key,
1006 Handle<Value> value,
1007 PropertyAttribute attribs = None);
1008 Local<Value> Get(Handle<Value> key);
1009
1010 // TODO(1245389): Replace the type-specific versions of these
1011 // functions with generic ones that accept a Handle<Value> key.
1012 bool Has(Handle<String> key);
1013 bool Delete(Handle<String> key);
1014 bool Has(uint32_t index);
1015 bool Delete(uint32_t index);
1016
1017 /**
1018 * Get the prototype object. This does not skip objects marked to
1019 * be skipped by __proto__ and it does not consult the security
1020 * handler.
1021 */
1022 Local<Value> GetPrototype();
1023
1024 /**
1025 * Call builtin Object.prototype.toString on this object.
1026 * This is different from Value::ToString() that may call
1027 * user-defined toString function. This one does not.
1028 */
1029 Local<String> ObjectProtoToString();
1030
kasper.lund212ac232008-07-16 07:07:30 +00001031 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001033 /** Gets the value in an internal field. */
1034 Local<Value> GetInternalField(int index);
1035 /** Sets the value in an internal field. */
1036 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037
1038 // Testers for local properties.
1039 bool HasRealNamedProperty(Handle<String> key);
1040 bool HasRealIndexedProperty(uint32_t index);
1041 bool HasRealNamedCallbackProperty(Handle<String> key);
1042
1043 /**
1044 * If result.IsEmpty() no real property was located in the prototype chain.
1045 * This means interceptors in the prototype chain are not called.
1046 */
1047 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1048
1049 /** Tests for a named lookup interceptor.*/
1050 bool HasNamedLookupInterceptor();
1051
kasper.lund212ac232008-07-16 07:07:30 +00001052 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 bool HasIndexedLookupInterceptor();
1054
1055
1056 static Local<Object> New();
1057 static Object* Cast(Value* obj);
1058 private:
1059 Object();
1060};
1061
1062
1063/**
1064 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1065 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001066class EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 public:
1068 uint32_t Length();
1069
1070 static Local<Array> New(int length = 0);
1071 static Array* Cast(Value* obj);
1072 private:
1073 Array();
1074};
1075
1076
1077/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001078 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001080class EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 public:
1082 Local<Object> NewInstance();
1083 Local<Object> NewInstance(int argc, Handle<Value> argv[]);
1084 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1085 void SetName(Handle<String> name);
1086 Handle<Value> GetName();
1087 static Function* Cast(Value* obj);
1088 private:
1089 Function();
1090};
1091
1092
1093/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001094 * A JavaScript value that wraps a c++ void*. This type of value is
1095 * mainly used to associate c++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 * objects.
1097 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001098class EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 public:
1100 static Local<External> New(void* value);
1101 static External* Cast(Value* obj);
1102 void* Value();
1103 private:
1104 External();
1105};
1106
1107
1108// --- T e m p l a t e s ---
1109
1110
1111/**
1112 * The superclass of object and function templates.
1113 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001114class EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 public:
1116 /** Adds a property to each instance created by this template.*/
1117 void Set(Handle<String> name, Handle<Data> value,
1118 PropertyAttribute attributes = None);
1119 inline void Set(const char* name, Handle<Data> value);
1120 private:
1121 Template();
1122
1123 friend class ObjectTemplate;
1124 friend class FunctionTemplate;
1125};
1126
1127
1128/**
1129 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001130 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 * including the receiver, the number and values of arguments, and
1132 * the holder of the function.
1133 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001134class EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 public:
1136 inline int Length() const;
1137 inline Local<Value> operator[](int i) const;
1138 inline Local<Function> Callee() const;
1139 inline Local<Object> This() const;
1140 inline Local<Object> Holder() const;
1141 inline bool IsConstructCall() const;
1142 inline Local<Value> Data() const;
1143 private:
1144 Arguments();
1145 friend class ImplementationUtilities;
1146 inline Arguments(Local<Value> data,
1147 Local<Object> holder,
1148 Local<Function> callee,
1149 bool is_construct_call,
1150 void** values, int length);
1151 Local<Value> data_;
1152 Local<Object> holder_;
1153 Local<Function> callee_;
1154 bool is_construct_call_;
1155 void** values_;
1156 int length_;
1157};
1158
1159
1160/**
1161 * The information passed to an accessor callback about the context
1162 * of the property access.
1163 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001164class EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 public:
1166 inline AccessorInfo(Local<Object> self,
1167 Local<Value> data,
1168 Local<Object> holder)
1169 : self_(self), data_(data), holder_(holder) { }
1170 inline Local<Value> Data() const;
1171 inline Local<Object> This() const;
1172 inline Local<Object> Holder() const;
1173 private:
1174 Local<Object> self_;
1175 Local<Value> data_;
1176 Local<Object> holder_;
1177};
1178
1179
1180typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1181
1182typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1183
1184/**
1185 * Accessor[Getter|Setter] are used as callback functions when
1186 * setting|getting a particular property. See objectTemplate::SetAccessor.
1187 */
1188typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1189 const AccessorInfo& info);
1190
1191
1192typedef void (*AccessorSetter)(Local<String> property,
1193 Local<Value> value,
1194 const AccessorInfo& info);
1195
1196
1197/**
1198 * NamedProperty[Getter|Setter] are used as interceptors on object.
1199 * See ObjectTemplate::SetNamedPropertyHandler.
1200 */
1201typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1202 const AccessorInfo& info);
1203
1204
1205/**
1206 * Returns the value if the setter intercepts the request.
1207 * Otherwise, returns an empty handle.
1208 */
1209typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1210 Local<Value> value,
1211 const AccessorInfo& info);
1212
1213
1214/**
1215 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001216 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 */
1218typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1219 const AccessorInfo& info);
1220
1221
1222/**
1223 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001224 * The return value is true if the property could be deleted and false
1225 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226 */
1227typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1228 const AccessorInfo& info);
1229
1230/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001231 * Returns an array containing the names of the properties the named
1232 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233 */
1234typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1235
v8.team.kasperl727e9952008-09-02 14:56:44 +00001236
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001238 * Returns the value of the property if the getter intercepts the
1239 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 */
1241typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1242 const AccessorInfo& info);
1243
1244
1245/**
1246 * Returns the value if the setter intercepts the request.
1247 * Otherwise, returns an empty handle.
1248 */
1249typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1250 Local<Value> value,
1251 const AccessorInfo& info);
1252
1253
1254/**
1255 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001256 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 */
1258typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1259 const AccessorInfo& info);
1260
1261/**
1262 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001263 * The return value is true if the property could be deleted and false
1264 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 */
1266typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1267 const AccessorInfo& info);
1268
v8.team.kasperl727e9952008-09-02 14:56:44 +00001269/**
1270 * Returns an array containing the indices of the properties the
1271 * indexed property getter intercepts.
1272 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1274
1275
1276/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001277 * Access control specifications.
1278 *
1279 * Some accessors should be accessible across contexts. These
1280 * accessors have an explicit access control parameter which specifies
1281 * the kind of cross-context access that should be allowed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 */
1283enum AccessControl {
1284 DEFAULT = 0,
1285 ALL_CAN_READ = 1,
1286 ALL_CAN_WRITE = 2
1287};
1288
1289
1290/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001291 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 */
1293enum AccessType {
1294 ACCESS_GET,
1295 ACCESS_SET,
1296 ACCESS_HAS,
1297 ACCESS_DELETE,
1298 ACCESS_KEYS
1299};
1300
v8.team.kasperl727e9952008-09-02 14:56:44 +00001301
1302/**
1303 * Returns true if cross-context access should be allowed to the named
1304 * property with the given key on the global object.
1305 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306typedef bool (*NamedSecurityCallback)(Local<Object> global,
1307 Local<Value> key,
1308 AccessType type,
1309 Local<Value> data);
1310
v8.team.kasperl727e9952008-09-02 14:56:44 +00001311
1312/**
1313 * Returns true if cross-context access should be allowed to the indexed
1314 * property with the given index on the global object.
1315 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1317 uint32_t index,
1318 AccessType type,
1319 Local<Value> data);
1320
1321
1322/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001323 * A FunctionTemplate is used to create functions at runtime. There
1324 * can only be one function created from a FunctionTemplate in a
1325 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 *
1327 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001328 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001330 * A FunctionTemplate has a corresponding instance template which is
1331 * used to create object instances when the function is used as a
1332 * constructor. Properties added to the instance template are added to
1333 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 *
1335 * A FunctionTemplate can have a prototype template. The prototype template
1336 * is used to create the prototype object of the function.
1337 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001338 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001340 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1342 * t->Set("func_property", v8::Number::New(1));
1343 *
1344 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1345 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1346 * proto_t->Set("proto_const", v8::Number::New(2));
1347 *
1348 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1349 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1350 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1351 * instance_t->Set("instance_property", Number::New(3));
1352 *
1353 * v8::Local<v8::Function> function = t->GetFunction();
1354 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001355 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 *
1357 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001358 * and "instance" for the instance object created above. The function
1359 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001361 * \code
1362 * func_property in function == true;
1363 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001365 * function.prototype.proto_method() invokes 'InvokeCallback'
1366 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001368 * instance instanceof function == true;
1369 * instance.instance_accessor calls 'InstanceAccessorCallback'
1370 * instance.instance_property == 3;
1371 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001373 * A FunctionTemplate can inherit from another one by calling the
1374 * FunctionTemplate::Inherit method. The following graph illustrates
1375 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001377 * \code
1378 * FunctionTemplate Parent -> Parent() . prototype -> { }
1379 * ^ ^
1380 * | Inherit(Parent) | .__proto__
1381 * | |
1382 * FunctionTemplate Child -> Child() . prototype -> { }
1383 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001385 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1386 * object of the Child() function has __proto__ pointing to the
1387 * Parent() function's prototype object. An instance of the Child
1388 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001390 * Let Parent be the FunctionTemplate initialized in the previous
1391 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001393 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 * Local<FunctionTemplate> parent = t;
1395 * Local<FunctionTemplate> child = FunctionTemplate::New();
1396 * child->Inherit(parent);
1397 *
1398 * Local<Function> child_function = child->GetFunction();
1399 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001400 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001402 * The Child function and Child instance will have the following
1403 * properties:
1404 *
1405 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001407 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001409 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001411class EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 public:
1413 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001414 static Local<FunctionTemplate> New(
1415 InvocationCallback callback = 0,
1416 Handle<Value> data = Handle<Value>(),
1417 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 /** Returns the unique function instance in the current execution context.*/
1419 Local<Function> GetFunction();
1420
v8.team.kasperl727e9952008-09-02 14:56:44 +00001421 /**
1422 * Set the call-handler callback for a FunctionTemplate. This
1423 * callback is called whenever the function created from this
1424 * FunctionTemplate is called.
1425 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 void SetCallHandler(InvocationCallback callback,
1427 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428
v8.team.kasperl727e9952008-09-02 14:56:44 +00001429 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 Local<ObjectTemplate> InstanceTemplate();
1431
1432 /** Causes the function template to inherit from a parent function template.*/
1433 void Inherit(Handle<FunctionTemplate> parent);
1434
1435 /**
1436 * A PrototypeTemplate is the template used to create the prototype object
1437 * of the function created by this template.
1438 */
1439 Local<ObjectTemplate> PrototypeTemplate();
1440
v8.team.kasperl727e9952008-09-02 14:56:44 +00001441
1442 /**
1443 * Set the class name of the FunctionTemplate. This is used for
1444 * printing objects created with the function created from the
1445 * FunctionTemplate as its constructor.
1446 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447 void SetClassName(Handle<String> name);
1448
1449 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001450 * Determines whether the __proto__ accessor ignores instances of
1451 * the function template. If instances of the function template are
1452 * ignored, __proto__ skips all instances and instead returns the
1453 * next object in the prototype chain.
1454 *
1455 * Call with a value of true to make the __proto__ accessor ignore
1456 * instances of the function template. Call with a value of false
1457 * to make the __proto__ accessor not ignore instances of the
1458 * function template. By default, instances of a function template
1459 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 */
1461 void SetHiddenPrototype(bool value);
1462
1463 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001464 * Returns true if the given object is an instance of this function
1465 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 */
1467 bool HasInstance(Handle<Value> object);
1468
1469 private:
1470 FunctionTemplate();
1471 void AddInstancePropertyAccessor(Handle<String> name,
1472 AccessorGetter getter,
1473 AccessorSetter setter,
1474 Handle<Value> data,
1475 AccessControl settings,
1476 PropertyAttribute attributes);
1477 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1478 NamedPropertySetter setter,
1479 NamedPropertyQuery query,
1480 NamedPropertyDeleter remover,
1481 NamedPropertyEnumerator enumerator,
1482 Handle<Value> data);
1483 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1484 IndexedPropertySetter setter,
1485 IndexedPropertyQuery query,
1486 IndexedPropertyDeleter remover,
1487 IndexedPropertyEnumerator enumerator,
1488 Handle<Value> data);
1489 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1490 Handle<Value> data);
1491
1492 friend class Context;
1493 friend class ObjectTemplate;
1494};
1495
1496
1497/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001498 * An ObjectTemplate is used to create objects at runtime.
1499 *
1500 * Properties added to an ObjectTemplate are added to each object
1501 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001503class EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001505 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 /** Creates a new instance of this template.*/
1509 Local<Object> NewInstance();
1510
1511 /**
1512 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001513 *
1514 * Whenever the property with the given name is accessed on objects
1515 * created from this ObjectTemplate the getter and setter callbacks
1516 * are called instead of getting and setting the property directly
1517 * on the JavaScript object.
1518 *
1519 * \param name The name of the property for which an accessor is added.
1520 * \param getter The callback to invoke when getting the property.
1521 * \param setter The callback to invoke when setting the property.
1522 * \param data A piece of data that will be passed to the getter and setter
1523 * callbacks whenever they are invoked.
1524 * \param settings Access control settings for the accessor. This is a bit
1525 * field consisting of one of more of
1526 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1527 * The default is to not allow cross-context access.
1528 * ALL_CAN_READ means that all cross-context reads are allowed.
1529 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1530 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1531 * cross-context access.
1532 * \param attribute The attributes of the property for which an accessor
1533 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 */
1535 void SetAccessor(Handle<String> name,
1536 AccessorGetter getter,
1537 AccessorSetter setter = 0,
1538 Handle<Value> data = Handle<Value>(),
1539 AccessControl settings = DEFAULT,
1540 PropertyAttribute attribute = None);
1541
1542 /**
1543 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001544 *
1545 * Whenever a named property is accessed on objects created from
1546 * this object template, the provided callback is invoked instead of
1547 * accessing the property directly on the JavaScript object.
1548 *
1549 * \param getter The callback to invoke when getting a property.
1550 * \param setter The callback to invoke when setting a property.
1551 * \param query The callback to invoke to check is an object has a property.
1552 * \param deleter The callback to invoke when deleting a property.
1553 * \param enumerator The callback to invoke to enumerate all the named
1554 * properties of an object.
1555 * \param data A piece of data that will be passed to the callbacks
1556 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557 */
1558 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1559 NamedPropertySetter setter = 0,
1560 NamedPropertyQuery query = 0,
1561 NamedPropertyDeleter deleter = 0,
1562 NamedPropertyEnumerator enumerator = 0,
1563 Handle<Value> data = Handle<Value>());
1564
1565 /**
1566 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001567 *
1568 * Whenever an indexed property is accessed on objects created from
1569 * this object template, the provided callback is invoked instead of
1570 * accessing the property directly on the JavaScript object.
1571 *
1572 * \param getter The callback to invoke when getting a property.
1573 * \param setter The callback to invoke when setting a property.
1574 * \param query The callback to invoke to check is an object has a property.
1575 * \param deleter The callback to invoke when deleting a property.
1576 * \param enumerator The callback to invoke to enumerate all the indexed
1577 * properties of an object.
1578 * \param data A piece of data that will be passed to the callbacks
1579 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 */
1581 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1582 IndexedPropertySetter setter = 0,
1583 IndexedPropertyQuery query = 0,
1584 IndexedPropertyDeleter deleter = 0,
1585 IndexedPropertyEnumerator enumerator = 0,
1586 Handle<Value> data = Handle<Value>());
1587 /**
1588 * Sets the callback to be used when calling instances created from
1589 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001590 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 * function.
1592 */
1593 void SetCallAsFunctionHandler(InvocationCallback callback,
1594 Handle<Value> data = Handle<Value>());
1595
v8.team.kasperl727e9952008-09-02 14:56:44 +00001596 /**
1597 * Mark object instances of the template as undetectable.
1598 *
1599 * In many ways, undetectable objects behave as though they are not
1600 * there. They behave like 'undefined' in conditionals and when
1601 * printed. However, properties can be accessed and called as on
1602 * normal objects.
1603 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 void MarkAsUndetectable();
1605
v8.team.kasperl727e9952008-09-02 14:56:44 +00001606 /**
1607 * Sets access check callbacks on the object template.
1608 *
1609 * When accessing properties on instances of this object template,
1610 * the access check callback will be called to determine whether or
1611 * not to allow cross-context access to the properties.
1612 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1614 IndexedSecurityCallback indexed_handler,
1615 Handle<Value> data = Handle<Value>());
1616
kasper.lund212ac232008-07-16 07:07:30 +00001617 /**
1618 * Gets the number of internal fields for objects generated from
1619 * this template.
1620 */
1621 int InternalFieldCount();
1622
1623 /**
1624 * Sets the number of internal fields for objects generated from
1625 * this template.
1626 */
1627 void SetInternalFieldCount(int value);
1628
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 private:
1630 ObjectTemplate();
1631 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1632 friend class FunctionTemplate;
1633};
1634
1635
1636/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001637 * A Signature specifies which receivers and arguments a function can
1638 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001640class EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 public:
1642 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1643 Handle<FunctionTemplate>(),
1644 int argc = 0,
1645 Handle<FunctionTemplate> argv[] = 0);
1646 private:
1647 Signature();
1648};
1649
1650
1651/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001652 * A utility for determining the type of objects based on the template
1653 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001655class EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656 public:
1657 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1658 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1659 int match(Handle<Value> value);
1660 private:
1661 TypeSwitch();
1662};
1663
1664
1665// --- E x t e n s i o n s ---
1666
1667
1668/**
1669 * Ignore
1670 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001671class EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 public:
1673 Extension(const char* name,
1674 const char* source = 0,
1675 int dep_count = 0,
1676 const char** deps = 0);
1677 virtual ~Extension() { }
1678 virtual v8::Handle<v8::FunctionTemplate>
1679 GetNativeFunction(v8::Handle<v8::String> name) {
1680 return v8::Handle<v8::FunctionTemplate>();
1681 }
1682
1683 const char* name() { return name_; }
1684 const char* source() { return source_; }
1685 int dependency_count() { return dep_count_; }
1686 const char** dependencies() { return deps_; }
1687 void set_auto_enable(bool value) { auto_enable_ = value; }
1688 bool auto_enable() { return auto_enable_; }
1689
1690 private:
1691 const char* name_;
1692 const char* source_;
1693 int dep_count_;
1694 const char** deps_;
1695 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001696
1697 // Disallow copying and assigning.
1698 Extension(const Extension&);
1699 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700};
1701
1702
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001703void EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704
1705
1706/**
1707 * Ignore
1708 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001709class EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710 public:
1711 inline DeclareExtension(Extension* extension) {
1712 RegisterExtension(extension);
1713 }
1714};
1715
1716
1717// --- S t a t i c s ---
1718
1719
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001720Handle<Primitive> EXPORT Undefined();
1721Handle<Primitive> EXPORT Null();
1722Handle<Boolean> EXPORT True();
1723Handle<Boolean> EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724
1725
1726/**
1727 * A set of constraints that specifies the limits of the runtime's
1728 * memory use.
1729 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001730class EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731 public:
1732 ResourceConstraints();
1733 int max_young_space_size() { return max_young_space_size_; }
1734 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
1735 int max_old_space_size() { return max_old_space_size_; }
1736 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
1737 uint32_t* stack_limit() { return stack_limit_; }
1738 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1739 private:
1740 int max_young_space_size_;
1741 int max_old_space_size_;
1742 uint32_t* stack_limit_;
1743};
1744
1745
1746bool SetResourceConstraints(ResourceConstraints* constraints);
1747
1748
1749// --- E x c e p t i o n s ---
1750
1751
1752typedef void (*FatalErrorCallback)(const char* location, const char* message);
1753
1754
1755typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1756
1757
1758/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001759 * Schedules an exception to be thrown when returning to JavaScript. When an
1760 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001762 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001764Handle<Value> EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765
1766/**
1767 * Create new error objects by calling the corresponding error object
1768 * constructor with the message.
1769 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001770class EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 public:
1772 static Local<Value> RangeError(Handle<String> message);
1773 static Local<Value> ReferenceError(Handle<String> message);
1774 static Local<Value> SyntaxError(Handle<String> message);
1775 static Local<Value> TypeError(Handle<String> message);
1776 static Local<Value> Error(Handle<String> message);
1777};
1778
1779
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780// --- C o u n t e r s C a l l b a c k s
1781
1782typedef int* (*CounterLookupCallback)(const wchar_t* name);
1783
1784// --- 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 ---
1785typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1786 AccessType type,
1787 Local<Value> data);
1788
1789// --- 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
1790
1791/**
1792 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001793 * before and after a major garbage collection. Allocations are not
1794 * allowed in the callback function, you therefore cannot manipulate
1795 * objects (set or delete properties for example) since it is possible
1796 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 */
1798typedef void (*GCCallback)();
1799
1800
1801// --- C o n t e x t G e n e r a t o r
1802
1803/**
1804 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001805 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807typedef Persistent<Context> (*ContextGenerator)();
1808
1809
1810/**
1811 * Container class for static utility functions.
1812 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001813class EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001815 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 static void SetFatalErrorHandler(FatalErrorCallback that);
1817
v8.team.kasperl727e9952008-09-02 14:56:44 +00001818 /**
1819 * Ignore out-of-memory exceptions.
1820 *
1821 * V8 running out of memory is treated as a fatal error by default.
1822 * This means that the fatal error handler is called and that V8 is
1823 * terminated.
1824 *
1825 * IgnoreOutOfMemoryException can be used to not treat a
1826 * out-of-memory situation as a fatal error. This way, the contexts
1827 * that did not cause the out of memory problem might be able to
1828 * continue execution.
1829 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830 static void IgnoreOutOfMemoryException();
1831
v8.team.kasperl727e9952008-09-02 14:56:44 +00001832 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001833 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001834 * fatal errors such as out-of-memory situations.
1835 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 static bool IsDead();
1837
1838 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001839 * Adds a message listener.
1840 *
1841 * The same message listener can be added more than once and it that
1842 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 */
1844 static bool AddMessageListener(MessageCallback that,
1845 Handle<Value> data = Handle<Value>());
1846
1847 /**
1848 * Remove all message listeners from the specified callback function.
1849 */
1850 static void RemoveMessageListeners(MessageCallback that);
1851
1852 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001853 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 */
1855 static void SetFlagsFromString(const char* str, int length);
1856
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001857 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001858 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001859 */
1860 static void SetFlagsFromCommandLine(int* argc,
1861 char** argv,
1862 bool remove_flags);
1863
kasper.lund7276f142008-07-30 08:49:36 +00001864 /** Get the version string. */
1865 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866
1867 /**
1868 * Enables the host application to provide a mechanism for recording
1869 * statistics counters.
1870 */
1871 static void SetCounterFunction(CounterLookupCallback);
1872
1873 /**
1874 * Enables the computation of a sliding window of states. The sliding
1875 * window information is recorded in statistics counters.
1876 */
1877 static void EnableSlidingStateWindow();
1878
1879 /** Callback function for reporting failed access checks.*/
1880 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
1881
1882 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001883 * Enables the host application to receive a notification before a
1884 * major garbage colletion. Allocations are not allowed in the
1885 * callback function, you therefore cannot manipulate objects (set
1886 * or delete properties for example) since it is possible such
1887 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 */
1889 static void SetGlobalGCPrologueCallback(GCCallback);
1890
1891 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001892 * Enables the host application to receive a notification after a
1893 * major garbage collection. Allocations are not allowed in the
1894 * callback function, you therefore cannot manipulate objects (set
1895 * or delete properties for example) since it is possible such
1896 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 */
1898 static void SetGlobalGCEpilogueCallback(GCCallback);
1899
1900 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001901 * Allows the host application to group objects together. If one
1902 * object in the group is alive, all objects in the group are alive.
1903 * After each garbage collection, object groups are removed. It is
1904 * intended to be used in the before-garbage-collection callback
1905 * function for istance to simulate DOM tree connections among JS
1906 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 */
1908 static void AddObjectToGroup(void* id, Persistent<Object> obj);
1909
1910 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001911 * Initializes from snapshot if possible. Otherwise, attempts to
1912 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 */
1914 static bool Initialize();
1915
kasper.lund7276f142008-07-30 08:49:36 +00001916 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001917 * Adjusts the amount of registered external memory. Used to give
1918 * V8 an indication of the amount of externally allocated memory
1919 * that is kept alive by JavaScript objects. V8 uses this to decide
1920 * when to perform global garbage collections. Registering
1921 * externally allocated memory will trigger global garbage
1922 * collections more often than otherwise in an attempt to garbage
1923 * collect the JavaScript objects keeping the externally allocated
1924 * memory alive.
1925 *
1926 * \param change_in_bytes the change in externally allocated memory
1927 * that is kept alive by JavaScript objects.
1928 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00001929 */
1930 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
1931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 private:
1933 V8();
1934
1935 static void** GlobalizeReference(void** handle);
1936 static void DisposeGlobal(void** global_handle);
1937 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
1938 static void ClearWeak(void** global_handle);
1939 static bool IsGlobalNearDeath(void** global_handle);
1940 static bool IsGlobalWeak(void** global_handle);
1941
1942 template <class T> friend class Handle;
1943 template <class T> friend class Local;
1944 template <class T> friend class Persistent;
1945 friend class Context;
1946};
1947
1948
1949/**
1950 * An external exception handler.
1951 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001952class EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 public:
1954
1955 /**
1956 * Creates a new try/catch block and registers it with v8.
1957 */
1958 TryCatch();
1959
1960 /**
1961 * Unregisters and deletes this try/catch block.
1962 */
1963 ~TryCatch();
1964
1965 /**
1966 * Returns true if an exception has been caught by this try/catch block.
1967 */
1968 bool HasCaught();
1969
1970 /**
1971 * Returns the exception caught by this try/catch block. If no exception has
1972 * been caught an empty handle is returned.
1973 *
1974 * The returned handle is valid until this TryCatch block has been destroyed.
1975 */
1976 Local<Value> Exception();
1977
1978 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001979 * Returns the message associated with this exception. If there is
1980 * no message associated an empty handle is returned.
1981 *
1982 * The returned handle is valid until this TryCatch block has been
1983 * destroyed.
1984 */
1985 Local<v8::Message> Message();
1986
1987 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 * Clears any exceptions that may have been caught by this try/catch block.
1989 * After this method has been called, HasCaught() will return false.
1990 *
1991 * It is not necessary to clear a try/catch block before using it again; if
1992 * another exception is thrown the previously caught exception will just be
1993 * overwritten. However, it is often a good idea since it makes it easier
1994 * to determine which operation threw a given exception.
1995 */
1996 void Reset();
1997
v8.team.kasperl727e9952008-09-02 14:56:44 +00001998 /**
1999 * Set verbosity of the external exception handler.
2000 *
2001 * By default, exceptions that are caught by an external exception
2002 * handler are not reported. Call SetVerbose with true on an
2003 * external exception handler to have exceptions caught by the
2004 * handler reported as if they were not caught.
2005 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 void SetVerbose(bool value);
2007
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002008 /**
2009 * Set whether or not this TryCatch should capture a Message object
2010 * which holds source information about where the exception
2011 * occurred. True by default.
2012 */
2013 void SetCaptureMessage(bool value);
2014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 public:
2016 TryCatch* next_;
2017 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002018 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002020 bool capture_message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021};
2022
2023
2024// --- C o n t e x t ---
2025
2026
2027/**
2028 * Ignore
2029 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002030class EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 public:
2032 ExtensionConfiguration(int name_count, const char* names[])
2033 : name_count_(name_count), names_(names) { }
2034 private:
2035 friend class ImplementationUtilities;
2036 int name_count_;
2037 const char** names_;
2038};
2039
2040
2041/**
2042 * A sandboxed execution context with its own set of built-in objects
2043 * and functions.
2044 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002045class EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 public:
2047 Local<Object> Global();
2048
v8.team.kasperl727e9952008-09-02 14:56:44 +00002049 /** Creates a new context. */
2050 static Persistent<Context> New(
2051 ExtensionConfiguration* extensions = 0,
2052 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2053 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054
kasper.lund44510672008-07-25 07:37:58 +00002055 /** Returns the last entered context. */
2056 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057
kasper.lund44510672008-07-25 07:37:58 +00002058 /** Returns the context that is on the top of the stack. */
2059 static Local<Context> GetCurrent();
2060
2061 /** Returns the security context that is currently used. */
2062 static Local<Context> GetCurrentSecurityContext();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063
2064 /**
2065 * Sets the security token for the context. To access an object in
2066 * another context, the security tokens must match.
2067 */
2068 void SetSecurityToken(Handle<Value> token);
2069
2070 /** Returns the security token of this context.*/
2071 Handle<Value> GetSecurityToken();
2072
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 /**
2074 * Enter this context. After entering a context, all code compiled
2075 * and run is compiled and run in this context. If another context
2076 * is already entered, this old context is saved so it can be
2077 * restored when the new context is exited.
2078 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002080
2081 /**
2082 * Exit this context. Exiting the current context restores the
2083 * context that was in place when entering the current context.
2084 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 void Exit();
2086
v8.team.kasperl727e9952008-09-02 14:56:44 +00002087 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 bool HasOutOfMemoryException();
2089
v8.team.kasperl727e9952008-09-02 14:56:44 +00002090 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 static bool InContext();
2092
v8.team.kasperl727e9952008-09-02 14:56:44 +00002093 /** Returns true if V8 has a current security context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 static bool InSecurityContext();
2095
2096 /**
2097 * Stack-allocated class which sets the execution context for all
2098 * operations executed within a local scope.
2099 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002100 class EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 public:
2102 inline Scope(Handle<Context> context) : context_(context) {
2103 context_->Enter();
2104 }
2105 inline ~Scope() { context_->Exit(); }
2106 private:
2107 Handle<Context> context_;
2108 };
2109
2110 private:
2111 friend class Value;
2112 friend class Script;
2113 friend class Object;
2114 friend class Function;
2115};
2116
2117
2118/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002119 * Multiple threads in V8 are allowed, but only one thread at a time
2120 * is allowed to use V8. The definition of 'using V8' includes
2121 * accessing handles or holding onto object pointers obtained from V8
2122 * handles. It is up to the user of V8 to ensure (perhaps with
2123 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124 *
2125 * If you wish to start using V8 in a thread you can do this by constructing
2126 * a v8::Locker object. After the code using V8 has completed for the
2127 * current thread you can call the destructor. This can be combined
2128 * with C++ scope-based construction as follows:
2129 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 * ...
2132 * {
2133 * v8::Locker locker;
2134 * ...
2135 * // Code using V8 goes here.
2136 * ...
2137 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002138 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139 *
2140 * If you wish to stop using V8 in a thread A you can do this by either
2141 * by destroying the v8::Locker object as above or by constructing a
2142 * v8::Unlocker object:
2143 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002144 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 * {
2146 * v8::Unlocker unlocker;
2147 * ...
2148 * // Code not using V8 goes here while V8 can run in another thread.
2149 * ...
2150 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002151 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 *
2153 * The Unlocker object is intended for use in a long-running callback
2154 * from V8, where you want to release the V8 lock for other threads to
2155 * use.
2156 *
2157 * The v8::Locker is a recursive lock. That is, you can lock more than
2158 * once in a given thread. This can be useful if you have code that can
2159 * be called either from code that holds the lock or from code that does
2160 * not. The Unlocker is not recursive so you can not have several
2161 * Unlockers on the stack at once, and you can not use an Unlocker in a
2162 * thread that is not inside a Locker's scope.
2163 *
2164 * An unlocker will unlock several lockers if it has to and reinstate
2165 * the correct depth of locking on its destruction. eg.:
2166 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002167 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 * // V8 not locked.
2169 * {
2170 * v8::Locker locker;
2171 * // V8 locked.
2172 * {
2173 * v8::Locker another_locker;
2174 * // V8 still locked (2 levels).
2175 * {
2176 * v8::Unlocker unlocker;
2177 * // V8 not locked.
2178 * }
2179 * // V8 locked again (2 levels).
2180 * }
2181 * // V8 still locked (1 level).
2182 * }
2183 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002184 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 */
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002186class EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 public:
2188 Unlocker();
2189 ~Unlocker();
2190};
2191
2192
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002193class EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 public:
2195 Locker();
2196 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197
2198 /**
2199 * Start preemption.
2200 *
2201 * When preemption is started, a timer is fired every n milli seconds
2202 * that will switch between multiple threads that are in contention
2203 * for the V8 lock.
2204 */
2205 static void StartPreemption(int every_n_ms);
2206
2207 /**
2208 * Stop preemption.
2209 */
2210 static void StopPreemption();
2211
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002212 /**
2213 * Returns whether or not the locker is locked by the current thread.
2214 */
2215 static bool IsLocked();
2216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 private:
2218 bool has_lock_;
2219 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002220
2221 // Disallow copying and assigning.
2222 Locker(const Locker&);
2223 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224};
2225
2226
2227
2228// --- I m p l e m e n t a t i o n ---
2229
2230template <class T>
2231Handle<T>::Handle() : val_(0) { }
2232
2233
2234template <class T>
2235Local<T>::Local() : Handle<T>() { }
2236
2237
2238template <class T>
2239Local<T> Local<T>::New(Handle<T> that) {
2240 if (that.IsEmpty()) return Local<T>();
2241 void** p = reinterpret_cast<void**>(*that);
2242 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2243}
2244
2245
2246template <class T>
2247Persistent<T> Persistent<T>::New(Handle<T> that) {
2248 if (that.IsEmpty()) return Persistent<T>();
2249 void** p = reinterpret_cast<void**>(*that);
2250 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2251}
2252
2253
2254template <class T>
2255bool Persistent<T>::IsNearDeath() {
2256 if (this->IsEmpty()) return false;
2257 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2258}
2259
2260
2261template <class T>
2262bool Persistent<T>::IsWeak() {
2263 if (this->IsEmpty()) return false;
2264 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2265}
2266
2267
2268template <class T>
2269void Persistent<T>::Dispose() {
2270 if (this->IsEmpty()) return;
2271 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2272}
2273
2274
2275template <class T>
2276Persistent<T>::Persistent() : Handle<T>() { }
2277
2278template <class T>
2279void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2280 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2281}
2282
2283template <class T>
2284void Persistent<T>::ClearWeak() {
2285 V8::ClearWeak(reinterpret_cast<void**>(**this));
2286}
2287
2288template <class T>
2289T* Handle<T>::operator->() {
2290 return val_;
2291}
2292
2293
2294template <class T>
2295T* Handle<T>::operator*() {
2296 return val_;
2297}
2298
2299
2300Local<Value> Arguments::operator[](int i) const {
2301 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2302 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2303}
2304
2305
2306Local<Function> Arguments::Callee() const {
2307 return callee_;
2308}
2309
2310
2311Local<Object> Arguments::This() const {
2312 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2313}
2314
2315
2316Local<Object> Arguments::Holder() const {
2317 return holder_;
2318}
2319
2320
2321Local<Value> Arguments::Data() const {
2322 return data_;
2323}
2324
2325
2326bool Arguments::IsConstructCall() const {
2327 return is_construct_call_;
2328}
2329
2330
2331int Arguments::Length() const {
2332 return length_;
2333}
2334
2335
2336Local<Value> AccessorInfo::Data() const {
2337 return data_;
2338}
2339
2340
2341Local<Object> AccessorInfo::This() const {
2342 return self_;
2343}
2344
2345
2346Local<Object> AccessorInfo::Holder() const {
2347 return holder_;
2348}
2349
2350
2351template <class T>
2352Local<T> HandleScope::Close(Handle<T> value) {
2353 void** after = RawClose(reinterpret_cast<void**>(*value));
2354 return Local<T>(reinterpret_cast<T*>(after));
2355}
2356
mads.s.agercbaa0602008-08-14 13:41:48 +00002357Handle<Value> ScriptOrigin::ResourceName() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358 return resource_name_;
2359}
2360
2361
2362Handle<Integer> ScriptOrigin::ResourceLineOffset() {
2363 return resource_line_offset_;
2364}
2365
2366
2367Handle<Integer> ScriptOrigin::ResourceColumnOffset() {
2368 return resource_column_offset_;
2369}
2370
2371
2372Handle<Boolean> Boolean::New(bool value) {
2373 return value ? True() : False();
2374}
2375
2376
2377void Template::Set(const char* name, v8::Handle<Data> value) {
2378 Set(v8::String::New(name), value);
2379}
2380
2381
2382/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002383 * \example shell.cc
2384 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385 * command-line and executes them.
2386 */
2387
2388
2389/**
2390 * \example process.cc
2391 */
2392
2393
2394} // namespace v8
2395
2396
2397#undef EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002398#undef EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399#undef TYPE_CHECK
2400
2401
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002402#endif // V8_H_