blob: 068a1cdef58ee29d7dfdb0b52f1884f9ae6e757b [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
ager@chromium.org5ec48922009-05-05 07:25:34 +000044typedef signed char int8_t;
45typedef unsigned char uint8_t;
46typedef short int16_t; // NOLINT
47typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048typedef int int32_t;
49typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000050typedef __int64 int64_t;
51typedef unsigned __int64 uint64_t;
52// intptr_t is defined in crtdefs.h through stdio.h.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000053
54// Setup for Windows DLL export/import. When building the V8 DLL the
55// BUILDING_V8_SHARED needs to be defined. When building a program which uses
56// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
57// static library or building a program which uses the V8 static library neither
58// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000059// The reason for having both V8EXPORT and V8EXPORT_INLINE is that classes which
60// have their code inside this header file need to have __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061// when building the DLL but cannot have __declspec(dllimport) when building
62// a program which uses the DLL.
63#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
64#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
65 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066#endif
67
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __declspec(dllexport)
70#define V8EXPORT_INLINE __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000071#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000072#define V8EXPORT __declspec(dllimport)
73#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000074#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000075#define V8EXPORT
76#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000077#endif // BUILDING_V8_SHARED
78
79#else // _WIN32
80
81#include <stdint.h>
82
83// Setup for Linux shared library export. There is no need to destinguish
84// neither between building or using the V8 shared library nor between using
85// the shared or static V8 library as there is on Windows. Therefore there is
ager@chromium.org7c537e22008-10-16 08:43:32 +000086// no checking of BUILDING_V8_SHARED and USING_V8_SHARED.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000087#if defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000088#define V8EXPORT __attribute__ ((visibility("default")))
89#define V8EXPORT_INLINE __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000090#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000091#define V8EXPORT
92#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000093#endif // defined(__GNUC__) && (__GNUC__ >= 4)
94
95#endif // _WIN32
96
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000098 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 */
100namespace v8 {
101
102class Context;
103class String;
104class Value;
105class Utils;
106class Number;
107class Object;
108class Array;
109class Int32;
110class Uint32;
111class External;
112class Primitive;
113class Boolean;
114class Integer;
115class Function;
116class Date;
117class ImplementationUtilities;
118class Signature;
119template <class T> class Handle;
120template <class T> class Local;
121template <class T> class Persistent;
122class FunctionTemplate;
123class ObjectTemplate;
124class Data;
125
126
127// --- W e a k H a n d l e s
128
129
130/**
131 * A weak reference callback function.
132 *
133 * \param object the weak global object to be reclaimed by the garbage collector
134 * \param parameter the value passed in when making the weak global object
135 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000136typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 void* parameter);
138
139
140// --- H a n d l e s ---
141
142#define TYPE_CHECK(T, S) \
143 while (false) { \
144 *(static_cast<T**>(0)) = static_cast<S*>(0); \
145 }
146
147/**
148 * An object reference managed by the v8 garbage collector.
149 *
150 * All objects returned from v8 have to be tracked by the garbage
151 * collector so that it knows that the objects are still alive. Also,
152 * because the garbage collector may move objects, it is unsafe to
153 * point directly to an object. Instead, all objects are stored in
154 * handles which are known by the garbage collector and updated
155 * whenever an object moves. Handles should always be passed by value
156 * (except in cases like out-parameters) and they should never be
157 * allocated on the heap.
158 *
159 * There are two types of handles: local and persistent handles.
160 * Local handles are light-weight and transient and typically used in
161 * local operations. They are managed by HandleScopes. Persistent
162 * handles can be used when storing objects across several independent
163 * operations and have to be explicitly deallocated when they're no
164 * longer used.
165 *
166 * It is safe to extract the object stored in the handle by
167 * dereferencing the handle (for instance, to extract the Object* from
168 * an Handle<Object>); the value will still be governed by a handle
169 * behind the scenes and the same rules apply to these values as to
170 * their handles.
171 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000172template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 public:
174
175 /**
176 * Creates an empty handle.
177 */
178 Handle();
179
180 /**
181 * Creates a new handle for the specified value.
182 */
183 explicit Handle(T* val) : val_(val) { }
184
185 /**
186 * Creates a handle for the contents of the specified handle. This
187 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000188 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 * incompatible handles, for instance from a Handle<String> to a
190 * Handle<Number> it will cause a compiletime error. Assigning
191 * between compatible handles, for instance assigning a
192 * Handle<String> to a variable declared as Handle<Value>, is legal
193 * because String is a subclass of Value.
194 */
195 template <class S> inline Handle(Handle<S> that)
196 : val_(reinterpret_cast<T*>(*that)) {
197 /**
198 * This check fails when trying to convert between incompatible
199 * handles. For example, converting from a Handle<String> to a
200 * Handle<Number>.
201 */
202 TYPE_CHECK(T, S);
203 }
204
205 /**
206 * Returns true if the handle is empty.
207 */
ager@chromium.org32912102009-01-16 10:38:43 +0000208 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209
ager@chromium.org32912102009-01-16 10:38:43 +0000210 T* operator->() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211
ager@chromium.org32912102009-01-16 10:38:43 +0000212 T* operator*() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213
214 /**
215 * Sets the handle to be empty. IsEmpty() will then return true.
216 */
217 void Clear() { this->val_ = 0; }
218
219 /**
220 * Checks whether two handles are the same.
221 * Returns true if both are empty, or if the objects
222 * to which they refer are identical.
223 * The handles' references are not checked.
224 */
ager@chromium.org32912102009-01-16 10:38:43 +0000225 template <class S> bool operator==(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 void** a = reinterpret_cast<void**>(**this);
227 void** b = reinterpret_cast<void**>(*that);
228 if (a == 0) return b == 0;
229 if (b == 0) return false;
230 return *a == *b;
231 }
232
233 /**
234 * Checks whether two handles are different.
235 * Returns true if only one of the handles is empty, or if
236 * the objects to which they refer are different.
237 * The handles' references are not checked.
238 */
ager@chromium.org32912102009-01-16 10:38:43 +0000239 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 return !operator==(that);
241 }
242
243 template <class S> static inline Handle<T> Cast(Handle<S> that) {
244 if (that.IsEmpty()) return Handle<T>();
245 return Handle<T>(T::Cast(*that));
246 }
247
248 private:
249 T* val_;
250};
251
252
253/**
254 * A light-weight stack-allocated object handle. All operations
255 * that return objects from within v8 return them in local handles. They
256 * are created within HandleScopes, and all local handles allocated within a
257 * handle scope are destroyed when the handle scope is destroyed. Hence it
258 * is not necessary to explicitly deallocate local handles.
259 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000260template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 public:
262 Local();
263 template <class S> inline Local(Local<S> that)
264 : Handle<T>(reinterpret_cast<T*>(*that)) {
265 /**
266 * This check fails when trying to convert between incompatible
267 * handles. For example, converting from a Handle<String> to a
268 * Handle<Number>.
269 */
270 TYPE_CHECK(T, S);
271 }
272 template <class S> inline Local(S* that) : Handle<T>(that) { }
273 template <class S> static inline Local<T> Cast(Local<S> that) {
274 if (that.IsEmpty()) return Local<T>();
275 return Local<T>(T::Cast(*that));
276 }
277
278 /** Create a local handle for the content of another handle.
279 * The referee is kept alive by the local handle even when
280 * the original handle is destroyed/disposed.
281 */
282 static Local<T> New(Handle<T> that);
283};
284
285
286/**
287 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000288 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 * allocated, a Persistent handle remains valid until it is explicitly
290 * disposed.
291 *
292 * A persistent handle contains a reference to a storage cell within
293 * the v8 engine which holds an object value and which is updated by
294 * the garbage collector whenever the object is moved. A new storage
295 * cell can be created using Persistent::New and existing handles can
296 * be disposed using Persistent::Dispose. Since persistent handles
297 * are passed by value you may have many persistent handle objects
298 * that point to the same storage cell. For instance, if you pass a
299 * persistent handle as an argument to a function you will not get two
300 * different storage cells but rather two references to the same
301 * storage cell.
302 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000303template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 public:
305
306 /**
307 * Creates an empty persistent handle that doesn't point to any
308 * storage cell.
309 */
310 Persistent();
311
312 /**
313 * Creates a persistent handle for the same storage cell as the
314 * specified handle. This constructor allows you to pass persistent
315 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000316 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000318 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 * between compatible persistent handles, for instance assigning a
320 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000321 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 */
323 template <class S> inline Persistent(Persistent<S> that)
324 : Handle<T>(reinterpret_cast<T*>(*that)) {
325 /**
326 * This check fails when trying to convert between incompatible
327 * handles. For example, converting from a Handle<String> to a
328 * Handle<Number>.
329 */
330 TYPE_CHECK(T, S);
331 }
332
333 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
334
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000335 /**
336 * "Casts" a plain handle which is known to be a persistent handle
337 * to a persistent handle.
338 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 template <class S> explicit inline Persistent(Handle<S> that)
340 : Handle<T>(*that) { }
341
342 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
343 if (that.IsEmpty()) return Persistent<T>();
344 return Persistent<T>(T::Cast(*that));
345 }
346
347 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000348 * Creates a new persistent handle for an existing local or
349 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 */
351 static Persistent<T> New(Handle<T> that);
352
353 /**
354 * Releases the storage cell referenced by this persistent handle.
355 * Does not remove the reference to the cell from any handles.
356 * This handle's reference, and any any other references to the storage
357 * cell remain and IsEmpty will still return false.
358 */
359 void Dispose();
360
361 /**
362 * Make the reference to this object weak. When only weak handles
363 * refer to the object, the garbage collector will perform a
364 * callback to the given V8::WeakReferenceCallback function, passing
365 * it the object reference and the given parameters.
366 */
367 void MakeWeak(void* parameters, WeakReferenceCallback callback);
368
369 /** Clears the weak reference to this object.*/
370 void ClearWeak();
371
372 /**
373 *Checks if the handle holds the only reference to an object.
374 */
ager@chromium.org32912102009-01-16 10:38:43 +0000375 bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Returns true if the handle's reference is weak.
379 */
ager@chromium.org32912102009-01-16 10:38:43 +0000380 bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381
382 private:
383 friend class ImplementationUtilities;
384 friend class ObjectTemplate;
385};
386
387
v8.team.kasperl727e9952008-09-02 14:56:44 +0000388 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 * A stack-allocated class that governs a number of local handles.
390 * After a handle scope has been created, all local handles will be
391 * allocated within that handle scope until either the handle scope is
392 * deleted or another handle scope is created. If there is already a
393 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000394 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395 * new handles will again be allocated in the original handle scope.
396 *
397 * After the handle scope of a local handle has been deleted the
398 * garbage collector will no longer track the object stored in the
399 * handle and may deallocate it. The behavior of accessing a handle
400 * for which the handle scope has been deleted is undefined.
401 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000402class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000404 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000406 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409 * Closes the handle scope and returns the value as a handle in the
410 * previous scope, which is the new current scope after the call.
411 */
412 template <class T> Local<T> Close(Handle<T> value);
413
414 /**
415 * Counts the number of allocated handles.
416 */
417 static int NumberOfHandles();
418
419 /**
420 * Creates a new handle with the given value.
421 */
422 static void** CreateHandle(void* value);
423
424 private:
425 // Make it impossible to create heap-allocated or illegal handle
426 // scopes by disallowing certain operations.
427 HandleScope(const HandleScope&);
428 void operator=(const HandleScope&);
429 void* operator new(size_t size);
430 void operator delete(void*, size_t);
431
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000432 // This Data class is accessible internally through a typedef in the
433 // ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000434 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 public:
436 int extensions;
437 void** next;
438 void** limit;
439 inline void Initialize() {
440 extensions = -1;
441 next = limit = NULL;
442 }
443 };
444
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000445 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000447 // Allow for the active closing of HandleScopes which allows to pass a handle
448 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 bool is_closed_;
450 void** RawClose(void** value);
451
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 friend class ImplementationUtilities;
453};
454
455
456// --- S p e c i a l o b j e c t s ---
457
458
459/**
460 * The superclass of values and API object templates.
461 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000462class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 private:
464 Data();
465};
466
467
468/**
469 * Pre-compilation data that can be associated with a script. This
470 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000471 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000472 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000474class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 public:
476 virtual ~ScriptData() { }
477 static ScriptData* PreCompile(const char* input, int length);
478 static ScriptData* New(unsigned* data, int length);
479
480 virtual int Length() = 0;
481 virtual unsigned* Data() = 0;
482};
483
484
485/**
486 * The origin, within a file, of a script.
487 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000488class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000490 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 Handle<Integer> resource_line_offset = Handle<Integer>(),
492 Handle<Integer> resource_column_offset = Handle<Integer>())
493 : resource_name_(resource_name),
494 resource_line_offset_(resource_line_offset),
495 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000496 inline Handle<Value> ResourceName() const;
497 inline Handle<Integer> ResourceLineOffset() const;
498 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000500 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 Handle<Integer> resource_line_offset_;
502 Handle<Integer> resource_column_offset_;
503};
504
505
506/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000507 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000509class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 public:
511
512 /**
513 * Compiles the specified script. The ScriptOrigin* and ScriptData*
514 * parameters are owned by the caller of Script::Compile. No
515 * references to these objects are kept after compilation finishes.
516 */
517 static Local<Script> Compile(Handle<String> source,
518 ScriptOrigin* origin = NULL,
519 ScriptData* pre_data = NULL);
520
mads.s.agercbaa0602008-08-14 13:41:48 +0000521 /**
522 * Compiles the specified script using the specified file name
523 * object (typically a string) as the script's origin.
524 */
525 static Local<Script> Compile(Handle<String> source,
526 Handle<Value> file_name);
527
v8.team.kasperl727e9952008-09-02 14:56:44 +0000528 /**
529 * Runs the script returning the resulting value.
530 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532
533 /**
534 * Returns the script id value.
535 */
536 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000537
538 /**
539 * Associate an additional data object with the script. This is mainly used
540 * with the debugger as this data object is only available through the
541 * debugger API.
542 */
543 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544};
545
546
547/**
548 * An error message.
549 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000550class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000552 Local<String> Get() const;
553 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000555 /**
556 * Returns the resource name for the script from where the function causing
557 * the error originates.
558 */
ager@chromium.org32912102009-01-16 10:38:43 +0000559 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000561 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000562 * Returns the resource data for the script from where the function causing
563 * the error originates.
564 */
565 Handle<Value> GetScriptData() const;
566
567 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000568 * Returns the number, 1-based, of the line where the error occurred.
569 */
ager@chromium.org32912102009-01-16 10:38:43 +0000570 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000572 /**
573 * Returns the index within the script of the first character where
574 * the error occurred.
575 */
ager@chromium.org32912102009-01-16 10:38:43 +0000576 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000577
578 /**
579 * Returns the index within the script of the last character where
580 * the error occurred.
581 */
ager@chromium.org32912102009-01-16 10:38:43 +0000582 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000583
584 /**
585 * Returns the index within the line of the first character where
586 * the error occurred.
587 */
ager@chromium.org32912102009-01-16 10:38:43 +0000588 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000589
590 /**
591 * Returns the index within the line of the last character where
592 * the error occurred.
593 */
ager@chromium.org32912102009-01-16 10:38:43 +0000594 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 // TODO(1245381): Print to a string instead of on a FILE.
597 static void PrintCurrentStackTrace(FILE* out);
598};
599
600
601// --- V a l u e ---
602
603
604/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000605 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000607class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 public:
609
610 /**
611 * Returns true if this value is the undefined value. See ECMA-262
612 * 4.3.10.
613 */
ager@chromium.org32912102009-01-16 10:38:43 +0000614 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
616 /**
617 * Returns true if this value is the null value. See ECMA-262
618 * 4.3.11.
619 */
ager@chromium.org32912102009-01-16 10:38:43 +0000620 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621
622 /**
623 * Returns true if this value is true.
624 */
ager@chromium.org32912102009-01-16 10:38:43 +0000625 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626
627 /**
628 * Returns true if this value is false.
629 */
ager@chromium.org32912102009-01-16 10:38:43 +0000630 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631
632 /**
633 * Returns true if this value is an instance of the String type.
634 * See ECMA-262 8.4.
635 */
ager@chromium.org32912102009-01-16 10:38:43 +0000636 bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
638 /**
639 * Returns true if this value is a function.
640 */
ager@chromium.org32912102009-01-16 10:38:43 +0000641 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642
643 /**
644 * Returns true if this value is an array.
645 */
ager@chromium.org32912102009-01-16 10:38:43 +0000646 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647
v8.team.kasperl727e9952008-09-02 14:56:44 +0000648 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 * Returns true if this value is an object.
650 */
ager@chromium.org32912102009-01-16 10:38:43 +0000651 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652
v8.team.kasperl727e9952008-09-02 14:56:44 +0000653 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 * Returns true if this value is boolean.
655 */
ager@chromium.org32912102009-01-16 10:38:43 +0000656 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000657
v8.team.kasperl727e9952008-09-02 14:56:44 +0000658 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 * Returns true if this value is a number.
660 */
ager@chromium.org32912102009-01-16 10:38:43 +0000661 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
v8.team.kasperl727e9952008-09-02 14:56:44 +0000663 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 * Returns true if this value is external.
665 */
ager@chromium.org32912102009-01-16 10:38:43 +0000666 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667
v8.team.kasperl727e9952008-09-02 14:56:44 +0000668 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 * Returns true if this value is a 32-bit signed integer.
670 */
ager@chromium.org32912102009-01-16 10:38:43 +0000671 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000673 /**
674 * Returns true if this value is a Date.
675 */
ager@chromium.org32912102009-01-16 10:38:43 +0000676 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000677
ager@chromium.org32912102009-01-16 10:38:43 +0000678 Local<Boolean> ToBoolean() const;
679 Local<Number> ToNumber() const;
680 Local<String> ToString() const;
681 Local<String> ToDetailString() const;
682 Local<Object> ToObject() const;
683 Local<Integer> ToInteger() const;
684 Local<Uint32> ToUint32() const;
685 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686
687 /**
688 * Attempts to convert a string to an array index.
689 * Returns an empty handle if the conversion fails.
690 */
ager@chromium.org32912102009-01-16 10:38:43 +0000691 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692
ager@chromium.org32912102009-01-16 10:38:43 +0000693 bool BooleanValue() const;
694 double NumberValue() const;
695 int64_t IntegerValue() const;
696 uint32_t Uint32Value() const;
697 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
699 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000700 bool Equals(Handle<Value> that) const;
701 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702};
703
704
705/**
706 * The superclass of primitive values. See ECMA-262 4.3.2.
707 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000708class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709
710
711/**
712 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
713 * or false value.
714 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000715class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000717 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 static inline Handle<Boolean> New(bool value);
719};
720
721
722/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000723 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000725class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000727
728 /**
729 * Returns the number of characters in this string.
730 */
ager@chromium.org32912102009-01-16 10:38:43 +0000731 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732
v8.team.kasperl727e9952008-09-02 14:56:44 +0000733 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000734 * Returns the number of bytes in the UTF-8 encoded
735 * representation of this string.
736 */
ager@chromium.org32912102009-01-16 10:38:43 +0000737 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000738
739 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000740 * Write the contents of the string to an external buffer.
741 * If no arguments are given, expects the buffer to be large
742 * enough to hold the entire string and NULL terminator. Copies
743 * the contents of the string and the NULL terminator into the
744 * buffer.
745 *
746 * Copies up to length characters into the output buffer.
747 * Only null-terminates if there is enough space in the buffer.
748 *
749 * \param buffer The buffer into which the string will be copied.
750 * \param start The starting position within the string at which
751 * copying begins.
752 * \param length The number of bytes to copy from the string.
753 * \return The number of characters copied to the buffer
754 * excluding the NULL terminator.
755 */
ager@chromium.org32912102009-01-16 10:38:43 +0000756 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
757 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
758 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759
v8.team.kasperl727e9952008-09-02 14:56:44 +0000760 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000761 * A zero length string.
762 */
763 static v8::Local<v8::String> Empty();
764
765 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000766 * Returns true if the string is external
767 */
ager@chromium.org32912102009-01-16 10:38:43 +0000768 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769
v8.team.kasperl727e9952008-09-02 14:56:44 +0000770 /**
771 * Returns true if the string is both external and ascii
772 */
ager@chromium.org32912102009-01-16 10:38:43 +0000773 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000774 /**
775 * An ExternalStringResource is a wrapper around a two-byte string
776 * buffer that resides outside V8's heap. Implement an
777 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000778 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000779 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000780 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000781 public:
782 /**
783 * Override the destructor to manage the life cycle of the underlying
784 * buffer.
785 */
786 virtual ~ExternalStringResource() {}
787 /** The string data from the underlying buffer.*/
788 virtual const uint16_t* data() const = 0;
789 /** The length of the string. That is, the number of two-byte characters.*/
790 virtual size_t length() const = 0;
791 protected:
792 ExternalStringResource() {}
793 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000794 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 ExternalStringResource(const ExternalStringResource&);
796 void operator=(const ExternalStringResource&);
797 };
798
799 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000800 * An ExternalAsciiStringResource is a wrapper around an ascii
801 * string buffer that resides outside V8's heap. Implement an
802 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000803 * underlying buffer. Note that the string data must be immutable
804 * and that the data must be strict 7-bit ASCII, not Latin1 or
805 * UTF-8, which would require special treatment internally in the
806 * engine and, in the case of UTF-8, do not allow efficient indexing.
807 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000808 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000810 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 public:
812 /**
813 * Override the destructor to manage the life cycle of the underlying
814 * buffer.
815 */
816 virtual ~ExternalAsciiStringResource() {}
817 /** The string data from the underlying buffer.*/
818 virtual const char* data() const = 0;
819 /** The number of ascii characters in the string.*/
820 virtual size_t length() const = 0;
821 protected:
822 ExternalAsciiStringResource() {}
823 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000824 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
826 void operator=(const ExternalAsciiStringResource&);
827 };
828
829 /**
830 * Get the ExternalStringResource for an external string. Only
831 * valid if IsExternal() returns true.
832 */
ager@chromium.org32912102009-01-16 10:38:43 +0000833 ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834
835 /**
836 * Get the ExternalAsciiStringResource for an external ascii string.
837 * Only valid if IsExternalAscii() returns true.
838 */
ager@chromium.org32912102009-01-16 10:38:43 +0000839 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
841 static String* Cast(v8::Value* obj);
842
843 /**
844 * Allocates a new string from either utf-8 encoded or ascii data.
845 * The second parameter 'length' gives the buffer length.
846 * If the data is utf-8 encoded, the caller must
847 * be careful to supply the length parameter.
848 * If it is not given, the function calls
849 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000850 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 */
852 static Local<String> New(const char* data, int length = -1);
853
854 /** Allocates a new string from utf16 data.*/
855 static Local<String> New(const uint16_t* data, int length = -1);
856
857 /** Creates a symbol. Returns one if it exists already.*/
858 static Local<String> NewSymbol(const char* data, int length = -1);
859
v8.team.kasperl727e9952008-09-02 14:56:44 +0000860 /**
861 * Creates a new external string using the data defined in the given
862 * resource. The resource is deleted when the external string is no
863 * longer live on V8's heap. The caller of this function should not
864 * delete or modify the resource. Neither should the underlying buffer be
865 * deallocated or modified except through the destructor of the
866 * external string resource.
867 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000869
ager@chromium.org6f10e412009-02-13 10:11:16 +0000870 /**
871 * Associate an external string resource with this string by transforming it
872 * in place so that existing references to this string in the JavaScript heap
873 * will use the external string resource. The external string resource's
874 * character contents needs to be equivalent to this string.
875 * Returns true if the string has been changed to be an external string.
876 * The string is not modified if the operation fails.
877 */
878 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
v8.team.kasperl727e9952008-09-02 14:56:44 +0000880 /**
881 * Creates a new external string using the ascii data defined in the given
882 * resource. The resource is deleted when the external string is no
883 * longer live on V8's heap. The caller of this function should not
884 * delete or modify the resource. Neither should the underlying buffer be
885 * deallocated or modified except through the destructor of the
886 * external string resource.
887 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000889
ager@chromium.org6f10e412009-02-13 10:11:16 +0000890 /**
891 * Associate an external string resource with this string by transforming it
892 * in place so that existing references to this string in the JavaScript heap
893 * will use the external string resource. The external string resource's
894 * character contents needs to be equivalent to this string.
895 * Returns true if the string has been changed to be an external string.
896 * The string is not modified if the operation fails.
897 */
898 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
kasper.lund7276f142008-07-30 08:49:36 +0000900 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 static Local<String> NewUndetectable(const char* data, int length = -1);
902
kasper.lund7276f142008-07-30 08:49:36 +0000903 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
905
906 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000907 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000908 * you want to print the object. If conversion to a string fails
909 * (eg. due to an exception in the toString() method of the object)
910 * then the length() method returns 0 and the * operator returns
911 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000912 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000913 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000914 public:
915 explicit Utf8Value(Handle<v8::Value> obj);
916 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000917 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000918 int length() { return length_; }
919 private:
920 char* str_;
921 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000922
923 // Disallow copying and assigning.
924 Utf8Value(const Utf8Value&);
925 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000926 };
927
928 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 * Converts an object to an ascii string.
930 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000931 * If conversion to a string fails (eg. due to an exception in the toString()
932 * method of the object) then the length() method returns 0 and the * operator
933 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000935 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 public:
937 explicit AsciiValue(Handle<v8::Value> obj);
938 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000939 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000940 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 private:
942 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000943 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000944
945 // Disallow copying and assigning.
946 AsciiValue(const AsciiValue&);
947 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 };
949
950 /**
951 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000952 * If conversion to a string fails (eg. due to an exception in the toString()
953 * method of the object) then the length() method returns 0 and the * operator
954 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000956 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 public:
958 explicit Value(Handle<v8::Value> obj);
959 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000960 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000961 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 private:
963 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000964 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000965
966 // Disallow copying and assigning.
967 Value(const Value&);
968 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 };
970};
971
972
973/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000974 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000976class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000978 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 static Local<Number> New(double value);
980 static Number* Cast(v8::Value* obj);
981 private:
982 Number();
983};
984
985
986/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000987 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000989class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 public:
991 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +0000992 int64_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 static Integer* Cast(v8::Value* obj);
994 private:
995 Integer();
996};
997
998
999/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001000 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001002class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001004 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 private:
1006 Int32();
1007};
1008
1009
1010/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001011 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001013class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001015 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 private:
1017 Uint32();
1018};
1019
1020
1021/**
1022 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1023 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001024class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 public:
1026 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001027
1028 /**
1029 * A specialization of Value::NumberValue that is more efficient
1030 * because we know the structure of this object.
1031 */
ager@chromium.org32912102009-01-16 10:38:43 +00001032 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001033
1034 static Date* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035};
1036
1037
1038enum PropertyAttribute {
1039 None = 0,
1040 ReadOnly = 1 << 0,
1041 DontEnum = 1 << 1,
1042 DontDelete = 1 << 2
1043};
1044
1045/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001046 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001048class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 public:
1050 bool Set(Handle<Value> key,
1051 Handle<Value> value,
1052 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001053
1054 // Sets a local property on this object, bypassing interceptors and
1055 // overriding accessors or read-only properties.
1056 //
1057 // Note that if the object has an interceptor the property will be set
1058 // locally, but since the interceptor takes precedence the local property
1059 // will only be returned if the interceptor doesn't return a value.
1060 //
1061 // Note also that this only works for named properties.
1062 bool ForceSet(Handle<Value> key,
1063 Handle<Value> value,
1064 PropertyAttribute attribs = None);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 Local<Value> Get(Handle<Value> key);
1066
1067 // TODO(1245389): Replace the type-specific versions of these
1068 // functions with generic ones that accept a Handle<Value> key.
1069 bool Has(Handle<String> key);
1070 bool Delete(Handle<String> key);
1071 bool Has(uint32_t index);
1072 bool Delete(uint32_t index);
1073
1074 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001075 * Returns an array containing the names of the enumerable properties
1076 * of this object, including properties from prototype objects. The
1077 * array returned by this method contains the same values as would
1078 * be enumerated by a for-in statement over this object.
1079 */
1080 Local<Array> GetPropertyNames();
1081
1082 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 * Get the prototype object. This does not skip objects marked to
1084 * be skipped by __proto__ and it does not consult the security
1085 * handler.
1086 */
1087 Local<Value> GetPrototype();
1088
1089 /**
1090 * Call builtin Object.prototype.toString on this object.
1091 * This is different from Value::ToString() that may call
1092 * user-defined toString function. This one does not.
1093 */
1094 Local<String> ObjectProtoToString();
1095
kasper.lund212ac232008-07-16 07:07:30 +00001096 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001098 /** Gets the value in an internal field. */
1099 Local<Value> GetInternalField(int index);
1100 /** Sets the value in an internal field. */
1101 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102
1103 // Testers for local properties.
1104 bool HasRealNamedProperty(Handle<String> key);
1105 bool HasRealIndexedProperty(uint32_t index);
1106 bool HasRealNamedCallbackProperty(Handle<String> key);
1107
1108 /**
1109 * If result.IsEmpty() no real property was located in the prototype chain.
1110 * This means interceptors in the prototype chain are not called.
1111 */
1112 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1113
1114 /** Tests for a named lookup interceptor.*/
1115 bool HasNamedLookupInterceptor();
1116
kasper.lund212ac232008-07-16 07:07:30 +00001117 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 bool HasIndexedLookupInterceptor();
1119
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001120 /**
1121 * Turns on access check on the object if the object is an instance of
1122 * a template that has access check callbacks. If an object has no
1123 * access check info, the object cannot be accessed by anyone.
1124 */
1125 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001126
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001127 /**
1128 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org5ec48922009-05-05 07:25:34 +00001129 * a hidden property on the object to store the identity hash.
1130 *
1131 * The return value will never be 0. Also, it is not guaranteed to be
1132 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001133 */
1134 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001135
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001136 /**
1137 * Access hidden properties on JavaScript objects. These properties are
1138 * hidden from the executing JavaScript and only accessible through the V8
1139 * C++ API. Hidden properties introduced by V8 internally (for example the
1140 * identity hash) are prefixed with "v8::".
1141 */
1142 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1143 Local<Value> GetHiddenValue(Handle<String> key);
1144 bool DeleteHiddenValue(Handle<String> key);
1145
1146 /**
1147 * Clone this object with a fast but shallow copy. Values will point
1148 * to the same values as the original object.
1149 */
1150 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 static Local<Object> New();
1153 static Object* Cast(Value* obj);
1154 private:
1155 Object();
1156};
1157
1158
1159/**
1160 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1161 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001162class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001164 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165
1166 static Local<Array> New(int length = 0);
1167 static Array* Cast(Value* obj);
1168 private:
1169 Array();
1170};
1171
1172
1173/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001174 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001176class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001178 Local<Object> NewInstance() const;
1179 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1181 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001182 Handle<Value> GetName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 static Function* Cast(Value* obj);
1184 private:
1185 Function();
1186};
1187
1188
1189/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001190 * A JavaScript value that wraps a C++ void*. This type of value is
1191 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001193 *
1194 * The Wrap function V8 will return the most optimal Value object wrapping the
1195 * C++ void*. The type of the value is not guaranteed to be an External object
1196 * and no assumptions about its type should be made. To access the wrapped
1197 * value Unwrap should be used, all other operations on that object will lead
1198 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001200class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001202 static Local<Value> Wrap(void* data);
1203 static void* Unwrap(Handle<Value> obj);
1204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 static Local<External> New(void* value);
1206 static External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001207 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 private:
1209 External();
1210};
1211
1212
1213// --- T e m p l a t e s ---
1214
1215
1216/**
1217 * The superclass of object and function templates.
1218 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001219class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 public:
1221 /** Adds a property to each instance created by this template.*/
1222 void Set(Handle<String> name, Handle<Data> value,
1223 PropertyAttribute attributes = None);
1224 inline void Set(const char* name, Handle<Data> value);
1225 private:
1226 Template();
1227
1228 friend class ObjectTemplate;
1229 friend class FunctionTemplate;
1230};
1231
1232
1233/**
1234 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001235 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 * including the receiver, the number and values of arguments, and
1237 * the holder of the function.
1238 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001239class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 public:
1241 inline int Length() const;
1242 inline Local<Value> operator[](int i) const;
1243 inline Local<Function> Callee() const;
1244 inline Local<Object> This() const;
1245 inline Local<Object> Holder() const;
1246 inline bool IsConstructCall() const;
1247 inline Local<Value> Data() const;
1248 private:
1249 Arguments();
1250 friend class ImplementationUtilities;
1251 inline Arguments(Local<Value> data,
1252 Local<Object> holder,
1253 Local<Function> callee,
1254 bool is_construct_call,
1255 void** values, int length);
1256 Local<Value> data_;
1257 Local<Object> holder_;
1258 Local<Function> callee_;
1259 bool is_construct_call_;
1260 void** values_;
1261 int length_;
1262};
1263
1264
1265/**
1266 * The information passed to an accessor callback about the context
1267 * of the property access.
1268 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001269class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 public:
1271 inline AccessorInfo(Local<Object> self,
1272 Local<Value> data,
1273 Local<Object> holder)
1274 : self_(self), data_(data), holder_(holder) { }
1275 inline Local<Value> Data() const;
1276 inline Local<Object> This() const;
1277 inline Local<Object> Holder() const;
1278 private:
1279 Local<Object> self_;
1280 Local<Value> data_;
1281 Local<Object> holder_;
1282};
1283
1284
1285typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1286
1287typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1288
1289/**
1290 * Accessor[Getter|Setter] are used as callback functions when
1291 * setting|getting a particular property. See objectTemplate::SetAccessor.
1292 */
1293typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1294 const AccessorInfo& info);
1295
1296
1297typedef void (*AccessorSetter)(Local<String> property,
1298 Local<Value> value,
1299 const AccessorInfo& info);
1300
1301
1302/**
1303 * NamedProperty[Getter|Setter] are used as interceptors on object.
1304 * See ObjectTemplate::SetNamedPropertyHandler.
1305 */
1306typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1307 const AccessorInfo& info);
1308
1309
1310/**
1311 * Returns the value if the setter intercepts the request.
1312 * Otherwise, returns an empty handle.
1313 */
1314typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1315 Local<Value> value,
1316 const AccessorInfo& info);
1317
1318
1319/**
1320 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001321 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 */
1323typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1324 const AccessorInfo& info);
1325
1326
1327/**
1328 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001329 * The return value is true if the property could be deleted and false
1330 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 */
1332typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1333 const AccessorInfo& info);
1334
1335/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001336 * Returns an array containing the names of the properties the named
1337 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 */
1339typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1340
v8.team.kasperl727e9952008-09-02 14:56:44 +00001341
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001343 * Returns the value of the property if the getter intercepts the
1344 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 */
1346typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1347 const AccessorInfo& info);
1348
1349
1350/**
1351 * Returns the value if the setter intercepts the request.
1352 * Otherwise, returns an empty handle.
1353 */
1354typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1355 Local<Value> value,
1356 const AccessorInfo& info);
1357
1358
1359/**
1360 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001361 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 */
1363typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1364 const AccessorInfo& info);
1365
1366/**
1367 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001368 * The return value is true if the property could be deleted and false
1369 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 */
1371typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1372 const AccessorInfo& info);
1373
v8.team.kasperl727e9952008-09-02 14:56:44 +00001374/**
1375 * Returns an array containing the indices of the properties the
1376 * indexed property getter intercepts.
1377 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1379
1380
1381/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001382 * Access control specifications.
1383 *
1384 * Some accessors should be accessible across contexts. These
1385 * accessors have an explicit access control parameter which specifies
1386 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001387 *
1388 * Additionally, for security, accessors can prohibit overwriting by
1389 * accessors defined in JavaScript. For objects that have such
1390 * accessors either locally or in their prototype chain it is not
1391 * possible to overwrite the accessor by using __defineGetter__ or
1392 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 */
1394enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001395 DEFAULT = 0,
1396 ALL_CAN_READ = 1,
1397 ALL_CAN_WRITE = 1 << 1,
1398 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399};
1400
1401
1402/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001403 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 */
1405enum AccessType {
1406 ACCESS_GET,
1407 ACCESS_SET,
1408 ACCESS_HAS,
1409 ACCESS_DELETE,
1410 ACCESS_KEYS
1411};
1412
v8.team.kasperl727e9952008-09-02 14:56:44 +00001413
1414/**
1415 * Returns true if cross-context access should be allowed to the named
1416 * property with the given key on the global object.
1417 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418typedef bool (*NamedSecurityCallback)(Local<Object> global,
1419 Local<Value> key,
1420 AccessType type,
1421 Local<Value> data);
1422
v8.team.kasperl727e9952008-09-02 14:56:44 +00001423
1424/**
1425 * Returns true if cross-context access should be allowed to the indexed
1426 * property with the given index on the global object.
1427 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1429 uint32_t index,
1430 AccessType type,
1431 Local<Value> data);
1432
1433
1434/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001435 * A FunctionTemplate is used to create functions at runtime. There
1436 * can only be one function created from a FunctionTemplate in a
1437 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 *
1439 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001440 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001442 * A FunctionTemplate has a corresponding instance template which is
1443 * used to create object instances when the function is used as a
1444 * constructor. Properties added to the instance template are added to
1445 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 *
1447 * A FunctionTemplate can have a prototype template. The prototype template
1448 * is used to create the prototype object of the function.
1449 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001450 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001452 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1454 * t->Set("func_property", v8::Number::New(1));
1455 *
1456 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1457 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1458 * proto_t->Set("proto_const", v8::Number::New(2));
1459 *
1460 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1461 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1462 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1463 * instance_t->Set("instance_property", Number::New(3));
1464 *
1465 * v8::Local<v8::Function> function = t->GetFunction();
1466 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001467 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 *
1469 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001470 * and "instance" for the instance object created above. The function
1471 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001473 * \code
1474 * func_property in function == true;
1475 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001477 * function.prototype.proto_method() invokes 'InvokeCallback'
1478 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001480 * instance instanceof function == true;
1481 * instance.instance_accessor calls 'InstanceAccessorCallback'
1482 * instance.instance_property == 3;
1483 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001485 * A FunctionTemplate can inherit from another one by calling the
1486 * FunctionTemplate::Inherit method. The following graph illustrates
1487 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001489 * \code
1490 * FunctionTemplate Parent -> Parent() . prototype -> { }
1491 * ^ ^
1492 * | Inherit(Parent) | .__proto__
1493 * | |
1494 * FunctionTemplate Child -> Child() . prototype -> { }
1495 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001497 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1498 * object of the Child() function has __proto__ pointing to the
1499 * Parent() function's prototype object. An instance of the Child
1500 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001502 * Let Parent be the FunctionTemplate initialized in the previous
1503 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001505 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 * Local<FunctionTemplate> parent = t;
1507 * Local<FunctionTemplate> child = FunctionTemplate::New();
1508 * child->Inherit(parent);
1509 *
1510 * Local<Function> child_function = child->GetFunction();
1511 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001512 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001514 * The Child function and Child instance will have the following
1515 * properties:
1516 *
1517 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001519 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001521 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001523class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 public:
1525 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001526 static Local<FunctionTemplate> New(
1527 InvocationCallback callback = 0,
1528 Handle<Value> data = Handle<Value>(),
1529 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 /** Returns the unique function instance in the current execution context.*/
1531 Local<Function> GetFunction();
1532
v8.team.kasperl727e9952008-09-02 14:56:44 +00001533 /**
1534 * Set the call-handler callback for a FunctionTemplate. This
1535 * callback is called whenever the function created from this
1536 * FunctionTemplate is called.
1537 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 void SetCallHandler(InvocationCallback callback,
1539 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
v8.team.kasperl727e9952008-09-02 14:56:44 +00001541 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 Local<ObjectTemplate> InstanceTemplate();
1543
1544 /** Causes the function template to inherit from a parent function template.*/
1545 void Inherit(Handle<FunctionTemplate> parent);
1546
1547 /**
1548 * A PrototypeTemplate is the template used to create the prototype object
1549 * of the function created by this template.
1550 */
1551 Local<ObjectTemplate> PrototypeTemplate();
1552
v8.team.kasperl727e9952008-09-02 14:56:44 +00001553
1554 /**
1555 * Set the class name of the FunctionTemplate. This is used for
1556 * printing objects created with the function created from the
1557 * FunctionTemplate as its constructor.
1558 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 void SetClassName(Handle<String> name);
1560
1561 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001562 * Determines whether the __proto__ accessor ignores instances of
1563 * the function template. If instances of the function template are
1564 * ignored, __proto__ skips all instances and instead returns the
1565 * next object in the prototype chain.
1566 *
1567 * Call with a value of true to make the __proto__ accessor ignore
1568 * instances of the function template. Call with a value of false
1569 * to make the __proto__ accessor not ignore instances of the
1570 * function template. By default, instances of a function template
1571 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 */
1573 void SetHiddenPrototype(bool value);
1574
1575 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001576 * Returns true if the given object is an instance of this function
1577 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 */
1579 bool HasInstance(Handle<Value> object);
1580
1581 private:
1582 FunctionTemplate();
1583 void AddInstancePropertyAccessor(Handle<String> name,
1584 AccessorGetter getter,
1585 AccessorSetter setter,
1586 Handle<Value> data,
1587 AccessControl settings,
1588 PropertyAttribute attributes);
1589 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1590 NamedPropertySetter setter,
1591 NamedPropertyQuery query,
1592 NamedPropertyDeleter remover,
1593 NamedPropertyEnumerator enumerator,
1594 Handle<Value> data);
1595 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1596 IndexedPropertySetter setter,
1597 IndexedPropertyQuery query,
1598 IndexedPropertyDeleter remover,
1599 IndexedPropertyEnumerator enumerator,
1600 Handle<Value> data);
1601 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1602 Handle<Value> data);
1603
1604 friend class Context;
1605 friend class ObjectTemplate;
1606};
1607
1608
1609/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001610 * An ObjectTemplate is used to create objects at runtime.
1611 *
1612 * Properties added to an ObjectTemplate are added to each object
1613 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001615class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001617 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 /** Creates a new instance of this template.*/
1621 Local<Object> NewInstance();
1622
1623 /**
1624 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001625 *
1626 * Whenever the property with the given name is accessed on objects
1627 * created from this ObjectTemplate the getter and setter callbacks
1628 * are called instead of getting and setting the property directly
1629 * on the JavaScript object.
1630 *
1631 * \param name The name of the property for which an accessor is added.
1632 * \param getter The callback to invoke when getting the property.
1633 * \param setter The callback to invoke when setting the property.
1634 * \param data A piece of data that will be passed to the getter and setter
1635 * callbacks whenever they are invoked.
1636 * \param settings Access control settings for the accessor. This is a bit
1637 * field consisting of one of more of
1638 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1639 * The default is to not allow cross-context access.
1640 * ALL_CAN_READ means that all cross-context reads are allowed.
1641 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1642 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1643 * cross-context access.
1644 * \param attribute The attributes of the property for which an accessor
1645 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 */
1647 void SetAccessor(Handle<String> name,
1648 AccessorGetter getter,
1649 AccessorSetter setter = 0,
1650 Handle<Value> data = Handle<Value>(),
1651 AccessControl settings = DEFAULT,
1652 PropertyAttribute attribute = None);
1653
1654 /**
1655 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001656 *
1657 * Whenever a named property is accessed on objects created from
1658 * this object template, the provided callback is invoked instead of
1659 * accessing the property directly on the JavaScript object.
1660 *
1661 * \param getter The callback to invoke when getting a property.
1662 * \param setter The callback to invoke when setting a property.
1663 * \param query The callback to invoke to check is an object has a property.
1664 * \param deleter The callback to invoke when deleting a property.
1665 * \param enumerator The callback to invoke to enumerate all the named
1666 * properties of an object.
1667 * \param data A piece of data that will be passed to the callbacks
1668 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 */
1670 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1671 NamedPropertySetter setter = 0,
1672 NamedPropertyQuery query = 0,
1673 NamedPropertyDeleter deleter = 0,
1674 NamedPropertyEnumerator enumerator = 0,
1675 Handle<Value> data = Handle<Value>());
1676
1677 /**
1678 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001679 *
1680 * Whenever an indexed property is accessed on objects created from
1681 * this object template, the provided callback is invoked instead of
1682 * accessing the property directly on the JavaScript object.
1683 *
1684 * \param getter The callback to invoke when getting a property.
1685 * \param setter The callback to invoke when setting a property.
1686 * \param query The callback to invoke to check is an object has a property.
1687 * \param deleter The callback to invoke when deleting a property.
1688 * \param enumerator The callback to invoke to enumerate all the indexed
1689 * properties of an object.
1690 * \param data A piece of data that will be passed to the callbacks
1691 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 */
1693 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1694 IndexedPropertySetter setter = 0,
1695 IndexedPropertyQuery query = 0,
1696 IndexedPropertyDeleter deleter = 0,
1697 IndexedPropertyEnumerator enumerator = 0,
1698 Handle<Value> data = Handle<Value>());
1699 /**
1700 * Sets the callback to be used when calling instances created from
1701 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001702 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703 * function.
1704 */
1705 void SetCallAsFunctionHandler(InvocationCallback callback,
1706 Handle<Value> data = Handle<Value>());
1707
v8.team.kasperl727e9952008-09-02 14:56:44 +00001708 /**
1709 * Mark object instances of the template as undetectable.
1710 *
1711 * In many ways, undetectable objects behave as though they are not
1712 * there. They behave like 'undefined' in conditionals and when
1713 * printed. However, properties can be accessed and called as on
1714 * normal objects.
1715 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 void MarkAsUndetectable();
1717
v8.team.kasperl727e9952008-09-02 14:56:44 +00001718 /**
1719 * Sets access check callbacks on the object template.
1720 *
1721 * When accessing properties on instances of this object template,
1722 * the access check callback will be called to determine whether or
1723 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001724 * The last parameter specifies whether access checks are turned
1725 * on by default on instances. If access checks are off by default,
1726 * they can be turned on on individual instances by calling
1727 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001728 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1730 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001731 Handle<Value> data = Handle<Value>(),
1732 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733
kasper.lund212ac232008-07-16 07:07:30 +00001734 /**
1735 * Gets the number of internal fields for objects generated from
1736 * this template.
1737 */
1738 int InternalFieldCount();
1739
1740 /**
1741 * Sets the number of internal fields for objects generated from
1742 * this template.
1743 */
1744 void SetInternalFieldCount(int value);
1745
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746 private:
1747 ObjectTemplate();
1748 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1749 friend class FunctionTemplate;
1750};
1751
1752
1753/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001754 * A Signature specifies which receivers and arguments a function can
1755 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001757class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 public:
1759 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1760 Handle<FunctionTemplate>(),
1761 int argc = 0,
1762 Handle<FunctionTemplate> argv[] = 0);
1763 private:
1764 Signature();
1765};
1766
1767
1768/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001769 * A utility for determining the type of objects based on the template
1770 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001772class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 public:
1774 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1775 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1776 int match(Handle<Value> value);
1777 private:
1778 TypeSwitch();
1779};
1780
1781
1782// --- E x t e n s i o n s ---
1783
1784
1785/**
1786 * Ignore
1787 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001788class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 public:
1790 Extension(const char* name,
1791 const char* source = 0,
1792 int dep_count = 0,
1793 const char** deps = 0);
1794 virtual ~Extension() { }
1795 virtual v8::Handle<v8::FunctionTemplate>
1796 GetNativeFunction(v8::Handle<v8::String> name) {
1797 return v8::Handle<v8::FunctionTemplate>();
1798 }
1799
1800 const char* name() { return name_; }
1801 const char* source() { return source_; }
1802 int dependency_count() { return dep_count_; }
1803 const char** dependencies() { return deps_; }
1804 void set_auto_enable(bool value) { auto_enable_ = value; }
1805 bool auto_enable() { return auto_enable_; }
1806
1807 private:
1808 const char* name_;
1809 const char* source_;
1810 int dep_count_;
1811 const char** deps_;
1812 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001813
1814 // Disallow copying and assigning.
1815 Extension(const Extension&);
1816 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817};
1818
1819
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001820void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821
1822
1823/**
1824 * Ignore
1825 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001826class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 public:
1828 inline DeclareExtension(Extension* extension) {
1829 RegisterExtension(extension);
1830 }
1831};
1832
1833
1834// --- S t a t i c s ---
1835
1836
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001837Handle<Primitive> V8EXPORT Undefined();
1838Handle<Primitive> V8EXPORT Null();
1839Handle<Boolean> V8EXPORT True();
1840Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841
1842
1843/**
1844 * A set of constraints that specifies the limits of the runtime's
1845 * memory use.
1846 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001847class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 public:
1849 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001850 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001852 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001854 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1856 private:
1857 int max_young_space_size_;
1858 int max_old_space_size_;
1859 uint32_t* stack_limit_;
1860};
1861
1862
1863bool SetResourceConstraints(ResourceConstraints* constraints);
1864
1865
1866// --- E x c e p t i o n s ---
1867
1868
1869typedef void (*FatalErrorCallback)(const char* location, const char* message);
1870
1871
1872typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1873
1874
1875/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001876 * Schedules an exception to be thrown when returning to JavaScript. When an
1877 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00001879 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001881Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882
1883/**
1884 * Create new error objects by calling the corresponding error object
1885 * constructor with the message.
1886 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001887class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 public:
1889 static Local<Value> RangeError(Handle<String> message);
1890 static Local<Value> ReferenceError(Handle<String> message);
1891 static Local<Value> SyntaxError(Handle<String> message);
1892 static Local<Value> TypeError(Handle<String> message);
1893 static Local<Value> Error(Handle<String> message);
1894};
1895
1896
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001897// --- C o u n t e r s C a l l b a c k s ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001899typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001901typedef void* (*CreateHistogramCallback)(const char* name,
1902 int min,
1903 int max,
1904 size_t buckets);
1905
1906typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
1907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908// --- 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 ---
1909typedef void (*FailedAccessCheckCallback)(Local<Object> target,
1910 AccessType type,
1911 Local<Value> data);
1912
1913// --- 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
1914
1915/**
1916 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00001917 * before and after a major garbage collection. Allocations are not
1918 * allowed in the callback function, you therefore cannot manipulate
1919 * objects (set or delete properties for example) since it is possible
1920 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 */
1922typedef void (*GCCallback)();
1923
1924
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001925// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926
1927/**
1928 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00001929 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931typedef Persistent<Context> (*ContextGenerator)();
1932
1933
1934/**
1935 * Container class for static utility functions.
1936 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001937class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001939 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 static void SetFatalErrorHandler(FatalErrorCallback that);
1941
v8.team.kasperl727e9952008-09-02 14:56:44 +00001942 /**
1943 * Ignore out-of-memory exceptions.
1944 *
1945 * V8 running out of memory is treated as a fatal error by default.
1946 * This means that the fatal error handler is called and that V8 is
1947 * terminated.
1948 *
1949 * IgnoreOutOfMemoryException can be used to not treat a
1950 * out-of-memory situation as a fatal error. This way, the contexts
1951 * that did not cause the out of memory problem might be able to
1952 * continue execution.
1953 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 static void IgnoreOutOfMemoryException();
1955
v8.team.kasperl727e9952008-09-02 14:56:44 +00001956 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001957 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00001958 * fatal errors such as out-of-memory situations.
1959 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 static bool IsDead();
1961
1962 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001963 * Adds a message listener.
1964 *
1965 * The same message listener can be added more than once and it that
1966 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 */
1968 static bool AddMessageListener(MessageCallback that,
1969 Handle<Value> data = Handle<Value>());
1970
1971 /**
1972 * Remove all message listeners from the specified callback function.
1973 */
1974 static void RemoveMessageListeners(MessageCallback that);
1975
1976 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001977 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 */
1979 static void SetFlagsFromString(const char* str, int length);
1980
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001981 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001982 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001983 */
1984 static void SetFlagsFromCommandLine(int* argc,
1985 char** argv,
1986 bool remove_flags);
1987
kasper.lund7276f142008-07-30 08:49:36 +00001988 /** Get the version string. */
1989 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990
1991 /**
1992 * Enables the host application to provide a mechanism for recording
1993 * statistics counters.
1994 */
1995 static void SetCounterFunction(CounterLookupCallback);
1996
1997 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001998 * Enables the host application to provide a mechanism for recording
1999 * histograms. The CreateHistogram function returns a
2000 * histogram which will later be passed to the AddHistogramSample
2001 * function.
2002 */
2003 static void SetCreateHistogramFunction(CreateHistogramCallback);
2004 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2005
2006 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 * Enables the computation of a sliding window of states. The sliding
2008 * window information is recorded in statistics counters.
2009 */
2010 static void EnableSlidingStateWindow();
2011
2012 /** Callback function for reporting failed access checks.*/
2013 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2014
2015 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002016 * Enables the host application to receive a notification before a
2017 * major garbage colletion. Allocations are not allowed in the
2018 * callback function, you therefore cannot manipulate objects (set
2019 * or delete properties for example) since it is possible such
2020 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 */
2022 static void SetGlobalGCPrologueCallback(GCCallback);
2023
2024 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002025 * Enables the host application to receive a notification after a
2026 * major garbage collection. Allocations are not allowed in the
2027 * callback function, you therefore cannot manipulate objects (set
2028 * or delete properties for example) since it is possible such
2029 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 */
2031 static void SetGlobalGCEpilogueCallback(GCCallback);
2032
2033 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002034 * Allows the host application to group objects together. If one
2035 * object in the group is alive, all objects in the group are alive.
2036 * After each garbage collection, object groups are removed. It is
2037 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002038 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002039 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002041 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042
2043 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002044 * Initializes from snapshot if possible. Otherwise, attempts to
2045 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 */
2047 static bool Initialize();
2048
kasper.lund7276f142008-07-30 08:49:36 +00002049 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002050 * Adjusts the amount of registered external memory. Used to give
2051 * V8 an indication of the amount of externally allocated memory
2052 * that is kept alive by JavaScript objects. V8 uses this to decide
2053 * when to perform global garbage collections. Registering
2054 * externally allocated memory will trigger global garbage
2055 * collections more often than otherwise in an attempt to garbage
2056 * collect the JavaScript objects keeping the externally allocated
2057 * memory alive.
2058 *
2059 * \param change_in_bytes the change in externally allocated memory
2060 * that is kept alive by JavaScript objects.
2061 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002062 */
2063 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2064
iposva@chromium.org245aa852009-02-10 00:49:54 +00002065 /**
2066 * Suspends recording of tick samples in the profiler.
2067 * When the V8 profiling mode is enabled (usually via command line
2068 * switches) this function suspends recording of tick samples.
2069 * Profiling ticks are discarded until ResumeProfiler() is called.
2070 *
2071 * See also the --prof and --prof_auto command line switches to
2072 * enable V8 profiling.
2073 */
2074 static void PauseProfiler();
2075
2076 /**
2077 * Resumes recording of tick samples in the profiler.
2078 * See also PauseProfiler().
2079 */
2080 static void ResumeProfiler();
2081
ager@chromium.org41826e72009-03-30 13:30:57 +00002082 /**
2083 * Releases any resources used by v8 and stops any utility threads
2084 * that may be running. Note that disposing v8 is permanent, it
2085 * cannot be reinitialized.
2086 *
2087 * It should generally not be necessary to dispose v8 before exiting
2088 * a process, this should happen automatically. It is only necessary
2089 * to use if the process needs the resources taken up by v8.
2090 */
2091 static bool Dispose();
2092
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 private:
2094 V8();
2095
2096 static void** GlobalizeReference(void** handle);
2097 static void DisposeGlobal(void** global_handle);
2098 static void MakeWeak(void** global_handle, void* data, WeakReferenceCallback);
2099 static void ClearWeak(void** global_handle);
2100 static bool IsGlobalNearDeath(void** global_handle);
2101 static bool IsGlobalWeak(void** global_handle);
2102
2103 template <class T> friend class Handle;
2104 template <class T> friend class Local;
2105 template <class T> friend class Persistent;
2106 friend class Context;
2107};
2108
2109
2110/**
2111 * An external exception handler.
2112 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002113class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 public:
2115
2116 /**
2117 * Creates a new try/catch block and registers it with v8.
2118 */
2119 TryCatch();
2120
2121 /**
2122 * Unregisters and deletes this try/catch block.
2123 */
2124 ~TryCatch();
2125
2126 /**
2127 * Returns true if an exception has been caught by this try/catch block.
2128 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002129 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
2131 /**
2132 * Returns the exception caught by this try/catch block. If no exception has
2133 * been caught an empty handle is returned.
2134 *
2135 * The returned handle is valid until this TryCatch block has been destroyed.
2136 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002137 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
2139 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002140 * Returns the message associated with this exception. If there is
2141 * no message associated an empty handle is returned.
2142 *
2143 * The returned handle is valid until this TryCatch block has been
2144 * destroyed.
2145 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002146 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002147
2148 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 * Clears any exceptions that may have been caught by this try/catch block.
2150 * After this method has been called, HasCaught() will return false.
2151 *
2152 * It is not necessary to clear a try/catch block before using it again; if
2153 * another exception is thrown the previously caught exception will just be
2154 * overwritten. However, it is often a good idea since it makes it easier
2155 * to determine which operation threw a given exception.
2156 */
2157 void Reset();
2158
v8.team.kasperl727e9952008-09-02 14:56:44 +00002159 /**
2160 * Set verbosity of the external exception handler.
2161 *
2162 * By default, exceptions that are caught by an external exception
2163 * handler are not reported. Call SetVerbose with true on an
2164 * external exception handler to have exceptions caught by the
2165 * handler reported as if they were not caught.
2166 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 void SetVerbose(bool value);
2168
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002169 /**
2170 * Set whether or not this TryCatch should capture a Message object
2171 * which holds source information about where the exception
2172 * occurred. True by default.
2173 */
2174 void SetCaptureMessage(bool value);
2175
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 public:
2177 TryCatch* next_;
2178 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002179 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 bool is_verbose_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002181 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002182 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183};
2184
2185
2186// --- C o n t e x t ---
2187
2188
2189/**
2190 * Ignore
2191 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002192class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 public:
2194 ExtensionConfiguration(int name_count, const char* names[])
2195 : name_count_(name_count), names_(names) { }
2196 private:
2197 friend class ImplementationUtilities;
2198 int name_count_;
2199 const char** names_;
2200};
2201
2202
2203/**
2204 * A sandboxed execution context with its own set of built-in objects
2205 * and functions.
2206 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002207class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002209 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 Local<Object> Global();
2211
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002212 /**
2213 * Detaches the global object from its context before
2214 * the global object can be reused to create a new context.
2215 */
2216 void DetachGlobal();
2217
v8.team.kasperl727e9952008-09-02 14:56:44 +00002218 /** Creates a new context. */
2219 static Persistent<Context> New(
2220 ExtensionConfiguration* extensions = 0,
2221 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2222 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223
kasper.lund44510672008-07-25 07:37:58 +00002224 /** Returns the last entered context. */
2225 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226
kasper.lund44510672008-07-25 07:37:58 +00002227 /** Returns the context that is on the top of the stack. */
2228 static Local<Context> GetCurrent();
2229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 /**
2231 * Sets the security token for the context. To access an object in
2232 * another context, the security tokens must match.
2233 */
2234 void SetSecurityToken(Handle<Value> token);
2235
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002236 /** Restores the security token to the default value. */
2237 void UseDefaultSecurityToken();
2238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 /** Returns the security token of this context.*/
2240 Handle<Value> GetSecurityToken();
2241
v8.team.kasperl727e9952008-09-02 14:56:44 +00002242 /**
2243 * Enter this context. After entering a context, all code compiled
2244 * and run is compiled and run in this context. If another context
2245 * is already entered, this old context is saved so it can be
2246 * restored when the new context is exited.
2247 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002249
2250 /**
2251 * Exit this context. Exiting the current context restores the
2252 * context that was in place when entering the current context.
2253 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 void Exit();
2255
v8.team.kasperl727e9952008-09-02 14:56:44 +00002256 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 bool HasOutOfMemoryException();
2258
v8.team.kasperl727e9952008-09-02 14:56:44 +00002259 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 static bool InContext();
2261
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 /**
2263 * Stack-allocated class which sets the execution context for all
2264 * operations executed within a local scope.
2265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002266 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 public:
2268 inline Scope(Handle<Context> context) : context_(context) {
2269 context_->Enter();
2270 }
2271 inline ~Scope() { context_->Exit(); }
2272 private:
2273 Handle<Context> context_;
2274 };
2275
2276 private:
2277 friend class Value;
2278 friend class Script;
2279 friend class Object;
2280 friend class Function;
2281};
2282
2283
2284/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002285 * Multiple threads in V8 are allowed, but only one thread at a time
2286 * is allowed to use V8. The definition of 'using V8' includes
2287 * accessing handles or holding onto object pointers obtained from V8
2288 * handles. It is up to the user of V8 to ensure (perhaps with
2289 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 *
2291 * If you wish to start using V8 in a thread you can do this by constructing
2292 * a v8::Locker object. After the code using V8 has completed for the
2293 * current thread you can call the destructor. This can be combined
2294 * with C++ scope-based construction as follows:
2295 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002296 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 * ...
2298 * {
2299 * v8::Locker locker;
2300 * ...
2301 * // Code using V8 goes here.
2302 * ...
2303 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002304 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 *
2306 * If you wish to stop using V8 in a thread A you can do this by either
2307 * by destroying the v8::Locker object as above or by constructing a
2308 * v8::Unlocker object:
2309 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002310 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 * {
2312 * v8::Unlocker unlocker;
2313 * ...
2314 * // Code not using V8 goes here while V8 can run in another thread.
2315 * ...
2316 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002317 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 *
2319 * The Unlocker object is intended for use in a long-running callback
2320 * from V8, where you want to release the V8 lock for other threads to
2321 * use.
2322 *
2323 * The v8::Locker is a recursive lock. That is, you can lock more than
2324 * once in a given thread. This can be useful if you have code that can
2325 * be called either from code that holds the lock or from code that does
2326 * not. The Unlocker is not recursive so you can not have several
2327 * Unlockers on the stack at once, and you can not use an Unlocker in a
2328 * thread that is not inside a Locker's scope.
2329 *
2330 * An unlocker will unlock several lockers if it has to and reinstate
2331 * the correct depth of locking on its destruction. eg.:
2332 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002333 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 * // V8 not locked.
2335 * {
2336 * v8::Locker locker;
2337 * // V8 locked.
2338 * {
2339 * v8::Locker another_locker;
2340 * // V8 still locked (2 levels).
2341 * {
2342 * v8::Unlocker unlocker;
2343 * // V8 not locked.
2344 * }
2345 * // V8 locked again (2 levels).
2346 * }
2347 * // V8 still locked (1 level).
2348 * }
2349 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002350 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002352class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 public:
2354 Unlocker();
2355 ~Unlocker();
2356};
2357
2358
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002359class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 public:
2361 Locker();
2362 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002363
2364 /**
2365 * Start preemption.
2366 *
2367 * When preemption is started, a timer is fired every n milli seconds
2368 * that will switch between multiple threads that are in contention
2369 * for the V8 lock.
2370 */
2371 static void StartPreemption(int every_n_ms);
2372
2373 /**
2374 * Stop preemption.
2375 */
2376 static void StopPreemption();
2377
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002378 /**
2379 * Returns whether or not the locker is locked by the current thread.
2380 */
2381 static bool IsLocked();
2382
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002383 /**
2384 * Returns whether v8::Locker is being used by this V8 instance.
2385 */
2386 static bool IsActive() { return active_; }
2387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 private:
2389 bool has_lock_;
2390 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002391
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002392 static bool active_;
2393
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002394 // Disallow copying and assigning.
2395 Locker(const Locker&);
2396 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397};
2398
2399
2400
2401// --- I m p l e m e n t a t i o n ---
2402
2403template <class T>
2404Handle<T>::Handle() : val_(0) { }
2405
2406
2407template <class T>
2408Local<T>::Local() : Handle<T>() { }
2409
2410
2411template <class T>
2412Local<T> Local<T>::New(Handle<T> that) {
2413 if (that.IsEmpty()) return Local<T>();
2414 void** p = reinterpret_cast<void**>(*that);
2415 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2416}
2417
2418
2419template <class T>
2420Persistent<T> Persistent<T>::New(Handle<T> that) {
2421 if (that.IsEmpty()) return Persistent<T>();
2422 void** p = reinterpret_cast<void**>(*that);
2423 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2424}
2425
2426
2427template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002428bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 if (this->IsEmpty()) return false;
2430 return V8::IsGlobalNearDeath(reinterpret_cast<void**>(**this));
2431}
2432
2433
2434template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002435bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 if (this->IsEmpty()) return false;
2437 return V8::IsGlobalWeak(reinterpret_cast<void**>(**this));
2438}
2439
2440
2441template <class T>
2442void Persistent<T>::Dispose() {
2443 if (this->IsEmpty()) return;
2444 V8::DisposeGlobal(reinterpret_cast<void**>(**this));
2445}
2446
2447
2448template <class T>
2449Persistent<T>::Persistent() : Handle<T>() { }
2450
2451template <class T>
2452void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
2453 V8::MakeWeak(reinterpret_cast<void**>(**this), parameters, callback);
2454}
2455
2456template <class T>
2457void Persistent<T>::ClearWeak() {
2458 V8::ClearWeak(reinterpret_cast<void**>(**this));
2459}
2460
2461template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002462T* Handle<T>::operator->() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463 return val_;
2464}
2465
2466
2467template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002468T* Handle<T>::operator*() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469 return val_;
2470}
2471
2472
2473Local<Value> Arguments::operator[](int i) const {
2474 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2475 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2476}
2477
2478
2479Local<Function> Arguments::Callee() const {
2480 return callee_;
2481}
2482
2483
2484Local<Object> Arguments::This() const {
2485 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2486}
2487
2488
2489Local<Object> Arguments::Holder() const {
2490 return holder_;
2491}
2492
2493
2494Local<Value> Arguments::Data() const {
2495 return data_;
2496}
2497
2498
2499bool Arguments::IsConstructCall() const {
2500 return is_construct_call_;
2501}
2502
2503
2504int Arguments::Length() const {
2505 return length_;
2506}
2507
2508
2509Local<Value> AccessorInfo::Data() const {
2510 return data_;
2511}
2512
2513
2514Local<Object> AccessorInfo::This() const {
2515 return self_;
2516}
2517
2518
2519Local<Object> AccessorInfo::Holder() const {
2520 return holder_;
2521}
2522
2523
2524template <class T>
2525Local<T> HandleScope::Close(Handle<T> value) {
2526 void** after = RawClose(reinterpret_cast<void**>(*value));
2527 return Local<T>(reinterpret_cast<T*>(after));
2528}
2529
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002530Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 return resource_name_;
2532}
2533
2534
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002535Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 return resource_line_offset_;
2537}
2538
2539
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002540Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 return resource_column_offset_;
2542}
2543
2544
2545Handle<Boolean> Boolean::New(bool value) {
2546 return value ? True() : False();
2547}
2548
2549
2550void Template::Set(const char* name, v8::Handle<Data> value) {
2551 Set(v8::String::New(name), value);
2552}
2553
2554
2555/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002556 * \example shell.cc
2557 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 * command-line and executes them.
2559 */
2560
2561
2562/**
2563 * \example process.cc
2564 */
2565
2566
2567} // namespace v8
2568
2569
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002570#undef V8EXPORT
2571#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572#undef TYPE_CHECK
2573
2574
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002575#endif // V8_H_