blob: adb9f431766a6e173b5a9df029c915c756dab9f9 [file] [log] [blame]
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001// Copyright 2007-2009 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;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000133class Arguments;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000134
135}
136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137
138// --- W e a k H a n d l e s
139
140
141/**
142 * A weak reference callback function.
143 *
144 * \param object the weak global object to be reclaimed by the garbage collector
145 * \param parameter the value passed in when making the weak global object
146 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000147typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 void* parameter);
149
150
151// --- H a n d l e s ---
152
153#define TYPE_CHECK(T, S) \
154 while (false) { \
155 *(static_cast<T**>(0)) = static_cast<S*>(0); \
156 }
157
158/**
159 * An object reference managed by the v8 garbage collector.
160 *
161 * All objects returned from v8 have to be tracked by the garbage
162 * collector so that it knows that the objects are still alive. Also,
163 * because the garbage collector may move objects, it is unsafe to
164 * point directly to an object. Instead, all objects are stored in
165 * handles which are known by the garbage collector and updated
166 * whenever an object moves. Handles should always be passed by value
167 * (except in cases like out-parameters) and they should never be
168 * allocated on the heap.
169 *
170 * There are two types of handles: local and persistent handles.
171 * Local handles are light-weight and transient and typically used in
172 * local operations. They are managed by HandleScopes. Persistent
173 * handles can be used when storing objects across several independent
174 * operations and have to be explicitly deallocated when they're no
175 * longer used.
176 *
177 * It is safe to extract the object stored in the handle by
178 * dereferencing the handle (for instance, to extract the Object* from
179 * an Handle<Object>); the value will still be governed by a handle
180 * behind the scenes and the same rules apply to these values as to
181 * their handles.
182 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000183template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 public:
185
186 /**
187 * Creates an empty handle.
188 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000189 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190
191 /**
192 * Creates a new handle for the specified value.
193 */
194 explicit Handle(T* val) : val_(val) { }
195
196 /**
197 * Creates a handle for the contents of the specified handle. This
198 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000199 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 * incompatible handles, for instance from a Handle<String> to a
201 * Handle<Number> it will cause a compiletime error. Assigning
202 * between compatible handles, for instance assigning a
203 * Handle<String> to a variable declared as Handle<Value>, is legal
204 * because String is a subclass of Value.
205 */
206 template <class S> inline Handle(Handle<S> that)
207 : val_(reinterpret_cast<T*>(*that)) {
208 /**
209 * This check fails when trying to convert between incompatible
210 * handles. For example, converting from a Handle<String> to a
211 * Handle<Number>.
212 */
213 TYPE_CHECK(T, S);
214 }
215
216 /**
217 * Returns true if the handle is empty.
218 */
ager@chromium.org32912102009-01-16 10:38:43 +0000219 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000221 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000223 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224
225 /**
226 * Sets the handle to be empty. IsEmpty() will then return true.
227 */
228 void Clear() { this->val_ = 0; }
229
230 /**
231 * Checks whether two handles are the same.
232 * Returns true if both are empty, or if the objects
233 * to which they refer are identical.
234 * The handles' references are not checked.
235 */
ager@chromium.org32912102009-01-16 10:38:43 +0000236 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000237 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
238 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 if (a == 0) return b == 0;
240 if (b == 0) return false;
241 return *a == *b;
242 }
243
244 /**
245 * Checks whether two handles are different.
246 * Returns true if only one of the handles is empty, or if
247 * the objects to which they refer are different.
248 * The handles' references are not checked.
249 */
ager@chromium.org32912102009-01-16 10:38:43 +0000250 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 return !operator==(that);
252 }
253
254 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000255#ifdef V8_ENABLE_CHECKS
256 // If we're going to perform the type check then we have to check
257 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000259#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 return Handle<T>(T::Cast(*that));
261 }
262
263 private:
264 T* val_;
265};
266
267
268/**
269 * A light-weight stack-allocated object handle. All operations
270 * that return objects from within v8 return them in local handles. They
271 * are created within HandleScopes, and all local handles allocated within a
272 * handle scope are destroyed when the handle scope is destroyed. Hence it
273 * is not necessary to explicitly deallocate local handles.
274 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000275template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000277 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 template <class S> inline Local(Local<S> that)
279 : Handle<T>(reinterpret_cast<T*>(*that)) {
280 /**
281 * This check fails when trying to convert between incompatible
282 * handles. For example, converting from a Handle<String> to a
283 * Handle<Number>.
284 */
285 TYPE_CHECK(T, S);
286 }
287 template <class S> inline Local(S* that) : Handle<T>(that) { }
288 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000289#ifdef V8_ENABLE_CHECKS
290 // If we're going to perform the type check then we have to check
291 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000293#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 return Local<T>(T::Cast(*that));
295 }
296
297 /** Create a local handle for the content of another handle.
298 * The referee is kept alive by the local handle even when
299 * the original handle is destroyed/disposed.
300 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000301 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302};
303
304
305/**
306 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000307 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 * allocated, a Persistent handle remains valid until it is explicitly
309 * disposed.
310 *
311 * A persistent handle contains a reference to a storage cell within
312 * the v8 engine which holds an object value and which is updated by
313 * the garbage collector whenever the object is moved. A new storage
314 * cell can be created using Persistent::New and existing handles can
315 * be disposed using Persistent::Dispose. Since persistent handles
316 * are passed by value you may have many persistent handle objects
317 * that point to the same storage cell. For instance, if you pass a
318 * persistent handle as an argument to a function you will not get two
319 * different storage cells but rather two references to the same
320 * storage cell.
321 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000322template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 public:
324
325 /**
326 * Creates an empty persistent handle that doesn't point to any
327 * storage cell.
328 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000329 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330
331 /**
332 * Creates a persistent handle for the same storage cell as the
333 * specified handle. This constructor allows you to pass persistent
334 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000335 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000337 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 * between compatible persistent handles, for instance assigning a
339 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000340 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 */
342 template <class S> inline Persistent(Persistent<S> that)
343 : Handle<T>(reinterpret_cast<T*>(*that)) {
344 /**
345 * This check fails when trying to convert between incompatible
346 * handles. For example, converting from a Handle<String> to a
347 * Handle<Number>.
348 */
349 TYPE_CHECK(T, S);
350 }
351
352 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
353
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000354 /**
355 * "Casts" a plain handle which is known to be a persistent handle
356 * to a persistent handle.
357 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 template <class S> explicit inline Persistent(Handle<S> that)
359 : Handle<T>(*that) { }
360
361 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000362#ifdef V8_ENABLE_CHECKS
363 // If we're going to perform the type check then we have to check
364 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000366#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 return Persistent<T>(T::Cast(*that));
368 }
369
370 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000371 * Creates a new persistent handle for an existing local or
372 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000374 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 /**
377 * Releases the storage cell referenced by this persistent handle.
378 * Does not remove the reference to the cell from any handles.
379 * This handle's reference, and any any other references to the storage
380 * cell remain and IsEmpty will still return false.
381 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000382 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383
384 /**
385 * Make the reference to this object weak. When only weak handles
386 * refer to the object, the garbage collector will perform a
387 * callback to the given V8::WeakReferenceCallback function, passing
388 * it the object reference and the given parameters.
389 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000390 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391
392 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000393 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395 /**
396 *Checks if the handle holds the only reference to an object.
397 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000398 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399
400 /**
401 * Returns true if the handle's reference is weak.
402 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000403 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
405 private:
406 friend class ImplementationUtilities;
407 friend class ObjectTemplate;
408};
409
410
v8.team.kasperl727e9952008-09-02 14:56:44 +0000411 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 * A stack-allocated class that governs a number of local handles.
413 * After a handle scope has been created, all local handles will be
414 * allocated within that handle scope until either the handle scope is
415 * deleted or another handle scope is created. If there is already a
416 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000417 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 * new handles will again be allocated in the original handle scope.
419 *
420 * After the handle scope of a local handle has been deleted the
421 * garbage collector will no longer track the object stored in the
422 * handle and may deallocate it. The behavior of accessing a handle
423 * for which the handle scope has been deleted is undefined.
424 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000425class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000427 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000429 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430
431 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 * Closes the handle scope and returns the value as a handle in the
433 * previous scope, which is the new current scope after the call.
434 */
435 template <class T> Local<T> Close(Handle<T> value);
436
437 /**
438 * Counts the number of allocated handles.
439 */
440 static int NumberOfHandles();
441
442 /**
443 * Creates a new handle with the given value.
444 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000445 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
447 private:
448 // Make it impossible to create heap-allocated or illegal handle
449 // scopes by disallowing certain operations.
450 HandleScope(const HandleScope&);
451 void operator=(const HandleScope&);
452 void* operator new(size_t size);
453 void operator delete(void*, size_t);
454
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000455 // This Data class is accessible internally through a typedef in the
456 // ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000457 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 public:
459 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000460 internal::Object** next;
461 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 inline void Initialize() {
463 extensions = -1;
464 next = limit = NULL;
465 }
466 };
467
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000468 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000470 // Allow for the active closing of HandleScopes which allows to pass a handle
471 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000473 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 friend class ImplementationUtilities;
476};
477
478
479// --- S p e c i a l o b j e c t s ---
480
481
482/**
483 * The superclass of values and API object templates.
484 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000485class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 private:
487 Data();
488};
489
490
491/**
492 * Pre-compilation data that can be associated with a script. This
493 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000494 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000495 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000497class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 public:
499 virtual ~ScriptData() { }
500 static ScriptData* PreCompile(const char* input, int length);
501 static ScriptData* New(unsigned* data, int length);
502
503 virtual int Length() = 0;
504 virtual unsigned* Data() = 0;
505};
506
507
508/**
509 * The origin, within a file, of a script.
510 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000511class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000513 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 Handle<Integer> resource_line_offset = Handle<Integer>(),
515 Handle<Integer> resource_column_offset = Handle<Integer>())
516 : resource_name_(resource_name),
517 resource_line_offset_(resource_line_offset),
518 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000519 inline Handle<Value> ResourceName() const;
520 inline Handle<Integer> ResourceLineOffset() const;
521 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000523 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 Handle<Integer> resource_line_offset_;
525 Handle<Integer> resource_column_offset_;
526};
527
528
529/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000530 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000532class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 public:
534
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000535 /**
536 * Compiles the specified script. The ScriptOrigin* and ScriptData*
537 * parameters are owned by the caller of Script::Compile. No
538 * references to these objects are kept after compilation finishes.
539 *
540 * The script object returned is context independent; when run it
541 * will use the currently entered context.
542 */
543 static Local<Script> New(Handle<String> source,
544 ScriptOrigin* origin = NULL,
545 ScriptData* pre_data = NULL);
546
547 /**
548 * Compiles the specified script using the specified file name
549 * object (typically a string) as the script's origin.
550 *
551 * The script object returned is context independent; when run it
552 * will use the currently entered context.
553 */
554 static Local<Script> New(Handle<String> source,
555 Handle<Value> file_name);
556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 /**
558 * Compiles the specified script. The ScriptOrigin* and ScriptData*
559 * parameters are owned by the caller of Script::Compile. No
560 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000561 *
562 * The script object returned is bound to the context that was active
563 * when this function was called. When run it will always use this
564 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 */
566 static Local<Script> Compile(Handle<String> source,
567 ScriptOrigin* origin = NULL,
568 ScriptData* pre_data = NULL);
569
mads.s.agercbaa0602008-08-14 13:41:48 +0000570 /**
571 * Compiles the specified script using the specified file name
572 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000573 *
574 * The script object returned is bound to the context that was active
575 * when this function was called. When run it will always use this
576 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000577 */
578 static Local<Script> Compile(Handle<String> source,
579 Handle<Value> file_name);
580
v8.team.kasperl727e9952008-09-02 14:56:44 +0000581 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000582 * Runs the script returning the resulting value. If the script is
583 * context independent (created using ::New) it will be run in the
584 * currently entered context. If it is context specific (created
585 * using ::Compile) it will be run in the context in which it was
586 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000587 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000589
590 /**
591 * Returns the script id value.
592 */
593 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000594
595 /**
596 * Associate an additional data object with the script. This is mainly used
597 * with the debugger as this data object is only available through the
598 * debugger API.
599 */
600 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601};
602
603
604/**
605 * An error message.
606 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000607class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000609 Local<String> Get() const;
610 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000612 /**
613 * Returns the resource name for the script from where the function causing
614 * the error originates.
615 */
ager@chromium.org32912102009-01-16 10:38:43 +0000616 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000618 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000619 * Returns the resource data for the script from where the function causing
620 * the error originates.
621 */
622 Handle<Value> GetScriptData() const;
623
624 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000625 * Returns the number, 1-based, of the line where the error occurred.
626 */
ager@chromium.org32912102009-01-16 10:38:43 +0000627 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000629 /**
630 * Returns the index within the script of the first character where
631 * the error occurred.
632 */
ager@chromium.org32912102009-01-16 10:38:43 +0000633 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000634
635 /**
636 * Returns the index within the script of the last character where
637 * the error occurred.
638 */
ager@chromium.org32912102009-01-16 10:38:43 +0000639 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000640
641 /**
642 * Returns the index within the line of the first character where
643 * the error occurred.
644 */
ager@chromium.org32912102009-01-16 10:38:43 +0000645 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000646
647 /**
648 * Returns the index within the line of the last character where
649 * the error occurred.
650 */
ager@chromium.org32912102009-01-16 10:38:43 +0000651 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000652
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 // TODO(1245381): Print to a string instead of on a FILE.
654 static void PrintCurrentStackTrace(FILE* out);
655};
656
657
658// --- V a l u e ---
659
660
661/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000662 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000664class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 public:
666
667 /**
668 * Returns true if this value is the undefined value. See ECMA-262
669 * 4.3.10.
670 */
ager@chromium.org32912102009-01-16 10:38:43 +0000671 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
673 /**
674 * Returns true if this value is the null value. See ECMA-262
675 * 4.3.11.
676 */
ager@chromium.org32912102009-01-16 10:38:43 +0000677 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
679 /**
680 * Returns true if this value is true.
681 */
ager@chromium.org32912102009-01-16 10:38:43 +0000682 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
684 /**
685 * Returns true if this value is false.
686 */
ager@chromium.org32912102009-01-16 10:38:43 +0000687 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
689 /**
690 * Returns true if this value is an instance of the String type.
691 * See ECMA-262 8.4.
692 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000693 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
695 /**
696 * Returns true if this value is a function.
697 */
ager@chromium.org32912102009-01-16 10:38:43 +0000698 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
700 /**
701 * Returns true if this value is an array.
702 */
ager@chromium.org32912102009-01-16 10:38:43 +0000703 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704
v8.team.kasperl727e9952008-09-02 14:56:44 +0000705 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 * Returns true if this value is an object.
707 */
ager@chromium.org32912102009-01-16 10:38:43 +0000708 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709
v8.team.kasperl727e9952008-09-02 14:56:44 +0000710 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 * Returns true if this value is boolean.
712 */
ager@chromium.org32912102009-01-16 10:38:43 +0000713 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000714
v8.team.kasperl727e9952008-09-02 14:56:44 +0000715 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 * Returns true if this value is a number.
717 */
ager@chromium.org32912102009-01-16 10:38:43 +0000718 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
v8.team.kasperl727e9952008-09-02 14:56:44 +0000720 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 * Returns true if this value is external.
722 */
ager@chromium.org32912102009-01-16 10:38:43 +0000723 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724
v8.team.kasperl727e9952008-09-02 14:56:44 +0000725 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 * Returns true if this value is a 32-bit signed integer.
727 */
ager@chromium.org32912102009-01-16 10:38:43 +0000728 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000730 /**
731 * Returns true if this value is a Date.
732 */
ager@chromium.org32912102009-01-16 10:38:43 +0000733 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000734
ager@chromium.org32912102009-01-16 10:38:43 +0000735 Local<Boolean> ToBoolean() const;
736 Local<Number> ToNumber() const;
737 Local<String> ToString() const;
738 Local<String> ToDetailString() const;
739 Local<Object> ToObject() const;
740 Local<Integer> ToInteger() const;
741 Local<Uint32> ToUint32() const;
742 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743
744 /**
745 * Attempts to convert a string to an array index.
746 * Returns an empty handle if the conversion fails.
747 */
ager@chromium.org32912102009-01-16 10:38:43 +0000748 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749
ager@chromium.org32912102009-01-16 10:38:43 +0000750 bool BooleanValue() const;
751 double NumberValue() const;
752 int64_t IntegerValue() const;
753 uint32_t Uint32Value() const;
754 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755
756 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000757 bool Equals(Handle<Value> that) const;
758 bool StrictEquals(Handle<Value> that) const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000759
760 private:
761 inline bool QuickIsString() const;
762 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763};
764
765
766/**
767 * The superclass of primitive values. See ECMA-262 4.3.2.
768 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000769class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770
771
772/**
773 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
774 * or false value.
775 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000776class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000778 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 static inline Handle<Boolean> New(bool value);
780};
781
782
783/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000784 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000786class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000788
789 /**
790 * Returns the number of characters in this string.
791 */
ager@chromium.org32912102009-01-16 10:38:43 +0000792 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
v8.team.kasperl727e9952008-09-02 14:56:44 +0000794 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000795 * Returns the number of bytes in the UTF-8 encoded
796 * representation of this string.
797 */
ager@chromium.org32912102009-01-16 10:38:43 +0000798 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000799
800 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000801 * Write the contents of the string to an external buffer.
802 * If no arguments are given, expects the buffer to be large
803 * enough to hold the entire string and NULL terminator. Copies
804 * the contents of the string and the NULL terminator into the
805 * buffer.
806 *
807 * Copies up to length characters into the output buffer.
808 * Only null-terminates if there is enough space in the buffer.
809 *
810 * \param buffer The buffer into which the string will be copied.
811 * \param start The starting position within the string at which
812 * copying begins.
813 * \param length The number of bytes to copy from the string.
814 * \return The number of characters copied to the buffer
815 * excluding the NULL terminator.
816 */
ager@chromium.org32912102009-01-16 10:38:43 +0000817 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
818 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
819 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820
v8.team.kasperl727e9952008-09-02 14:56:44 +0000821 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000822 * A zero length string.
823 */
824 static v8::Local<v8::String> Empty();
825
826 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000827 * Returns true if the string is external
828 */
ager@chromium.org32912102009-01-16 10:38:43 +0000829 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830
v8.team.kasperl727e9952008-09-02 14:56:44 +0000831 /**
832 * Returns true if the string is both external and ascii
833 */
ager@chromium.org32912102009-01-16 10:38:43 +0000834 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000835 /**
836 * An ExternalStringResource is a wrapper around a two-byte string
837 * buffer that resides outside V8's heap. Implement an
838 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000839 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000840 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000841 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 public:
843 /**
844 * Override the destructor to manage the life cycle of the underlying
845 * buffer.
846 */
847 virtual ~ExternalStringResource() {}
848 /** The string data from the underlying buffer.*/
849 virtual const uint16_t* data() const = 0;
850 /** The length of the string. That is, the number of two-byte characters.*/
851 virtual size_t length() const = 0;
852 protected:
853 ExternalStringResource() {}
854 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000855 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 ExternalStringResource(const ExternalStringResource&);
857 void operator=(const ExternalStringResource&);
858 };
859
860 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000861 * An ExternalAsciiStringResource is a wrapper around an ascii
862 * string buffer that resides outside V8's heap. Implement an
863 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000864 * underlying buffer. Note that the string data must be immutable
865 * and that the data must be strict 7-bit ASCII, not Latin1 or
866 * UTF-8, which would require special treatment internally in the
867 * engine and, in the case of UTF-8, do not allow efficient indexing.
868 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000869 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000871 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 public:
873 /**
874 * Override the destructor to manage the life cycle of the underlying
875 * buffer.
876 */
877 virtual ~ExternalAsciiStringResource() {}
878 /** The string data from the underlying buffer.*/
879 virtual const char* data() const = 0;
880 /** The number of ascii characters in the string.*/
881 virtual size_t length() const = 0;
882 protected:
883 ExternalAsciiStringResource() {}
884 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000885 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
887 void operator=(const ExternalAsciiStringResource&);
888 };
889
890 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000891 * Get the ExternalStringResource for an external string. Returns
892 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000894 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
896 /**
897 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000898 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 */
ager@chromium.org32912102009-01-16 10:38:43 +0000900 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000902 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
904 /**
905 * Allocates a new string from either utf-8 encoded or ascii data.
906 * The second parameter 'length' gives the buffer length.
907 * If the data is utf-8 encoded, the caller must
908 * be careful to supply the length parameter.
909 * If it is not given, the function calls
910 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000911 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 */
913 static Local<String> New(const char* data, int length = -1);
914
915 /** Allocates a new string from utf16 data.*/
916 static Local<String> New(const uint16_t* data, int length = -1);
917
918 /** Creates a symbol. Returns one if it exists already.*/
919 static Local<String> NewSymbol(const char* data, int length = -1);
920
v8.team.kasperl727e9952008-09-02 14:56:44 +0000921 /**
922 * Creates a new external string using the data defined in the given
923 * resource. The resource is deleted when the external string is no
924 * longer live on V8's heap. The caller of this function should not
925 * delete or modify the resource. Neither should the underlying buffer be
926 * deallocated or modified except through the destructor of the
927 * external string resource.
928 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000930
ager@chromium.org6f10e412009-02-13 10:11:16 +0000931 /**
932 * Associate an external string resource with this string by transforming it
933 * in place so that existing references to this string in the JavaScript heap
934 * will use the external string resource. The external string resource's
935 * character contents needs to be equivalent to this string.
936 * Returns true if the string has been changed to be an external string.
937 * The string is not modified if the operation fails.
938 */
939 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940
v8.team.kasperl727e9952008-09-02 14:56:44 +0000941 /**
942 * Creates a new external string using the ascii data defined in the given
943 * resource. The resource is deleted when the external string is no
944 * longer live on V8's heap. The caller of this function should not
945 * delete or modify the resource. Neither should the underlying buffer be
946 * deallocated or modified except through the destructor of the
947 * external string resource.
948 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000950
ager@chromium.org6f10e412009-02-13 10:11:16 +0000951 /**
952 * Associate an external string resource with this string by transforming it
953 * in place so that existing references to this string in the JavaScript heap
954 * will use the external string resource. The external string resource's
955 * character contents needs to be equivalent to this string.
956 * Returns true if the string has been changed to be an external string.
957 * The string is not modified if the operation fails.
958 */
959 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000961 /**
962 * Returns true if this string can be made external.
963 */
964 bool CanMakeExternal();
965
kasper.lund7276f142008-07-30 08:49:36 +0000966 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967 static Local<String> NewUndetectable(const char* data, int length = -1);
968
kasper.lund7276f142008-07-30 08:49:36 +0000969 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
971
972 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000973 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000974 * you want to print the object. If conversion to a string fails
975 * (eg. due to an exception in the toString() method of the object)
976 * then the length() method returns 0 and the * operator returns
977 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000978 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000979 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000980 public:
981 explicit Utf8Value(Handle<v8::Value> obj);
982 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000983 char* operator*() { return str_; }
984 const char* operator*() const { return str_; }
985 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000986 private:
987 char* str_;
988 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000989
990 // Disallow copying and assigning.
991 Utf8Value(const Utf8Value&);
992 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000993 };
994
995 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 * Converts an object to an ascii string.
997 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000998 * If conversion to a string fails (eg. due to an exception in the toString()
999 * method of the object) then the length() method returns 0 and the * operator
1000 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001002 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 public:
1004 explicit AsciiValue(Handle<v8::Value> obj);
1005 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001006 char* operator*() { return str_; }
1007 const char* operator*() const { return str_; }
1008 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 private:
1010 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001011 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001012
1013 // Disallow copying and assigning.
1014 AsciiValue(const AsciiValue&);
1015 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 };
1017
1018 /**
1019 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001020 * If conversion to a string fails (eg. due to an exception in the toString()
1021 * method of the object) then the length() method returns 0 and the * operator
1022 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001024 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 public:
1026 explicit Value(Handle<v8::Value> obj);
1027 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001028 uint16_t* operator*() { return str_; }
1029 const uint16_t* operator*() const { return str_; }
1030 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031 private:
1032 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001033 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001034
1035 // Disallow copying and assigning.
1036 Value(const Value&);
1037 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 };
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001039
1040 private:
1041 void VerifyExternalStringResource(ExternalStringResource* val) const;
1042 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043};
1044
1045
1046/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001047 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001049class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001051 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001053 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 private:
1055 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001056 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057};
1058
1059
1060/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001061 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001063class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 public:
1065 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001066 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001067 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 private:
1069 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001070 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071};
1072
1073
1074/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001075 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001077class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001079 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 private:
1081 Int32();
1082};
1083
1084
1085/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001088class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001090 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 private:
1092 Uint32();
1093};
1094
1095
1096/**
1097 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1098 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001099class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 public:
1101 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001102
1103 /**
1104 * A specialization of Value::NumberValue that is more efficient
1105 * because we know the structure of this object.
1106 */
ager@chromium.org32912102009-01-16 10:38:43 +00001107 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001108
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001109 static inline Date* Cast(v8::Value* obj);
1110 private:
1111 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112};
1113
1114
1115enum PropertyAttribute {
1116 None = 0,
1117 ReadOnly = 1 << 0,
1118 DontEnum = 1 << 1,
1119 DontDelete = 1 << 2
1120};
1121
1122/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001123 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001125class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 public:
1127 bool Set(Handle<Value> key,
1128 Handle<Value> value,
1129 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001130
ager@chromium.orge2902be2009-06-08 12:21:35 +00001131 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001132 // overriding accessors or read-only properties.
1133 //
1134 // Note that if the object has an interceptor the property will be set
1135 // locally, but since the interceptor takes precedence the local property
1136 // will only be returned if the interceptor doesn't return a value.
1137 //
1138 // Note also that this only works for named properties.
1139 bool ForceSet(Handle<Value> key,
1140 Handle<Value> value,
1141 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 Local<Value> Get(Handle<Value> key);
1144
1145 // TODO(1245389): Replace the type-specific versions of these
1146 // functions with generic ones that accept a Handle<Value> key.
1147 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001150
1151 // Delete a property on this object bypassing interceptors and
1152 // ignoring dont-delete attributes.
1153 bool ForceDelete(Handle<Value> key);
1154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 bool Delete(uint32_t index);
1158
1159 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001160 * Returns an array containing the names of the enumerable properties
1161 * of this object, including properties from prototype objects. The
1162 * array returned by this method contains the same values as would
1163 * be enumerated by a for-in statement over this object.
1164 */
1165 Local<Array> GetPropertyNames();
1166
1167 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 * Get the prototype object. This does not skip objects marked to
1169 * be skipped by __proto__ and it does not consult the security
1170 * handler.
1171 */
1172 Local<Value> GetPrototype();
1173
1174 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001175 * Finds an instance of the given function template in the prototype
1176 * chain.
1177 */
1178 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1179
1180 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 * Call builtin Object.prototype.toString on this object.
1182 * This is different from Value::ToString() that may call
1183 * user-defined toString function. This one does not.
1184 */
1185 Local<String> ObjectProtoToString();
1186
kasper.lund212ac232008-07-16 07:07:30 +00001187 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001189 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001190 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001191 /** Sets the value in an internal field. */
1192 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001194 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001195 inline void* GetPointerFromInternalField(int index);
1196
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001197 /** Sets a native pointer in an internal field. */
1198 void SetPointerInInternalField(int index, void* value);
1199
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 // Testers for local properties.
1201 bool HasRealNamedProperty(Handle<String> key);
1202 bool HasRealIndexedProperty(uint32_t index);
1203 bool HasRealNamedCallbackProperty(Handle<String> key);
1204
1205 /**
1206 * If result.IsEmpty() no real property was located in the prototype chain.
1207 * This means interceptors in the prototype chain are not called.
1208 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001209 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1210
1211 /**
1212 * If result.IsEmpty() no real property was located on the object or
1213 * in the prototype chain.
1214 * This means interceptors in the prototype chain are not called.
1215 */
1216 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217
1218 /** Tests for a named lookup interceptor.*/
1219 bool HasNamedLookupInterceptor();
1220
kasper.lund212ac232008-07-16 07:07:30 +00001221 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 bool HasIndexedLookupInterceptor();
1223
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001224 /**
1225 * Turns on access check on the object if the object is an instance of
1226 * a template that has access check callbacks. If an object has no
1227 * access check info, the object cannot be accessed by anyone.
1228 */
1229 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001230
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001231 /**
1232 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001233 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001234 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001235 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001236 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001237 */
1238 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001239
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001240 /**
1241 * Access hidden properties on JavaScript objects. These properties are
1242 * hidden from the executing JavaScript and only accessible through the V8
1243 * C++ API. Hidden properties introduced by V8 internally (for example the
1244 * identity hash) are prefixed with "v8::".
1245 */
1246 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1247 Local<Value> GetHiddenValue(Handle<String> key);
1248 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001249
1250 /**
1251 * Returns true if this is an instance of an api function (one
1252 * created from a function created from a function template) and has
1253 * been modified since it was created. Note that this method is
1254 * conservative and may return true for objects that haven't actually
1255 * been modified.
1256 */
1257 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001258
1259 /**
1260 * Clone this object with a fast but shallow copy. Values will point
1261 * to the same values as the original object.
1262 */
1263 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001265 /**
1266 * Set the backing store of the indexed properties to be managed by the
1267 * embedding layer. Access to the indexed properties will follow the rules
1268 * spelled out in CanvasPixelArray.
1269 * Note: The embedding program still owns the data and needs to ensure that
1270 * the backing store is preserved while V8 has a reference.
1271 */
1272 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1273
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001275 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 private:
1277 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001278 static void CheckCast(Value* obj);
1279 Local<Value> CheckedGetInternalField(int index);
1280
1281 /**
1282 * If quick access to the internal field is possible this method
1283 * returns the value. Otherwise an empty handle is returned.
1284 */
1285 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286};
1287
1288
1289/**
1290 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1291 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001292class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001294 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295
ager@chromium.org3e875802009-06-29 08:26:34 +00001296 /**
1297 * Clones an element at index |index|. Returns an empty
1298 * handle if cloning fails (for any reason).
1299 */
1300 Local<Object> CloneElementAt(uint32_t index);
1301
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001303 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 private:
1305 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001306 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307};
1308
1309
1310/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001311 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001313class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001315 Local<Object> NewInstance() const;
1316 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1318 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001319 Handle<Value> GetName() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001320 static inline Function* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 private:
1322 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001323 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324};
1325
1326
1327/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001328 * A JavaScript value that wraps a C++ void*. This type of value is
1329 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001331 *
1332 * The Wrap function V8 will return the most optimal Value object wrapping the
1333 * C++ void*. The type of the value is not guaranteed to be an External object
1334 * and no assumptions about its type should be made. To access the wrapped
1335 * value Unwrap should be used, all other operations on that object will lead
1336 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001338class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001340 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001341 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001344 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001345 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 private:
1347 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001348 static void CheckCast(v8::Value* obj);
1349 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1350 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351};
1352
1353
1354// --- T e m p l a t e s ---
1355
1356
1357/**
1358 * The superclass of object and function templates.
1359 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001360class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 public:
1362 /** Adds a property to each instance created by this template.*/
1363 void Set(Handle<String> name, Handle<Data> value,
1364 PropertyAttribute attributes = None);
1365 inline void Set(const char* name, Handle<Data> value);
1366 private:
1367 Template();
1368
1369 friend class ObjectTemplate;
1370 friend class FunctionTemplate;
1371};
1372
1373
1374/**
1375 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001376 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377 * including the receiver, the number and values of arguments, and
1378 * the holder of the function.
1379 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001380class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 public:
1382 inline int Length() const;
1383 inline Local<Value> operator[](int i) const;
1384 inline Local<Function> Callee() const;
1385 inline Local<Object> This() const;
1386 inline Local<Object> Holder() const;
1387 inline bool IsConstructCall() const;
1388 inline Local<Value> Data() const;
1389 private:
1390 Arguments();
1391 friend class ImplementationUtilities;
1392 inline Arguments(Local<Value> data,
1393 Local<Object> holder,
1394 Local<Function> callee,
1395 bool is_construct_call,
1396 void** values, int length);
1397 Local<Value> data_;
1398 Local<Object> holder_;
1399 Local<Function> callee_;
1400 bool is_construct_call_;
1401 void** values_;
1402 int length_;
1403};
1404
1405
1406/**
1407 * The information passed to an accessor callback about the context
1408 * of the property access.
1409 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001410class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001412 inline AccessorInfo(internal::Object** args)
1413 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 inline Local<Value> Data() const;
1415 inline Local<Object> This() const;
1416 inline Local<Object> Holder() const;
1417 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001418 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419};
1420
1421
1422typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1423
1424typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1425
1426/**
1427 * Accessor[Getter|Setter] are used as callback functions when
1428 * setting|getting a particular property. See objectTemplate::SetAccessor.
1429 */
1430typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1431 const AccessorInfo& info);
1432
1433
1434typedef void (*AccessorSetter)(Local<String> property,
1435 Local<Value> value,
1436 const AccessorInfo& info);
1437
1438
1439/**
1440 * NamedProperty[Getter|Setter] are used as interceptors on object.
1441 * See ObjectTemplate::SetNamedPropertyHandler.
1442 */
1443typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1444 const AccessorInfo& info);
1445
1446
1447/**
1448 * Returns the value if the setter intercepts the request.
1449 * Otherwise, returns an empty handle.
1450 */
1451typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1452 Local<Value> value,
1453 const AccessorInfo& info);
1454
1455
1456/**
1457 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001458 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 */
1460typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1461 const AccessorInfo& info);
1462
1463
1464/**
1465 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001466 * The return value is true if the property could be deleted and false
1467 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 */
1469typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1470 const AccessorInfo& info);
1471
1472/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001473 * Returns an array containing the names of the properties the named
1474 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 */
1476typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1477
v8.team.kasperl727e9952008-09-02 14:56:44 +00001478
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001480 * Returns the value of the property if the getter intercepts the
1481 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 */
1483typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1484 const AccessorInfo& info);
1485
1486
1487/**
1488 * Returns the value if the setter intercepts the request.
1489 * Otherwise, returns an empty handle.
1490 */
1491typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1492 Local<Value> value,
1493 const AccessorInfo& info);
1494
1495
1496/**
1497 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001498 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 */
1500typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1501 const AccessorInfo& info);
1502
1503/**
1504 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001505 * The return value is true if the property could be deleted and false
1506 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 */
1508typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1509 const AccessorInfo& info);
1510
v8.team.kasperl727e9952008-09-02 14:56:44 +00001511/**
1512 * Returns an array containing the indices of the properties the
1513 * indexed property getter intercepts.
1514 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1516
1517
1518/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001519 * Access control specifications.
1520 *
1521 * Some accessors should be accessible across contexts. These
1522 * accessors have an explicit access control parameter which specifies
1523 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001524 *
1525 * Additionally, for security, accessors can prohibit overwriting by
1526 * accessors defined in JavaScript. For objects that have such
1527 * accessors either locally or in their prototype chain it is not
1528 * possible to overwrite the accessor by using __defineGetter__ or
1529 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 */
1531enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001532 DEFAULT = 0,
1533 ALL_CAN_READ = 1,
1534 ALL_CAN_WRITE = 1 << 1,
1535 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536};
1537
1538
1539/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001540 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 */
1542enum AccessType {
1543 ACCESS_GET,
1544 ACCESS_SET,
1545 ACCESS_HAS,
1546 ACCESS_DELETE,
1547 ACCESS_KEYS
1548};
1549
v8.team.kasperl727e9952008-09-02 14:56:44 +00001550
1551/**
1552 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001553 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001554 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001555typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 Local<Value> key,
1557 AccessType type,
1558 Local<Value> data);
1559
v8.team.kasperl727e9952008-09-02 14:56:44 +00001560
1561/**
1562 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001563 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001564 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001565typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 uint32_t index,
1567 AccessType type,
1568 Local<Value> data);
1569
1570
1571/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001572 * A FunctionTemplate is used to create functions at runtime. There
1573 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001574 * context. The lifetime of the created function is equal to the
1575 * lifetime of the context. So in case the embedder needs to create
1576 * temporary functions that can be collected using Scripts is
1577 * preferred.
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/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001984 * A set of constraints that specifies the limits of the runtime's memory use.
1985 * You must set the heap size before initializing the VM - the size cannot be
1986 * adjusted after the VM is initialized.
1987 *
1988 * If you are using threads then you should hold the V8::Locker lock while
1989 * setting the stack limit and you must set a non-default stack limit separately
1990 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001992class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 public:
1994 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001995 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001997 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001999 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002000 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2002 private:
2003 int max_young_space_size_;
2004 int max_old_space_size_;
2005 uint32_t* stack_limit_;
2006};
2007
2008
2009bool SetResourceConstraints(ResourceConstraints* constraints);
2010
2011
2012// --- E x c e p t i o n s ---
2013
2014
2015typedef void (*FatalErrorCallback)(const char* location, const char* message);
2016
2017
2018typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2019
2020
2021/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002022 * Schedules an exception to be thrown when returning to JavaScript. When an
2023 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002025 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002027Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028
2029/**
2030 * Create new error objects by calling the corresponding error object
2031 * constructor with the message.
2032 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002033class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 public:
2035 static Local<Value> RangeError(Handle<String> message);
2036 static Local<Value> ReferenceError(Handle<String> message);
2037 static Local<Value> SyntaxError(Handle<String> message);
2038 static Local<Value> TypeError(Handle<String> message);
2039 static Local<Value> Error(Handle<String> message);
2040};
2041
2042
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002043// --- 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 +00002044
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002045typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002047typedef void* (*CreateHistogramCallback)(const char* name,
2048 int min,
2049 int max,
2050 size_t buckets);
2051
2052typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054// --- 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 ---
2055typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2056 AccessType type,
2057 Local<Value> data);
2058
2059// --- 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
2060
2061/**
2062 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002063 * before and after a major garbage collection. Allocations are not
2064 * allowed in the callback function, you therefore cannot manipulate
2065 * objects (set or delete properties for example) since it is possible
2066 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067 */
2068typedef void (*GCCallback)();
2069
2070
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002071// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072
2073/**
2074 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002075 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077typedef Persistent<Context> (*ContextGenerator)();
2078
2079
2080/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002081 * Profiler modules.
2082 *
2083 * In V8, profiler consists of several modules: CPU profiler, and different
2084 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002085 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2086 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002087 */
2088enum ProfilerModules {
2089 PROFILER_MODULE_NONE = 0,
2090 PROFILER_MODULE_CPU = 1,
2091 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002092 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2093 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002094};
2095
2096
2097/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 * Container class for static utility functions.
2099 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002100class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002102 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 static void SetFatalErrorHandler(FatalErrorCallback that);
2104
v8.team.kasperl727e9952008-09-02 14:56:44 +00002105 /**
2106 * Ignore out-of-memory exceptions.
2107 *
2108 * V8 running out of memory is treated as a fatal error by default.
2109 * This means that the fatal error handler is called and that V8 is
2110 * terminated.
2111 *
2112 * IgnoreOutOfMemoryException can be used to not treat a
2113 * out-of-memory situation as a fatal error. This way, the contexts
2114 * that did not cause the out of memory problem might be able to
2115 * continue execution.
2116 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 static void IgnoreOutOfMemoryException();
2118
v8.team.kasperl727e9952008-09-02 14:56:44 +00002119 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002120 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121 * fatal errors such as out-of-memory situations.
2122 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 static bool IsDead();
2124
2125 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002126 * Adds a message listener.
2127 *
2128 * The same message listener can be added more than once and it that
2129 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 */
2131 static bool AddMessageListener(MessageCallback that,
2132 Handle<Value> data = Handle<Value>());
2133
2134 /**
2135 * Remove all message listeners from the specified callback function.
2136 */
2137 static void RemoveMessageListeners(MessageCallback that);
2138
2139 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002140 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141 */
2142 static void SetFlagsFromString(const char* str, int length);
2143
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002144 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002145 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002146 */
2147 static void SetFlagsFromCommandLine(int* argc,
2148 char** argv,
2149 bool remove_flags);
2150
kasper.lund7276f142008-07-30 08:49:36 +00002151 /** Get the version string. */
2152 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153
2154 /**
2155 * Enables the host application to provide a mechanism for recording
2156 * statistics counters.
2157 */
2158 static void SetCounterFunction(CounterLookupCallback);
2159
2160 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002161 * Enables the host application to provide a mechanism for recording
2162 * histograms. The CreateHistogram function returns a
2163 * histogram which will later be passed to the AddHistogramSample
2164 * function.
2165 */
2166 static void SetCreateHistogramFunction(CreateHistogramCallback);
2167 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2168
2169 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 * Enables the computation of a sliding window of states. The sliding
2171 * window information is recorded in statistics counters.
2172 */
2173 static void EnableSlidingStateWindow();
2174
2175 /** Callback function for reporting failed access checks.*/
2176 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2177
2178 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002179 * Enables the host application to receive a notification before a
2180 * major garbage colletion. Allocations are not allowed in the
2181 * callback function, you therefore cannot manipulate objects (set
2182 * or delete properties for example) since it is possible such
2183 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 */
2185 static void SetGlobalGCPrologueCallback(GCCallback);
2186
2187 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 * Enables the host application to receive a notification after a
2189 * major garbage collection. Allocations are not allowed in the
2190 * callback function, you therefore cannot manipulate objects (set
2191 * or delete properties for example) since it is possible such
2192 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 */
2194 static void SetGlobalGCEpilogueCallback(GCCallback);
2195
2196 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197 * Allows the host application to group objects together. If one
2198 * object in the group is alive, all objects in the group are alive.
2199 * After each garbage collection, object groups are removed. It is
2200 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002201 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002202 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002204 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205
2206 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002207 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002208 * initialize from scratch. This function is called implicitly if
2209 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 */
2211 static bool Initialize();
2212
kasper.lund7276f142008-07-30 08:49:36 +00002213 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002214 * Adjusts the amount of registered external memory. Used to give
2215 * V8 an indication of the amount of externally allocated memory
2216 * that is kept alive by JavaScript objects. V8 uses this to decide
2217 * when to perform global garbage collections. Registering
2218 * externally allocated memory will trigger global garbage
2219 * collections more often than otherwise in an attempt to garbage
2220 * collect the JavaScript objects keeping the externally allocated
2221 * memory alive.
2222 *
2223 * \param change_in_bytes the change in externally allocated memory
2224 * that is kept alive by JavaScript objects.
2225 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002226 */
2227 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2228
iposva@chromium.org245aa852009-02-10 00:49:54 +00002229 /**
2230 * Suspends recording of tick samples in the profiler.
2231 * When the V8 profiling mode is enabled (usually via command line
2232 * switches) this function suspends recording of tick samples.
2233 * Profiling ticks are discarded until ResumeProfiler() is called.
2234 *
2235 * See also the --prof and --prof_auto command line switches to
2236 * enable V8 profiling.
2237 */
2238 static void PauseProfiler();
2239
2240 /**
2241 * Resumes recording of tick samples in the profiler.
2242 * See also PauseProfiler().
2243 */
2244 static void ResumeProfiler();
2245
ager@chromium.org41826e72009-03-30 13:30:57 +00002246 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002247 * Return whether profiler is currently paused.
2248 */
2249 static bool IsProfilerPaused();
2250
2251 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002252 * Resumes specified profiler modules.
2253 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2254 * See ProfilerModules enum.
2255 *
2256 * \param flags Flags specifying profiler modules.
2257 */
2258 static void ResumeProfilerEx(int flags);
2259
2260 /**
2261 * Pauses specified profiler modules.
2262 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2263 * See ProfilerModules enum.
2264 *
2265 * \param flags Flags specifying profiler modules.
2266 */
2267 static void PauseProfilerEx(int flags);
2268
2269 /**
2270 * Returns active (resumed) profiler modules.
2271 * See ProfilerModules enum.
2272 *
2273 * \returns active profiler modules.
2274 */
2275 static int GetActiveProfilerModules();
2276
2277 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002278 * If logging is performed into a memory buffer (via --logfile=*), allows to
2279 * retrieve previously written messages. This can be used for retrieving
2280 * profiler log data in the application. This function is thread-safe.
2281 *
2282 * Caller provides a destination buffer that must exist during GetLogLines
2283 * call. Only whole log lines are copied into the buffer.
2284 *
2285 * \param from_pos specified a point in a buffer to read from, 0 is the
2286 * beginning of a buffer. It is assumed that caller updates its current
2287 * position using returned size value from the previous call.
2288 * \param dest_buf destination buffer for log data.
2289 * \param max_size size of the destination buffer.
2290 * \returns actual size of log data copied into buffer.
2291 */
2292 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2293
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002294 /**
2295 * Retrieve the V8 thread id of the calling thread.
2296 *
2297 * The thread id for a thread should only be retrieved after the V8
2298 * lock has been acquired with a Locker object with that thread.
2299 */
2300 static int GetCurrentThreadId();
2301
2302 /**
2303 * Forcefully terminate execution of a JavaScript thread. This can
2304 * be used to terminate long-running scripts.
2305 *
2306 * TerminateExecution should only be called when then V8 lock has
2307 * been acquired with a Locker object. Therefore, in order to be
2308 * able to terminate long-running threads, preemption must be
2309 * enabled to allow the user of TerminateExecution to acquire the
2310 * lock.
2311 *
2312 * The termination is achieved by throwing an exception that is
2313 * uncatchable by JavaScript exception handlers. Termination
2314 * exceptions act as if they were caught by a C++ TryCatch exception
2315 * handlers. If forceful termination is used, any C++ TryCatch
2316 * exception handler that catches an exception should check if that
2317 * exception is a termination exception and immediately return if
2318 * that is the case. Returning immediately in that case will
2319 * continue the propagation of the termination exception if needed.
2320 *
2321 * The thread id passed to TerminateExecution must have been
2322 * obtained by calling GetCurrentThreadId on the thread in question.
2323 *
2324 * \param thread_id The thread id of the thread to terminate.
2325 */
2326 static void TerminateExecution(int thread_id);
2327
2328 /**
2329 * Forcefully terminate the current thread of JavaScript execution.
2330 *
2331 * This method can be used by any thread even if that thread has not
2332 * acquired the V8 lock with a Locker object.
2333 */
2334 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002335
2336 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002337 * Releases any resources used by v8 and stops any utility threads
2338 * that may be running. Note that disposing v8 is permanent, it
2339 * cannot be reinitialized.
2340 *
2341 * It should generally not be necessary to dispose v8 before exiting
2342 * a process, this should happen automatically. It is only necessary
2343 * to use if the process needs the resources taken up by v8.
2344 */
2345 static bool Dispose();
2346
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002347
2348 /**
2349 * Optional notification that the embedder is idle.
2350 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002351 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002352 * Returns true if the embedder should stop calling IdleNotification
2353 * until real work has been done. This indicates that V8 has done
2354 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002355 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002356 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002357
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002358 /**
2359 * Optional notification that the system is running low on memory.
2360 * V8 uses these notifications to attempt to free memory.
2361 */
2362 static void LowMemoryNotification();
2363
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 private:
2365 V8();
2366
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002367 static internal::Object** GlobalizeReference(internal::Object** handle);
2368 static void DisposeGlobal(internal::Object** global_handle);
2369 static void MakeWeak(internal::Object** global_handle,
2370 void* data,
2371 WeakReferenceCallback);
2372 static void ClearWeak(internal::Object** global_handle);
2373 static bool IsGlobalNearDeath(internal::Object** global_handle);
2374 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375
2376 template <class T> friend class Handle;
2377 template <class T> friend class Local;
2378 template <class T> friend class Persistent;
2379 friend class Context;
2380};
2381
2382
2383/**
2384 * An external exception handler.
2385 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002386class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 public:
2388
2389 /**
2390 * Creates a new try/catch block and registers it with v8.
2391 */
2392 TryCatch();
2393
2394 /**
2395 * Unregisters and deletes this try/catch block.
2396 */
2397 ~TryCatch();
2398
2399 /**
2400 * Returns true if an exception has been caught by this try/catch block.
2401 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002402 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403
2404 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002405 * For certain types of exceptions, it makes no sense to continue
2406 * execution.
2407 *
2408 * Currently, the only type of exception that can be caught by a
2409 * TryCatch handler and for which it does not make sense to continue
2410 * is termination exception. Such exceptions are thrown when the
2411 * TerminateExecution methods are called to terminate a long-running
2412 * script.
2413 *
2414 * If CanContinue returns false, the correct action is to perform
2415 * any C++ cleanup needed and then return.
2416 */
2417 bool CanContinue() const;
2418
2419 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 * Returns the exception caught by this try/catch block. If no exception has
2421 * been caught an empty handle is returned.
2422 *
2423 * The returned handle is valid until this TryCatch block has been destroyed.
2424 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002425 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426
2427 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002428 * Returns the .stack property of the thrown object. If no .stack
2429 * property is present an empty handle is returned.
2430 */
2431 Local<Value> StackTrace() const;
2432
2433 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002434 * Returns the message associated with this exception. If there is
2435 * no message associated an empty handle is returned.
2436 *
2437 * The returned handle is valid until this TryCatch block has been
2438 * destroyed.
2439 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002440 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002441
2442 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 * Clears any exceptions that may have been caught by this try/catch block.
2444 * After this method has been called, HasCaught() will return false.
2445 *
2446 * It is not necessary to clear a try/catch block before using it again; if
2447 * another exception is thrown the previously caught exception will just be
2448 * overwritten. However, it is often a good idea since it makes it easier
2449 * to determine which operation threw a given exception.
2450 */
2451 void Reset();
2452
v8.team.kasperl727e9952008-09-02 14:56:44 +00002453 /**
2454 * Set verbosity of the external exception handler.
2455 *
2456 * By default, exceptions that are caught by an external exception
2457 * handler are not reported. Call SetVerbose with true on an
2458 * external exception handler to have exceptions caught by the
2459 * handler reported as if they were not caught.
2460 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 void SetVerbose(bool value);
2462
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002463 /**
2464 * Set whether or not this TryCatch should capture a Message object
2465 * which holds source information about where the exception
2466 * occurred. True by default.
2467 */
2468 void SetCaptureMessage(bool value);
2469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 public:
2471 TryCatch* next_;
2472 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002473 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 bool is_verbose_;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002475 bool can_continue_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002476 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002477 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478};
2479
2480
2481// --- C o n t e x t ---
2482
2483
2484/**
2485 * Ignore
2486 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002487class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 public:
2489 ExtensionConfiguration(int name_count, const char* names[])
2490 : name_count_(name_count), names_(names) { }
2491 private:
2492 friend class ImplementationUtilities;
2493 int name_count_;
2494 const char** names_;
2495};
2496
2497
2498/**
2499 * A sandboxed execution context with its own set of built-in objects
2500 * and functions.
2501 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002502class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002504 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 Local<Object> Global();
2506
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002507 /**
2508 * Detaches the global object from its context before
2509 * the global object can be reused to create a new context.
2510 */
2511 void DetachGlobal();
2512
v8.team.kasperl727e9952008-09-02 14:56:44 +00002513 /** Creates a new context. */
2514 static Persistent<Context> New(
2515 ExtensionConfiguration* extensions = 0,
2516 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2517 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518
kasper.lund44510672008-07-25 07:37:58 +00002519 /** Returns the last entered context. */
2520 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521
kasper.lund44510672008-07-25 07:37:58 +00002522 /** Returns the context that is on the top of the stack. */
2523 static Local<Context> GetCurrent();
2524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002526 * Returns the context of the calling JavaScript code. That is the
2527 * context of the top-most JavaScript frame. If there are no
2528 * JavaScript frames an empty handle is returned.
2529 */
2530 static Local<Context> GetCalling();
2531
2532 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 * Sets the security token for the context. To access an object in
2534 * another context, the security tokens must match.
2535 */
2536 void SetSecurityToken(Handle<Value> token);
2537
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002538 /** Restores the security token to the default value. */
2539 void UseDefaultSecurityToken();
2540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 /** Returns the security token of this context.*/
2542 Handle<Value> GetSecurityToken();
2543
v8.team.kasperl727e9952008-09-02 14:56:44 +00002544 /**
2545 * Enter this context. After entering a context, all code compiled
2546 * and run is compiled and run in this context. If another context
2547 * is already entered, this old context is saved so it can be
2548 * restored when the new context is exited.
2549 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002551
2552 /**
2553 * Exit this context. Exiting the current context restores the
2554 * context that was in place when entering the current context.
2555 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 void Exit();
2557
v8.team.kasperl727e9952008-09-02 14:56:44 +00002558 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 bool HasOutOfMemoryException();
2560
v8.team.kasperl727e9952008-09-02 14:56:44 +00002561 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 static bool InContext();
2563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002565 * Associate an additional data object with the context. This is mainly used
2566 * with the debugger to provide additional information on the context through
2567 * the debugger API.
2568 */
2569 void SetData(Handle<Value> data);
2570 Local<Value> GetData();
2571
2572 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 * Stack-allocated class which sets the execution context for all
2574 * operations executed within a local scope.
2575 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002576 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 public:
2578 inline Scope(Handle<Context> context) : context_(context) {
2579 context_->Enter();
2580 }
2581 inline ~Scope() { context_->Exit(); }
2582 private:
2583 Handle<Context> context_;
2584 };
2585
2586 private:
2587 friend class Value;
2588 friend class Script;
2589 friend class Object;
2590 friend class Function;
2591};
2592
2593
2594/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002595 * Multiple threads in V8 are allowed, but only one thread at a time
2596 * is allowed to use V8. The definition of 'using V8' includes
2597 * accessing handles or holding onto object pointers obtained from V8
2598 * handles. It is up to the user of V8 to ensure (perhaps with
2599 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600 *
2601 * If you wish to start using V8 in a thread you can do this by constructing
2602 * a v8::Locker object. After the code using V8 has completed for the
2603 * current thread you can call the destructor. This can be combined
2604 * with C++ scope-based construction as follows:
2605 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002606 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607 * ...
2608 * {
2609 * v8::Locker locker;
2610 * ...
2611 * // Code using V8 goes here.
2612 * ...
2613 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002614 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 *
2616 * If you wish to stop using V8 in a thread A you can do this by either
2617 * by destroying the v8::Locker object as above or by constructing a
2618 * v8::Unlocker object:
2619 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002620 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 * {
2622 * v8::Unlocker unlocker;
2623 * ...
2624 * // Code not using V8 goes here while V8 can run in another thread.
2625 * ...
2626 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002627 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 *
2629 * The Unlocker object is intended for use in a long-running callback
2630 * from V8, where you want to release the V8 lock for other threads to
2631 * use.
2632 *
2633 * The v8::Locker is a recursive lock. That is, you can lock more than
2634 * once in a given thread. This can be useful if you have code that can
2635 * be called either from code that holds the lock or from code that does
2636 * not. The Unlocker is not recursive so you can not have several
2637 * Unlockers on the stack at once, and you can not use an Unlocker in a
2638 * thread that is not inside a Locker's scope.
2639 *
2640 * An unlocker will unlock several lockers if it has to and reinstate
2641 * the correct depth of locking on its destruction. eg.:
2642 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002643 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 * // V8 not locked.
2645 * {
2646 * v8::Locker locker;
2647 * // V8 locked.
2648 * {
2649 * v8::Locker another_locker;
2650 * // V8 still locked (2 levels).
2651 * {
2652 * v8::Unlocker unlocker;
2653 * // V8 not locked.
2654 * }
2655 * // V8 locked again (2 levels).
2656 * }
2657 * // V8 still locked (1 level).
2658 * }
2659 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002660 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002662class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 public:
2664 Unlocker();
2665 ~Unlocker();
2666};
2667
2668
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002669class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 public:
2671 Locker();
2672 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002673
2674 /**
2675 * Start preemption.
2676 *
2677 * When preemption is started, a timer is fired every n milli seconds
2678 * that will switch between multiple threads that are in contention
2679 * for the V8 lock.
2680 */
2681 static void StartPreemption(int every_n_ms);
2682
2683 /**
2684 * Stop preemption.
2685 */
2686 static void StopPreemption();
2687
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002688 /**
2689 * Returns whether or not the locker is locked by the current thread.
2690 */
2691 static bool IsLocked();
2692
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002693 /**
2694 * Returns whether v8::Locker is being used by this V8 instance.
2695 */
2696 static bool IsActive() { return active_; }
2697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 private:
2699 bool has_lock_;
2700 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002701
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002702 static bool active_;
2703
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002704 // Disallow copying and assigning.
2705 Locker(const Locker&);
2706 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707};
2708
2709
2710
2711// --- I m p l e m e n t a t i o n ---
2712
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002713
2714namespace internal {
2715
2716
2717// Tag information for HeapObject.
2718const int kHeapObjectTag = 1;
2719const int kHeapObjectTagSize = 2;
2720const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2721
2722
2723// Tag information for Smi.
2724const int kSmiTag = 0;
2725const int kSmiTagSize = 1;
2726const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2727
2728
2729/**
2730 * This class exports constants and functionality from within v8 that
2731 * is necessary to implement inline functions in the v8 api. Don't
2732 * depend on functions and constants defined here.
2733 */
2734class Internals {
2735 public:
2736
2737 // These values match non-compiler-dependent values defined within
2738 // the implementation of v8.
2739 static const int kHeapObjectMapOffset = 0;
2740 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
2741 static const int kStringResourceOffset = 2 * sizeof(void*);
2742 static const int kProxyProxyOffset = sizeof(void*);
2743 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2744 static const int kFullStringRepresentationMask = 0x07;
2745 static const int kExternalTwoByteRepresentationTag = 0x03;
2746 static const int kAlignedPointerShift = 2;
2747
2748 // These constants are compiler dependent so their values must be
2749 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002750 V8EXPORT static int kJSObjectType;
2751 V8EXPORT static int kFirstNonstringType;
2752 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002753
2754 static inline bool HasHeapObjectTag(internal::Object* value) {
2755 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2756 kHeapObjectTag);
2757 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002758
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002759 static inline bool HasSmiTag(internal::Object* value) {
2760 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2761 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002762
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002763 static inline int SmiValue(internal::Object* value) {
2764 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> kSmiTagSize;
2765 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002766
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002767 static inline bool IsExternalTwoByteString(int instance_type) {
2768 int representation = (instance_type & kFullStringRepresentationMask);
2769 return representation == kExternalTwoByteRepresentationTag;
2770 }
2771
2772 template <typename T>
2773 static inline T ReadField(Object* ptr, int offset) {
2774 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2775 return *reinterpret_cast<T*>(addr);
2776 }
2777
2778};
2779
2780}
2781
2782
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783template <class T>
2784Handle<T>::Handle() : val_(0) { }
2785
2786
2787template <class T>
2788Local<T>::Local() : Handle<T>() { }
2789
2790
2791template <class T>
2792Local<T> Local<T>::New(Handle<T> that) {
2793 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002794 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002795 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2796}
2797
2798
2799template <class T>
2800Persistent<T> Persistent<T>::New(Handle<T> that) {
2801 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002802 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2804}
2805
2806
2807template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002808bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002810 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811}
2812
2813
2814template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002815bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002817 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818}
2819
2820
2821template <class T>
2822void Persistent<T>::Dispose() {
2823 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002824 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825}
2826
2827
2828template <class T>
2829Persistent<T>::Persistent() : Handle<T>() { }
2830
2831template <class T>
2832void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002833 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
2834 parameters,
2835 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002836}
2837
2838template <class T>
2839void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002840 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002841}
2842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843Local<Value> Arguments::operator[](int i) const {
2844 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2845 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2846}
2847
2848
2849Local<Function> Arguments::Callee() const {
2850 return callee_;
2851}
2852
2853
2854Local<Object> Arguments::This() const {
2855 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2856}
2857
2858
2859Local<Object> Arguments::Holder() const {
2860 return holder_;
2861}
2862
2863
2864Local<Value> Arguments::Data() const {
2865 return data_;
2866}
2867
2868
2869bool Arguments::IsConstructCall() const {
2870 return is_construct_call_;
2871}
2872
2873
2874int Arguments::Length() const {
2875 return length_;
2876}
2877
2878
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879template <class T>
2880Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002881 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
2882 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883 return Local<T>(reinterpret_cast<T*>(after));
2884}
2885
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002886Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002887 return resource_name_;
2888}
2889
2890
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002891Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 return resource_line_offset_;
2893}
2894
2895
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002896Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897 return resource_column_offset_;
2898}
2899
2900
2901Handle<Boolean> Boolean::New(bool value) {
2902 return value ? True() : False();
2903}
2904
2905
2906void Template::Set(const char* name, v8::Handle<Data> value) {
2907 Set(v8::String::New(name), value);
2908}
2909
2910
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002911Local<Value> Object::GetInternalField(int index) {
2912#ifndef V8_ENABLE_CHECKS
2913 Local<Value> quick_result = UncheckedGetInternalField(index);
2914 if (!quick_result.IsEmpty()) return quick_result;
2915#endif
2916 return CheckedGetInternalField(index);
2917}
2918
2919
2920Local<Value> Object::UncheckedGetInternalField(int index) {
2921 typedef internal::Object O;
2922 typedef internal::Internals I;
2923 O* obj = *reinterpret_cast<O**>(this);
2924 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2925 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2926 if (instance_type == I::kJSObjectType) {
2927 // If the object is a plain JSObject, which is the common case,
2928 // we know where to find the internal fields and can return the
2929 // value directly.
2930 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
2931 O* value = I::ReadField<O*>(obj, offset);
2932 O** result = HandleScope::CreateHandle(value);
2933 return Local<Value>(reinterpret_cast<Value*>(result));
2934 } else {
2935 return Local<Value>();
2936 }
2937}
2938
2939
2940void* External::Unwrap(Handle<v8::Value> obj) {
2941#ifdef V8_ENABLE_CHECKS
2942 return FullUnwrap(obj);
2943#else
2944 return QuickUnwrap(obj);
2945#endif
2946}
2947
2948
2949void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
2950 typedef internal::Object O;
2951 typedef internal::Internals I;
2952 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
2953 if (I::HasSmiTag(obj)) {
2954 int value = I::SmiValue(obj) << I::kAlignedPointerShift;
2955 return reinterpret_cast<void*>(value);
2956 } else {
2957 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2958 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2959 if (instance_type == I::kProxyType) {
2960 return I::ReadField<void*>(obj, I::kProxyProxyOffset);
2961 } else {
2962 return NULL;
2963 }
2964 }
2965}
2966
2967
2968void* Object::GetPointerFromInternalField(int index) {
2969 return External::Unwrap(GetInternalField(index));
2970}
2971
2972
2973String* String::Cast(v8::Value* value) {
2974#ifdef V8_ENABLE_CHECKS
2975 CheckCast(value);
2976#endif
2977 return static_cast<String*>(value);
2978}
2979
2980
2981String::ExternalStringResource* String::GetExternalStringResource() const {
2982 typedef internal::Object O;
2983 typedef internal::Internals I;
2984 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
2985 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2986 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2987 String::ExternalStringResource* result;
2988 if (I::IsExternalTwoByteString(instance_type)) {
2989 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
2990 result = reinterpret_cast<String::ExternalStringResource*>(value);
2991 } else {
2992 result = NULL;
2993 }
2994#ifdef V8_ENABLE_CHECKS
2995 VerifyExternalStringResource(result);
2996#endif
2997 return result;
2998}
2999
3000
3001bool Value::IsString() const {
3002#ifdef V8_ENABLE_CHECKS
3003 return FullIsString();
3004#else
3005 return QuickIsString();
3006#endif
3007}
3008
3009bool Value::QuickIsString() const {
3010 typedef internal::Object O;
3011 typedef internal::Internals I;
3012 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3013 if (!I::HasHeapObjectTag(obj)) return false;
3014 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
3015 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
3016 return (instance_type < I::kFirstNonstringType);
3017}
3018
3019
3020Number* Number::Cast(v8::Value* value) {
3021#ifdef V8_ENABLE_CHECKS
3022 CheckCast(value);
3023#endif
3024 return static_cast<Number*>(value);
3025}
3026
3027
3028Integer* Integer::Cast(v8::Value* value) {
3029#ifdef V8_ENABLE_CHECKS
3030 CheckCast(value);
3031#endif
3032 return static_cast<Integer*>(value);
3033}
3034
3035
3036Date* Date::Cast(v8::Value* value) {
3037#ifdef V8_ENABLE_CHECKS
3038 CheckCast(value);
3039#endif
3040 return static_cast<Date*>(value);
3041}
3042
3043
3044Object* Object::Cast(v8::Value* value) {
3045#ifdef V8_ENABLE_CHECKS
3046 CheckCast(value);
3047#endif
3048 return static_cast<Object*>(value);
3049}
3050
3051
3052Array* Array::Cast(v8::Value* value) {
3053#ifdef V8_ENABLE_CHECKS
3054 CheckCast(value);
3055#endif
3056 return static_cast<Array*>(value);
3057}
3058
3059
3060Function* Function::Cast(v8::Value* value) {
3061#ifdef V8_ENABLE_CHECKS
3062 CheckCast(value);
3063#endif
3064 return static_cast<Function*>(value);
3065}
3066
3067
3068External* External::Cast(v8::Value* value) {
3069#ifdef V8_ENABLE_CHECKS
3070 CheckCast(value);
3071#endif
3072 return static_cast<External*>(value);
3073}
3074
3075
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003076Local<Value> AccessorInfo::Data() const {
3077 return Local<Value>(reinterpret_cast<Value*>(&args_[-3]));
3078}
3079
3080
3081Local<Object> AccessorInfo::This() const {
3082 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3083}
3084
3085
3086Local<Object> AccessorInfo::Holder() const {
3087 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3088}
3089
3090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003091/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003092 * \example shell.cc
3093 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094 * command-line and executes them.
3095 */
3096
3097
3098/**
3099 * \example process.cc
3100 */
3101
3102
3103} // namespace v8
3104
3105
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003106#undef V8EXPORT
3107#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003108#undef TYPE_CHECK
3109
3110
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003111#endif // V8_H_