blob: 78b46136fd252b2734bdd538fb70a883e1485b72 [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
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000132class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000133class Object;
134class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000135
136}
137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139// --- W e a k H a n d l e s
140
141
142/**
143 * A weak reference callback function.
144 *
145 * \param object the weak global object to be reclaimed by the garbage collector
146 * \param parameter the value passed in when making the weak global object
147 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000148typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 void* parameter);
150
151
152// --- H a n d l e s ---
153
154#define TYPE_CHECK(T, S) \
155 while (false) { \
156 *(static_cast<T**>(0)) = static_cast<S*>(0); \
157 }
158
159/**
160 * An object reference managed by the v8 garbage collector.
161 *
162 * All objects returned from v8 have to be tracked by the garbage
163 * collector so that it knows that the objects are still alive. Also,
164 * because the garbage collector may move objects, it is unsafe to
165 * point directly to an object. Instead, all objects are stored in
166 * handles which are known by the garbage collector and updated
167 * whenever an object moves. Handles should always be passed by value
168 * (except in cases like out-parameters) and they should never be
169 * allocated on the heap.
170 *
171 * There are two types of handles: local and persistent handles.
172 * Local handles are light-weight and transient and typically used in
173 * local operations. They are managed by HandleScopes. Persistent
174 * handles can be used when storing objects across several independent
175 * operations and have to be explicitly deallocated when they're no
176 * longer used.
177 *
178 * It is safe to extract the object stored in the handle by
179 * dereferencing the handle (for instance, to extract the Object* from
180 * an Handle<Object>); the value will still be governed by a handle
181 * behind the scenes and the same rules apply to these values as to
182 * their handles.
183 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000184template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 public:
186
187 /**
188 * Creates an empty handle.
189 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000190 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
192 /**
193 * Creates a new handle for the specified value.
194 */
195 explicit Handle(T* val) : val_(val) { }
196
197 /**
198 * Creates a handle for the contents of the specified handle. This
199 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000200 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 * incompatible handles, for instance from a Handle<String> to a
202 * Handle<Number> it will cause a compiletime error. Assigning
203 * between compatible handles, for instance assigning a
204 * Handle<String> to a variable declared as Handle<Value>, is legal
205 * because String is a subclass of Value.
206 */
207 template <class S> inline Handle(Handle<S> that)
208 : val_(reinterpret_cast<T*>(*that)) {
209 /**
210 * This check fails when trying to convert between incompatible
211 * handles. For example, converting from a Handle<String> to a
212 * Handle<Number>.
213 */
214 TYPE_CHECK(T, S);
215 }
216
217 /**
218 * Returns true if the handle is empty.
219 */
ager@chromium.org32912102009-01-16 10:38:43 +0000220 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000222 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000224 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
226 /**
227 * Sets the handle to be empty. IsEmpty() will then return true.
228 */
229 void Clear() { this->val_ = 0; }
230
231 /**
232 * Checks whether two handles are the same.
233 * Returns true if both are empty, or if the objects
234 * to which they refer are identical.
235 * The handles' references are not checked.
236 */
ager@chromium.org32912102009-01-16 10:38:43 +0000237 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
239 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 if (a == 0) return b == 0;
241 if (b == 0) return false;
242 return *a == *b;
243 }
244
245 /**
246 * Checks whether two handles are different.
247 * Returns true if only one of the handles is empty, or if
248 * the objects to which they refer are different.
249 * The handles' references are not checked.
250 */
ager@chromium.org32912102009-01-16 10:38:43 +0000251 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 return !operator==(that);
253 }
254
255 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000256#ifdef V8_ENABLE_CHECKS
257 // If we're going to perform the type check then we have to check
258 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000260#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 return Handle<T>(T::Cast(*that));
262 }
263
264 private:
265 T* val_;
266};
267
268
269/**
270 * A light-weight stack-allocated object handle. All operations
271 * that return objects from within v8 return them in local handles. They
272 * are created within HandleScopes, and all local handles allocated within a
273 * handle scope are destroyed when the handle scope is destroyed. Hence it
274 * is not necessary to explicitly deallocate local handles.
275 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000276template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000278 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 template <class S> inline Local(Local<S> that)
280 : Handle<T>(reinterpret_cast<T*>(*that)) {
281 /**
282 * This check fails when trying to convert between incompatible
283 * handles. For example, converting from a Handle<String> to a
284 * Handle<Number>.
285 */
286 TYPE_CHECK(T, S);
287 }
288 template <class S> inline Local(S* that) : Handle<T>(that) { }
289 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000290#ifdef V8_ENABLE_CHECKS
291 // If we're going to perform the type check then we have to check
292 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000294#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 return Local<T>(T::Cast(*that));
296 }
297
298 /** Create a local handle for the content of another handle.
299 * The referee is kept alive by the local handle even when
300 * the original handle is destroyed/disposed.
301 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000302 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303};
304
305
306/**
307 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000308 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 * allocated, a Persistent handle remains valid until it is explicitly
310 * disposed.
311 *
312 * A persistent handle contains a reference to a storage cell within
313 * the v8 engine which holds an object value and which is updated by
314 * the garbage collector whenever the object is moved. A new storage
315 * cell can be created using Persistent::New and existing handles can
316 * be disposed using Persistent::Dispose. Since persistent handles
317 * are passed by value you may have many persistent handle objects
318 * that point to the same storage cell. For instance, if you pass a
319 * persistent handle as an argument to a function you will not get two
320 * different storage cells but rather two references to the same
321 * storage cell.
322 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000323template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 public:
325
326 /**
327 * Creates an empty persistent handle that doesn't point to any
328 * storage cell.
329 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000330 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331
332 /**
333 * Creates a persistent handle for the same storage cell as the
334 * specified handle. This constructor allows you to pass persistent
335 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000336 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000338 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 * between compatible persistent handles, for instance assigning a
340 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000341 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 */
343 template <class S> inline Persistent(Persistent<S> that)
344 : Handle<T>(reinterpret_cast<T*>(*that)) {
345 /**
346 * This check fails when trying to convert between incompatible
347 * handles. For example, converting from a Handle<String> to a
348 * Handle<Number>.
349 */
350 TYPE_CHECK(T, S);
351 }
352
353 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
354
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000355 /**
356 * "Casts" a plain handle which is known to be a persistent handle
357 * to a persistent handle.
358 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 template <class S> explicit inline Persistent(Handle<S> that)
360 : Handle<T>(*that) { }
361
362 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000363#ifdef V8_ENABLE_CHECKS
364 // If we're going to perform the type check then we have to check
365 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000367#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 return Persistent<T>(T::Cast(*that));
369 }
370
371 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000372 * Creates a new persistent handle for an existing local or
373 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Releases the storage cell referenced by this persistent handle.
379 * Does not remove the reference to the cell from any handles.
380 * This handle's reference, and any any other references to the storage
381 * cell remain and IsEmpty will still return false.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /**
386 * Make the reference to this object weak. When only weak handles
387 * refer to the object, the garbage collector will perform a
388 * callback to the given V8::WeakReferenceCallback function, passing
389 * it the object reference and the given parameters.
390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
393 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000394 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395
396 /**
397 *Checks if the handle holds the only reference to an object.
398 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000399 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400
401 /**
402 * Returns true if the handle's reference is weak.
403 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000404 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
406 private:
407 friend class ImplementationUtilities;
408 friend class ObjectTemplate;
409};
410
411
v8.team.kasperl727e9952008-09-02 14:56:44 +0000412 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 * A stack-allocated class that governs a number of local handles.
414 * After a handle scope has been created, all local handles will be
415 * allocated within that handle scope until either the handle scope is
416 * deleted or another handle scope is created. If there is already a
417 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000418 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 * new handles will again be allocated in the original handle scope.
420 *
421 * After the handle scope of a local handle has been deleted the
422 * garbage collector will no longer track the object stored in the
423 * handle and may deallocate it. The behavior of accessing a handle
424 * for which the handle scope has been deleted is undefined.
425 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000426class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000428 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000430 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431
432 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 * Closes the handle scope and returns the value as a handle in the
434 * previous scope, which is the new current scope after the call.
435 */
436 template <class T> Local<T> Close(Handle<T> value);
437
438 /**
439 * Counts the number of allocated handles.
440 */
441 static int NumberOfHandles();
442
443 /**
444 * Creates a new handle with the given value.
445 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000446 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447
448 private:
449 // Make it impossible to create heap-allocated or illegal handle
450 // scopes by disallowing certain operations.
451 HandleScope(const HandleScope&);
452 void operator=(const HandleScope&);
453 void* operator new(size_t size);
454 void operator delete(void*, size_t);
455
ager@chromium.org3811b432009-10-28 14:53:37 +0000456 // This Data class is accessible internally as HandleScopeData through a
457 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000458 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 public:
460 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000461 internal::Object** next;
462 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 inline void Initialize() {
464 extensions = -1;
465 next = limit = NULL;
466 }
467 };
468
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000469 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000471 // Allow for the active closing of HandleScopes which allows to pass a handle
472 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000474 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 friend class ImplementationUtilities;
477};
478
479
480// --- S p e c i a l o b j e c t s ---
481
482
483/**
484 * The superclass of values and API object templates.
485 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000486class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487 private:
488 Data();
489};
490
491
492/**
493 * Pre-compilation data that can be associated with a script. This
494 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000495 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000496 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000498class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 public:
500 virtual ~ScriptData() { }
501 static ScriptData* PreCompile(const char* input, int length);
502 static ScriptData* New(unsigned* data, int length);
503
504 virtual int Length() = 0;
505 virtual unsigned* Data() = 0;
506};
507
508
509/**
510 * The origin, within a file, of a script.
511 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000512class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000514 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 Handle<Integer> resource_line_offset = Handle<Integer>(),
516 Handle<Integer> resource_column_offset = Handle<Integer>())
517 : resource_name_(resource_name),
518 resource_line_offset_(resource_line_offset),
519 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000520 inline Handle<Value> ResourceName() const;
521 inline Handle<Integer> ResourceLineOffset() const;
522 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000524 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 Handle<Integer> resource_line_offset_;
526 Handle<Integer> resource_column_offset_;
527};
528
529
530/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000531 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000533class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 public:
535
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000536 /**
537 * Compiles the specified script. The ScriptOrigin* and ScriptData*
538 * parameters are owned by the caller of Script::Compile. No
539 * references to these objects are kept after compilation finishes.
540 *
541 * The script object returned is context independent; when run it
542 * will use the currently entered context.
543 */
544 static Local<Script> New(Handle<String> source,
545 ScriptOrigin* origin = NULL,
546 ScriptData* pre_data = NULL);
547
548 /**
549 * Compiles the specified script using the specified file name
550 * object (typically a string) as the script's origin.
551 *
552 * The script object returned is context independent; when run it
553 * will use the currently entered context.
554 */
555 static Local<Script> New(Handle<String> source,
556 Handle<Value> file_name);
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 /**
559 * Compiles the specified script. The ScriptOrigin* and ScriptData*
560 * parameters are owned by the caller of Script::Compile. No
561 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000562 *
563 * The script object returned is bound to the context that was active
564 * when this function was called. When run it will always use this
565 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 */
567 static Local<Script> Compile(Handle<String> source,
568 ScriptOrigin* origin = NULL,
569 ScriptData* pre_data = NULL);
570
mads.s.agercbaa0602008-08-14 13:41:48 +0000571 /**
572 * Compiles the specified script using the specified file name
573 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000574 *
575 * The script object returned is bound to the context that was active
576 * when this function was called. When run it will always use this
577 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000578 */
579 static Local<Script> Compile(Handle<String> source,
580 Handle<Value> file_name);
581
v8.team.kasperl727e9952008-09-02 14:56:44 +0000582 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000583 * Runs the script returning the resulting value. If the script is
584 * context independent (created using ::New) it will be run in the
585 * currently entered context. If it is context specific (created
586 * using ::Compile) it will be run in the context in which it was
587 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000588 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000590
591 /**
592 * Returns the script id value.
593 */
594 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000595
596 /**
597 * Associate an additional data object with the script. This is mainly used
598 * with the debugger as this data object is only available through the
599 * debugger API.
600 */
601 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602};
603
604
605/**
606 * An error message.
607 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000608class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000610 Local<String> Get() const;
611 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000613 /**
614 * Returns the resource name for the script from where the function causing
615 * the error originates.
616 */
ager@chromium.org32912102009-01-16 10:38:43 +0000617 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000619 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000620 * Returns the resource data for the script from where the function causing
621 * the error originates.
622 */
623 Handle<Value> GetScriptData() const;
624
625 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000626 * Returns the number, 1-based, of the line where the error occurred.
627 */
ager@chromium.org32912102009-01-16 10:38:43 +0000628 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000630 /**
631 * Returns the index within the script of the first character where
632 * the error occurred.
633 */
ager@chromium.org32912102009-01-16 10:38:43 +0000634 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000635
636 /**
637 * Returns the index within the script of the last character where
638 * the error occurred.
639 */
ager@chromium.org32912102009-01-16 10:38:43 +0000640 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000641
642 /**
643 * Returns the index within the line of the first character where
644 * the error occurred.
645 */
ager@chromium.org32912102009-01-16 10:38:43 +0000646 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000647
648 /**
649 * Returns the index within the line of the last character where
650 * the error occurred.
651 */
ager@chromium.org32912102009-01-16 10:38:43 +0000652 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 // TODO(1245381): Print to a string instead of on a FILE.
655 static void PrintCurrentStackTrace(FILE* out);
656};
657
658
659// --- V a l u e ---
660
661
662/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000663 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000665class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 public:
667
668 /**
669 * Returns true if this value is the undefined value. See ECMA-262
670 * 4.3.10.
671 */
ager@chromium.org32912102009-01-16 10:38:43 +0000672 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
674 /**
675 * Returns true if this value is the null value. See ECMA-262
676 * 4.3.11.
677 */
ager@chromium.org32912102009-01-16 10:38:43 +0000678 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679
680 /**
681 * Returns true if this value is true.
682 */
ager@chromium.org32912102009-01-16 10:38:43 +0000683 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684
685 /**
686 * Returns true if this value is false.
687 */
ager@chromium.org32912102009-01-16 10:38:43 +0000688 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
690 /**
691 * Returns true if this value is an instance of the String type.
692 * See ECMA-262 8.4.
693 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000694 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
696 /**
697 * Returns true if this value is a function.
698 */
ager@chromium.org32912102009-01-16 10:38:43 +0000699 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
701 /**
702 * Returns true if this value is an array.
703 */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
v8.team.kasperl727e9952008-09-02 14:56:44 +0000706 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 * Returns true if this value is an object.
708 */
ager@chromium.org32912102009-01-16 10:38:43 +0000709 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
v8.team.kasperl727e9952008-09-02 14:56:44 +0000711 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 * Returns true if this value is boolean.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000715
v8.team.kasperl727e9952008-09-02 14:56:44 +0000716 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 * Returns true if this value is a number.
718 */
ager@chromium.org32912102009-01-16 10:38:43 +0000719 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
v8.team.kasperl727e9952008-09-02 14:56:44 +0000721 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 * Returns true if this value is external.
723 */
ager@chromium.org32912102009-01-16 10:38:43 +0000724 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
v8.team.kasperl727e9952008-09-02 14:56:44 +0000726 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 * Returns true if this value is a 32-bit signed integer.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000731 /**
732 * Returns true if this value is a Date.
733 */
ager@chromium.org32912102009-01-16 10:38:43 +0000734 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000735
ager@chromium.org32912102009-01-16 10:38:43 +0000736 Local<Boolean> ToBoolean() const;
737 Local<Number> ToNumber() const;
738 Local<String> ToString() const;
739 Local<String> ToDetailString() const;
740 Local<Object> ToObject() const;
741 Local<Integer> ToInteger() const;
742 Local<Uint32> ToUint32() const;
743 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744
745 /**
746 * Attempts to convert a string to an array index.
747 * Returns an empty handle if the conversion fails.
748 */
ager@chromium.org32912102009-01-16 10:38:43 +0000749 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750
ager@chromium.org32912102009-01-16 10:38:43 +0000751 bool BooleanValue() const;
752 double NumberValue() const;
753 int64_t IntegerValue() const;
754 uint32_t Uint32Value() const;
755 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
757 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000758 bool Equals(Handle<Value> that) const;
759 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000760
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000761 private:
762 inline bool QuickIsString() const;
763 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764};
765
766
767/**
768 * The superclass of primitive values. See ECMA-262 4.3.2.
769 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000770class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
772
773/**
774 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
775 * or false value.
776 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000777class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000779 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 static inline Handle<Boolean> New(bool value);
781};
782
783
784/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000785 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000787class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000789
790 /**
791 * Returns the number of characters in this string.
792 */
ager@chromium.org32912102009-01-16 10:38:43 +0000793 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
v8.team.kasperl727e9952008-09-02 14:56:44 +0000795 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000796 * Returns the number of bytes in the UTF-8 encoded
797 * representation of this string.
798 */
ager@chromium.org32912102009-01-16 10:38:43 +0000799 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000800
801 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000802 * Write the contents of the string to an external buffer.
803 * If no arguments are given, expects the buffer to be large
804 * enough to hold the entire string and NULL terminator. Copies
805 * the contents of the string and the NULL terminator into the
806 * buffer.
807 *
808 * Copies up to length characters into the output buffer.
809 * Only null-terminates if there is enough space in the buffer.
810 *
811 * \param buffer The buffer into which the string will be copied.
812 * \param start The starting position within the string at which
813 * copying begins.
814 * \param length The number of bytes to copy from the string.
815 * \return The number of characters copied to the buffer
816 * excluding the NULL terminator.
817 */
ager@chromium.org32912102009-01-16 10:38:43 +0000818 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
819 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
820 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821
v8.team.kasperl727e9952008-09-02 14:56:44 +0000822 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000823 * A zero length string.
824 */
825 static v8::Local<v8::String> Empty();
826
827 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000828 * Returns true if the string is external
829 */
ager@chromium.org32912102009-01-16 10:38:43 +0000830 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
v8.team.kasperl727e9952008-09-02 14:56:44 +0000832 /**
833 * Returns true if the string is both external and ascii
834 */
ager@chromium.org32912102009-01-16 10:38:43 +0000835 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000836 /**
837 * An ExternalStringResource is a wrapper around a two-byte string
838 * buffer that resides outside V8's heap. Implement an
839 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000840 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000841 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000842 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 public:
844 /**
845 * Override the destructor to manage the life cycle of the underlying
846 * buffer.
847 */
848 virtual ~ExternalStringResource() {}
849 /** The string data from the underlying buffer.*/
850 virtual const uint16_t* data() const = 0;
851 /** The length of the string. That is, the number of two-byte characters.*/
852 virtual size_t length() const = 0;
853 protected:
854 ExternalStringResource() {}
855 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000856 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 ExternalStringResource(const ExternalStringResource&);
858 void operator=(const ExternalStringResource&);
859 };
860
861 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000862 * An ExternalAsciiStringResource is a wrapper around an ascii
863 * string buffer that resides outside V8's heap. Implement an
864 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000865 * underlying buffer. Note that the string data must be immutable
866 * and that the data must be strict 7-bit ASCII, not Latin1 or
867 * UTF-8, which would require special treatment internally in the
868 * engine and, in the case of UTF-8, do not allow efficient indexing.
869 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000870 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000872 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 public:
874 /**
875 * Override the destructor to manage the life cycle of the underlying
876 * buffer.
877 */
878 virtual ~ExternalAsciiStringResource() {}
879 /** The string data from the underlying buffer.*/
880 virtual const char* data() const = 0;
881 /** The number of ascii characters in the string.*/
882 virtual size_t length() const = 0;
883 protected:
884 ExternalAsciiStringResource() {}
885 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000886 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
888 void operator=(const ExternalAsciiStringResource&);
889 };
890
891 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000892 * Get the ExternalStringResource for an external string. Returns
893 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000895 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
897 /**
898 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000899 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 */
ager@chromium.org32912102009-01-16 10:38:43 +0000901 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000903 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
905 /**
906 * Allocates a new string from either utf-8 encoded or ascii data.
907 * The second parameter 'length' gives the buffer length.
908 * If the data is utf-8 encoded, the caller must
909 * be careful to supply the length parameter.
910 * If it is not given, the function calls
911 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000912 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 */
914 static Local<String> New(const char* data, int length = -1);
915
916 /** Allocates a new string from utf16 data.*/
917 static Local<String> New(const uint16_t* data, int length = -1);
918
919 /** Creates a symbol. Returns one if it exists already.*/
920 static Local<String> NewSymbol(const char* data, int length = -1);
921
v8.team.kasperl727e9952008-09-02 14:56:44 +0000922 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000923 * Creates a new string by concatenating the left and the right strings
924 * passed in as parameters.
925 */
926 static Local<String> Concat(Handle<String> left, Handle<String>right);
927
928 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000929 * Creates a new external string using the data defined in the given
930 * resource. The resource is deleted when the external string is no
931 * longer live on V8's heap. The caller of this function should not
932 * delete or modify the resource. Neither should the underlying buffer be
933 * deallocated or modified except through the destructor of the
934 * external string resource.
935 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000937
ager@chromium.org6f10e412009-02-13 10:11:16 +0000938 /**
939 * Associate an external string resource with this string by transforming it
940 * in place so that existing references to this string in the JavaScript heap
941 * will use the external string resource. The external string resource's
942 * character contents needs to be equivalent to this string.
943 * Returns true if the string has been changed to be an external string.
944 * The string is not modified if the operation fails.
945 */
946 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947
v8.team.kasperl727e9952008-09-02 14:56:44 +0000948 /**
949 * Creates a new external string using the ascii data defined in the given
950 * resource. The resource is deleted when the external string is no
951 * longer live on V8's heap. The caller of this function should not
952 * delete or modify the resource. Neither should the underlying buffer be
953 * deallocated or modified except through the destructor of the
954 * external string resource.
955 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000957
ager@chromium.org6f10e412009-02-13 10:11:16 +0000958 /**
959 * Associate an external string resource with this string by transforming it
960 * in place so that existing references to this string in the JavaScript heap
961 * will use the external string resource. The external string resource's
962 * character contents needs to be equivalent to this string.
963 * Returns true if the string has been changed to be an external string.
964 * The string is not modified if the operation fails.
965 */
966 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000968 /**
969 * Returns true if this string can be made external.
970 */
971 bool CanMakeExternal();
972
kasper.lund7276f142008-07-30 08:49:36 +0000973 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 static Local<String> NewUndetectable(const char* data, int length = -1);
975
kasper.lund7276f142008-07-30 08:49:36 +0000976 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
978
979 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000980 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000981 * you want to print the object. If conversion to a string fails
982 * (eg. due to an exception in the toString() method of the object)
983 * then the length() method returns 0 and the * operator returns
984 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000985 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000986 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000987 public:
988 explicit Utf8Value(Handle<v8::Value> obj);
989 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000990 char* operator*() { return str_; }
991 const char* operator*() const { return str_; }
992 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000993 private:
994 char* str_;
995 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000996
997 // Disallow copying and assigning.
998 Utf8Value(const Utf8Value&);
999 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001000 };
1001
1002 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 * Converts an object to an ascii string.
1004 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001005 * If conversion to a string fails (eg. due to an exception in the toString()
1006 * method of the object) then the length() method returns 0 and the * operator
1007 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001009 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 public:
1011 explicit AsciiValue(Handle<v8::Value> obj);
1012 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001013 char* operator*() { return str_; }
1014 const char* operator*() const { return str_; }
1015 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 private:
1017 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001018 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001019
1020 // Disallow copying and assigning.
1021 AsciiValue(const AsciiValue&);
1022 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 };
1024
1025 /**
1026 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001027 * If conversion to a string fails (eg. due to an exception in the toString()
1028 * method of the object) then the length() method returns 0 and the * operator
1029 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001031 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 public:
1033 explicit Value(Handle<v8::Value> obj);
1034 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001035 uint16_t* operator*() { return str_; }
1036 const uint16_t* operator*() const { return str_; }
1037 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 private:
1039 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001040 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001041
1042 // Disallow copying and assigning.
1043 Value(const Value&);
1044 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001046
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001047 private:
1048 void VerifyExternalStringResource(ExternalStringResource* val) const;
1049 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050};
1051
1052
1053/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001054 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001056class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001058 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001060 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 private:
1062 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001063 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064};
1065
1066
1067/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001068 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001070class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 public:
1072 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001073 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001074 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001075 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 private:
1077 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001078 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079};
1080
1081
1082/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001083 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001085class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001087 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 private:
1089 Int32();
1090};
1091
1092
1093/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001094 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001096class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001098 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 private:
1100 Uint32();
1101};
1102
1103
1104/**
1105 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1106 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001107class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 public:
1109 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001110
1111 /**
1112 * A specialization of Value::NumberValue that is more efficient
1113 * because we know the structure of this object.
1114 */
ager@chromium.org32912102009-01-16 10:38:43 +00001115 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001116
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001117 static inline Date* Cast(v8::Value* obj);
1118 private:
1119 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120};
1121
1122
1123enum PropertyAttribute {
1124 None = 0,
1125 ReadOnly = 1 << 0,
1126 DontEnum = 1 << 1,
1127 DontDelete = 1 << 2
1128};
1129
ager@chromium.org3811b432009-10-28 14:53:37 +00001130enum ExternalArrayType {
1131 kExternalByteArray = 1,
1132 kExternalUnsignedByteArray,
1133 kExternalShortArray,
1134 kExternalUnsignedShortArray,
1135 kExternalIntArray,
1136 kExternalUnsignedIntArray,
1137 kExternalFloatArray
1138};
1139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001141 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001143class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 public:
1145 bool Set(Handle<Value> key,
1146 Handle<Value> value,
1147 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001148
ager@chromium.orge2902be2009-06-08 12:21:35 +00001149 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001150 // overriding accessors or read-only properties.
1151 //
1152 // Note that if the object has an interceptor the property will be set
1153 // locally, but since the interceptor takes precedence the local property
1154 // will only be returned if the interceptor doesn't return a value.
1155 //
1156 // Note also that this only works for named properties.
1157 bool ForceSet(Handle<Value> key,
1158 Handle<Value> value,
1159 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 Local<Value> Get(Handle<Value> key);
1162
1163 // TODO(1245389): Replace the type-specific versions of these
1164 // functions with generic ones that accept a Handle<Value> key.
1165 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001168
1169 // Delete a property on this object bypassing interceptors and
1170 // ignoring dont-delete attributes.
1171 bool ForceDelete(Handle<Value> key);
1172
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 bool Delete(uint32_t index);
1176
1177 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001178 * Returns an array containing the names of the enumerable properties
1179 * of this object, including properties from prototype objects. The
1180 * array returned by this method contains the same values as would
1181 * be enumerated by a for-in statement over this object.
1182 */
1183 Local<Array> GetPropertyNames();
1184
1185 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 * Get the prototype object. This does not skip objects marked to
1187 * be skipped by __proto__ and it does not consult the security
1188 * handler.
1189 */
1190 Local<Value> GetPrototype();
1191
1192 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001193 * Finds an instance of the given function template in the prototype
1194 * chain.
1195 */
1196 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1197
1198 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 * Call builtin Object.prototype.toString on this object.
1200 * This is different from Value::ToString() that may call
1201 * user-defined toString function. This one does not.
1202 */
1203 Local<String> ObjectProtoToString();
1204
kasper.lund212ac232008-07-16 07:07:30 +00001205 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001207 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001208 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001209 /** Sets the value in an internal field. */
1210 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001212 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001213 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001214
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001215 /** Sets a native pointer in an internal field. */
1216 void SetPointerInInternalField(int index, void* value);
1217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 // Testers for local properties.
1219 bool HasRealNamedProperty(Handle<String> key);
1220 bool HasRealIndexedProperty(uint32_t index);
1221 bool HasRealNamedCallbackProperty(Handle<String> key);
1222
1223 /**
1224 * If result.IsEmpty() no real property was located in the prototype chain.
1225 * This means interceptors in the prototype chain are not called.
1226 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001227 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1228
1229 /**
1230 * If result.IsEmpty() no real property was located on the object or
1231 * in the prototype chain.
1232 * This means interceptors in the prototype chain are not called.
1233 */
1234 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235
1236 /** Tests for a named lookup interceptor.*/
1237 bool HasNamedLookupInterceptor();
1238
kasper.lund212ac232008-07-16 07:07:30 +00001239 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 bool HasIndexedLookupInterceptor();
1241
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001242 /**
1243 * Turns on access check on the object if the object is an instance of
1244 * a template that has access check callbacks. If an object has no
1245 * access check info, the object cannot be accessed by anyone.
1246 */
1247 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001248
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001249 /**
1250 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001251 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001252 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001253 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001254 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001255 */
1256 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001257
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001258 /**
1259 * Access hidden properties on JavaScript objects. These properties are
1260 * hidden from the executing JavaScript and only accessible through the V8
1261 * C++ API. Hidden properties introduced by V8 internally (for example the
1262 * identity hash) are prefixed with "v8::".
1263 */
1264 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1265 Local<Value> GetHiddenValue(Handle<String> key);
1266 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001267
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001268 /**
1269 * Returns true if this is an instance of an api function (one
1270 * created from a function created from a function template) and has
1271 * been modified since it was created. Note that this method is
1272 * conservative and may return true for objects that haven't actually
1273 * been modified.
1274 */
1275 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001276
1277 /**
1278 * Clone this object with a fast but shallow copy. Values will point
1279 * to the same values as the original object.
1280 */
1281 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001283 /**
1284 * Set the backing store of the indexed properties to be managed by the
1285 * embedding layer. Access to the indexed properties will follow the rules
1286 * spelled out in CanvasPixelArray.
1287 * Note: The embedding program still owns the data and needs to ensure that
1288 * the backing store is preserved while V8 has a reference.
1289 */
1290 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1291
ager@chromium.org3811b432009-10-28 14:53:37 +00001292 /**
1293 * Set the backing store of the indexed properties to be managed by the
1294 * embedding layer. Access to the indexed properties will follow the rules
1295 * spelled out for the CanvasArray subtypes in the WebGL specification.
1296 * Note: The embedding program still owns the data and needs to ensure that
1297 * the backing store is preserved while V8 has a reference.
1298 */
1299 void SetIndexedPropertiesToExternalArrayData(void* data,
1300 ExternalArrayType array_type,
1301 int number_of_elements);
1302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001304 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001305 private:
1306 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001307 static void CheckCast(Value* obj);
1308 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001309 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001310
1311 /**
1312 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001313 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001314 */
1315 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316};
1317
1318
1319/**
1320 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1321 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001322class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001324 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325
ager@chromium.org3e875802009-06-29 08:26:34 +00001326 /**
1327 * Clones an element at index |index|. Returns an empty
1328 * handle if cloning fails (for any reason).
1329 */
1330 Local<Object> CloneElementAt(uint32_t index);
1331
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001333 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 private:
1335 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001336 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337};
1338
1339
1340/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001341 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001343class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001345 Local<Object> NewInstance() const;
1346 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1348 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001349 Handle<Value> GetName() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001350 static inline Function* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 private:
1352 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001353 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354};
1355
1356
1357/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001358 * A JavaScript value that wraps a C++ void*. This type of value is
1359 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001361 *
1362 * The Wrap function V8 will return the most optimal Value object wrapping the
1363 * C++ void*. The type of the value is not guaranteed to be an External object
1364 * and no assumptions about its type should be made. To access the wrapped
1365 * value Unwrap should be used, all other operations on that object will lead
1366 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001368class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001370 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001371 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001372
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001374 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001375 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 private:
1377 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001378 static void CheckCast(v8::Value* obj);
1379 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1380 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381};
1382
1383
1384// --- T e m p l a t e s ---
1385
1386
1387/**
1388 * The superclass of object and function templates.
1389 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001390class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 public:
1392 /** Adds a property to each instance created by this template.*/
1393 void Set(Handle<String> name, Handle<Data> value,
1394 PropertyAttribute attributes = None);
1395 inline void Set(const char* name, Handle<Data> value);
1396 private:
1397 Template();
1398
1399 friend class ObjectTemplate;
1400 friend class FunctionTemplate;
1401};
1402
1403
1404/**
1405 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001406 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 * including the receiver, the number and values of arguments, and
1408 * the holder of the function.
1409 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001410class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 public:
1412 inline int Length() const;
1413 inline Local<Value> operator[](int i) const;
1414 inline Local<Function> Callee() const;
1415 inline Local<Object> This() const;
1416 inline Local<Object> Holder() const;
1417 inline bool IsConstructCall() const;
1418 inline Local<Value> Data() const;
1419 private:
1420 Arguments();
1421 friend class ImplementationUtilities;
1422 inline Arguments(Local<Value> data,
1423 Local<Object> holder,
1424 Local<Function> callee,
1425 bool is_construct_call,
1426 void** values, int length);
1427 Local<Value> data_;
1428 Local<Object> holder_;
1429 Local<Function> callee_;
1430 bool is_construct_call_;
1431 void** values_;
1432 int length_;
1433};
1434
1435
1436/**
1437 * The information passed to an accessor callback about the context
1438 * of the property access.
1439 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001440class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001442 inline AccessorInfo(internal::Object** args)
1443 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 inline Local<Value> Data() const;
1445 inline Local<Object> This() const;
1446 inline Local<Object> Holder() const;
1447 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001448 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449};
1450
1451
1452typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1453
1454typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1455
1456/**
1457 * Accessor[Getter|Setter] are used as callback functions when
1458 * setting|getting a particular property. See objectTemplate::SetAccessor.
1459 */
1460typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1461 const AccessorInfo& info);
1462
1463
1464typedef void (*AccessorSetter)(Local<String> property,
1465 Local<Value> value,
1466 const AccessorInfo& info);
1467
1468
1469/**
1470 * NamedProperty[Getter|Setter] are used as interceptors on object.
1471 * See ObjectTemplate::SetNamedPropertyHandler.
1472 */
1473typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1474 const AccessorInfo& info);
1475
1476
1477/**
1478 * Returns the value if the setter intercepts the request.
1479 * Otherwise, returns an empty handle.
1480 */
1481typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1482 Local<Value> value,
1483 const AccessorInfo& info);
1484
1485
1486/**
1487 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001488 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489 */
1490typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1491 const AccessorInfo& info);
1492
1493
1494/**
1495 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001496 * The return value is true if the property could be deleted and false
1497 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 */
1499typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1500 const AccessorInfo& info);
1501
1502/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001503 * Returns an array containing the names of the properties the named
1504 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 */
1506typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1507
v8.team.kasperl727e9952008-09-02 14:56:44 +00001508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001510 * Returns the value of the property if the getter intercepts the
1511 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 */
1513typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1514 const AccessorInfo& info);
1515
1516
1517/**
1518 * Returns the value if the setter intercepts the request.
1519 * Otherwise, returns an empty handle.
1520 */
1521typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1522 Local<Value> value,
1523 const AccessorInfo& info);
1524
1525
1526/**
1527 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001528 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001529 */
1530typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1531 const AccessorInfo& info);
1532
1533/**
1534 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001535 * The return value is true if the property could be deleted and false
1536 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 */
1538typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1539 const AccessorInfo& info);
1540
v8.team.kasperl727e9952008-09-02 14:56:44 +00001541/**
1542 * Returns an array containing the indices of the properties the
1543 * indexed property getter intercepts.
1544 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1546
1547
1548/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001549 * Access control specifications.
1550 *
1551 * Some accessors should be accessible across contexts. These
1552 * accessors have an explicit access control parameter which specifies
1553 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001554 *
1555 * Additionally, for security, accessors can prohibit overwriting by
1556 * accessors defined in JavaScript. For objects that have such
1557 * accessors either locally or in their prototype chain it is not
1558 * possible to overwrite the accessor by using __defineGetter__ or
1559 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 */
1561enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001562 DEFAULT = 0,
1563 ALL_CAN_READ = 1,
1564 ALL_CAN_WRITE = 1 << 1,
1565 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566};
1567
1568
1569/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001570 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 */
1572enum AccessType {
1573 ACCESS_GET,
1574 ACCESS_SET,
1575 ACCESS_HAS,
1576 ACCESS_DELETE,
1577 ACCESS_KEYS
1578};
1579
v8.team.kasperl727e9952008-09-02 14:56:44 +00001580
1581/**
1582 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001583 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001584 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001585typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 Local<Value> key,
1587 AccessType type,
1588 Local<Value> data);
1589
v8.team.kasperl727e9952008-09-02 14:56:44 +00001590
1591/**
1592 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001593 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001594 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001595typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 uint32_t index,
1597 AccessType type,
1598 Local<Value> data);
1599
1600
1601/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001602 * A FunctionTemplate is used to create functions at runtime. There
1603 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001604 * context. The lifetime of the created function is equal to the
1605 * lifetime of the context. So in case the embedder needs to create
1606 * temporary functions that can be collected using Scripts is
1607 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 *
1609 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001610 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001612 * A FunctionTemplate has a corresponding instance template which is
1613 * used to create object instances when the function is used as a
1614 * constructor. Properties added to the instance template are added to
1615 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 *
1617 * A FunctionTemplate can have a prototype template. The prototype template
1618 * is used to create the prototype object of the function.
1619 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001620 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001622 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1624 * t->Set("func_property", v8::Number::New(1));
1625 *
1626 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1627 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1628 * proto_t->Set("proto_const", v8::Number::New(2));
1629 *
1630 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1631 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1632 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1633 * instance_t->Set("instance_property", Number::New(3));
1634 *
1635 * v8::Local<v8::Function> function = t->GetFunction();
1636 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001637 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638 *
1639 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001640 * and "instance" for the instance object created above. The function
1641 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001643 * \code
1644 * func_property in function == true;
1645 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001647 * function.prototype.proto_method() invokes 'InvokeCallback'
1648 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001650 * instance instanceof function == true;
1651 * instance.instance_accessor calls 'InstanceAccessorCallback'
1652 * instance.instance_property == 3;
1653 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001655 * A FunctionTemplate can inherit from another one by calling the
1656 * FunctionTemplate::Inherit method. The following graph illustrates
1657 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001659 * \code
1660 * FunctionTemplate Parent -> Parent() . prototype -> { }
1661 * ^ ^
1662 * | Inherit(Parent) | .__proto__
1663 * | |
1664 * FunctionTemplate Child -> Child() . prototype -> { }
1665 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001667 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1668 * object of the Child() function has __proto__ pointing to the
1669 * Parent() function's prototype object. An instance of the Child
1670 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001672 * Let Parent be the FunctionTemplate initialized in the previous
1673 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001675 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 * Local<FunctionTemplate> parent = t;
1677 * Local<FunctionTemplate> child = FunctionTemplate::New();
1678 * child->Inherit(parent);
1679 *
1680 * Local<Function> child_function = child->GetFunction();
1681 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001682 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001684 * The Child function and Child instance will have the following
1685 * properties:
1686 *
1687 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001689 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001691 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001693class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 public:
1695 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001696 static Local<FunctionTemplate> New(
1697 InvocationCallback callback = 0,
1698 Handle<Value> data = Handle<Value>(),
1699 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 /** Returns the unique function instance in the current execution context.*/
1701 Local<Function> GetFunction();
1702
v8.team.kasperl727e9952008-09-02 14:56:44 +00001703 /**
1704 * Set the call-handler callback for a FunctionTemplate. This
1705 * callback is called whenever the function created from this
1706 * FunctionTemplate is called.
1707 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 void SetCallHandler(InvocationCallback callback,
1709 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710
v8.team.kasperl727e9952008-09-02 14:56:44 +00001711 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 Local<ObjectTemplate> InstanceTemplate();
1713
1714 /** Causes the function template to inherit from a parent function template.*/
1715 void Inherit(Handle<FunctionTemplate> parent);
1716
1717 /**
1718 * A PrototypeTemplate is the template used to create the prototype object
1719 * of the function created by this template.
1720 */
1721 Local<ObjectTemplate> PrototypeTemplate();
1722
v8.team.kasperl727e9952008-09-02 14:56:44 +00001723
1724 /**
1725 * Set the class name of the FunctionTemplate. This is used for
1726 * printing objects created with the function created from the
1727 * FunctionTemplate as its constructor.
1728 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 void SetClassName(Handle<String> name);
1730
1731 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001732 * Determines whether the __proto__ accessor ignores instances of
1733 * the function template. If instances of the function template are
1734 * ignored, __proto__ skips all instances and instead returns the
1735 * next object in the prototype chain.
1736 *
1737 * Call with a value of true to make the __proto__ accessor ignore
1738 * instances of the function template. Call with a value of false
1739 * to make the __proto__ accessor not ignore instances of the
1740 * function template. By default, instances of a function template
1741 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 */
1743 void SetHiddenPrototype(bool value);
1744
1745 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001746 * Returns true if the given object is an instance of this function
1747 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 */
1749 bool HasInstance(Handle<Value> object);
1750
1751 private:
1752 FunctionTemplate();
1753 void AddInstancePropertyAccessor(Handle<String> name,
1754 AccessorGetter getter,
1755 AccessorSetter setter,
1756 Handle<Value> data,
1757 AccessControl settings,
1758 PropertyAttribute attributes);
1759 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1760 NamedPropertySetter setter,
1761 NamedPropertyQuery query,
1762 NamedPropertyDeleter remover,
1763 NamedPropertyEnumerator enumerator,
1764 Handle<Value> data);
1765 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1766 IndexedPropertySetter setter,
1767 IndexedPropertyQuery query,
1768 IndexedPropertyDeleter remover,
1769 IndexedPropertyEnumerator enumerator,
1770 Handle<Value> data);
1771 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1772 Handle<Value> data);
1773
1774 friend class Context;
1775 friend class ObjectTemplate;
1776};
1777
1778
1779/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001780 * An ObjectTemplate is used to create objects at runtime.
1781 *
1782 * Properties added to an ObjectTemplate are added to each object
1783 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001785class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001787 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 /** Creates a new instance of this template.*/
1791 Local<Object> NewInstance();
1792
1793 /**
1794 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001795 *
1796 * Whenever the property with the given name is accessed on objects
1797 * created from this ObjectTemplate the getter and setter callbacks
1798 * are called instead of getting and setting the property directly
1799 * on the JavaScript object.
1800 *
1801 * \param name The name of the property for which an accessor is added.
1802 * \param getter The callback to invoke when getting the property.
1803 * \param setter The callback to invoke when setting the property.
1804 * \param data A piece of data that will be passed to the getter and setter
1805 * callbacks whenever they are invoked.
1806 * \param settings Access control settings for the accessor. This is a bit
1807 * field consisting of one of more of
1808 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1809 * The default is to not allow cross-context access.
1810 * ALL_CAN_READ means that all cross-context reads are allowed.
1811 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1812 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1813 * cross-context access.
1814 * \param attribute The attributes of the property for which an accessor
1815 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 */
1817 void SetAccessor(Handle<String> name,
1818 AccessorGetter getter,
1819 AccessorSetter setter = 0,
1820 Handle<Value> data = Handle<Value>(),
1821 AccessControl settings = DEFAULT,
1822 PropertyAttribute attribute = None);
1823
1824 /**
1825 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001826 *
1827 * Whenever a named property is accessed on objects created from
1828 * this object template, the provided callback is invoked instead of
1829 * accessing the property directly on the JavaScript object.
1830 *
1831 * \param getter The callback to invoke when getting a property.
1832 * \param setter The callback to invoke when setting a property.
1833 * \param query The callback to invoke to check is an object has a property.
1834 * \param deleter The callback to invoke when deleting a property.
1835 * \param enumerator The callback to invoke to enumerate all the named
1836 * properties of an object.
1837 * \param data A piece of data that will be passed to the callbacks
1838 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 */
1840 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1841 NamedPropertySetter setter = 0,
1842 NamedPropertyQuery query = 0,
1843 NamedPropertyDeleter deleter = 0,
1844 NamedPropertyEnumerator enumerator = 0,
1845 Handle<Value> data = Handle<Value>());
1846
1847 /**
1848 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001849 *
1850 * Whenever an indexed property is accessed on objects created from
1851 * this object template, the provided callback is invoked instead of
1852 * accessing the property directly on the JavaScript object.
1853 *
1854 * \param getter The callback to invoke when getting a property.
1855 * \param setter The callback to invoke when setting a property.
1856 * \param query The callback to invoke to check is an object has a property.
1857 * \param deleter The callback to invoke when deleting a property.
1858 * \param enumerator The callback to invoke to enumerate all the indexed
1859 * properties of an object.
1860 * \param data A piece of data that will be passed to the callbacks
1861 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 */
1863 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1864 IndexedPropertySetter setter = 0,
1865 IndexedPropertyQuery query = 0,
1866 IndexedPropertyDeleter deleter = 0,
1867 IndexedPropertyEnumerator enumerator = 0,
1868 Handle<Value> data = Handle<Value>());
1869 /**
1870 * Sets the callback to be used when calling instances created from
1871 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001872 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 * function.
1874 */
1875 void SetCallAsFunctionHandler(InvocationCallback callback,
1876 Handle<Value> data = Handle<Value>());
1877
v8.team.kasperl727e9952008-09-02 14:56:44 +00001878 /**
1879 * Mark object instances of the template as undetectable.
1880 *
1881 * In many ways, undetectable objects behave as though they are not
1882 * there. They behave like 'undefined' in conditionals and when
1883 * printed. However, properties can be accessed and called as on
1884 * normal objects.
1885 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 void MarkAsUndetectable();
1887
v8.team.kasperl727e9952008-09-02 14:56:44 +00001888 /**
1889 * Sets access check callbacks on the object template.
1890 *
1891 * When accessing properties on instances of this object template,
1892 * the access check callback will be called to determine whether or
1893 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001894 * The last parameter specifies whether access checks are turned
1895 * on by default on instances. If access checks are off by default,
1896 * they can be turned on on individual instances by calling
1897 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001898 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1900 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001901 Handle<Value> data = Handle<Value>(),
1902 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903
kasper.lund212ac232008-07-16 07:07:30 +00001904 /**
1905 * Gets the number of internal fields for objects generated from
1906 * this template.
1907 */
1908 int InternalFieldCount();
1909
1910 /**
1911 * Sets the number of internal fields for objects generated from
1912 * this template.
1913 */
1914 void SetInternalFieldCount(int value);
1915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 private:
1917 ObjectTemplate();
1918 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1919 friend class FunctionTemplate;
1920};
1921
1922
1923/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001924 * A Signature specifies which receivers and arguments a function can
1925 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001927class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 public:
1929 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1930 Handle<FunctionTemplate>(),
1931 int argc = 0,
1932 Handle<FunctionTemplate> argv[] = 0);
1933 private:
1934 Signature();
1935};
1936
1937
1938/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001939 * A utility for determining the type of objects based on the template
1940 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001942class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 public:
1944 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1945 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1946 int match(Handle<Value> value);
1947 private:
1948 TypeSwitch();
1949};
1950
1951
1952// --- E x t e n s i o n s ---
1953
1954
1955/**
1956 * Ignore
1957 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001958class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 public:
1960 Extension(const char* name,
1961 const char* source = 0,
1962 int dep_count = 0,
1963 const char** deps = 0);
1964 virtual ~Extension() { }
1965 virtual v8::Handle<v8::FunctionTemplate>
1966 GetNativeFunction(v8::Handle<v8::String> name) {
1967 return v8::Handle<v8::FunctionTemplate>();
1968 }
1969
1970 const char* name() { return name_; }
1971 const char* source() { return source_; }
1972 int dependency_count() { return dep_count_; }
1973 const char** dependencies() { return deps_; }
1974 void set_auto_enable(bool value) { auto_enable_ = value; }
1975 bool auto_enable() { return auto_enable_; }
1976
1977 private:
1978 const char* name_;
1979 const char* source_;
1980 int dep_count_;
1981 const char** deps_;
1982 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001983
1984 // Disallow copying and assigning.
1985 Extension(const Extension&);
1986 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987};
1988
1989
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001990void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991
1992
1993/**
1994 * Ignore
1995 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001996class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 public:
1998 inline DeclareExtension(Extension* extension) {
1999 RegisterExtension(extension);
2000 }
2001};
2002
2003
2004// --- S t a t i c s ---
2005
2006
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002007Handle<Primitive> V8EXPORT Undefined();
2008Handle<Primitive> V8EXPORT Null();
2009Handle<Boolean> V8EXPORT True();
2010Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011
2012
2013/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002014 * A set of constraints that specifies the limits of the runtime's memory use.
2015 * You must set the heap size before initializing the VM - the size cannot be
2016 * adjusted after the VM is initialized.
2017 *
2018 * If you are using threads then you should hold the V8::Locker lock while
2019 * setting the stack limit and you must set a non-default stack limit separately
2020 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002022class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 public:
2024 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002025 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002027 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002029 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002030 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2032 private:
2033 int max_young_space_size_;
2034 int max_old_space_size_;
2035 uint32_t* stack_limit_;
2036};
2037
2038
2039bool SetResourceConstraints(ResourceConstraints* constraints);
2040
2041
2042// --- E x c e p t i o n s ---
2043
2044
2045typedef void (*FatalErrorCallback)(const char* location, const char* message);
2046
2047
2048typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2049
2050
2051/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002052 * Schedules an exception to be thrown when returning to JavaScript. When an
2053 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002055 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002057Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058
2059/**
2060 * Create new error objects by calling the corresponding error object
2061 * constructor with the message.
2062 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002063class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 public:
2065 static Local<Value> RangeError(Handle<String> message);
2066 static Local<Value> ReferenceError(Handle<String> message);
2067 static Local<Value> SyntaxError(Handle<String> message);
2068 static Local<Value> TypeError(Handle<String> message);
2069 static Local<Value> Error(Handle<String> message);
2070};
2071
2072
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002073// --- 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 +00002074
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002075typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002077typedef void* (*CreateHistogramCallback)(const char* name,
2078 int min,
2079 int max,
2080 size_t buckets);
2081
2082typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2083
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084// --- 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 ---
2085typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2086 AccessType type,
2087 Local<Value> data);
2088
2089// --- 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
2090
2091/**
2092 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002093 * before and after a major garbage collection. Allocations are not
2094 * allowed in the callback function, you therefore cannot manipulate
2095 * objects (set or delete properties for example) since it is possible
2096 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 */
2098typedef void (*GCCallback)();
2099
2100
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002101// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102
2103/**
2104 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002105 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107typedef Persistent<Context> (*ContextGenerator)();
2108
2109
2110/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002111 * Profiler modules.
2112 *
2113 * In V8, profiler consists of several modules: CPU profiler, and different
2114 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002115 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2116 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002117 */
2118enum ProfilerModules {
2119 PROFILER_MODULE_NONE = 0,
2120 PROFILER_MODULE_CPU = 1,
2121 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002122 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2123 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002124};
2125
2126
2127/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002128 * Collection of V8 heap information.
2129 *
2130 * Instances of this class can be passed to v8::V8::HeapStatistics to
2131 * get heap statistics from V8.
2132 */
2133class V8EXPORT HeapStatistics {
2134 public:
2135 HeapStatistics();
2136 size_t total_heap_size() { return total_heap_size_; }
2137 size_t used_heap_size() { return used_heap_size_; }
2138
2139 private:
2140 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2141 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2142
2143 size_t total_heap_size_;
2144 size_t used_heap_size_;
2145
2146 friend class V8;
2147};
2148
2149
2150/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 * Container class for static utility functions.
2152 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002153class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002155 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 static void SetFatalErrorHandler(FatalErrorCallback that);
2157
v8.team.kasperl727e9952008-09-02 14:56:44 +00002158 /**
2159 * Ignore out-of-memory exceptions.
2160 *
2161 * V8 running out of memory is treated as a fatal error by default.
2162 * This means that the fatal error handler is called and that V8 is
2163 * terminated.
2164 *
2165 * IgnoreOutOfMemoryException can be used to not treat a
2166 * out-of-memory situation as a fatal error. This way, the contexts
2167 * that did not cause the out of memory problem might be able to
2168 * continue execution.
2169 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170 static void IgnoreOutOfMemoryException();
2171
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002173 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002174 * fatal errors such as out-of-memory situations.
2175 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 static bool IsDead();
2177
2178 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002179 * Adds a message listener.
2180 *
2181 * The same message listener can be added more than once and it that
2182 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 */
2184 static bool AddMessageListener(MessageCallback that,
2185 Handle<Value> data = Handle<Value>());
2186
2187 /**
2188 * Remove all message listeners from the specified callback function.
2189 */
2190 static void RemoveMessageListeners(MessageCallback that);
2191
2192 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002193 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 */
2195 static void SetFlagsFromString(const char* str, int length);
2196
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002197 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002199 */
2200 static void SetFlagsFromCommandLine(int* argc,
2201 char** argv,
2202 bool remove_flags);
2203
kasper.lund7276f142008-07-30 08:49:36 +00002204 /** Get the version string. */
2205 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206
2207 /**
2208 * Enables the host application to provide a mechanism for recording
2209 * statistics counters.
2210 */
2211 static void SetCounterFunction(CounterLookupCallback);
2212
2213 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002214 * Enables the host application to provide a mechanism for recording
2215 * histograms. The CreateHistogram function returns a
2216 * histogram which will later be passed to the AddHistogramSample
2217 * function.
2218 */
2219 static void SetCreateHistogramFunction(CreateHistogramCallback);
2220 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2221
2222 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 * Enables the computation of a sliding window of states. The sliding
2224 * window information is recorded in statistics counters.
2225 */
2226 static void EnableSlidingStateWindow();
2227
2228 /** Callback function for reporting failed access checks.*/
2229 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2230
2231 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002232 * Enables the host application to receive a notification before a
2233 * major garbage colletion. Allocations are not allowed in the
2234 * callback function, you therefore cannot manipulate objects (set
2235 * or delete properties for example) since it is possible such
2236 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 */
2238 static void SetGlobalGCPrologueCallback(GCCallback);
2239
2240 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002241 * Enables the host application to receive a notification after a
2242 * major garbage collection. Allocations are not allowed in the
2243 * callback function, you therefore cannot manipulate objects (set
2244 * or delete properties for example) since it is possible such
2245 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 */
2247 static void SetGlobalGCEpilogueCallback(GCCallback);
2248
2249 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002250 * Allows the host application to group objects together. If one
2251 * object in the group is alive, all objects in the group are alive.
2252 * After each garbage collection, object groups are removed. It is
2253 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002254 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002257 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258
2259 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002261 * initialize from scratch. This function is called implicitly if
2262 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 */
2264 static bool Initialize();
2265
kasper.lund7276f142008-07-30 08:49:36 +00002266 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002267 * Adjusts the amount of registered external memory. Used to give
2268 * V8 an indication of the amount of externally allocated memory
2269 * that is kept alive by JavaScript objects. V8 uses this to decide
2270 * when to perform global garbage collections. Registering
2271 * externally allocated memory will trigger global garbage
2272 * collections more often than otherwise in an attempt to garbage
2273 * collect the JavaScript objects keeping the externally allocated
2274 * memory alive.
2275 *
2276 * \param change_in_bytes the change in externally allocated memory
2277 * that is kept alive by JavaScript objects.
2278 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002279 */
2280 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2281
iposva@chromium.org245aa852009-02-10 00:49:54 +00002282 /**
2283 * Suspends recording of tick samples in the profiler.
2284 * When the V8 profiling mode is enabled (usually via command line
2285 * switches) this function suspends recording of tick samples.
2286 * Profiling ticks are discarded until ResumeProfiler() is called.
2287 *
2288 * See also the --prof and --prof_auto command line switches to
2289 * enable V8 profiling.
2290 */
2291 static void PauseProfiler();
2292
2293 /**
2294 * Resumes recording of tick samples in the profiler.
2295 * See also PauseProfiler().
2296 */
2297 static void ResumeProfiler();
2298
ager@chromium.org41826e72009-03-30 13:30:57 +00002299 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002300 * Return whether profiler is currently paused.
2301 */
2302 static bool IsProfilerPaused();
2303
2304 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002305 * Resumes specified profiler modules.
2306 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2307 * See ProfilerModules enum.
2308 *
2309 * \param flags Flags specifying profiler modules.
2310 */
2311 static void ResumeProfilerEx(int flags);
2312
2313 /**
2314 * Pauses specified profiler modules.
2315 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2316 * See ProfilerModules enum.
2317 *
2318 * \param flags Flags specifying profiler modules.
2319 */
2320 static void PauseProfilerEx(int flags);
2321
2322 /**
2323 * Returns active (resumed) profiler modules.
2324 * See ProfilerModules enum.
2325 *
2326 * \returns active profiler modules.
2327 */
2328 static int GetActiveProfilerModules();
2329
2330 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002331 * If logging is performed into a memory buffer (via --logfile=*), allows to
2332 * retrieve previously written messages. This can be used for retrieving
2333 * profiler log data in the application. This function is thread-safe.
2334 *
2335 * Caller provides a destination buffer that must exist during GetLogLines
2336 * call. Only whole log lines are copied into the buffer.
2337 *
2338 * \param from_pos specified a point in a buffer to read from, 0 is the
2339 * beginning of a buffer. It is assumed that caller updates its current
2340 * position using returned size value from the previous call.
2341 * \param dest_buf destination buffer for log data.
2342 * \param max_size size of the destination buffer.
2343 * \returns actual size of log data copied into buffer.
2344 */
2345 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2346
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002347 /**
2348 * Retrieve the V8 thread id of the calling thread.
2349 *
2350 * The thread id for a thread should only be retrieved after the V8
2351 * lock has been acquired with a Locker object with that thread.
2352 */
2353 static int GetCurrentThreadId();
2354
2355 /**
2356 * Forcefully terminate execution of a JavaScript thread. This can
2357 * be used to terminate long-running scripts.
2358 *
2359 * TerminateExecution should only be called when then V8 lock has
2360 * been acquired with a Locker object. Therefore, in order to be
2361 * able to terminate long-running threads, preemption must be
2362 * enabled to allow the user of TerminateExecution to acquire the
2363 * lock.
2364 *
2365 * The termination is achieved by throwing an exception that is
2366 * uncatchable by JavaScript exception handlers. Termination
2367 * exceptions act as if they were caught by a C++ TryCatch exception
2368 * handlers. If forceful termination is used, any C++ TryCatch
2369 * exception handler that catches an exception should check if that
2370 * exception is a termination exception and immediately return if
2371 * that is the case. Returning immediately in that case will
2372 * continue the propagation of the termination exception if needed.
2373 *
2374 * The thread id passed to TerminateExecution must have been
2375 * obtained by calling GetCurrentThreadId on the thread in question.
2376 *
2377 * \param thread_id The thread id of the thread to terminate.
2378 */
2379 static void TerminateExecution(int thread_id);
2380
2381 /**
2382 * Forcefully terminate the current thread of JavaScript execution.
2383 *
2384 * This method can be used by any thread even if that thread has not
2385 * acquired the V8 lock with a Locker object.
2386 */
2387 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002388
2389 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002390 * Releases any resources used by v8 and stops any utility threads
2391 * that may be running. Note that disposing v8 is permanent, it
2392 * cannot be reinitialized.
2393 *
2394 * It should generally not be necessary to dispose v8 before exiting
2395 * a process, this should happen automatically. It is only necessary
2396 * to use if the process needs the resources taken up by v8.
2397 */
2398 static bool Dispose();
2399
ager@chromium.org3811b432009-10-28 14:53:37 +00002400 /**
2401 * Get statistics about the heap memory usage.
2402 */
2403 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002404
2405 /**
2406 * Optional notification that the embedder is idle.
2407 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002408 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002409 * Returns true if the embedder should stop calling IdleNotification
2410 * until real work has been done. This indicates that V8 has done
2411 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002412 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002413 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002414
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002415 /**
2416 * Optional notification that the system is running low on memory.
2417 * V8 uses these notifications to attempt to free memory.
2418 */
2419 static void LowMemoryNotification();
2420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 private:
2422 V8();
2423
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002424 static internal::Object** GlobalizeReference(internal::Object** handle);
2425 static void DisposeGlobal(internal::Object** global_handle);
2426 static void MakeWeak(internal::Object** global_handle,
2427 void* data,
2428 WeakReferenceCallback);
2429 static void ClearWeak(internal::Object** global_handle);
2430 static bool IsGlobalNearDeath(internal::Object** global_handle);
2431 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432
2433 template <class T> friend class Handle;
2434 template <class T> friend class Local;
2435 template <class T> friend class Persistent;
2436 friend class Context;
2437};
2438
2439
2440/**
2441 * An external exception handler.
2442 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002443class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 public:
2445
2446 /**
2447 * Creates a new try/catch block and registers it with v8.
2448 */
2449 TryCatch();
2450
2451 /**
2452 * Unregisters and deletes this try/catch block.
2453 */
2454 ~TryCatch();
2455
2456 /**
2457 * Returns true if an exception has been caught by this try/catch block.
2458 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002459 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460
2461 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002462 * For certain types of exceptions, it makes no sense to continue
2463 * execution.
2464 *
2465 * Currently, the only type of exception that can be caught by a
2466 * TryCatch handler and for which it does not make sense to continue
2467 * is termination exception. Such exceptions are thrown when the
2468 * TerminateExecution methods are called to terminate a long-running
2469 * script.
2470 *
2471 * If CanContinue returns false, the correct action is to perform
2472 * any C++ cleanup needed and then return.
2473 */
2474 bool CanContinue() const;
2475
2476 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002477 * Throws the exception caught by this TryCatch in a way that avoids
2478 * it being caught again by this same TryCatch. As with ThrowException
2479 * it is illegal to execute any JavaScript operations after calling
2480 * ReThrow; the caller must return immediately to where the exception
2481 * is caught.
2482 */
2483 Handle<Value> ReThrow();
2484
2485 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486 * Returns the exception caught by this try/catch block. If no exception has
2487 * been caught an empty handle is returned.
2488 *
2489 * The returned handle is valid until this TryCatch block has been destroyed.
2490 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002491 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
2493 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002494 * Returns the .stack property of the thrown object. If no .stack
2495 * property is present an empty handle is returned.
2496 */
2497 Local<Value> StackTrace() const;
2498
2499 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002500 * Returns the message associated with this exception. If there is
2501 * no message associated an empty handle is returned.
2502 *
2503 * The returned handle is valid until this TryCatch block has been
2504 * destroyed.
2505 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002506 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002507
2508 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 * Clears any exceptions that may have been caught by this try/catch block.
2510 * After this method has been called, HasCaught() will return false.
2511 *
2512 * It is not necessary to clear a try/catch block before using it again; if
2513 * another exception is thrown the previously caught exception will just be
2514 * overwritten. However, it is often a good idea since it makes it easier
2515 * to determine which operation threw a given exception.
2516 */
2517 void Reset();
2518
v8.team.kasperl727e9952008-09-02 14:56:44 +00002519 /**
2520 * Set verbosity of the external exception handler.
2521 *
2522 * By default, exceptions that are caught by an external exception
2523 * handler are not reported. Call SetVerbose with true on an
2524 * external exception handler to have exceptions caught by the
2525 * handler reported as if they were not caught.
2526 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002527 void SetVerbose(bool value);
2528
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002529 /**
2530 * Set whether or not this TryCatch should capture a Message object
2531 * which holds source information about where the exception
2532 * occurred. True by default.
2533 */
2534 void SetCaptureMessage(bool value);
2535
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002536 private:
2537 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002539 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002540 bool is_verbose_ : 1;
2541 bool can_continue_ : 1;
2542 bool capture_message_ : 1;
2543 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002544
2545 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546};
2547
2548
2549// --- C o n t e x t ---
2550
2551
2552/**
2553 * Ignore
2554 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002555class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 public:
2557 ExtensionConfiguration(int name_count, const char* names[])
2558 : name_count_(name_count), names_(names) { }
2559 private:
2560 friend class ImplementationUtilities;
2561 int name_count_;
2562 const char** names_;
2563};
2564
2565
2566/**
2567 * A sandboxed execution context with its own set of built-in objects
2568 * and functions.
2569 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002570class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002572 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 Local<Object> Global();
2574
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002575 /**
2576 * Detaches the global object from its context before
2577 * the global object can be reused to create a new context.
2578 */
2579 void DetachGlobal();
2580
v8.team.kasperl727e9952008-09-02 14:56:44 +00002581 /** Creates a new context. */
2582 static Persistent<Context> New(
2583 ExtensionConfiguration* extensions = 0,
2584 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2585 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586
kasper.lund44510672008-07-25 07:37:58 +00002587 /** Returns the last entered context. */
2588 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
kasper.lund44510672008-07-25 07:37:58 +00002590 /** Returns the context that is on the top of the stack. */
2591 static Local<Context> GetCurrent();
2592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002594 * Returns the context of the calling JavaScript code. That is the
2595 * context of the top-most JavaScript frame. If there are no
2596 * JavaScript frames an empty handle is returned.
2597 */
2598 static Local<Context> GetCalling();
2599
2600 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 * Sets the security token for the context. To access an object in
2602 * another context, the security tokens must match.
2603 */
2604 void SetSecurityToken(Handle<Value> token);
2605
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002606 /** Restores the security token to the default value. */
2607 void UseDefaultSecurityToken();
2608
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002609 /** Returns the security token of this context.*/
2610 Handle<Value> GetSecurityToken();
2611
v8.team.kasperl727e9952008-09-02 14:56:44 +00002612 /**
2613 * Enter this context. After entering a context, all code compiled
2614 * and run is compiled and run in this context. If another context
2615 * is already entered, this old context is saved so it can be
2616 * restored when the new context is exited.
2617 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002619
2620 /**
2621 * Exit this context. Exiting the current context restores the
2622 * context that was in place when entering the current context.
2623 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 void Exit();
2625
v8.team.kasperl727e9952008-09-02 14:56:44 +00002626 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 bool HasOutOfMemoryException();
2628
v8.team.kasperl727e9952008-09-02 14:56:44 +00002629 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 static bool InContext();
2631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002633 * Associate an additional data object with the context. This is mainly used
2634 * with the debugger to provide additional information on the context through
2635 * the debugger API.
2636 */
2637 void SetData(Handle<Value> data);
2638 Local<Value> GetData();
2639
2640 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 * Stack-allocated class which sets the execution context for all
2642 * operations executed within a local scope.
2643 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002644 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 public:
2646 inline Scope(Handle<Context> context) : context_(context) {
2647 context_->Enter();
2648 }
2649 inline ~Scope() { context_->Exit(); }
2650 private:
2651 Handle<Context> context_;
2652 };
2653
2654 private:
2655 friend class Value;
2656 friend class Script;
2657 friend class Object;
2658 friend class Function;
2659};
2660
2661
2662/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002663 * Multiple threads in V8 are allowed, but only one thread at a time
2664 * is allowed to use V8. The definition of 'using V8' includes
2665 * accessing handles or holding onto object pointers obtained from V8
2666 * handles. It is up to the user of V8 to ensure (perhaps with
2667 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 *
2669 * If you wish to start using V8 in a thread you can do this by constructing
2670 * a v8::Locker object. After the code using V8 has completed for the
2671 * current thread you can call the destructor. This can be combined
2672 * with C++ scope-based construction as follows:
2673 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002674 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675 * ...
2676 * {
2677 * v8::Locker locker;
2678 * ...
2679 * // Code using V8 goes here.
2680 * ...
2681 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002682 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683 *
2684 * If you wish to stop using V8 in a thread A you can do this by either
2685 * by destroying the v8::Locker object as above or by constructing a
2686 * v8::Unlocker object:
2687 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002688 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 * {
2690 * v8::Unlocker unlocker;
2691 * ...
2692 * // Code not using V8 goes here while V8 can run in another thread.
2693 * ...
2694 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002695 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 *
2697 * The Unlocker object is intended for use in a long-running callback
2698 * from V8, where you want to release the V8 lock for other threads to
2699 * use.
2700 *
2701 * The v8::Locker is a recursive lock. That is, you can lock more than
2702 * once in a given thread. This can be useful if you have code that can
2703 * be called either from code that holds the lock or from code that does
2704 * not. The Unlocker is not recursive so you can not have several
2705 * Unlockers on the stack at once, and you can not use an Unlocker in a
2706 * thread that is not inside a Locker's scope.
2707 *
2708 * An unlocker will unlock several lockers if it has to and reinstate
2709 * the correct depth of locking on its destruction. eg.:
2710 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002711 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 * // V8 not locked.
2713 * {
2714 * v8::Locker locker;
2715 * // V8 locked.
2716 * {
2717 * v8::Locker another_locker;
2718 * // V8 still locked (2 levels).
2719 * {
2720 * v8::Unlocker unlocker;
2721 * // V8 not locked.
2722 * }
2723 * // V8 locked again (2 levels).
2724 * }
2725 * // V8 still locked (1 level).
2726 * }
2727 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002728 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002730class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 public:
2732 Unlocker();
2733 ~Unlocker();
2734};
2735
2736
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002737class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 public:
2739 Locker();
2740 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002741
2742 /**
2743 * Start preemption.
2744 *
2745 * When preemption is started, a timer is fired every n milli seconds
2746 * that will switch between multiple threads that are in contention
2747 * for the V8 lock.
2748 */
2749 static void StartPreemption(int every_n_ms);
2750
2751 /**
2752 * Stop preemption.
2753 */
2754 static void StopPreemption();
2755
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002756 /**
2757 * Returns whether or not the locker is locked by the current thread.
2758 */
2759 static bool IsLocked();
2760
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002761 /**
2762 * Returns whether v8::Locker is being used by this V8 instance.
2763 */
2764 static bool IsActive() { return active_; }
2765
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 private:
2767 bool has_lock_;
2768 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002769
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002770 static bool active_;
2771
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002772 // Disallow copying and assigning.
2773 Locker(const Locker&);
2774 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002775};
2776
2777
2778
2779// --- I m p l e m e n t a t i o n ---
2780
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002781
2782namespace internal {
2783
2784
2785// Tag information for HeapObject.
2786const int kHeapObjectTag = 1;
2787const int kHeapObjectTagSize = 2;
2788const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2789
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002790// Tag information for Smi.
2791const int kSmiTag = 0;
2792const int kSmiTagSize = 1;
2793const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2794
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002795template <size_t ptr_size> struct SmiConstants;
2796
2797// Smi constants for 32-bit systems.
2798template <> struct SmiConstants<4> {
2799 static const int kSmiShiftSize = 0;
2800 static const int kSmiValueSize = 31;
2801 static inline int SmiToInt(internal::Object* value) {
2802 int shift_bits = kSmiTagSize + kSmiShiftSize;
2803 // Throw away top 32 bits and shift down (requires >> to be sign extending).
2804 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
2805 }
2806};
2807
2808// Smi constants for 64-bit systems.
2809template <> struct SmiConstants<8> {
2810 static const int kSmiShiftSize = 31;
2811 static const int kSmiValueSize = 32;
2812 static inline int SmiToInt(internal::Object* value) {
2813 int shift_bits = kSmiTagSize + kSmiShiftSize;
2814 // Shift down and throw away top 32 bits.
2815 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
2816 }
2817};
2818
2819const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
2820const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002821
2822/**
2823 * This class exports constants and functionality from within v8 that
2824 * is necessary to implement inline functions in the v8 api. Don't
2825 * depend on functions and constants defined here.
2826 */
2827class Internals {
2828 public:
2829
2830 // These values match non-compiler-dependent values defined within
2831 // the implementation of v8.
2832 static const int kHeapObjectMapOffset = 0;
2833 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
2834 static const int kStringResourceOffset = 2 * sizeof(void*);
2835 static const int kProxyProxyOffset = sizeof(void*);
2836 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2837 static const int kFullStringRepresentationMask = 0x07;
2838 static const int kExternalTwoByteRepresentationTag = 0x03;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002839
2840 // These constants are compiler dependent so their values must be
2841 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002842 V8EXPORT static int kJSObjectType;
2843 V8EXPORT static int kFirstNonstringType;
2844 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002845
2846 static inline bool HasHeapObjectTag(internal::Object* value) {
2847 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2848 kHeapObjectTag);
2849 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002850
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002851 static inline bool HasSmiTag(internal::Object* value) {
2852 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2853 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002854
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002855 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002856 return SmiConstants<sizeof(void*)>::SmiToInt(value);
2857 }
2858
2859 static inline int GetInstanceType(internal::Object* obj) {
2860 typedef internal::Object O;
2861 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
2862 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
2863 }
2864
2865 static inline void* GetExternalPointer(internal::Object* obj) {
2866 if (HasSmiTag(obj)) {
2867 return obj;
2868 } else if (GetInstanceType(obj) == kProxyType) {
2869 return ReadField<void*>(obj, kProxyProxyOffset);
2870 } else {
2871 return NULL;
2872 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002873 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002874
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002875 static inline bool IsExternalTwoByteString(int instance_type) {
2876 int representation = (instance_type & kFullStringRepresentationMask);
2877 return representation == kExternalTwoByteRepresentationTag;
2878 }
2879
2880 template <typename T>
2881 static inline T ReadField(Object* ptr, int offset) {
2882 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2883 return *reinterpret_cast<T*>(addr);
2884 }
2885
2886};
2887
2888}
2889
2890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891template <class T>
2892Handle<T>::Handle() : val_(0) { }
2893
2894
2895template <class T>
2896Local<T>::Local() : Handle<T>() { }
2897
2898
2899template <class T>
2900Local<T> Local<T>::New(Handle<T> that) {
2901 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002902 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2904}
2905
2906
2907template <class T>
2908Persistent<T> Persistent<T>::New(Handle<T> that) {
2909 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002910 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2912}
2913
2914
2915template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002916bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002918 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919}
2920
2921
2922template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002923bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002924 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002925 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926}
2927
2928
2929template <class T>
2930void Persistent<T>::Dispose() {
2931 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002932 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002933}
2934
2935
2936template <class T>
2937Persistent<T>::Persistent() : Handle<T>() { }
2938
2939template <class T>
2940void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002941 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
2942 parameters,
2943 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944}
2945
2946template <class T>
2947void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002948 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949}
2950
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951Local<Value> Arguments::operator[](int i) const {
2952 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2953 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2954}
2955
2956
2957Local<Function> Arguments::Callee() const {
2958 return callee_;
2959}
2960
2961
2962Local<Object> Arguments::This() const {
2963 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2964}
2965
2966
2967Local<Object> Arguments::Holder() const {
2968 return holder_;
2969}
2970
2971
2972Local<Value> Arguments::Data() const {
2973 return data_;
2974}
2975
2976
2977bool Arguments::IsConstructCall() const {
2978 return is_construct_call_;
2979}
2980
2981
2982int Arguments::Length() const {
2983 return length_;
2984}
2985
2986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002987template <class T>
2988Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002989 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
2990 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991 return Local<T>(reinterpret_cast<T*>(after));
2992}
2993
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002994Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995 return resource_name_;
2996}
2997
2998
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002999Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000 return resource_line_offset_;
3001}
3002
3003
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003004Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005 return resource_column_offset_;
3006}
3007
3008
3009Handle<Boolean> Boolean::New(bool value) {
3010 return value ? True() : False();
3011}
3012
3013
3014void Template::Set(const char* name, v8::Handle<Data> value) {
3015 Set(v8::String::New(name), value);
3016}
3017
3018
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003019Local<Value> Object::GetInternalField(int index) {
3020#ifndef V8_ENABLE_CHECKS
3021 Local<Value> quick_result = UncheckedGetInternalField(index);
3022 if (!quick_result.IsEmpty()) return quick_result;
3023#endif
3024 return CheckedGetInternalField(index);
3025}
3026
3027
3028Local<Value> Object::UncheckedGetInternalField(int index) {
3029 typedef internal::Object O;
3030 typedef internal::Internals I;
3031 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003032 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003033 // If the object is a plain JSObject, which is the common case,
3034 // we know where to find the internal fields and can return the
3035 // value directly.
3036 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3037 O* value = I::ReadField<O*>(obj, offset);
3038 O** result = HandleScope::CreateHandle(value);
3039 return Local<Value>(reinterpret_cast<Value*>(result));
3040 } else {
3041 return Local<Value>();
3042 }
3043}
3044
3045
3046void* External::Unwrap(Handle<v8::Value> obj) {
3047#ifdef V8_ENABLE_CHECKS
3048 return FullUnwrap(obj);
3049#else
3050 return QuickUnwrap(obj);
3051#endif
3052}
3053
3054
3055void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3056 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003057 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003058 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003059}
3060
3061
3062void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003063 typedef internal::Object O;
3064 typedef internal::Internals I;
3065
3066 O* obj = *reinterpret_cast<O**>(this);
3067
3068 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3069 // If the object is a plain JSObject, which is the common case,
3070 // we know where to find the internal fields and can return the
3071 // value directly.
3072 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3073 O* value = I::ReadField<O*>(obj, offset);
3074 return I::GetExternalPointer(value);
3075 }
3076
3077 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003078}
3079
3080
3081String* String::Cast(v8::Value* value) {
3082#ifdef V8_ENABLE_CHECKS
3083 CheckCast(value);
3084#endif
3085 return static_cast<String*>(value);
3086}
3087
3088
3089String::ExternalStringResource* String::GetExternalStringResource() const {
3090 typedef internal::Object O;
3091 typedef internal::Internals I;
3092 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003093 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003094 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003095 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3096 result = reinterpret_cast<String::ExternalStringResource*>(value);
3097 } else {
3098 result = NULL;
3099 }
3100#ifdef V8_ENABLE_CHECKS
3101 VerifyExternalStringResource(result);
3102#endif
3103 return result;
3104}
3105
3106
3107bool Value::IsString() const {
3108#ifdef V8_ENABLE_CHECKS
3109 return FullIsString();
3110#else
3111 return QuickIsString();
3112#endif
3113}
3114
3115bool Value::QuickIsString() const {
3116 typedef internal::Object O;
3117 typedef internal::Internals I;
3118 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3119 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003120 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003121}
3122
3123
3124Number* Number::Cast(v8::Value* value) {
3125#ifdef V8_ENABLE_CHECKS
3126 CheckCast(value);
3127#endif
3128 return static_cast<Number*>(value);
3129}
3130
3131
3132Integer* Integer::Cast(v8::Value* value) {
3133#ifdef V8_ENABLE_CHECKS
3134 CheckCast(value);
3135#endif
3136 return static_cast<Integer*>(value);
3137}
3138
3139
3140Date* Date::Cast(v8::Value* value) {
3141#ifdef V8_ENABLE_CHECKS
3142 CheckCast(value);
3143#endif
3144 return static_cast<Date*>(value);
3145}
3146
3147
3148Object* Object::Cast(v8::Value* value) {
3149#ifdef V8_ENABLE_CHECKS
3150 CheckCast(value);
3151#endif
3152 return static_cast<Object*>(value);
3153}
3154
3155
3156Array* Array::Cast(v8::Value* value) {
3157#ifdef V8_ENABLE_CHECKS
3158 CheckCast(value);
3159#endif
3160 return static_cast<Array*>(value);
3161}
3162
3163
3164Function* Function::Cast(v8::Value* value) {
3165#ifdef V8_ENABLE_CHECKS
3166 CheckCast(value);
3167#endif
3168 return static_cast<Function*>(value);
3169}
3170
3171
3172External* External::Cast(v8::Value* value) {
3173#ifdef V8_ENABLE_CHECKS
3174 CheckCast(value);
3175#endif
3176 return static_cast<External*>(value);
3177}
3178
3179
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003180Local<Value> AccessorInfo::Data() const {
3181 return Local<Value>(reinterpret_cast<Value*>(&args_[-3]));
3182}
3183
3184
3185Local<Object> AccessorInfo::This() const {
3186 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3187}
3188
3189
3190Local<Object> AccessorInfo::Holder() const {
3191 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3192}
3193
3194
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003195/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003196 * \example shell.cc
3197 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 * command-line and executes them.
3199 */
3200
3201
3202/**
3203 * \example process.cc
3204 */
3205
3206
3207} // namespace v8
3208
3209
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003210#undef V8EXPORT
3211#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003212#undef TYPE_CHECK
3213
3214
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003215#endif // V8_H_