blob: 7dbab3031c2f8252967a04f6a86fb3f823bdf18d [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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
ager@chromium.org5ec48922009-05-05 07:25:34 +000048typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052typedef int int32_t;
53typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000054typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058
59// Setup for Windows DLL export/import. When building the V8 DLL the
60// BUILDING_V8_SHARED needs to be defined. When building a program which uses
61// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
62// static library or building a program which uses the V8 static library neither
63// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000064// The reason for having both V8EXPORT and V8EXPORT_INLINE is that classes which
65// have their code inside this header file need to have __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066// when building the DLL but cannot have __declspec(dllimport) when building
67// a program which uses the DLL.
68#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
69#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
70 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071#endif
72
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT __declspec(dllexport)
75#define V8EXPORT_INLINE __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT __declspec(dllimport)
78#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000080#define V8EXPORT
81#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#endif // BUILDING_V8_SHARED
83
84#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000085
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086#include <stdint.h>
87
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000088// Setup for Linux shared library export. There is no need to distinguish
89// between building or using the V8 shared library, but we should not
90// export symbols when we are building a static library.
91#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000092#define V8EXPORT __attribute__ ((visibility("default")))
93#define V8EXPORT_INLINE __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000094#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095#define V8EXPORT
96#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000097#endif // defined(__GNUC__) && (__GNUC__ >= 4)
98
99#endif // _WIN32
100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000102 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 */
104namespace v8 {
105
106class Context;
107class String;
108class Value;
109class Utils;
110class Number;
111class Object;
112class Array;
113class Int32;
114class Uint32;
115class External;
116class Primitive;
117class Boolean;
118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
123template <class T> class Handle;
124template <class T> class Local;
125template <class T> class Persistent;
126class FunctionTemplate;
127class ObjectTemplate;
128class Data;
129
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000130namespace internal {
131
132class Object;
133
134}
135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
137// --- W e a k H a n d l e s
138
139
140/**
141 * A weak reference callback function.
142 *
143 * \param object the weak global object to be reclaimed by the garbage collector
144 * \param parameter the value passed in when making the weak global object
145 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000146typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 void* parameter);
148
149
150// --- H a n d l e s ---
151
152#define TYPE_CHECK(T, S) \
153 while (false) { \
154 *(static_cast<T**>(0)) = static_cast<S*>(0); \
155 }
156
157/**
158 * An object reference managed by the v8 garbage collector.
159 *
160 * All objects returned from v8 have to be tracked by the garbage
161 * collector so that it knows that the objects are still alive. Also,
162 * because the garbage collector may move objects, it is unsafe to
163 * point directly to an object. Instead, all objects are stored in
164 * handles which are known by the garbage collector and updated
165 * whenever an object moves. Handles should always be passed by value
166 * (except in cases like out-parameters) and they should never be
167 * allocated on the heap.
168 *
169 * There are two types of handles: local and persistent handles.
170 * Local handles are light-weight and transient and typically used in
171 * local operations. They are managed by HandleScopes. Persistent
172 * handles can be used when storing objects across several independent
173 * operations and have to be explicitly deallocated when they're no
174 * longer used.
175 *
176 * It is safe to extract the object stored in the handle by
177 * dereferencing the handle (for instance, to extract the Object* from
178 * an Handle<Object>); the value will still be governed by a handle
179 * behind the scenes and the same rules apply to these values as to
180 * their handles.
181 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000182template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 public:
184
185 /**
186 * Creates an empty handle.
187 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000188 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189
190 /**
191 * Creates a new handle for the specified value.
192 */
193 explicit Handle(T* val) : val_(val) { }
194
195 /**
196 * Creates a handle for the contents of the specified handle. This
197 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000198 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 * incompatible handles, for instance from a Handle<String> to a
200 * Handle<Number> it will cause a compiletime error. Assigning
201 * between compatible handles, for instance assigning a
202 * Handle<String> to a variable declared as Handle<Value>, is legal
203 * because String is a subclass of Value.
204 */
205 template <class S> inline Handle(Handle<S> that)
206 : val_(reinterpret_cast<T*>(*that)) {
207 /**
208 * This check fails when trying to convert between incompatible
209 * handles. For example, converting from a Handle<String> to a
210 * Handle<Number>.
211 */
212 TYPE_CHECK(T, S);
213 }
214
215 /**
216 * Returns true if the handle is empty.
217 */
ager@chromium.org32912102009-01-16 10:38:43 +0000218 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000220 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000222 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
224 /**
225 * Sets the handle to be empty. IsEmpty() will then return true.
226 */
227 void Clear() { this->val_ = 0; }
228
229 /**
230 * Checks whether two handles are the same.
231 * Returns true if both are empty, or if the objects
232 * to which they refer are identical.
233 * The handles' references are not checked.
234 */
ager@chromium.org32912102009-01-16 10:38:43 +0000235 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000236 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
237 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 if (a == 0) return b == 0;
239 if (b == 0) return false;
240 return *a == *b;
241 }
242
243 /**
244 * Checks whether two handles are different.
245 * Returns true if only one of the handles is empty, or if
246 * the objects to which they refer are different.
247 * The handles' references are not checked.
248 */
ager@chromium.org32912102009-01-16 10:38:43 +0000249 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 return !operator==(that);
251 }
252
253 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000254#ifdef V8_ENABLE_CHECKS
255 // If we're going to perform the type check then we have to check
256 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000258#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 return Handle<T>(T::Cast(*that));
260 }
261
262 private:
263 T* val_;
264};
265
266
267/**
268 * A light-weight stack-allocated object handle. All operations
269 * that return objects from within v8 return them in local handles. They
270 * are created within HandleScopes, and all local handles allocated within a
271 * handle scope are destroyed when the handle scope is destroyed. Hence it
272 * is not necessary to explicitly deallocate local handles.
273 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000274template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000276 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 template <class S> inline Local(Local<S> that)
278 : Handle<T>(reinterpret_cast<T*>(*that)) {
279 /**
280 * This check fails when trying to convert between incompatible
281 * handles. For example, converting from a Handle<String> to a
282 * Handle<Number>.
283 */
284 TYPE_CHECK(T, S);
285 }
286 template <class S> inline Local(S* that) : Handle<T>(that) { }
287 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000288#ifdef V8_ENABLE_CHECKS
289 // If we're going to perform the type check then we have to check
290 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000292#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 return Local<T>(T::Cast(*that));
294 }
295
296 /** Create a local handle for the content of another handle.
297 * The referee is kept alive by the local handle even when
298 * the original handle is destroyed/disposed.
299 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000300 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301};
302
303
304/**
305 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000306 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 * allocated, a Persistent handle remains valid until it is explicitly
308 * disposed.
309 *
310 * A persistent handle contains a reference to a storage cell within
311 * the v8 engine which holds an object value and which is updated by
312 * the garbage collector whenever the object is moved. A new storage
313 * cell can be created using Persistent::New and existing handles can
314 * be disposed using Persistent::Dispose. Since persistent handles
315 * are passed by value you may have many persistent handle objects
316 * that point to the same storage cell. For instance, if you pass a
317 * persistent handle as an argument to a function you will not get two
318 * different storage cells but rather two references to the same
319 * storage cell.
320 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000321template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 public:
323
324 /**
325 * Creates an empty persistent handle that doesn't point to any
326 * storage cell.
327 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000328 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329
330 /**
331 * Creates a persistent handle for the same storage cell as the
332 * specified handle. This constructor allows you to pass persistent
333 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000334 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000336 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 * between compatible persistent handles, for instance assigning a
338 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000339 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 */
341 template <class S> inline Persistent(Persistent<S> that)
342 : Handle<T>(reinterpret_cast<T*>(*that)) {
343 /**
344 * This check fails when trying to convert between incompatible
345 * handles. For example, converting from a Handle<String> to a
346 * Handle<Number>.
347 */
348 TYPE_CHECK(T, S);
349 }
350
351 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
352
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000353 /**
354 * "Casts" a plain handle which is known to be a persistent handle
355 * to a persistent handle.
356 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 template <class S> explicit inline Persistent(Handle<S> that)
358 : Handle<T>(*that) { }
359
360 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000361#ifdef V8_ENABLE_CHECKS
362 // If we're going to perform the type check then we have to check
363 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000365#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 return Persistent<T>(T::Cast(*that));
367 }
368
369 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000370 * Creates a new persistent handle for an existing local or
371 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000373 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374
375 /**
376 * Releases the storage cell referenced by this persistent handle.
377 * Does not remove the reference to the cell from any handles.
378 * This handle's reference, and any any other references to the storage
379 * cell remain and IsEmpty will still return false.
380 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000381 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382
383 /**
384 * Make the reference to this object weak. When only weak handles
385 * refer to the object, the garbage collector will perform a
386 * callback to the given V8::WeakReferenceCallback function, passing
387 * it the object reference and the given parameters.
388 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000389 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390
391 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000392 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
394 /**
395 *Checks if the handle holds the only reference to an object.
396 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000397 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398
399 /**
400 * Returns true if the handle's reference is weak.
401 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000402 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403
404 private:
405 friend class ImplementationUtilities;
406 friend class ObjectTemplate;
407};
408
409
v8.team.kasperl727e9952008-09-02 14:56:44 +0000410 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 * A stack-allocated class that governs a number of local handles.
412 * After a handle scope has been created, all local handles will be
413 * allocated within that handle scope until either the handle scope is
414 * deleted or another handle scope is created. If there is already a
415 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000416 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 * new handles will again be allocated in the original handle scope.
418 *
419 * After the handle scope of a local handle has been deleted the
420 * garbage collector will no longer track the object stored in the
421 * handle and may deallocate it. The behavior of accessing a handle
422 * for which the handle scope has been deleted is undefined.
423 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000424class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000426 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000428 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
430 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 * Closes the handle scope and returns the value as a handle in the
432 * previous scope, which is the new current scope after the call.
433 */
434 template <class T> Local<T> Close(Handle<T> value);
435
436 /**
437 * Counts the number of allocated handles.
438 */
439 static int NumberOfHandles();
440
441 /**
442 * Creates a new handle with the given value.
443 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000444 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
446 private:
447 // Make it impossible to create heap-allocated or illegal handle
448 // scopes by disallowing certain operations.
449 HandleScope(const HandleScope&);
450 void operator=(const HandleScope&);
451 void* operator new(size_t size);
452 void operator delete(void*, size_t);
453
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000454 // This Data class is accessible internally through a typedef in the
455 // ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000456 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 public:
458 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000459 internal::Object** next;
460 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 inline void Initialize() {
462 extensions = -1;
463 next = limit = NULL;
464 }
465 };
466
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000467 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000469 // Allow for the active closing of HandleScopes which allows to pass a handle
470 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000472 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 friend class ImplementationUtilities;
475};
476
477
478// --- S p e c i a l o b j e c t s ---
479
480
481/**
482 * The superclass of values and API object templates.
483 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000484class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 private:
486 Data();
487};
488
489
490/**
491 * Pre-compilation data that can be associated with a script. This
492 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000493 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000494 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000496class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 public:
498 virtual ~ScriptData() { }
499 static ScriptData* PreCompile(const char* input, int length);
500 static ScriptData* New(unsigned* data, int length);
501
502 virtual int Length() = 0;
503 virtual unsigned* Data() = 0;
504};
505
506
507/**
508 * The origin, within a file, of a script.
509 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000510class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000512 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 Handle<Integer> resource_line_offset = Handle<Integer>(),
514 Handle<Integer> resource_column_offset = Handle<Integer>())
515 : resource_name_(resource_name),
516 resource_line_offset_(resource_line_offset),
517 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000518 inline Handle<Value> ResourceName() const;
519 inline Handle<Integer> ResourceLineOffset() const;
520 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000522 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 Handle<Integer> resource_line_offset_;
524 Handle<Integer> resource_column_offset_;
525};
526
527
528/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000529 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000531class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 public:
533
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000534 /**
535 * Compiles the specified script. The ScriptOrigin* and ScriptData*
536 * parameters are owned by the caller of Script::Compile. No
537 * references to these objects are kept after compilation finishes.
538 *
539 * The script object returned is context independent; when run it
540 * will use the currently entered context.
541 */
542 static Local<Script> New(Handle<String> source,
543 ScriptOrigin* origin = NULL,
544 ScriptData* pre_data = NULL);
545
546 /**
547 * Compiles the specified script using the specified file name
548 * object (typically a string) as the script's origin.
549 *
550 * The script object returned is context independent; when run it
551 * will use the currently entered context.
552 */
553 static Local<Script> New(Handle<String> source,
554 Handle<Value> file_name);
555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 /**
557 * Compiles the specified script. The ScriptOrigin* and ScriptData*
558 * parameters are owned by the caller of Script::Compile. No
559 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000560 *
561 * The script object returned is bound to the context that was active
562 * when this function was called. When run it will always use this
563 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 */
565 static Local<Script> Compile(Handle<String> source,
566 ScriptOrigin* origin = NULL,
567 ScriptData* pre_data = NULL);
568
mads.s.agercbaa0602008-08-14 13:41:48 +0000569 /**
570 * Compiles the specified script using the specified file name
571 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000572 *
573 * The script object returned is bound to the context that was active
574 * when this function was called. When run it will always use this
575 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000576 */
577 static Local<Script> Compile(Handle<String> source,
578 Handle<Value> file_name);
579
v8.team.kasperl727e9952008-09-02 14:56:44 +0000580 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000581 * Runs the script returning the resulting value. If the script is
582 * context independent (created using ::New) it will be run in the
583 * currently entered context. If it is context specific (created
584 * using ::Compile) it will be run in the context in which it was
585 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000586 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000588
589 /**
590 * Returns the script id value.
591 */
592 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000593
594 /**
595 * Associate an additional data object with the script. This is mainly used
596 * with the debugger as this data object is only available through the
597 * debugger API.
598 */
599 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600};
601
602
603/**
604 * An error message.
605 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000606class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000608 Local<String> Get() const;
609 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000611 /**
612 * Returns the resource name for the script from where the function causing
613 * the error originates.
614 */
ager@chromium.org32912102009-01-16 10:38:43 +0000615 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000617 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000618 * Returns the resource data for the script from where the function causing
619 * the error originates.
620 */
621 Handle<Value> GetScriptData() const;
622
623 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000624 * Returns the number, 1-based, of the line where the error occurred.
625 */
ager@chromium.org32912102009-01-16 10:38:43 +0000626 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000628 /**
629 * Returns the index within the script of the first character where
630 * the error occurred.
631 */
ager@chromium.org32912102009-01-16 10:38:43 +0000632 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000633
634 /**
635 * Returns the index within the script of the last character where
636 * the error occurred.
637 */
ager@chromium.org32912102009-01-16 10:38:43 +0000638 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000639
640 /**
641 * Returns the index within the line of the first character where
642 * the error occurred.
643 */
ager@chromium.org32912102009-01-16 10:38:43 +0000644 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000645
646 /**
647 * Returns the index within the line of the last character where
648 * the error occurred.
649 */
ager@chromium.org32912102009-01-16 10:38:43 +0000650 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 // TODO(1245381): Print to a string instead of on a FILE.
653 static void PrintCurrentStackTrace(FILE* out);
654};
655
656
657// --- V a l u e ---
658
659
660/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000663class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 public:
665
666 /**
667 * Returns true if this value is the undefined value. See ECMA-262
668 * 4.3.10.
669 */
ager@chromium.org32912102009-01-16 10:38:43 +0000670 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
672 /**
673 * Returns true if this value is the null value. See ECMA-262
674 * 4.3.11.
675 */
ager@chromium.org32912102009-01-16 10:38:43 +0000676 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677
678 /**
679 * Returns true if this value is true.
680 */
ager@chromium.org32912102009-01-16 10:38:43 +0000681 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682
683 /**
684 * Returns true if this value is false.
685 */
ager@chromium.org32912102009-01-16 10:38:43 +0000686 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687
688 /**
689 * Returns true if this value is an instance of the String type.
690 * See ECMA-262 8.4.
691 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000692 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693
694 /**
695 * Returns true if this value is a function.
696 */
ager@chromium.org32912102009-01-16 10:38:43 +0000697 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
699 /**
700 * Returns true if this value is an array.
701 */
ager@chromium.org32912102009-01-16 10:38:43 +0000702 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703
v8.team.kasperl727e9952008-09-02 14:56:44 +0000704 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 * Returns true if this value is an object.
706 */
ager@chromium.org32912102009-01-16 10:38:43 +0000707 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708
v8.team.kasperl727e9952008-09-02 14:56:44 +0000709 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710 * Returns true if this value is boolean.
711 */
ager@chromium.org32912102009-01-16 10:38:43 +0000712 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000713
v8.team.kasperl727e9952008-09-02 14:56:44 +0000714 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 * Returns true if this value is a number.
716 */
ager@chromium.org32912102009-01-16 10:38:43 +0000717 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718
v8.team.kasperl727e9952008-09-02 14:56:44 +0000719 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 * Returns true if this value is external.
721 */
ager@chromium.org32912102009-01-16 10:38:43 +0000722 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723
v8.team.kasperl727e9952008-09-02 14:56:44 +0000724 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 * Returns true if this value is a 32-bit signed integer.
726 */
ager@chromium.org32912102009-01-16 10:38:43 +0000727 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000729 /**
730 * Returns true if this value is a Date.
731 */
ager@chromium.org32912102009-01-16 10:38:43 +0000732 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000733
ager@chromium.org32912102009-01-16 10:38:43 +0000734 Local<Boolean> ToBoolean() const;
735 Local<Number> ToNumber() const;
736 Local<String> ToString() const;
737 Local<String> ToDetailString() const;
738 Local<Object> ToObject() const;
739 Local<Integer> ToInteger() const;
740 Local<Uint32> ToUint32() const;
741 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
743 /**
744 * Attempts to convert a string to an array index.
745 * Returns an empty handle if the conversion fails.
746 */
ager@chromium.org32912102009-01-16 10:38:43 +0000747 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
ager@chromium.org32912102009-01-16 10:38:43 +0000749 bool BooleanValue() const;
750 double NumberValue() const;
751 int64_t IntegerValue() const;
752 uint32_t Uint32Value() const;
753 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
755 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000756 bool Equals(Handle<Value> that) const;
757 bool StrictEquals(Handle<Value> that) const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000758
759 private:
760 inline bool QuickIsString() const;
761 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762};
763
764
765/**
766 * The superclass of primitive values. See ECMA-262 4.3.2.
767 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000768class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769
770
771/**
772 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
773 * or false value.
774 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000775class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000777 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 static inline Handle<Boolean> New(bool value);
779};
780
781
782/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000783 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000785class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000787
788 /**
789 * Returns the number of characters in this string.
790 */
ager@chromium.org32912102009-01-16 10:38:43 +0000791 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792
v8.team.kasperl727e9952008-09-02 14:56:44 +0000793 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000794 * Returns the number of bytes in the UTF-8 encoded
795 * representation of this string.
796 */
ager@chromium.org32912102009-01-16 10:38:43 +0000797 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000798
799 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000800 * Write the contents of the string to an external buffer.
801 * If no arguments are given, expects the buffer to be large
802 * enough to hold the entire string and NULL terminator. Copies
803 * the contents of the string and the NULL terminator into the
804 * buffer.
805 *
806 * Copies up to length characters into the output buffer.
807 * Only null-terminates if there is enough space in the buffer.
808 *
809 * \param buffer The buffer into which the string will be copied.
810 * \param start The starting position within the string at which
811 * copying begins.
812 * \param length The number of bytes to copy from the string.
813 * \return The number of characters copied to the buffer
814 * excluding the NULL terminator.
815 */
ager@chromium.org32912102009-01-16 10:38:43 +0000816 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
817 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
818 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819
v8.team.kasperl727e9952008-09-02 14:56:44 +0000820 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000821 * A zero length string.
822 */
823 static v8::Local<v8::String> Empty();
824
825 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000826 * Returns true if the string is external
827 */
ager@chromium.org32912102009-01-16 10:38:43 +0000828 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829
v8.team.kasperl727e9952008-09-02 14:56:44 +0000830 /**
831 * Returns true if the string is both external and ascii
832 */
ager@chromium.org32912102009-01-16 10:38:43 +0000833 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000834 /**
835 * An ExternalStringResource is a wrapper around a two-byte string
836 * buffer that resides outside V8's heap. Implement an
837 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000838 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000839 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000840 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841 public:
842 /**
843 * Override the destructor to manage the life cycle of the underlying
844 * buffer.
845 */
846 virtual ~ExternalStringResource() {}
847 /** The string data from the underlying buffer.*/
848 virtual const uint16_t* data() const = 0;
849 /** The length of the string. That is, the number of two-byte characters.*/
850 virtual size_t length() const = 0;
851 protected:
852 ExternalStringResource() {}
853 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000854 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 ExternalStringResource(const ExternalStringResource&);
856 void operator=(const ExternalStringResource&);
857 };
858
859 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000860 * An ExternalAsciiStringResource is a wrapper around an ascii
861 * string buffer that resides outside V8's heap. Implement an
862 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000863 * underlying buffer. Note that the string data must be immutable
864 * and that the data must be strict 7-bit ASCII, not Latin1 or
865 * UTF-8, which would require special treatment internally in the
866 * engine and, in the case of UTF-8, do not allow efficient indexing.
867 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000868 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000870 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 public:
872 /**
873 * Override the destructor to manage the life cycle of the underlying
874 * buffer.
875 */
876 virtual ~ExternalAsciiStringResource() {}
877 /** The string data from the underlying buffer.*/
878 virtual const char* data() const = 0;
879 /** The number of ascii characters in the string.*/
880 virtual size_t length() const = 0;
881 protected:
882 ExternalAsciiStringResource() {}
883 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000884 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
886 void operator=(const ExternalAsciiStringResource&);
887 };
888
889 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000890 * Get the ExternalStringResource for an external string. Returns
891 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000893 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894
895 /**
896 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000897 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 */
ager@chromium.org32912102009-01-16 10:38:43 +0000899 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000901 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
903 /**
904 * Allocates a new string from either utf-8 encoded or ascii data.
905 * The second parameter 'length' gives the buffer length.
906 * If the data is utf-8 encoded, the caller must
907 * be careful to supply the length parameter.
908 * If it is not given, the function calls
909 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000910 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 */
912 static Local<String> New(const char* data, int length = -1);
913
914 /** Allocates a new string from utf16 data.*/
915 static Local<String> New(const uint16_t* data, int length = -1);
916
917 /** Creates a symbol. Returns one if it exists already.*/
918 static Local<String> NewSymbol(const char* data, int length = -1);
919
v8.team.kasperl727e9952008-09-02 14:56:44 +0000920 /**
921 * Creates a new external string using the data defined in the given
922 * resource. The resource is deleted when the external string is no
923 * longer live on V8's heap. The caller of this function should not
924 * delete or modify the resource. Neither should the underlying buffer be
925 * deallocated or modified except through the destructor of the
926 * external string resource.
927 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000929
ager@chromium.org6f10e412009-02-13 10:11:16 +0000930 /**
931 * Associate an external string resource with this string by transforming it
932 * in place so that existing references to this string in the JavaScript heap
933 * will use the external string resource. The external string resource's
934 * character contents needs to be equivalent to this string.
935 * Returns true if the string has been changed to be an external string.
936 * The string is not modified if the operation fails.
937 */
938 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
v8.team.kasperl727e9952008-09-02 14:56:44 +0000940 /**
941 * Creates a new external string using the ascii data defined in the given
942 * resource. The resource is deleted when the external string is no
943 * longer live on V8's heap. The caller of this function should not
944 * delete or modify the resource. Neither should the underlying buffer be
945 * deallocated or modified except through the destructor of the
946 * external string resource.
947 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000949
ager@chromium.org6f10e412009-02-13 10:11:16 +0000950 /**
951 * Associate an external string resource with this string by transforming it
952 * in place so that existing references to this string in the JavaScript heap
953 * will use the external string resource. The external string resource's
954 * character contents needs to be equivalent to this string.
955 * Returns true if the string has been changed to be an external string.
956 * The string is not modified if the operation fails.
957 */
958 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000960 /**
961 * Returns true if this string can be made external.
962 */
963 bool CanMakeExternal();
964
kasper.lund7276f142008-07-30 08:49:36 +0000965 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 static Local<String> NewUndetectable(const char* data, int length = -1);
967
kasper.lund7276f142008-07-30 08:49:36 +0000968 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
970
971 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000972 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000973 * you want to print the object. If conversion to a string fails
974 * (eg. due to an exception in the toString() method of the object)
975 * then the length() method returns 0 and the * operator returns
976 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000977 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000978 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000979 public:
980 explicit Utf8Value(Handle<v8::Value> obj);
981 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000982 char* operator*() { return str_; }
983 const char* operator*() const { return str_; }
984 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000985 private:
986 char* str_;
987 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000988
989 // Disallow copying and assigning.
990 Utf8Value(const Utf8Value&);
991 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000992 };
993
994 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 * Converts an object to an ascii string.
996 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000997 * If conversion to a string fails (eg. due to an exception in the toString()
998 * method of the object) then the length() method returns 0 and the * operator
999 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001001 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 public:
1003 explicit AsciiValue(Handle<v8::Value> obj);
1004 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001005 char* operator*() { return str_; }
1006 const char* operator*() const { return str_; }
1007 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 private:
1009 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001010 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001011
1012 // Disallow copying and assigning.
1013 AsciiValue(const AsciiValue&);
1014 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 };
1016
1017 /**
1018 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001019 * If conversion to a string fails (eg. due to an exception in the toString()
1020 * method of the object) then the length() method returns 0 and the * operator
1021 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001023 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 public:
1025 explicit Value(Handle<v8::Value> obj);
1026 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001027 uint16_t* operator*() { return str_; }
1028 const uint16_t* operator*() const { return str_; }
1029 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030 private:
1031 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001032 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001033
1034 // Disallow copying and assigning.
1035 Value(const Value&);
1036 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 };
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001038
1039 private:
1040 void VerifyExternalStringResource(ExternalStringResource* val) const;
1041 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042};
1043
1044
1045/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001046 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001048class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001050 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001052 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 private:
1054 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001055 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056};
1057
1058
1059/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001060 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001062class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 public:
1064 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001065 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001066 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 private:
1068 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001069 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070};
1071
1072
1073/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001074 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001076class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001078 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 private:
1080 Int32();
1081};
1082
1083
1084/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001085 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001087class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001089 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 private:
1091 Uint32();
1092};
1093
1094
1095/**
1096 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1097 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001098class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 public:
1100 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001101
1102 /**
1103 * A specialization of Value::NumberValue that is more efficient
1104 * because we know the structure of this object.
1105 */
ager@chromium.org32912102009-01-16 10:38:43 +00001106 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001107
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001108 static inline Date* Cast(v8::Value* obj);
1109 private:
1110 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111};
1112
1113
1114enum PropertyAttribute {
1115 None = 0,
1116 ReadOnly = 1 << 0,
1117 DontEnum = 1 << 1,
1118 DontDelete = 1 << 2
1119};
1120
1121/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001122 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001124class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 public:
1126 bool Set(Handle<Value> key,
1127 Handle<Value> value,
1128 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001129
ager@chromium.orge2902be2009-06-08 12:21:35 +00001130 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001131 // overriding accessors or read-only properties.
1132 //
1133 // Note that if the object has an interceptor the property will be set
1134 // locally, but since the interceptor takes precedence the local property
1135 // will only be returned if the interceptor doesn't return a value.
1136 //
1137 // Note also that this only works for named properties.
1138 bool ForceSet(Handle<Value> key,
1139 Handle<Value> value,
1140 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 Local<Value> Get(Handle<Value> key);
1143
1144 // TODO(1245389): Replace the type-specific versions of these
1145 // functions with generic ones that accept a Handle<Value> key.
1146 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001149
1150 // Delete a property on this object bypassing interceptors and
1151 // ignoring dont-delete attributes.
1152 bool ForceDelete(Handle<Value> key);
1153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 bool Delete(uint32_t index);
1157
1158 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001159 * Returns an array containing the names of the enumerable properties
1160 * of this object, including properties from prototype objects. The
1161 * array returned by this method contains the same values as would
1162 * be enumerated by a for-in statement over this object.
1163 */
1164 Local<Array> GetPropertyNames();
1165
1166 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 * Get the prototype object. This does not skip objects marked to
1168 * be skipped by __proto__ and it does not consult the security
1169 * handler.
1170 */
1171 Local<Value> GetPrototype();
1172
1173 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001174 * Finds an instance of the given function template in the prototype
1175 * chain.
1176 */
1177 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1178
1179 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 * Call builtin Object.prototype.toString on this object.
1181 * This is different from Value::ToString() that may call
1182 * user-defined toString function. This one does not.
1183 */
1184 Local<String> ObjectProtoToString();
1185
kasper.lund212ac232008-07-16 07:07:30 +00001186 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001188 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001189 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001190 /** Sets the value in an internal field. */
1191 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001193 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001194 inline void* GetPointerFromInternalField(int index);
1195
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001196 /** Sets a native pointer in an internal field. */
1197 void SetPointerInInternalField(int index, void* value);
1198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 // Testers for local properties.
1200 bool HasRealNamedProperty(Handle<String> key);
1201 bool HasRealIndexedProperty(uint32_t index);
1202 bool HasRealNamedCallbackProperty(Handle<String> key);
1203
1204 /**
1205 * If result.IsEmpty() no real property was located in the prototype chain.
1206 * This means interceptors in the prototype chain are not called.
1207 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001208 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1209
1210 /**
1211 * If result.IsEmpty() no real property was located on the object or
1212 * in the prototype chain.
1213 * This means interceptors in the prototype chain are not called.
1214 */
1215 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216
1217 /** Tests for a named lookup interceptor.*/
1218 bool HasNamedLookupInterceptor();
1219
kasper.lund212ac232008-07-16 07:07:30 +00001220 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221 bool HasIndexedLookupInterceptor();
1222
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001223 /**
1224 * Turns on access check on the object if the object is an instance of
1225 * a template that has access check callbacks. If an object has no
1226 * access check info, the object cannot be accessed by anyone.
1227 */
1228 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001229
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001230 /**
1231 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001232 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001233 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001234 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001235 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001236 */
1237 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001238
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001239 /**
1240 * Access hidden properties on JavaScript objects. These properties are
1241 * hidden from the executing JavaScript and only accessible through the V8
1242 * C++ API. Hidden properties introduced by V8 internally (for example the
1243 * identity hash) are prefixed with "v8::".
1244 */
1245 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1246 Local<Value> GetHiddenValue(Handle<String> key);
1247 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001248
1249 /**
1250 * Returns true if this is an instance of an api function (one
1251 * created from a function created from a function template) and has
1252 * been modified since it was created. Note that this method is
1253 * conservative and may return true for objects that haven't actually
1254 * been modified.
1255 */
1256 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001257
1258 /**
1259 * Clone this object with a fast but shallow copy. Values will point
1260 * to the same values as the original object.
1261 */
1262 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001264 /**
1265 * Set the backing store of the indexed properties to be managed by the
1266 * embedding layer. Access to the indexed properties will follow the rules
1267 * spelled out in CanvasPixelArray.
1268 * Note: The embedding program still owns the data and needs to ensure that
1269 * the backing store is preserved while V8 has a reference.
1270 */
1271 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001274 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 private:
1276 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001277 static void CheckCast(Value* obj);
1278 Local<Value> CheckedGetInternalField(int index);
1279
1280 /**
1281 * If quick access to the internal field is possible this method
1282 * returns the value. Otherwise an empty handle is returned.
1283 */
1284 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285};
1286
1287
1288/**
1289 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1290 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001291class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001293 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
ager@chromium.org3e875802009-06-29 08:26:34 +00001295 /**
1296 * Clones an element at index |index|. Returns an empty
1297 * handle if cloning fails (for any reason).
1298 */
1299 Local<Object> CloneElementAt(uint32_t index);
1300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001302 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 private:
1304 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001305 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306};
1307
1308
1309/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001310 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001312class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001314 Local<Object> NewInstance() const;
1315 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1317 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001318 Handle<Value> GetName() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001319 static inline Function* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 private:
1321 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001322 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323};
1324
1325
1326/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001327 * A JavaScript value that wraps a C++ void*. This type of value is
1328 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001330 *
1331 * The Wrap function V8 will return the most optimal Value object wrapping the
1332 * C++ void*. The type of the value is not guaranteed to be an External object
1333 * and no assumptions about its type should be made. To access the wrapped
1334 * value Unwrap should be used, all other operations on that object will lead
1335 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001337class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001339 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001340 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001341
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001343 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001344 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 private:
1346 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001347 static void CheckCast(v8::Value* obj);
1348 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1349 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350};
1351
1352
1353// --- T e m p l a t e s ---
1354
1355
1356/**
1357 * The superclass of object and function templates.
1358 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001359class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 public:
1361 /** Adds a property to each instance created by this template.*/
1362 void Set(Handle<String> name, Handle<Data> value,
1363 PropertyAttribute attributes = None);
1364 inline void Set(const char* name, Handle<Data> value);
1365 private:
1366 Template();
1367
1368 friend class ObjectTemplate;
1369 friend class FunctionTemplate;
1370};
1371
1372
1373/**
1374 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001375 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 * including the receiver, the number and values of arguments, and
1377 * the holder of the function.
1378 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001379class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 public:
1381 inline int Length() const;
1382 inline Local<Value> operator[](int i) const;
1383 inline Local<Function> Callee() const;
1384 inline Local<Object> This() const;
1385 inline Local<Object> Holder() const;
1386 inline bool IsConstructCall() const;
1387 inline Local<Value> Data() const;
1388 private:
1389 Arguments();
1390 friend class ImplementationUtilities;
1391 inline Arguments(Local<Value> data,
1392 Local<Object> holder,
1393 Local<Function> callee,
1394 bool is_construct_call,
1395 void** values, int length);
1396 Local<Value> data_;
1397 Local<Object> holder_;
1398 Local<Function> callee_;
1399 bool is_construct_call_;
1400 void** values_;
1401 int length_;
1402};
1403
1404
1405/**
1406 * The information passed to an accessor callback about the context
1407 * of the property access.
1408 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001409class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 public:
1411 inline AccessorInfo(Local<Object> self,
1412 Local<Value> data,
1413 Local<Object> holder)
1414 : self_(self), data_(data), holder_(holder) { }
1415 inline Local<Value> Data() const;
1416 inline Local<Object> This() const;
1417 inline Local<Object> Holder() const;
1418 private:
1419 Local<Object> self_;
1420 Local<Value> data_;
1421 Local<Object> holder_;
1422};
1423
1424
1425typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1426
1427typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1428
1429/**
1430 * Accessor[Getter|Setter] are used as callback functions when
1431 * setting|getting a particular property. See objectTemplate::SetAccessor.
1432 */
1433typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1434 const AccessorInfo& info);
1435
1436
1437typedef void (*AccessorSetter)(Local<String> property,
1438 Local<Value> value,
1439 const AccessorInfo& info);
1440
1441
1442/**
1443 * NamedProperty[Getter|Setter] are used as interceptors on object.
1444 * See ObjectTemplate::SetNamedPropertyHandler.
1445 */
1446typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1447 const AccessorInfo& info);
1448
1449
1450/**
1451 * Returns the value if the setter intercepts the request.
1452 * Otherwise, returns an empty handle.
1453 */
1454typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1455 Local<Value> value,
1456 const AccessorInfo& info);
1457
1458
1459/**
1460 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001461 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 */
1463typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1464 const AccessorInfo& info);
1465
1466
1467/**
1468 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001469 * The return value is true if the property could be deleted and false
1470 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 */
1472typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1473 const AccessorInfo& info);
1474
1475/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001476 * Returns an array containing the names of the properties the named
1477 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 */
1479typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1480
v8.team.kasperl727e9952008-09-02 14:56:44 +00001481
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001483 * Returns the value of the property if the getter intercepts the
1484 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 */
1486typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1487 const AccessorInfo& info);
1488
1489
1490/**
1491 * Returns the value if the setter intercepts the request.
1492 * Otherwise, returns an empty handle.
1493 */
1494typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1495 Local<Value> value,
1496 const AccessorInfo& info);
1497
1498
1499/**
1500 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001501 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502 */
1503typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1504 const AccessorInfo& info);
1505
1506/**
1507 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001508 * The return value is true if the property could be deleted and false
1509 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510 */
1511typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1512 const AccessorInfo& info);
1513
v8.team.kasperl727e9952008-09-02 14:56:44 +00001514/**
1515 * Returns an array containing the indices of the properties the
1516 * indexed property getter intercepts.
1517 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1519
1520
1521/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001522 * Access control specifications.
1523 *
1524 * Some accessors should be accessible across contexts. These
1525 * accessors have an explicit access control parameter which specifies
1526 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001527 *
1528 * Additionally, for security, accessors can prohibit overwriting by
1529 * accessors defined in JavaScript. For objects that have such
1530 * accessors either locally or in their prototype chain it is not
1531 * possible to overwrite the accessor by using __defineGetter__ or
1532 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 */
1534enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001535 DEFAULT = 0,
1536 ALL_CAN_READ = 1,
1537 ALL_CAN_WRITE = 1 << 1,
1538 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539};
1540
1541
1542/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001543 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544 */
1545enum AccessType {
1546 ACCESS_GET,
1547 ACCESS_SET,
1548 ACCESS_HAS,
1549 ACCESS_DELETE,
1550 ACCESS_KEYS
1551};
1552
v8.team.kasperl727e9952008-09-02 14:56:44 +00001553
1554/**
1555 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001556 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001557 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001558typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 Local<Value> key,
1560 AccessType type,
1561 Local<Value> data);
1562
v8.team.kasperl727e9952008-09-02 14:56:44 +00001563
1564/**
1565 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001566 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001567 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001568typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569 uint32_t index,
1570 AccessType type,
1571 Local<Value> data);
1572
1573
1574/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001575 * A FunctionTemplate is used to create functions at runtime. There
1576 * can only be one function created from a FunctionTemplate in a
1577 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 *
1579 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001580 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001582 * A FunctionTemplate has a corresponding instance template which is
1583 * used to create object instances when the function is used as a
1584 * constructor. Properties added to the instance template are added to
1585 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 *
1587 * A FunctionTemplate can have a prototype template. The prototype template
1588 * is used to create the prototype object of the function.
1589 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001590 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001592 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1594 * t->Set("func_property", v8::Number::New(1));
1595 *
1596 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1597 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1598 * proto_t->Set("proto_const", v8::Number::New(2));
1599 *
1600 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1601 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1602 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1603 * instance_t->Set("instance_property", Number::New(3));
1604 *
1605 * v8::Local<v8::Function> function = t->GetFunction();
1606 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001607 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 *
1609 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001610 * and "instance" for the instance object created above. The function
1611 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001613 * \code
1614 * func_property in function == true;
1615 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001617 * function.prototype.proto_method() invokes 'InvokeCallback'
1618 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001620 * instance instanceof function == true;
1621 * instance.instance_accessor calls 'InstanceAccessorCallback'
1622 * instance.instance_property == 3;
1623 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001625 * A FunctionTemplate can inherit from another one by calling the
1626 * FunctionTemplate::Inherit method. The following graph illustrates
1627 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001629 * \code
1630 * FunctionTemplate Parent -> Parent() . prototype -> { }
1631 * ^ ^
1632 * | Inherit(Parent) | .__proto__
1633 * | |
1634 * FunctionTemplate Child -> Child() . prototype -> { }
1635 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001637 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1638 * object of the Child() function has __proto__ pointing to the
1639 * Parent() function's prototype object. An instance of the Child
1640 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001642 * Let Parent be the FunctionTemplate initialized in the previous
1643 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001645 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 * Local<FunctionTemplate> parent = t;
1647 * Local<FunctionTemplate> child = FunctionTemplate::New();
1648 * child->Inherit(parent);
1649 *
1650 * Local<Function> child_function = child->GetFunction();
1651 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001652 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001654 * The Child function and Child instance will have the following
1655 * properties:
1656 *
1657 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001659 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001661 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001663class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 public:
1665 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001666 static Local<FunctionTemplate> New(
1667 InvocationCallback callback = 0,
1668 Handle<Value> data = Handle<Value>(),
1669 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 /** Returns the unique function instance in the current execution context.*/
1671 Local<Function> GetFunction();
1672
v8.team.kasperl727e9952008-09-02 14:56:44 +00001673 /**
1674 * Set the call-handler callback for a FunctionTemplate. This
1675 * callback is called whenever the function created from this
1676 * FunctionTemplate is called.
1677 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 void SetCallHandler(InvocationCallback callback,
1679 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
v8.team.kasperl727e9952008-09-02 14:56:44 +00001681 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 Local<ObjectTemplate> InstanceTemplate();
1683
1684 /** Causes the function template to inherit from a parent function template.*/
1685 void Inherit(Handle<FunctionTemplate> parent);
1686
1687 /**
1688 * A PrototypeTemplate is the template used to create the prototype object
1689 * of the function created by this template.
1690 */
1691 Local<ObjectTemplate> PrototypeTemplate();
1692
v8.team.kasperl727e9952008-09-02 14:56:44 +00001693
1694 /**
1695 * Set the class name of the FunctionTemplate. This is used for
1696 * printing objects created with the function created from the
1697 * FunctionTemplate as its constructor.
1698 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 void SetClassName(Handle<String> name);
1700
1701 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001702 * Determines whether the __proto__ accessor ignores instances of
1703 * the function template. If instances of the function template are
1704 * ignored, __proto__ skips all instances and instead returns the
1705 * next object in the prototype chain.
1706 *
1707 * Call with a value of true to make the __proto__ accessor ignore
1708 * instances of the function template. Call with a value of false
1709 * to make the __proto__ accessor not ignore instances of the
1710 * function template. By default, instances of a function template
1711 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 */
1713 void SetHiddenPrototype(bool value);
1714
1715 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001716 * Returns true if the given object is an instance of this function
1717 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718 */
1719 bool HasInstance(Handle<Value> object);
1720
1721 private:
1722 FunctionTemplate();
1723 void AddInstancePropertyAccessor(Handle<String> name,
1724 AccessorGetter getter,
1725 AccessorSetter setter,
1726 Handle<Value> data,
1727 AccessControl settings,
1728 PropertyAttribute attributes);
1729 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1730 NamedPropertySetter setter,
1731 NamedPropertyQuery query,
1732 NamedPropertyDeleter remover,
1733 NamedPropertyEnumerator enumerator,
1734 Handle<Value> data);
1735 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1736 IndexedPropertySetter setter,
1737 IndexedPropertyQuery query,
1738 IndexedPropertyDeleter remover,
1739 IndexedPropertyEnumerator enumerator,
1740 Handle<Value> data);
1741 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1742 Handle<Value> data);
1743
1744 friend class Context;
1745 friend class ObjectTemplate;
1746};
1747
1748
1749/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001750 * An ObjectTemplate is used to create objects at runtime.
1751 *
1752 * Properties added to an ObjectTemplate are added to each object
1753 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001755class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001757 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001759
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 /** Creates a new instance of this template.*/
1761 Local<Object> NewInstance();
1762
1763 /**
1764 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001765 *
1766 * Whenever the property with the given name is accessed on objects
1767 * created from this ObjectTemplate the getter and setter callbacks
1768 * are called instead of getting and setting the property directly
1769 * on the JavaScript object.
1770 *
1771 * \param name The name of the property for which an accessor is added.
1772 * \param getter The callback to invoke when getting the property.
1773 * \param setter The callback to invoke when setting the property.
1774 * \param data A piece of data that will be passed to the getter and setter
1775 * callbacks whenever they are invoked.
1776 * \param settings Access control settings for the accessor. This is a bit
1777 * field consisting of one of more of
1778 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1779 * The default is to not allow cross-context access.
1780 * ALL_CAN_READ means that all cross-context reads are allowed.
1781 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1782 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1783 * cross-context access.
1784 * \param attribute The attributes of the property for which an accessor
1785 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 */
1787 void SetAccessor(Handle<String> name,
1788 AccessorGetter getter,
1789 AccessorSetter setter = 0,
1790 Handle<Value> data = Handle<Value>(),
1791 AccessControl settings = DEFAULT,
1792 PropertyAttribute attribute = None);
1793
1794 /**
1795 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001796 *
1797 * Whenever a named property is accessed on objects created from
1798 * this object template, the provided callback is invoked instead of
1799 * accessing the property directly on the JavaScript object.
1800 *
1801 * \param getter The callback to invoke when getting a property.
1802 * \param setter The callback to invoke when setting a property.
1803 * \param query The callback to invoke to check is an object has a property.
1804 * \param deleter The callback to invoke when deleting a property.
1805 * \param enumerator The callback to invoke to enumerate all the named
1806 * properties of an object.
1807 * \param data A piece of data that will be passed to the callbacks
1808 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 */
1810 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1811 NamedPropertySetter setter = 0,
1812 NamedPropertyQuery query = 0,
1813 NamedPropertyDeleter deleter = 0,
1814 NamedPropertyEnumerator enumerator = 0,
1815 Handle<Value> data = Handle<Value>());
1816
1817 /**
1818 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001819 *
1820 * Whenever an indexed property is accessed on objects created from
1821 * this object template, the provided callback is invoked instead of
1822 * accessing the property directly on the JavaScript object.
1823 *
1824 * \param getter The callback to invoke when getting a property.
1825 * \param setter The callback to invoke when setting a property.
1826 * \param query The callback to invoke to check is an object has a property.
1827 * \param deleter The callback to invoke when deleting a property.
1828 * \param enumerator The callback to invoke to enumerate all the indexed
1829 * properties of an object.
1830 * \param data A piece of data that will be passed to the callbacks
1831 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832 */
1833 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1834 IndexedPropertySetter setter = 0,
1835 IndexedPropertyQuery query = 0,
1836 IndexedPropertyDeleter deleter = 0,
1837 IndexedPropertyEnumerator enumerator = 0,
1838 Handle<Value> data = Handle<Value>());
1839 /**
1840 * Sets the callback to be used when calling instances created from
1841 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001842 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 * function.
1844 */
1845 void SetCallAsFunctionHandler(InvocationCallback callback,
1846 Handle<Value> data = Handle<Value>());
1847
v8.team.kasperl727e9952008-09-02 14:56:44 +00001848 /**
1849 * Mark object instances of the template as undetectable.
1850 *
1851 * In many ways, undetectable objects behave as though they are not
1852 * there. They behave like 'undefined' in conditionals and when
1853 * printed. However, properties can be accessed and called as on
1854 * normal objects.
1855 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 void MarkAsUndetectable();
1857
v8.team.kasperl727e9952008-09-02 14:56:44 +00001858 /**
1859 * Sets access check callbacks on the object template.
1860 *
1861 * When accessing properties on instances of this object template,
1862 * the access check callback will be called to determine whether or
1863 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001864 * The last parameter specifies whether access checks are turned
1865 * on by default on instances. If access checks are off by default,
1866 * they can be turned on on individual instances by calling
1867 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001868 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1870 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001871 Handle<Value> data = Handle<Value>(),
1872 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873
kasper.lund212ac232008-07-16 07:07:30 +00001874 /**
1875 * Gets the number of internal fields for objects generated from
1876 * this template.
1877 */
1878 int InternalFieldCount();
1879
1880 /**
1881 * Sets the number of internal fields for objects generated from
1882 * this template.
1883 */
1884 void SetInternalFieldCount(int value);
1885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 private:
1887 ObjectTemplate();
1888 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1889 friend class FunctionTemplate;
1890};
1891
1892
1893/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001894 * A Signature specifies which receivers and arguments a function can
1895 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001897class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 public:
1899 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1900 Handle<FunctionTemplate>(),
1901 int argc = 0,
1902 Handle<FunctionTemplate> argv[] = 0);
1903 private:
1904 Signature();
1905};
1906
1907
1908/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001909 * A utility for determining the type of objects based on the template
1910 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001912class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 public:
1914 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1915 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1916 int match(Handle<Value> value);
1917 private:
1918 TypeSwitch();
1919};
1920
1921
1922// --- E x t e n s i o n s ---
1923
1924
1925/**
1926 * Ignore
1927 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001928class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 public:
1930 Extension(const char* name,
1931 const char* source = 0,
1932 int dep_count = 0,
1933 const char** deps = 0);
1934 virtual ~Extension() { }
1935 virtual v8::Handle<v8::FunctionTemplate>
1936 GetNativeFunction(v8::Handle<v8::String> name) {
1937 return v8::Handle<v8::FunctionTemplate>();
1938 }
1939
1940 const char* name() { return name_; }
1941 const char* source() { return source_; }
1942 int dependency_count() { return dep_count_; }
1943 const char** dependencies() { return deps_; }
1944 void set_auto_enable(bool value) { auto_enable_ = value; }
1945 bool auto_enable() { return auto_enable_; }
1946
1947 private:
1948 const char* name_;
1949 const char* source_;
1950 int dep_count_;
1951 const char** deps_;
1952 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001953
1954 // Disallow copying and assigning.
1955 Extension(const Extension&);
1956 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957};
1958
1959
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001960void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961
1962
1963/**
1964 * Ignore
1965 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001966class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 public:
1968 inline DeclareExtension(Extension* extension) {
1969 RegisterExtension(extension);
1970 }
1971};
1972
1973
1974// --- S t a t i c s ---
1975
1976
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001977Handle<Primitive> V8EXPORT Undefined();
1978Handle<Primitive> V8EXPORT Null();
1979Handle<Boolean> V8EXPORT True();
1980Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981
1982
1983/**
1984 * A set of constraints that specifies the limits of the runtime's
1985 * memory use.
1986 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001987class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 public:
1989 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001990 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001992 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001994 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1996 private:
1997 int max_young_space_size_;
1998 int max_old_space_size_;
1999 uint32_t* stack_limit_;
2000};
2001
2002
2003bool SetResourceConstraints(ResourceConstraints* constraints);
2004
2005
2006// --- E x c e p t i o n s ---
2007
2008
2009typedef void (*FatalErrorCallback)(const char* location, const char* message);
2010
2011
2012typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2013
2014
2015/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002016 * Schedules an exception to be thrown when returning to JavaScript. When an
2017 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002019 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002021Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022
2023/**
2024 * Create new error objects by calling the corresponding error object
2025 * constructor with the message.
2026 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002027class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 public:
2029 static Local<Value> RangeError(Handle<String> message);
2030 static Local<Value> ReferenceError(Handle<String> message);
2031 static Local<Value> SyntaxError(Handle<String> message);
2032 static Local<Value> TypeError(Handle<String> message);
2033 static Local<Value> Error(Handle<String> message);
2034};
2035
2036
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002037// --- 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 +00002038
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002039typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002041typedef void* (*CreateHistogramCallback)(const char* name,
2042 int min,
2043 int max,
2044 size_t buckets);
2045
2046typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2047
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048// --- 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 ---
2049typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2050 AccessType type,
2051 Local<Value> data);
2052
2053// --- 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
2054
2055/**
2056 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002057 * before and after a major garbage collection. Allocations are not
2058 * allowed in the callback function, you therefore cannot manipulate
2059 * objects (set or delete properties for example) since it is possible
2060 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 */
2062typedef void (*GCCallback)();
2063
2064
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002065// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066
2067/**
2068 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002069 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071typedef Persistent<Context> (*ContextGenerator)();
2072
2073
2074/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002075 * Profiler modules.
2076 *
2077 * In V8, profiler consists of several modules: CPU profiler, and different
2078 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002079 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2080 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002081 */
2082enum ProfilerModules {
2083 PROFILER_MODULE_NONE = 0,
2084 PROFILER_MODULE_CPU = 1,
2085 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002086 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2087 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002088};
2089
2090
2091/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 * Container class for static utility functions.
2093 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002094class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002096 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 static void SetFatalErrorHandler(FatalErrorCallback that);
2098
v8.team.kasperl727e9952008-09-02 14:56:44 +00002099 /**
2100 * Ignore out-of-memory exceptions.
2101 *
2102 * V8 running out of memory is treated as a fatal error by default.
2103 * This means that the fatal error handler is called and that V8 is
2104 * terminated.
2105 *
2106 * IgnoreOutOfMemoryException can be used to not treat a
2107 * out-of-memory situation as a fatal error. This way, the contexts
2108 * that did not cause the out of memory problem might be able to
2109 * continue execution.
2110 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 static void IgnoreOutOfMemoryException();
2112
v8.team.kasperl727e9952008-09-02 14:56:44 +00002113 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002114 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115 * fatal errors such as out-of-memory situations.
2116 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 static bool IsDead();
2118
2119 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002120 * Adds a message listener.
2121 *
2122 * The same message listener can be added more than once and it that
2123 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124 */
2125 static bool AddMessageListener(MessageCallback that,
2126 Handle<Value> data = Handle<Value>());
2127
2128 /**
2129 * Remove all message listeners from the specified callback function.
2130 */
2131 static void RemoveMessageListeners(MessageCallback that);
2132
2133 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002134 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 */
2136 static void SetFlagsFromString(const char* str, int length);
2137
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002138 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002139 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002140 */
2141 static void SetFlagsFromCommandLine(int* argc,
2142 char** argv,
2143 bool remove_flags);
2144
kasper.lund7276f142008-07-30 08:49:36 +00002145 /** Get the version string. */
2146 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147
2148 /**
2149 * Enables the host application to provide a mechanism for recording
2150 * statistics counters.
2151 */
2152 static void SetCounterFunction(CounterLookupCallback);
2153
2154 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002155 * Enables the host application to provide a mechanism for recording
2156 * histograms. The CreateHistogram function returns a
2157 * histogram which will later be passed to the AddHistogramSample
2158 * function.
2159 */
2160 static void SetCreateHistogramFunction(CreateHistogramCallback);
2161 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2162
2163 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 * Enables the computation of a sliding window of states. The sliding
2165 * window information is recorded in statistics counters.
2166 */
2167 static void EnableSlidingStateWindow();
2168
2169 /** Callback function for reporting failed access checks.*/
2170 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2171
2172 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002173 * Enables the host application to receive a notification before a
2174 * major garbage colletion. Allocations are not allowed in the
2175 * callback function, you therefore cannot manipulate objects (set
2176 * or delete properties for example) since it is possible such
2177 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 */
2179 static void SetGlobalGCPrologueCallback(GCCallback);
2180
2181 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 * Enables the host application to receive a notification after a
2183 * major garbage collection. Allocations are not allowed in the
2184 * callback function, you therefore cannot manipulate objects (set
2185 * or delete properties for example) since it is possible such
2186 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 */
2188 static void SetGlobalGCEpilogueCallback(GCCallback);
2189
2190 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002191 * Allows the host application to group objects together. If one
2192 * object in the group is alive, all objects in the group are alive.
2193 * After each garbage collection, object groups are removed. It is
2194 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002195 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002196 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002198 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199
2200 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002201 * Initializes from snapshot if possible. Otherwise, attempts to
2202 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 */
2204 static bool Initialize();
2205
kasper.lund7276f142008-07-30 08:49:36 +00002206 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002207 * Adjusts the amount of registered external memory. Used to give
2208 * V8 an indication of the amount of externally allocated memory
2209 * that is kept alive by JavaScript objects. V8 uses this to decide
2210 * when to perform global garbage collections. Registering
2211 * externally allocated memory will trigger global garbage
2212 * collections more often than otherwise in an attempt to garbage
2213 * collect the JavaScript objects keeping the externally allocated
2214 * memory alive.
2215 *
2216 * \param change_in_bytes the change in externally allocated memory
2217 * that is kept alive by JavaScript objects.
2218 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002219 */
2220 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2221
iposva@chromium.org245aa852009-02-10 00:49:54 +00002222 /**
2223 * Suspends recording of tick samples in the profiler.
2224 * When the V8 profiling mode is enabled (usually via command line
2225 * switches) this function suspends recording of tick samples.
2226 * Profiling ticks are discarded until ResumeProfiler() is called.
2227 *
2228 * See also the --prof and --prof_auto command line switches to
2229 * enable V8 profiling.
2230 */
2231 static void PauseProfiler();
2232
2233 /**
2234 * Resumes recording of tick samples in the profiler.
2235 * See also PauseProfiler().
2236 */
2237 static void ResumeProfiler();
2238
ager@chromium.org41826e72009-03-30 13:30:57 +00002239 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002240 * Return whether profiler is currently paused.
2241 */
2242 static bool IsProfilerPaused();
2243
2244 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002245 * Resumes specified profiler modules.
2246 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2247 * See ProfilerModules enum.
2248 *
2249 * \param flags Flags specifying profiler modules.
2250 */
2251 static void ResumeProfilerEx(int flags);
2252
2253 /**
2254 * Pauses specified profiler modules.
2255 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2256 * See ProfilerModules enum.
2257 *
2258 * \param flags Flags specifying profiler modules.
2259 */
2260 static void PauseProfilerEx(int flags);
2261
2262 /**
2263 * Returns active (resumed) profiler modules.
2264 * See ProfilerModules enum.
2265 *
2266 * \returns active profiler modules.
2267 */
2268 static int GetActiveProfilerModules();
2269
2270 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002271 * If logging is performed into a memory buffer (via --logfile=*), allows to
2272 * retrieve previously written messages. This can be used for retrieving
2273 * profiler log data in the application. This function is thread-safe.
2274 *
2275 * Caller provides a destination buffer that must exist during GetLogLines
2276 * call. Only whole log lines are copied into the buffer.
2277 *
2278 * \param from_pos specified a point in a buffer to read from, 0 is the
2279 * beginning of a buffer. It is assumed that caller updates its current
2280 * position using returned size value from the previous call.
2281 * \param dest_buf destination buffer for log data.
2282 * \param max_size size of the destination buffer.
2283 * \returns actual size of log data copied into buffer.
2284 */
2285 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2286
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002287 /**
2288 * Retrieve the V8 thread id of the calling thread.
2289 *
2290 * The thread id for a thread should only be retrieved after the V8
2291 * lock has been acquired with a Locker object with that thread.
2292 */
2293 static int GetCurrentThreadId();
2294
2295 /**
2296 * Forcefully terminate execution of a JavaScript thread. This can
2297 * be used to terminate long-running scripts.
2298 *
2299 * TerminateExecution should only be called when then V8 lock has
2300 * been acquired with a Locker object. Therefore, in order to be
2301 * able to terminate long-running threads, preemption must be
2302 * enabled to allow the user of TerminateExecution to acquire the
2303 * lock.
2304 *
2305 * The termination is achieved by throwing an exception that is
2306 * uncatchable by JavaScript exception handlers. Termination
2307 * exceptions act as if they were caught by a C++ TryCatch exception
2308 * handlers. If forceful termination is used, any C++ TryCatch
2309 * exception handler that catches an exception should check if that
2310 * exception is a termination exception and immediately return if
2311 * that is the case. Returning immediately in that case will
2312 * continue the propagation of the termination exception if needed.
2313 *
2314 * The thread id passed to TerminateExecution must have been
2315 * obtained by calling GetCurrentThreadId on the thread in question.
2316 *
2317 * \param thread_id The thread id of the thread to terminate.
2318 */
2319 static void TerminateExecution(int thread_id);
2320
2321 /**
2322 * Forcefully terminate the current thread of JavaScript execution.
2323 *
2324 * This method can be used by any thread even if that thread has not
2325 * acquired the V8 lock with a Locker object.
2326 */
2327 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002328
2329 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002330 * Releases any resources used by v8 and stops any utility threads
2331 * that may be running. Note that disposing v8 is permanent, it
2332 * cannot be reinitialized.
2333 *
2334 * It should generally not be necessary to dispose v8 before exiting
2335 * a process, this should happen automatically. It is only necessary
2336 * to use if the process needs the resources taken up by v8.
2337 */
2338 static bool Dispose();
2339
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002340
2341 /**
2342 * Optional notification that the embedder is idle.
2343 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002344 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002345 * \param is_high_priority tells whether the embedder is high priority.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002346 * Returns true if the embedder should stop calling IdleNotification
2347 * until real work has been done. This indicates that V8 has done
2348 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002349 */
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002350 static bool IdleNotification(bool is_high_priority);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002351
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002352 /**
2353 * Optional notification that the system is running low on memory.
2354 * V8 uses these notifications to attempt to free memory.
2355 */
2356 static void LowMemoryNotification();
2357
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358 private:
2359 V8();
2360
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002361 static internal::Object** GlobalizeReference(internal::Object** handle);
2362 static void DisposeGlobal(internal::Object** global_handle);
2363 static void MakeWeak(internal::Object** global_handle,
2364 void* data,
2365 WeakReferenceCallback);
2366 static void ClearWeak(internal::Object** global_handle);
2367 static bool IsGlobalNearDeath(internal::Object** global_handle);
2368 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369
2370 template <class T> friend class Handle;
2371 template <class T> friend class Local;
2372 template <class T> friend class Persistent;
2373 friend class Context;
2374};
2375
2376
2377/**
2378 * An external exception handler.
2379 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002380class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 public:
2382
2383 /**
2384 * Creates a new try/catch block and registers it with v8.
2385 */
2386 TryCatch();
2387
2388 /**
2389 * Unregisters and deletes this try/catch block.
2390 */
2391 ~TryCatch();
2392
2393 /**
2394 * Returns true if an exception has been caught by this try/catch block.
2395 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002396 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397
2398 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002399 * For certain types of exceptions, it makes no sense to continue
2400 * execution.
2401 *
2402 * Currently, the only type of exception that can be caught by a
2403 * TryCatch handler and for which it does not make sense to continue
2404 * is termination exception. Such exceptions are thrown when the
2405 * TerminateExecution methods are called to terminate a long-running
2406 * script.
2407 *
2408 * If CanContinue returns false, the correct action is to perform
2409 * any C++ cleanup needed and then return.
2410 */
2411 bool CanContinue() const;
2412
2413 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 * Returns the exception caught by this try/catch block. If no exception has
2415 * been caught an empty handle is returned.
2416 *
2417 * The returned handle is valid until this TryCatch block has been destroyed.
2418 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002419 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420
2421 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002422 * Returns the .stack property of the thrown object. If no .stack
2423 * property is present an empty handle is returned.
2424 */
2425 Local<Value> StackTrace() const;
2426
2427 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002428 * Returns the message associated with this exception. If there is
2429 * no message associated an empty handle is returned.
2430 *
2431 * The returned handle is valid until this TryCatch block has been
2432 * destroyed.
2433 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002434 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002435
2436 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 * Clears any exceptions that may have been caught by this try/catch block.
2438 * After this method has been called, HasCaught() will return false.
2439 *
2440 * It is not necessary to clear a try/catch block before using it again; if
2441 * another exception is thrown the previously caught exception will just be
2442 * overwritten. However, it is often a good idea since it makes it easier
2443 * to determine which operation threw a given exception.
2444 */
2445 void Reset();
2446
v8.team.kasperl727e9952008-09-02 14:56:44 +00002447 /**
2448 * Set verbosity of the external exception handler.
2449 *
2450 * By default, exceptions that are caught by an external exception
2451 * handler are not reported. Call SetVerbose with true on an
2452 * external exception handler to have exceptions caught by the
2453 * handler reported as if they were not caught.
2454 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 void SetVerbose(bool value);
2456
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002457 /**
2458 * Set whether or not this TryCatch should capture a Message object
2459 * which holds source information about where the exception
2460 * occurred. True by default.
2461 */
2462 void SetCaptureMessage(bool value);
2463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 public:
2465 TryCatch* next_;
2466 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002467 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 bool is_verbose_;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002469 bool can_continue_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002470 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002471 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472};
2473
2474
2475// --- C o n t e x t ---
2476
2477
2478/**
2479 * Ignore
2480 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002481class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 public:
2483 ExtensionConfiguration(int name_count, const char* names[])
2484 : name_count_(name_count), names_(names) { }
2485 private:
2486 friend class ImplementationUtilities;
2487 int name_count_;
2488 const char** names_;
2489};
2490
2491
2492/**
2493 * A sandboxed execution context with its own set of built-in objects
2494 * and functions.
2495 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002496class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002498 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 Local<Object> Global();
2500
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002501 /**
2502 * Detaches the global object from its context before
2503 * the global object can be reused to create a new context.
2504 */
2505 void DetachGlobal();
2506
v8.team.kasperl727e9952008-09-02 14:56:44 +00002507 /** Creates a new context. */
2508 static Persistent<Context> New(
2509 ExtensionConfiguration* extensions = 0,
2510 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2511 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512
kasper.lund44510672008-07-25 07:37:58 +00002513 /** Returns the last entered context. */
2514 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
kasper.lund44510672008-07-25 07:37:58 +00002516 /** Returns the context that is on the top of the stack. */
2517 static Local<Context> GetCurrent();
2518
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002520 * Returns the context of the calling JavaScript code. That is the
2521 * context of the top-most JavaScript frame. If there are no
2522 * JavaScript frames an empty handle is returned.
2523 */
2524 static Local<Context> GetCalling();
2525
2526 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 * Sets the security token for the context. To access an object in
2528 * another context, the security tokens must match.
2529 */
2530 void SetSecurityToken(Handle<Value> token);
2531
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002532 /** Restores the security token to the default value. */
2533 void UseDefaultSecurityToken();
2534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 /** Returns the security token of this context.*/
2536 Handle<Value> GetSecurityToken();
2537
v8.team.kasperl727e9952008-09-02 14:56:44 +00002538 /**
2539 * Enter this context. After entering a context, all code compiled
2540 * and run is compiled and run in this context. If another context
2541 * is already entered, this old context is saved so it can be
2542 * restored when the new context is exited.
2543 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002545
2546 /**
2547 * Exit this context. Exiting the current context restores the
2548 * context that was in place when entering the current context.
2549 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 void Exit();
2551
v8.team.kasperl727e9952008-09-02 14:56:44 +00002552 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 bool HasOutOfMemoryException();
2554
v8.team.kasperl727e9952008-09-02 14:56:44 +00002555 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 static bool InContext();
2557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002559 * Associate an additional data object with the context. This is mainly used
2560 * with the debugger to provide additional information on the context through
2561 * the debugger API.
2562 */
2563 void SetData(Handle<Value> data);
2564 Local<Value> GetData();
2565
2566 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 * Stack-allocated class which sets the execution context for all
2568 * operations executed within a local scope.
2569 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002570 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 public:
2572 inline Scope(Handle<Context> context) : context_(context) {
2573 context_->Enter();
2574 }
2575 inline ~Scope() { context_->Exit(); }
2576 private:
2577 Handle<Context> context_;
2578 };
2579
2580 private:
2581 friend class Value;
2582 friend class Script;
2583 friend class Object;
2584 friend class Function;
2585};
2586
2587
2588/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002589 * Multiple threads in V8 are allowed, but only one thread at a time
2590 * is allowed to use V8. The definition of 'using V8' includes
2591 * accessing handles or holding onto object pointers obtained from V8
2592 * handles. It is up to the user of V8 to ensure (perhaps with
2593 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594 *
2595 * If you wish to start using V8 in a thread you can do this by constructing
2596 * a v8::Locker object. After the code using V8 has completed for the
2597 * current thread you can call the destructor. This can be combined
2598 * with C++ scope-based construction as follows:
2599 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002600 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 * ...
2602 * {
2603 * v8::Locker locker;
2604 * ...
2605 * // Code using V8 goes here.
2606 * ...
2607 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002608 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 *
2610 * If you wish to stop using V8 in a thread A you can do this by either
2611 * by destroying the v8::Locker object as above or by constructing a
2612 * v8::Unlocker object:
2613 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002614 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 * {
2616 * v8::Unlocker unlocker;
2617 * ...
2618 * // Code not using V8 goes here while V8 can run in another thread.
2619 * ...
2620 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002621 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 *
2623 * The Unlocker object is intended for use in a long-running callback
2624 * from V8, where you want to release the V8 lock for other threads to
2625 * use.
2626 *
2627 * The v8::Locker is a recursive lock. That is, you can lock more than
2628 * once in a given thread. This can be useful if you have code that can
2629 * be called either from code that holds the lock or from code that does
2630 * not. The Unlocker is not recursive so you can not have several
2631 * Unlockers on the stack at once, and you can not use an Unlocker in a
2632 * thread that is not inside a Locker's scope.
2633 *
2634 * An unlocker will unlock several lockers if it has to and reinstate
2635 * the correct depth of locking on its destruction. eg.:
2636 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002637 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 * // V8 not locked.
2639 * {
2640 * v8::Locker locker;
2641 * // V8 locked.
2642 * {
2643 * v8::Locker another_locker;
2644 * // V8 still locked (2 levels).
2645 * {
2646 * v8::Unlocker unlocker;
2647 * // V8 not locked.
2648 * }
2649 * // V8 locked again (2 levels).
2650 * }
2651 * // V8 still locked (1 level).
2652 * }
2653 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002654 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002656class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 public:
2658 Unlocker();
2659 ~Unlocker();
2660};
2661
2662
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002663class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 public:
2665 Locker();
2666 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002667
2668 /**
2669 * Start preemption.
2670 *
2671 * When preemption is started, a timer is fired every n milli seconds
2672 * that will switch between multiple threads that are in contention
2673 * for the V8 lock.
2674 */
2675 static void StartPreemption(int every_n_ms);
2676
2677 /**
2678 * Stop preemption.
2679 */
2680 static void StopPreemption();
2681
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002682 /**
2683 * Returns whether or not the locker is locked by the current thread.
2684 */
2685 static bool IsLocked();
2686
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002687 /**
2688 * Returns whether v8::Locker is being used by this V8 instance.
2689 */
2690 static bool IsActive() { return active_; }
2691
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 private:
2693 bool has_lock_;
2694 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002695
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002696 static bool active_;
2697
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002698 // Disallow copying and assigning.
2699 Locker(const Locker&);
2700 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701};
2702
2703
2704
2705// --- I m p l e m e n t a t i o n ---
2706
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002707
2708namespace internal {
2709
2710
2711// Tag information for HeapObject.
2712const int kHeapObjectTag = 1;
2713const int kHeapObjectTagSize = 2;
2714const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2715
2716
2717// Tag information for Smi.
2718const int kSmiTag = 0;
2719const int kSmiTagSize = 1;
2720const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2721
2722
2723/**
2724 * This class exports constants and functionality from within v8 that
2725 * is necessary to implement inline functions in the v8 api. Don't
2726 * depend on functions and constants defined here.
2727 */
2728class Internals {
2729 public:
2730
2731 // These values match non-compiler-dependent values defined within
2732 // the implementation of v8.
2733 static const int kHeapObjectMapOffset = 0;
2734 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
2735 static const int kStringResourceOffset = 2 * sizeof(void*);
2736 static const int kProxyProxyOffset = sizeof(void*);
2737 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2738 static const int kFullStringRepresentationMask = 0x07;
2739 static const int kExternalTwoByteRepresentationTag = 0x03;
2740 static const int kAlignedPointerShift = 2;
2741
2742 // These constants are compiler dependent so their values must be
2743 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002744 V8EXPORT static int kJSObjectType;
2745 V8EXPORT static int kFirstNonstringType;
2746 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002747
2748 static inline bool HasHeapObjectTag(internal::Object* value) {
2749 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2750 kHeapObjectTag);
2751 }
2752
2753 static inline bool HasSmiTag(internal::Object* value) {
2754 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2755 }
2756
2757 static inline int SmiValue(internal::Object* value) {
2758 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> kSmiTagSize;
2759 }
2760
2761 static inline bool IsExternalTwoByteString(int instance_type) {
2762 int representation = (instance_type & kFullStringRepresentationMask);
2763 return representation == kExternalTwoByteRepresentationTag;
2764 }
2765
2766 template <typename T>
2767 static inline T ReadField(Object* ptr, int offset) {
2768 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2769 return *reinterpret_cast<T*>(addr);
2770 }
2771
2772};
2773
2774}
2775
2776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777template <class T>
2778Handle<T>::Handle() : val_(0) { }
2779
2780
2781template <class T>
2782Local<T>::Local() : Handle<T>() { }
2783
2784
2785template <class T>
2786Local<T> Local<T>::New(Handle<T> that) {
2787 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002788 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2790}
2791
2792
2793template <class T>
2794Persistent<T> Persistent<T>::New(Handle<T> that) {
2795 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002796 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2798}
2799
2800
2801template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002802bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002804 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002805}
2806
2807
2808template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002809bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002811 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812}
2813
2814
2815template <class T>
2816void Persistent<T>::Dispose() {
2817 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002818 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819}
2820
2821
2822template <class T>
2823Persistent<T>::Persistent() : Handle<T>() { }
2824
2825template <class T>
2826void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002827 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
2828 parameters,
2829 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830}
2831
2832template <class T>
2833void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002834 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835}
2836
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837Local<Value> Arguments::operator[](int i) const {
2838 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2839 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2840}
2841
2842
2843Local<Function> Arguments::Callee() const {
2844 return callee_;
2845}
2846
2847
2848Local<Object> Arguments::This() const {
2849 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2850}
2851
2852
2853Local<Object> Arguments::Holder() const {
2854 return holder_;
2855}
2856
2857
2858Local<Value> Arguments::Data() const {
2859 return data_;
2860}
2861
2862
2863bool Arguments::IsConstructCall() const {
2864 return is_construct_call_;
2865}
2866
2867
2868int Arguments::Length() const {
2869 return length_;
2870}
2871
2872
2873Local<Value> AccessorInfo::Data() const {
2874 return data_;
2875}
2876
2877
2878Local<Object> AccessorInfo::This() const {
2879 return self_;
2880}
2881
2882
2883Local<Object> AccessorInfo::Holder() const {
2884 return holder_;
2885}
2886
2887
2888template <class T>
2889Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002890 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
2891 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 return Local<T>(reinterpret_cast<T*>(after));
2893}
2894
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002895Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 return resource_name_;
2897}
2898
2899
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002900Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 return resource_line_offset_;
2902}
2903
2904
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002905Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 return resource_column_offset_;
2907}
2908
2909
2910Handle<Boolean> Boolean::New(bool value) {
2911 return value ? True() : False();
2912}
2913
2914
2915void Template::Set(const char* name, v8::Handle<Data> value) {
2916 Set(v8::String::New(name), value);
2917}
2918
2919
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002920Local<Value> Object::GetInternalField(int index) {
2921#ifndef V8_ENABLE_CHECKS
2922 Local<Value> quick_result = UncheckedGetInternalField(index);
2923 if (!quick_result.IsEmpty()) return quick_result;
2924#endif
2925 return CheckedGetInternalField(index);
2926}
2927
2928
2929Local<Value> Object::UncheckedGetInternalField(int index) {
2930 typedef internal::Object O;
2931 typedef internal::Internals I;
2932 O* obj = *reinterpret_cast<O**>(this);
2933 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2934 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2935 if (instance_type == I::kJSObjectType) {
2936 // If the object is a plain JSObject, which is the common case,
2937 // we know where to find the internal fields and can return the
2938 // value directly.
2939 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
2940 O* value = I::ReadField<O*>(obj, offset);
2941 O** result = HandleScope::CreateHandle(value);
2942 return Local<Value>(reinterpret_cast<Value*>(result));
2943 } else {
2944 return Local<Value>();
2945 }
2946}
2947
2948
2949void* External::Unwrap(Handle<v8::Value> obj) {
2950#ifdef V8_ENABLE_CHECKS
2951 return FullUnwrap(obj);
2952#else
2953 return QuickUnwrap(obj);
2954#endif
2955}
2956
2957
2958void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
2959 typedef internal::Object O;
2960 typedef internal::Internals I;
2961 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
2962 if (I::HasSmiTag(obj)) {
2963 int value = I::SmiValue(obj) << I::kAlignedPointerShift;
2964 return reinterpret_cast<void*>(value);
2965 } else {
2966 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2967 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2968 if (instance_type == I::kProxyType) {
2969 return I::ReadField<void*>(obj, I::kProxyProxyOffset);
2970 } else {
2971 return NULL;
2972 }
2973 }
2974}
2975
2976
2977void* Object::GetPointerFromInternalField(int index) {
2978 return External::Unwrap(GetInternalField(index));
2979}
2980
2981
2982String* String::Cast(v8::Value* value) {
2983#ifdef V8_ENABLE_CHECKS
2984 CheckCast(value);
2985#endif
2986 return static_cast<String*>(value);
2987}
2988
2989
2990String::ExternalStringResource* String::GetExternalStringResource() const {
2991 typedef internal::Object O;
2992 typedef internal::Internals I;
2993 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
2994 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2995 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2996 String::ExternalStringResource* result;
2997 if (I::IsExternalTwoByteString(instance_type)) {
2998 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
2999 result = reinterpret_cast<String::ExternalStringResource*>(value);
3000 } else {
3001 result = NULL;
3002 }
3003#ifdef V8_ENABLE_CHECKS
3004 VerifyExternalStringResource(result);
3005#endif
3006 return result;
3007}
3008
3009
3010bool Value::IsString() const {
3011#ifdef V8_ENABLE_CHECKS
3012 return FullIsString();
3013#else
3014 return QuickIsString();
3015#endif
3016}
3017
3018bool Value::QuickIsString() const {
3019 typedef internal::Object O;
3020 typedef internal::Internals I;
3021 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3022 if (!I::HasHeapObjectTag(obj)) return false;
3023 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
3024 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
3025 return (instance_type < I::kFirstNonstringType);
3026}
3027
3028
3029Number* Number::Cast(v8::Value* value) {
3030#ifdef V8_ENABLE_CHECKS
3031 CheckCast(value);
3032#endif
3033 return static_cast<Number*>(value);
3034}
3035
3036
3037Integer* Integer::Cast(v8::Value* value) {
3038#ifdef V8_ENABLE_CHECKS
3039 CheckCast(value);
3040#endif
3041 return static_cast<Integer*>(value);
3042}
3043
3044
3045Date* Date::Cast(v8::Value* value) {
3046#ifdef V8_ENABLE_CHECKS
3047 CheckCast(value);
3048#endif
3049 return static_cast<Date*>(value);
3050}
3051
3052
3053Object* Object::Cast(v8::Value* value) {
3054#ifdef V8_ENABLE_CHECKS
3055 CheckCast(value);
3056#endif
3057 return static_cast<Object*>(value);
3058}
3059
3060
3061Array* Array::Cast(v8::Value* value) {
3062#ifdef V8_ENABLE_CHECKS
3063 CheckCast(value);
3064#endif
3065 return static_cast<Array*>(value);
3066}
3067
3068
3069Function* Function::Cast(v8::Value* value) {
3070#ifdef V8_ENABLE_CHECKS
3071 CheckCast(value);
3072#endif
3073 return static_cast<Function*>(value);
3074}
3075
3076
3077External* External::Cast(v8::Value* value) {
3078#ifdef V8_ENABLE_CHECKS
3079 CheckCast(value);
3080#endif
3081 return static_cast<External*>(value);
3082}
3083
3084
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003086 * \example shell.cc
3087 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 * command-line and executes them.
3089 */
3090
3091
3092/**
3093 * \example process.cc
3094 */
3095
3096
3097} // namespace v8
3098
3099
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003100#undef V8EXPORT
3101#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102#undef TYPE_CHECK
3103
3104
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003105#endif // V8_H_