blob: c372d0cea94e2c46f3dea29c6b1af529ec9182c8 [file] [log] [blame]
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001// Copyright 2007-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41#include <stdio.h>
42
43#ifdef _WIN32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
ager@chromium.org5ec48922009-05-05 07:25:34 +000048typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052typedef int int32_t;
53typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000054typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058
59// Setup for Windows DLL export/import. When building the V8 DLL the
60// BUILDING_V8_SHARED needs to be defined. When building a program which uses
61// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
62// static library or building a program which uses the V8 static library neither
63// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000064// The reason for having both V8EXPORT and V8EXPORT_INLINE is that classes which
65// have their code inside this header file need to have __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066// when building the DLL but cannot have __declspec(dllimport) when building
67// a program which uses the DLL.
68#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
69#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
70 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071#endif
72
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT __declspec(dllexport)
75#define V8EXPORT_INLINE __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT __declspec(dllimport)
78#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000080#define V8EXPORT
81#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#endif // BUILDING_V8_SHARED
83
84#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000085
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086#include <stdint.h>
87
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000088// Setup for Linux shared library export. There is no need to distinguish
89// between building or using the V8 shared library, but we should not
90// export symbols when we are building a static library.
91#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000092#define V8EXPORT __attribute__ ((visibility("default")))
93#define V8EXPORT_INLINE __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000094#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095#define V8EXPORT
96#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000097#endif // defined(__GNUC__) && (__GNUC__ >= 4)
98
99#endif // _WIN32
100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000102 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 */
104namespace v8 {
105
106class Context;
107class String;
108class Value;
109class Utils;
110class Number;
111class Object;
112class Array;
113class Int32;
114class Uint32;
115class External;
116class Primitive;
117class Boolean;
118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
123template <class T> class Handle;
124template <class T> class Local;
125template <class T> class Persistent;
126class FunctionTemplate;
127class ObjectTemplate;
128class Data;
129
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000130namespace internal {
131
132class Object;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000133class Arguments;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000134
135}
136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137
138// --- W e a k H a n d l e s
139
140
141/**
142 * A weak reference callback function.
143 *
144 * \param object the weak global object to be reclaimed by the garbage collector
145 * \param parameter the value passed in when making the weak global object
146 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000147typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 void* parameter);
149
150
151// --- H a n d l e s ---
152
153#define TYPE_CHECK(T, S) \
154 while (false) { \
155 *(static_cast<T**>(0)) = static_cast<S*>(0); \
156 }
157
158/**
159 * An object reference managed by the v8 garbage collector.
160 *
161 * All objects returned from v8 have to be tracked by the garbage
162 * collector so that it knows that the objects are still alive. Also,
163 * because the garbage collector may move objects, it is unsafe to
164 * point directly to an object. Instead, all objects are stored in
165 * handles which are known by the garbage collector and updated
166 * whenever an object moves. Handles should always be passed by value
167 * (except in cases like out-parameters) and they should never be
168 * allocated on the heap.
169 *
170 * There are two types of handles: local and persistent handles.
171 * Local handles are light-weight and transient and typically used in
172 * local operations. They are managed by HandleScopes. Persistent
173 * handles can be used when storing objects across several independent
174 * operations and have to be explicitly deallocated when they're no
175 * longer used.
176 *
177 * It is safe to extract the object stored in the handle by
178 * dereferencing the handle (for instance, to extract the Object* from
179 * an Handle<Object>); the value will still be governed by a handle
180 * behind the scenes and the same rules apply to these values as to
181 * their handles.
182 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000183template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 public:
185
186 /**
187 * Creates an empty handle.
188 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000189 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190
191 /**
192 * Creates a new handle for the specified value.
193 */
194 explicit Handle(T* val) : val_(val) { }
195
196 /**
197 * Creates a handle for the contents of the specified handle. This
198 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000199 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 * incompatible handles, for instance from a Handle<String> to a
201 * Handle<Number> it will cause a compiletime error. Assigning
202 * between compatible handles, for instance assigning a
203 * Handle<String> to a variable declared as Handle<Value>, is legal
204 * because String is a subclass of Value.
205 */
206 template <class S> inline Handle(Handle<S> that)
207 : val_(reinterpret_cast<T*>(*that)) {
208 /**
209 * This check fails when trying to convert between incompatible
210 * handles. For example, converting from a Handle<String> to a
211 * Handle<Number>.
212 */
213 TYPE_CHECK(T, S);
214 }
215
216 /**
217 * Returns true if the handle is empty.
218 */
ager@chromium.org32912102009-01-16 10:38:43 +0000219 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000221 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000223 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224
225 /**
226 * Sets the handle to be empty. IsEmpty() will then return true.
227 */
228 void Clear() { this->val_ = 0; }
229
230 /**
231 * Checks whether two handles are the same.
232 * Returns true if both are empty, or if the objects
233 * to which they refer are identical.
234 * The handles' references are not checked.
235 */
ager@chromium.org32912102009-01-16 10:38:43 +0000236 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000237 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
238 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 if (a == 0) return b == 0;
240 if (b == 0) return false;
241 return *a == *b;
242 }
243
244 /**
245 * Checks whether two handles are different.
246 * Returns true if only one of the handles is empty, or if
247 * the objects to which they refer are different.
248 * The handles' references are not checked.
249 */
ager@chromium.org32912102009-01-16 10:38:43 +0000250 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 return !operator==(that);
252 }
253
254 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000255#ifdef V8_ENABLE_CHECKS
256 // If we're going to perform the type check then we have to check
257 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000259#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 return Handle<T>(T::Cast(*that));
261 }
262
263 private:
264 T* val_;
265};
266
267
268/**
269 * A light-weight stack-allocated object handle. All operations
270 * that return objects from within v8 return them in local handles. They
271 * are created within HandleScopes, and all local handles allocated within a
272 * handle scope are destroyed when the handle scope is destroyed. Hence it
273 * is not necessary to explicitly deallocate local handles.
274 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000275template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000277 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 template <class S> inline Local(Local<S> that)
279 : Handle<T>(reinterpret_cast<T*>(*that)) {
280 /**
281 * This check fails when trying to convert between incompatible
282 * handles. For example, converting from a Handle<String> to a
283 * Handle<Number>.
284 */
285 TYPE_CHECK(T, S);
286 }
287 template <class S> inline Local(S* that) : Handle<T>(that) { }
288 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000289#ifdef V8_ENABLE_CHECKS
290 // If we're going to perform the type check then we have to check
291 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000293#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 return Local<T>(T::Cast(*that));
295 }
296
297 /** Create a local handle for the content of another handle.
298 * The referee is kept alive by the local handle even when
299 * the original handle is destroyed/disposed.
300 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000301 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302};
303
304
305/**
306 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000307 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 * allocated, a Persistent handle remains valid until it is explicitly
309 * disposed.
310 *
311 * A persistent handle contains a reference to a storage cell within
312 * the v8 engine which holds an object value and which is updated by
313 * the garbage collector whenever the object is moved. A new storage
314 * cell can be created using Persistent::New and existing handles can
315 * be disposed using Persistent::Dispose. Since persistent handles
316 * are passed by value you may have many persistent handle objects
317 * that point to the same storage cell. For instance, if you pass a
318 * persistent handle as an argument to a function you will not get two
319 * different storage cells but rather two references to the same
320 * storage cell.
321 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000322template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 public:
324
325 /**
326 * Creates an empty persistent handle that doesn't point to any
327 * storage cell.
328 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000329 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330
331 /**
332 * Creates a persistent handle for the same storage cell as the
333 * specified handle. This constructor allows you to pass persistent
334 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000335 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000337 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338 * between compatible persistent handles, for instance assigning a
339 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000340 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 */
342 template <class S> inline Persistent(Persistent<S> that)
343 : Handle<T>(reinterpret_cast<T*>(*that)) {
344 /**
345 * This check fails when trying to convert between incompatible
346 * handles. For example, converting from a Handle<String> to a
347 * Handle<Number>.
348 */
349 TYPE_CHECK(T, S);
350 }
351
352 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
353
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000354 /**
355 * "Casts" a plain handle which is known to be a persistent handle
356 * to a persistent handle.
357 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 template <class S> explicit inline Persistent(Handle<S> that)
359 : Handle<T>(*that) { }
360
361 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000362#ifdef V8_ENABLE_CHECKS
363 // If we're going to perform the type check then we have to check
364 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000366#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 return Persistent<T>(T::Cast(*that));
368 }
369
370 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000371 * Creates a new persistent handle for an existing local or
372 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000374 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 /**
377 * Releases the storage cell referenced by this persistent handle.
378 * Does not remove the reference to the cell from any handles.
379 * This handle's reference, and any any other references to the storage
380 * cell remain and IsEmpty will still return false.
381 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000382 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383
384 /**
385 * Make the reference to this object weak. When only weak handles
386 * refer to the object, the garbage collector will perform a
387 * callback to the given V8::WeakReferenceCallback function, passing
388 * it the object reference and the given parameters.
389 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000390 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391
392 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000393 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395 /**
396 *Checks if the handle holds the only reference to an object.
397 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000398 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399
400 /**
401 * Returns true if the handle's reference is weak.
402 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000403 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
405 private:
406 friend class ImplementationUtilities;
407 friend class ObjectTemplate;
408};
409
410
v8.team.kasperl727e9952008-09-02 14:56:44 +0000411 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 * A stack-allocated class that governs a number of local handles.
413 * After a handle scope has been created, all local handles will be
414 * allocated within that handle scope until either the handle scope is
415 * deleted or another handle scope is created. If there is already a
416 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000417 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000418 * new handles will again be allocated in the original handle scope.
419 *
420 * After the handle scope of a local handle has been deleted the
421 * garbage collector will no longer track the object stored in the
422 * handle and may deallocate it. The behavior of accessing a handle
423 * for which the handle scope has been deleted is undefined.
424 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000425class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000427 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000429 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430
431 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432 * Closes the handle scope and returns the value as a handle in the
433 * previous scope, which is the new current scope after the call.
434 */
435 template <class T> Local<T> Close(Handle<T> value);
436
437 /**
438 * Counts the number of allocated handles.
439 */
440 static int NumberOfHandles();
441
442 /**
443 * Creates a new handle with the given value.
444 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000445 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
447 private:
448 // Make it impossible to create heap-allocated or illegal handle
449 // scopes by disallowing certain operations.
450 HandleScope(const HandleScope&);
451 void operator=(const HandleScope&);
452 void* operator new(size_t size);
453 void operator delete(void*, size_t);
454
ager@chromium.org3811b432009-10-28 14:53:37 +0000455 // This Data class is accessible internally as HandleScopeData through a
456 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000457 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 public:
459 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000460 internal::Object** next;
461 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462 inline void Initialize() {
463 extensions = -1;
464 next = limit = NULL;
465 }
466 };
467
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000468 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000470 // Allow for the active closing of HandleScopes which allows to pass a handle
471 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000473 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 friend class ImplementationUtilities;
476};
477
478
479// --- S p e c i a l o b j e c t s ---
480
481
482/**
483 * The superclass of values and API object templates.
484 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000485class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 private:
487 Data();
488};
489
490
491/**
492 * Pre-compilation data that can be associated with a script. This
493 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000494 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000495 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000497class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 public:
499 virtual ~ScriptData() { }
500 static ScriptData* PreCompile(const char* input, int length);
501 static ScriptData* New(unsigned* data, int length);
502
503 virtual int Length() = 0;
504 virtual unsigned* Data() = 0;
505};
506
507
508/**
509 * The origin, within a file, of a script.
510 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000511class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000513 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 Handle<Integer> resource_line_offset = Handle<Integer>(),
515 Handle<Integer> resource_column_offset = Handle<Integer>())
516 : resource_name_(resource_name),
517 resource_line_offset_(resource_line_offset),
518 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000519 inline Handle<Value> ResourceName() const;
520 inline Handle<Integer> ResourceLineOffset() const;
521 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000523 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 Handle<Integer> resource_line_offset_;
525 Handle<Integer> resource_column_offset_;
526};
527
528
529/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000530 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000532class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 public:
534
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000535 /**
536 * Compiles the specified script. The ScriptOrigin* and ScriptData*
537 * parameters are owned by the caller of Script::Compile. No
538 * references to these objects are kept after compilation finishes.
539 *
540 * The script object returned is context independent; when run it
541 * will use the currently entered context.
542 */
543 static Local<Script> New(Handle<String> source,
544 ScriptOrigin* origin = NULL,
545 ScriptData* pre_data = NULL);
546
547 /**
548 * Compiles the specified script using the specified file name
549 * object (typically a string) as the script's origin.
550 *
551 * The script object returned is context independent; when run it
552 * will use the currently entered context.
553 */
554 static Local<Script> New(Handle<String> source,
555 Handle<Value> file_name);
556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 /**
558 * Compiles the specified script. The ScriptOrigin* and ScriptData*
559 * parameters are owned by the caller of Script::Compile. No
560 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000561 *
562 * The script object returned is bound to the context that was active
563 * when this function was called. When run it will always use this
564 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 */
566 static Local<Script> Compile(Handle<String> source,
567 ScriptOrigin* origin = NULL,
568 ScriptData* pre_data = NULL);
569
mads.s.agercbaa0602008-08-14 13:41:48 +0000570 /**
571 * Compiles the specified script using the specified file name
572 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000573 *
574 * The script object returned is bound to the context that was active
575 * when this function was called. When run it will always use this
576 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000577 */
578 static Local<Script> Compile(Handle<String> source,
579 Handle<Value> file_name);
580
v8.team.kasperl727e9952008-09-02 14:56:44 +0000581 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000582 * Runs the script returning the resulting value. If the script is
583 * context independent (created using ::New) it will be run in the
584 * currently entered context. If it is context specific (created
585 * using ::Compile) it will be run in the context in which it was
586 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000587 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000589
590 /**
591 * Returns the script id value.
592 */
593 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000594
595 /**
596 * Associate an additional data object with the script. This is mainly used
597 * with the debugger as this data object is only available through the
598 * debugger API.
599 */
600 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601};
602
603
604/**
605 * An error message.
606 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000607class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000609 Local<String> Get() const;
610 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000612 /**
613 * Returns the resource name for the script from where the function causing
614 * the error originates.
615 */
ager@chromium.org32912102009-01-16 10:38:43 +0000616 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000618 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000619 * Returns the resource data for the script from where the function causing
620 * the error originates.
621 */
622 Handle<Value> GetScriptData() const;
623
624 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000625 * Returns the number, 1-based, of the line where the error occurred.
626 */
ager@chromium.org32912102009-01-16 10:38:43 +0000627 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000629 /**
630 * Returns the index within the script of the first character where
631 * the error occurred.
632 */
ager@chromium.org32912102009-01-16 10:38:43 +0000633 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000634
635 /**
636 * Returns the index within the script of the last character where
637 * the error occurred.
638 */
ager@chromium.org32912102009-01-16 10:38:43 +0000639 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000640
641 /**
642 * Returns the index within the line of the first character where
643 * the error occurred.
644 */
ager@chromium.org32912102009-01-16 10:38:43 +0000645 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000646
647 /**
648 * Returns the index within the line of the last character where
649 * the error occurred.
650 */
ager@chromium.org32912102009-01-16 10:38:43 +0000651 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000652
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 // TODO(1245381): Print to a string instead of on a FILE.
654 static void PrintCurrentStackTrace(FILE* out);
655};
656
657
658// --- V a l u e ---
659
660
661/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000662 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000664class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 public:
666
667 /**
668 * Returns true if this value is the undefined value. See ECMA-262
669 * 4.3.10.
670 */
ager@chromium.org32912102009-01-16 10:38:43 +0000671 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
673 /**
674 * Returns true if this value is the null value. See ECMA-262
675 * 4.3.11.
676 */
ager@chromium.org32912102009-01-16 10:38:43 +0000677 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
679 /**
680 * Returns true if this value is true.
681 */
ager@chromium.org32912102009-01-16 10:38:43 +0000682 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
684 /**
685 * Returns true if this value is false.
686 */
ager@chromium.org32912102009-01-16 10:38:43 +0000687 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
689 /**
690 * Returns true if this value is an instance of the String type.
691 * See ECMA-262 8.4.
692 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000693 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
695 /**
696 * Returns true if this value is a function.
697 */
ager@chromium.org32912102009-01-16 10:38:43 +0000698 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
700 /**
701 * Returns true if this value is an array.
702 */
ager@chromium.org32912102009-01-16 10:38:43 +0000703 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704
v8.team.kasperl727e9952008-09-02 14:56:44 +0000705 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 * Returns true if this value is an object.
707 */
ager@chromium.org32912102009-01-16 10:38:43 +0000708 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709
v8.team.kasperl727e9952008-09-02 14:56:44 +0000710 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 * Returns true if this value is boolean.
712 */
ager@chromium.org32912102009-01-16 10:38:43 +0000713 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000714
v8.team.kasperl727e9952008-09-02 14:56:44 +0000715 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 * Returns true if this value is a number.
717 */
ager@chromium.org32912102009-01-16 10:38:43 +0000718 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
v8.team.kasperl727e9952008-09-02 14:56:44 +0000720 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 * Returns true if this value is external.
722 */
ager@chromium.org32912102009-01-16 10:38:43 +0000723 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724
v8.team.kasperl727e9952008-09-02 14:56:44 +0000725 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 * Returns true if this value is a 32-bit signed integer.
727 */
ager@chromium.org32912102009-01-16 10:38:43 +0000728 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000730 /**
731 * Returns true if this value is a Date.
732 */
ager@chromium.org32912102009-01-16 10:38:43 +0000733 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000734
ager@chromium.org32912102009-01-16 10:38:43 +0000735 Local<Boolean> ToBoolean() const;
736 Local<Number> ToNumber() const;
737 Local<String> ToString() const;
738 Local<String> ToDetailString() const;
739 Local<Object> ToObject() const;
740 Local<Integer> ToInteger() const;
741 Local<Uint32> ToUint32() const;
742 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743
744 /**
745 * Attempts to convert a string to an array index.
746 * Returns an empty handle if the conversion fails.
747 */
ager@chromium.org32912102009-01-16 10:38:43 +0000748 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749
ager@chromium.org32912102009-01-16 10:38:43 +0000750 bool BooleanValue() const;
751 double NumberValue() const;
752 int64_t IntegerValue() const;
753 uint32_t Uint32Value() const;
754 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755
756 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000757 bool Equals(Handle<Value> that) const;
758 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000759
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000760 private:
761 inline bool QuickIsString() const;
762 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763};
764
765
766/**
767 * The superclass of primitive values. See ECMA-262 4.3.2.
768 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000769class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770
771
772/**
773 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
774 * or false value.
775 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000776class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000778 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 static inline Handle<Boolean> New(bool value);
780};
781
782
783/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000784 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000786class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000788
789 /**
790 * Returns the number of characters in this string.
791 */
ager@chromium.org32912102009-01-16 10:38:43 +0000792 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
v8.team.kasperl727e9952008-09-02 14:56:44 +0000794 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000795 * Returns the number of bytes in the UTF-8 encoded
796 * representation of this string.
797 */
ager@chromium.org32912102009-01-16 10:38:43 +0000798 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000799
800 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000801 * Write the contents of the string to an external buffer.
802 * If no arguments are given, expects the buffer to be large
803 * enough to hold the entire string and NULL terminator. Copies
804 * the contents of the string and the NULL terminator into the
805 * buffer.
806 *
807 * Copies up to length characters into the output buffer.
808 * Only null-terminates if there is enough space in the buffer.
809 *
810 * \param buffer The buffer into which the string will be copied.
811 * \param start The starting position within the string at which
812 * copying begins.
813 * \param length The number of bytes to copy from the string.
814 * \return The number of characters copied to the buffer
815 * excluding the NULL terminator.
816 */
ager@chromium.org32912102009-01-16 10:38:43 +0000817 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
818 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
819 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820
v8.team.kasperl727e9952008-09-02 14:56:44 +0000821 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000822 * A zero length string.
823 */
824 static v8::Local<v8::String> Empty();
825
826 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000827 * Returns true if the string is external
828 */
ager@chromium.org32912102009-01-16 10:38:43 +0000829 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830
v8.team.kasperl727e9952008-09-02 14:56:44 +0000831 /**
832 * Returns true if the string is both external and ascii
833 */
ager@chromium.org32912102009-01-16 10:38:43 +0000834 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000835 /**
836 * An ExternalStringResource is a wrapper around a two-byte string
837 * buffer that resides outside V8's heap. Implement an
838 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000839 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000840 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000841 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 public:
843 /**
844 * Override the destructor to manage the life cycle of the underlying
845 * buffer.
846 */
847 virtual ~ExternalStringResource() {}
848 /** The string data from the underlying buffer.*/
849 virtual const uint16_t* data() const = 0;
850 /** The length of the string. That is, the number of two-byte characters.*/
851 virtual size_t length() const = 0;
852 protected:
853 ExternalStringResource() {}
854 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000855 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 ExternalStringResource(const ExternalStringResource&);
857 void operator=(const ExternalStringResource&);
858 };
859
860 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000861 * An ExternalAsciiStringResource is a wrapper around an ascii
862 * string buffer that resides outside V8's heap. Implement an
863 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000864 * underlying buffer. Note that the string data must be immutable
865 * and that the data must be strict 7-bit ASCII, not Latin1 or
866 * UTF-8, which would require special treatment internally in the
867 * engine and, in the case of UTF-8, do not allow efficient indexing.
868 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000869 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000871 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 public:
873 /**
874 * Override the destructor to manage the life cycle of the underlying
875 * buffer.
876 */
877 virtual ~ExternalAsciiStringResource() {}
878 /** The string data from the underlying buffer.*/
879 virtual const char* data() const = 0;
880 /** The number of ascii characters in the string.*/
881 virtual size_t length() const = 0;
882 protected:
883 ExternalAsciiStringResource() {}
884 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000885 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
887 void operator=(const ExternalAsciiStringResource&);
888 };
889
890 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000891 * Get the ExternalStringResource for an external string. Returns
892 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000894 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
896 /**
897 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000898 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 */
ager@chromium.org32912102009-01-16 10:38:43 +0000900 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000902 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903
904 /**
905 * Allocates a new string from either utf-8 encoded or ascii data.
906 * The second parameter 'length' gives the buffer length.
907 * If the data is utf-8 encoded, the caller must
908 * be careful to supply the length parameter.
909 * If it is not given, the function calls
910 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000911 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 */
913 static Local<String> New(const char* data, int length = -1);
914
915 /** Allocates a new string from utf16 data.*/
916 static Local<String> New(const uint16_t* data, int length = -1);
917
918 /** Creates a symbol. Returns one if it exists already.*/
919 static Local<String> NewSymbol(const char* data, int length = -1);
920
v8.team.kasperl727e9952008-09-02 14:56:44 +0000921 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000922 * Creates a new string by concatenating the left and the right strings
923 * passed in as parameters.
924 */
925 static Local<String> Concat(Handle<String> left, Handle<String>right);
926
927 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000928 * Creates a new external string using the data defined in the given
929 * resource. The resource is deleted when the external string is no
930 * longer live on V8's heap. The caller of this function should not
931 * delete or modify the resource. Neither should the underlying buffer be
932 * deallocated or modified except through the destructor of the
933 * external string resource.
934 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000936
ager@chromium.org6f10e412009-02-13 10:11:16 +0000937 /**
938 * Associate an external string resource with this string by transforming it
939 * in place so that existing references to this string in the JavaScript heap
940 * will use the external string resource. The external string resource's
941 * character contents needs to be equivalent to this string.
942 * Returns true if the string has been changed to be an external string.
943 * The string is not modified if the operation fails.
944 */
945 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946
v8.team.kasperl727e9952008-09-02 14:56:44 +0000947 /**
948 * Creates a new external string using the ascii data defined in the given
949 * resource. The resource is deleted when the external string is no
950 * longer live on V8's heap. The caller of this function should not
951 * delete or modify the resource. Neither should the underlying buffer be
952 * deallocated or modified except through the destructor of the
953 * external string resource.
954 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000956
ager@chromium.org6f10e412009-02-13 10:11:16 +0000957 /**
958 * Associate an external string resource with this string by transforming it
959 * in place so that existing references to this string in the JavaScript heap
960 * will use the external string resource. The external string resource's
961 * character contents needs to be equivalent to this string.
962 * Returns true if the string has been changed to be an external string.
963 * The string is not modified if the operation fails.
964 */
965 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000967 /**
968 * Returns true if this string can be made external.
969 */
970 bool CanMakeExternal();
971
kasper.lund7276f142008-07-30 08:49:36 +0000972 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 static Local<String> NewUndetectable(const char* data, int length = -1);
974
kasper.lund7276f142008-07-30 08:49:36 +0000975 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
977
978 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000979 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000980 * you want to print the object. If conversion to a string fails
981 * (eg. due to an exception in the toString() method of the object)
982 * then the length() method returns 0 and the * operator returns
983 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000984 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000985 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000986 public:
987 explicit Utf8Value(Handle<v8::Value> obj);
988 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000989 char* operator*() { return str_; }
990 const char* operator*() const { return str_; }
991 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000992 private:
993 char* str_;
994 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000995
996 // Disallow copying and assigning.
997 Utf8Value(const Utf8Value&);
998 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000999 };
1000
1001 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 * Converts an object to an ascii string.
1003 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001004 * If conversion to a string fails (eg. due to an exception in the toString()
1005 * method of the object) then the length() method returns 0 and the * operator
1006 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001008 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 public:
1010 explicit AsciiValue(Handle<v8::Value> obj);
1011 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001012 char* operator*() { return str_; }
1013 const char* operator*() const { return str_; }
1014 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 private:
1016 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001017 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001018
1019 // Disallow copying and assigning.
1020 AsciiValue(const AsciiValue&);
1021 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 };
1023
1024 /**
1025 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001026 * If conversion to a string fails (eg. due to an exception in the toString()
1027 * method of the object) then the length() method returns 0 and the * operator
1028 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001030 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031 public:
1032 explicit Value(Handle<v8::Value> obj);
1033 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001034 uint16_t* operator*() { return str_; }
1035 const uint16_t* operator*() const { return str_; }
1036 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 private:
1038 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001039 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001040
1041 // Disallow copying and assigning.
1042 Value(const Value&);
1043 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001045
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001046 private:
1047 void VerifyExternalStringResource(ExternalStringResource* val) const;
1048 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049};
1050
1051
1052/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001053 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001055class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001057 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001059 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060 private:
1061 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001062 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063};
1064
1065
1066/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001067 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001069class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 public:
1071 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001072 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001073 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001074 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 private:
1076 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001077 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078};
1079
1080
1081/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001082 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001084class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001086 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 private:
1088 Int32();
1089};
1090
1091
1092/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001093 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001095class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001097 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 private:
1099 Uint32();
1100};
1101
1102
1103/**
1104 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1105 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001106class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 public:
1108 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001109
1110 /**
1111 * A specialization of Value::NumberValue that is more efficient
1112 * because we know the structure of this object.
1113 */
ager@chromium.org32912102009-01-16 10:38:43 +00001114 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001115
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001116 static inline Date* Cast(v8::Value* obj);
1117 private:
1118 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119};
1120
1121
1122enum PropertyAttribute {
1123 None = 0,
1124 ReadOnly = 1 << 0,
1125 DontEnum = 1 << 1,
1126 DontDelete = 1 << 2
1127};
1128
ager@chromium.org3811b432009-10-28 14:53:37 +00001129enum ExternalArrayType {
1130 kExternalByteArray = 1,
1131 kExternalUnsignedByteArray,
1132 kExternalShortArray,
1133 kExternalUnsignedShortArray,
1134 kExternalIntArray,
1135 kExternalUnsignedIntArray,
1136 kExternalFloatArray
1137};
1138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001140 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001142class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 public:
1144 bool Set(Handle<Value> key,
1145 Handle<Value> value,
1146 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001147
ager@chromium.orge2902be2009-06-08 12:21:35 +00001148 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001149 // overriding accessors or read-only properties.
1150 //
1151 // Note that if the object has an interceptor the property will be set
1152 // locally, but since the interceptor takes precedence the local property
1153 // will only be returned if the interceptor doesn't return a value.
1154 //
1155 // Note also that this only works for named properties.
1156 bool ForceSet(Handle<Value> key,
1157 Handle<Value> value,
1158 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001159
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160 Local<Value> Get(Handle<Value> key);
1161
1162 // TODO(1245389): Replace the type-specific versions of these
1163 // functions with generic ones that accept a Handle<Value> key.
1164 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001167
1168 // Delete a property on this object bypassing interceptors and
1169 // ignoring dont-delete attributes.
1170 bool ForceDelete(Handle<Value> key);
1171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001173
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 bool Delete(uint32_t index);
1175
1176 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001177 * Returns an array containing the names of the enumerable properties
1178 * of this object, including properties from prototype objects. The
1179 * array returned by this method contains the same values as would
1180 * be enumerated by a for-in statement over this object.
1181 */
1182 Local<Array> GetPropertyNames();
1183
1184 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 * Get the prototype object. This does not skip objects marked to
1186 * be skipped by __proto__ and it does not consult the security
1187 * handler.
1188 */
1189 Local<Value> GetPrototype();
1190
1191 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001192 * Finds an instance of the given function template in the prototype
1193 * chain.
1194 */
1195 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1196
1197 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 * Call builtin Object.prototype.toString on this object.
1199 * This is different from Value::ToString() that may call
1200 * user-defined toString function. This one does not.
1201 */
1202 Local<String> ObjectProtoToString();
1203
kasper.lund212ac232008-07-16 07:07:30 +00001204 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001206 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001207 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001208 /** Sets the value in an internal field. */
1209 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001211 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001212 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001213
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001214 /** Sets a native pointer in an internal field. */
1215 void SetPointerInInternalField(int index, void* value);
1216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 // Testers for local properties.
1218 bool HasRealNamedProperty(Handle<String> key);
1219 bool HasRealIndexedProperty(uint32_t index);
1220 bool HasRealNamedCallbackProperty(Handle<String> key);
1221
1222 /**
1223 * If result.IsEmpty() no real property was located in the prototype chain.
1224 * This means interceptors in the prototype chain are not called.
1225 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001226 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1227
1228 /**
1229 * If result.IsEmpty() no real property was located on the object or
1230 * in the prototype chain.
1231 * This means interceptors in the prototype chain are not called.
1232 */
1233 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234
1235 /** Tests for a named lookup interceptor.*/
1236 bool HasNamedLookupInterceptor();
1237
kasper.lund212ac232008-07-16 07:07:30 +00001238 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 bool HasIndexedLookupInterceptor();
1240
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001241 /**
1242 * Turns on access check on the object if the object is an instance of
1243 * a template that has access check callbacks. If an object has no
1244 * access check info, the object cannot be accessed by anyone.
1245 */
1246 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001247
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001248 /**
1249 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001250 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001251 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001252 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001253 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001254 */
1255 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001256
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001257 /**
1258 * Access hidden properties on JavaScript objects. These properties are
1259 * hidden from the executing JavaScript and only accessible through the V8
1260 * C++ API. Hidden properties introduced by V8 internally (for example the
1261 * identity hash) are prefixed with "v8::".
1262 */
1263 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1264 Local<Value> GetHiddenValue(Handle<String> key);
1265 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001266
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001267 /**
1268 * Returns true if this is an instance of an api function (one
1269 * created from a function created from a function template) and has
1270 * been modified since it was created. Note that this method is
1271 * conservative and may return true for objects that haven't actually
1272 * been modified.
1273 */
1274 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001275
1276 /**
1277 * Clone this object with a fast but shallow copy. Values will point
1278 * to the same values as the original object.
1279 */
1280 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001282 /**
1283 * Set the backing store of the indexed properties to be managed by the
1284 * embedding layer. Access to the indexed properties will follow the rules
1285 * spelled out in CanvasPixelArray.
1286 * Note: The embedding program still owns the data and needs to ensure that
1287 * the backing store is preserved while V8 has a reference.
1288 */
1289 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1290
ager@chromium.org3811b432009-10-28 14:53:37 +00001291 /**
1292 * Set the backing store of the indexed properties to be managed by the
1293 * embedding layer. Access to the indexed properties will follow the rules
1294 * spelled out for the CanvasArray subtypes in the WebGL specification.
1295 * Note: The embedding program still owns the data and needs to ensure that
1296 * the backing store is preserved while V8 has a reference.
1297 */
1298 void SetIndexedPropertiesToExternalArrayData(void* data,
1299 ExternalArrayType array_type,
1300 int number_of_elements);
1301
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001303 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 private:
1305 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001306 static void CheckCast(Value* obj);
1307 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001308 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001309
1310 /**
1311 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001312 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001313 */
1314 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315};
1316
1317
1318/**
1319 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1320 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001321class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001323 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324
ager@chromium.org3e875802009-06-29 08:26:34 +00001325 /**
1326 * Clones an element at index |index|. Returns an empty
1327 * handle if cloning fails (for any reason).
1328 */
1329 Local<Object> CloneElementAt(uint32_t index);
1330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001332 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 private:
1334 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001335 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336};
1337
1338
1339/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001340 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001342class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001344 Local<Object> NewInstance() const;
1345 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1347 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001348 Handle<Value> GetName() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001349 static inline Function* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 private:
1351 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001352 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353};
1354
1355
1356/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001357 * A JavaScript value that wraps a C++ void*. This type of value is
1358 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001360 *
1361 * The Wrap function V8 will return the most optimal Value object wrapping the
1362 * C++ void*. The type of the value is not guaranteed to be an External object
1363 * and no assumptions about its type should be made. To access the wrapped
1364 * value Unwrap should be used, all other operations on that object will lead
1365 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001367class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001369 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001370 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001371
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001373 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001374 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 private:
1376 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001377 static void CheckCast(v8::Value* obj);
1378 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1379 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380};
1381
1382
1383// --- T e m p l a t e s ---
1384
1385
1386/**
1387 * The superclass of object and function templates.
1388 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001389class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390 public:
1391 /** Adds a property to each instance created by this template.*/
1392 void Set(Handle<String> name, Handle<Data> value,
1393 PropertyAttribute attributes = None);
1394 inline void Set(const char* name, Handle<Data> value);
1395 private:
1396 Template();
1397
1398 friend class ObjectTemplate;
1399 friend class FunctionTemplate;
1400};
1401
1402
1403/**
1404 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001405 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 * including the receiver, the number and values of arguments, and
1407 * the holder of the function.
1408 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001409class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 public:
1411 inline int Length() const;
1412 inline Local<Value> operator[](int i) const;
1413 inline Local<Function> Callee() const;
1414 inline Local<Object> This() const;
1415 inline Local<Object> Holder() const;
1416 inline bool IsConstructCall() const;
1417 inline Local<Value> Data() const;
1418 private:
1419 Arguments();
1420 friend class ImplementationUtilities;
1421 inline Arguments(Local<Value> data,
1422 Local<Object> holder,
1423 Local<Function> callee,
1424 bool is_construct_call,
1425 void** values, int length);
1426 Local<Value> data_;
1427 Local<Object> holder_;
1428 Local<Function> callee_;
1429 bool is_construct_call_;
1430 void** values_;
1431 int length_;
1432};
1433
1434
1435/**
1436 * The information passed to an accessor callback about the context
1437 * of the property access.
1438 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001439class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001441 inline AccessorInfo(internal::Object** args)
1442 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 inline Local<Value> Data() const;
1444 inline Local<Object> This() const;
1445 inline Local<Object> Holder() const;
1446 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001447 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448};
1449
1450
1451typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1452
1453typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1454
1455/**
1456 * Accessor[Getter|Setter] are used as callback functions when
1457 * setting|getting a particular property. See objectTemplate::SetAccessor.
1458 */
1459typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1460 const AccessorInfo& info);
1461
1462
1463typedef void (*AccessorSetter)(Local<String> property,
1464 Local<Value> value,
1465 const AccessorInfo& info);
1466
1467
1468/**
1469 * NamedProperty[Getter|Setter] are used as interceptors on object.
1470 * See ObjectTemplate::SetNamedPropertyHandler.
1471 */
1472typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1473 const AccessorInfo& info);
1474
1475
1476/**
1477 * Returns the value if the setter intercepts the request.
1478 * Otherwise, returns an empty handle.
1479 */
1480typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1481 Local<Value> value,
1482 const AccessorInfo& info);
1483
1484
1485/**
1486 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001487 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 */
1489typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1490 const AccessorInfo& info);
1491
1492
1493/**
1494 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001495 * The return value is true if the property could be deleted and false
1496 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 */
1498typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1499 const AccessorInfo& info);
1500
1501/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001502 * Returns an array containing the names of the properties the named
1503 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 */
1505typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1506
v8.team.kasperl727e9952008-09-02 14:56:44 +00001507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001509 * Returns the value of the property if the getter intercepts the
1510 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 */
1512typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1513 const AccessorInfo& info);
1514
1515
1516/**
1517 * Returns the value if the setter intercepts the request.
1518 * Otherwise, returns an empty handle.
1519 */
1520typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1521 Local<Value> value,
1522 const AccessorInfo& info);
1523
1524
1525/**
1526 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001527 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 */
1529typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1530 const AccessorInfo& info);
1531
1532/**
1533 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001534 * The return value is true if the property could be deleted and false
1535 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 */
1537typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1538 const AccessorInfo& info);
1539
v8.team.kasperl727e9952008-09-02 14:56:44 +00001540/**
1541 * Returns an array containing the indices of the properties the
1542 * indexed property getter intercepts.
1543 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1545
1546
1547/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001548 * Access control specifications.
1549 *
1550 * Some accessors should be accessible across contexts. These
1551 * accessors have an explicit access control parameter which specifies
1552 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001553 *
1554 * Additionally, for security, accessors can prohibit overwriting by
1555 * accessors defined in JavaScript. For objects that have such
1556 * accessors either locally or in their prototype chain it is not
1557 * possible to overwrite the accessor by using __defineGetter__ or
1558 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 */
1560enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001561 DEFAULT = 0,
1562 ALL_CAN_READ = 1,
1563 ALL_CAN_WRITE = 1 << 1,
1564 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565};
1566
1567
1568/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001569 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 */
1571enum AccessType {
1572 ACCESS_GET,
1573 ACCESS_SET,
1574 ACCESS_HAS,
1575 ACCESS_DELETE,
1576 ACCESS_KEYS
1577};
1578
v8.team.kasperl727e9952008-09-02 14:56:44 +00001579
1580/**
1581 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001582 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001583 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001584typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 Local<Value> key,
1586 AccessType type,
1587 Local<Value> data);
1588
v8.team.kasperl727e9952008-09-02 14:56:44 +00001589
1590/**
1591 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001592 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001593 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001594typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595 uint32_t index,
1596 AccessType type,
1597 Local<Value> data);
1598
1599
1600/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001601 * A FunctionTemplate is used to create functions at runtime. There
1602 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001603 * context. The lifetime of the created function is equal to the
1604 * lifetime of the context. So in case the embedder needs to create
1605 * temporary functions that can be collected using Scripts is
1606 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 *
1608 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001609 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001611 * A FunctionTemplate has a corresponding instance template which is
1612 * used to create object instances when the function is used as a
1613 * constructor. Properties added to the instance template are added to
1614 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615 *
1616 * A FunctionTemplate can have a prototype template. The prototype template
1617 * is used to create the prototype object of the function.
1618 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001619 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001621 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1623 * t->Set("func_property", v8::Number::New(1));
1624 *
1625 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1626 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1627 * proto_t->Set("proto_const", v8::Number::New(2));
1628 *
1629 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1630 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1631 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1632 * instance_t->Set("instance_property", Number::New(3));
1633 *
1634 * v8::Local<v8::Function> function = t->GetFunction();
1635 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001636 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637 *
1638 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001639 * and "instance" for the instance object created above. The function
1640 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001642 * \code
1643 * func_property in function == true;
1644 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001646 * function.prototype.proto_method() invokes 'InvokeCallback'
1647 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001649 * instance instanceof function == true;
1650 * instance.instance_accessor calls 'InstanceAccessorCallback'
1651 * instance.instance_property == 3;
1652 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001654 * A FunctionTemplate can inherit from another one by calling the
1655 * FunctionTemplate::Inherit method. The following graph illustrates
1656 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001658 * \code
1659 * FunctionTemplate Parent -> Parent() . prototype -> { }
1660 * ^ ^
1661 * | Inherit(Parent) | .__proto__
1662 * | |
1663 * FunctionTemplate Child -> Child() . prototype -> { }
1664 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001666 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1667 * object of the Child() function has __proto__ pointing to the
1668 * Parent() function's prototype object. An instance of the Child
1669 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001671 * Let Parent be the FunctionTemplate initialized in the previous
1672 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001674 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 * Local<FunctionTemplate> parent = t;
1676 * Local<FunctionTemplate> child = FunctionTemplate::New();
1677 * child->Inherit(parent);
1678 *
1679 * Local<Function> child_function = child->GetFunction();
1680 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001681 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001683 * The Child function and Child instance will have the following
1684 * properties:
1685 *
1686 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001688 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001690 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001692class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 public:
1694 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001695 static Local<FunctionTemplate> New(
1696 InvocationCallback callback = 0,
1697 Handle<Value> data = Handle<Value>(),
1698 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 /** Returns the unique function instance in the current execution context.*/
1700 Local<Function> GetFunction();
1701
v8.team.kasperl727e9952008-09-02 14:56:44 +00001702 /**
1703 * Set the call-handler callback for a FunctionTemplate. This
1704 * callback is called whenever the function created from this
1705 * FunctionTemplate is called.
1706 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 void SetCallHandler(InvocationCallback callback,
1708 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709
v8.team.kasperl727e9952008-09-02 14:56:44 +00001710 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 Local<ObjectTemplate> InstanceTemplate();
1712
1713 /** Causes the function template to inherit from a parent function template.*/
1714 void Inherit(Handle<FunctionTemplate> parent);
1715
1716 /**
1717 * A PrototypeTemplate is the template used to create the prototype object
1718 * of the function created by this template.
1719 */
1720 Local<ObjectTemplate> PrototypeTemplate();
1721
v8.team.kasperl727e9952008-09-02 14:56:44 +00001722
1723 /**
1724 * Set the class name of the FunctionTemplate. This is used for
1725 * printing objects created with the function created from the
1726 * FunctionTemplate as its constructor.
1727 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 void SetClassName(Handle<String> name);
1729
1730 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001731 * Determines whether the __proto__ accessor ignores instances of
1732 * the function template. If instances of the function template are
1733 * ignored, __proto__ skips all instances and instead returns the
1734 * next object in the prototype chain.
1735 *
1736 * Call with a value of true to make the __proto__ accessor ignore
1737 * instances of the function template. Call with a value of false
1738 * to make the __proto__ accessor not ignore instances of the
1739 * function template. By default, instances of a function template
1740 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 */
1742 void SetHiddenPrototype(bool value);
1743
1744 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001745 * Returns true if the given object is an instance of this function
1746 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 */
1748 bool HasInstance(Handle<Value> object);
1749
1750 private:
1751 FunctionTemplate();
1752 void AddInstancePropertyAccessor(Handle<String> name,
1753 AccessorGetter getter,
1754 AccessorSetter setter,
1755 Handle<Value> data,
1756 AccessControl settings,
1757 PropertyAttribute attributes);
1758 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1759 NamedPropertySetter setter,
1760 NamedPropertyQuery query,
1761 NamedPropertyDeleter remover,
1762 NamedPropertyEnumerator enumerator,
1763 Handle<Value> data);
1764 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1765 IndexedPropertySetter setter,
1766 IndexedPropertyQuery query,
1767 IndexedPropertyDeleter remover,
1768 IndexedPropertyEnumerator enumerator,
1769 Handle<Value> data);
1770 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1771 Handle<Value> data);
1772
1773 friend class Context;
1774 friend class ObjectTemplate;
1775};
1776
1777
1778/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001779 * An ObjectTemplate is used to create objects at runtime.
1780 *
1781 * Properties added to an ObjectTemplate are added to each object
1782 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001784class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001786 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001788
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 /** Creates a new instance of this template.*/
1790 Local<Object> NewInstance();
1791
1792 /**
1793 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001794 *
1795 * Whenever the property with the given name is accessed on objects
1796 * created from this ObjectTemplate the getter and setter callbacks
1797 * are called instead of getting and setting the property directly
1798 * on the JavaScript object.
1799 *
1800 * \param name The name of the property for which an accessor is added.
1801 * \param getter The callback to invoke when getting the property.
1802 * \param setter The callback to invoke when setting the property.
1803 * \param data A piece of data that will be passed to the getter and setter
1804 * callbacks whenever they are invoked.
1805 * \param settings Access control settings for the accessor. This is a bit
1806 * field consisting of one of more of
1807 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1808 * The default is to not allow cross-context access.
1809 * ALL_CAN_READ means that all cross-context reads are allowed.
1810 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1811 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1812 * cross-context access.
1813 * \param attribute The attributes of the property for which an accessor
1814 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 */
1816 void SetAccessor(Handle<String> name,
1817 AccessorGetter getter,
1818 AccessorSetter setter = 0,
1819 Handle<Value> data = Handle<Value>(),
1820 AccessControl settings = DEFAULT,
1821 PropertyAttribute attribute = None);
1822
1823 /**
1824 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001825 *
1826 * Whenever a named property is accessed on objects created from
1827 * this object template, the provided callback is invoked instead of
1828 * accessing the property directly on the JavaScript object.
1829 *
1830 * \param getter The callback to invoke when getting a property.
1831 * \param setter The callback to invoke when setting a property.
1832 * \param query The callback to invoke to check is an object has a property.
1833 * \param deleter The callback to invoke when deleting a property.
1834 * \param enumerator The callback to invoke to enumerate all the named
1835 * properties of an object.
1836 * \param data A piece of data that will be passed to the callbacks
1837 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 */
1839 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1840 NamedPropertySetter setter = 0,
1841 NamedPropertyQuery query = 0,
1842 NamedPropertyDeleter deleter = 0,
1843 NamedPropertyEnumerator enumerator = 0,
1844 Handle<Value> data = Handle<Value>());
1845
1846 /**
1847 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001848 *
1849 * Whenever an indexed property is accessed on objects created from
1850 * this object template, the provided callback is invoked instead of
1851 * accessing the property directly on the JavaScript object.
1852 *
1853 * \param getter The callback to invoke when getting a property.
1854 * \param setter The callback to invoke when setting a property.
1855 * \param query The callback to invoke to check is an object has a property.
1856 * \param deleter The callback to invoke when deleting a property.
1857 * \param enumerator The callback to invoke to enumerate all the indexed
1858 * properties of an object.
1859 * \param data A piece of data that will be passed to the callbacks
1860 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 */
1862 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1863 IndexedPropertySetter setter = 0,
1864 IndexedPropertyQuery query = 0,
1865 IndexedPropertyDeleter deleter = 0,
1866 IndexedPropertyEnumerator enumerator = 0,
1867 Handle<Value> data = Handle<Value>());
1868 /**
1869 * Sets the callback to be used when calling instances created from
1870 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001871 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 * function.
1873 */
1874 void SetCallAsFunctionHandler(InvocationCallback callback,
1875 Handle<Value> data = Handle<Value>());
1876
v8.team.kasperl727e9952008-09-02 14:56:44 +00001877 /**
1878 * Mark object instances of the template as undetectable.
1879 *
1880 * In many ways, undetectable objects behave as though they are not
1881 * there. They behave like 'undefined' in conditionals and when
1882 * printed. However, properties can be accessed and called as on
1883 * normal objects.
1884 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 void MarkAsUndetectable();
1886
v8.team.kasperl727e9952008-09-02 14:56:44 +00001887 /**
1888 * Sets access check callbacks on the object template.
1889 *
1890 * When accessing properties on instances of this object template,
1891 * the access check callback will be called to determine whether or
1892 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001893 * The last parameter specifies whether access checks are turned
1894 * on by default on instances. If access checks are off by default,
1895 * they can be turned on on individual instances by calling
1896 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001897 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1899 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001900 Handle<Value> data = Handle<Value>(),
1901 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902
kasper.lund212ac232008-07-16 07:07:30 +00001903 /**
1904 * Gets the number of internal fields for objects generated from
1905 * this template.
1906 */
1907 int InternalFieldCount();
1908
1909 /**
1910 * Sets the number of internal fields for objects generated from
1911 * this template.
1912 */
1913 void SetInternalFieldCount(int value);
1914
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 private:
1916 ObjectTemplate();
1917 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1918 friend class FunctionTemplate;
1919};
1920
1921
1922/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001923 * A Signature specifies which receivers and arguments a function can
1924 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001926class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 public:
1928 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1929 Handle<FunctionTemplate>(),
1930 int argc = 0,
1931 Handle<FunctionTemplate> argv[] = 0);
1932 private:
1933 Signature();
1934};
1935
1936
1937/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001938 * A utility for determining the type of objects based on the template
1939 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001941class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 public:
1943 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1944 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1945 int match(Handle<Value> value);
1946 private:
1947 TypeSwitch();
1948};
1949
1950
1951// --- E x t e n s i o n s ---
1952
1953
1954/**
1955 * Ignore
1956 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001957class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 public:
1959 Extension(const char* name,
1960 const char* source = 0,
1961 int dep_count = 0,
1962 const char** deps = 0);
1963 virtual ~Extension() { }
1964 virtual v8::Handle<v8::FunctionTemplate>
1965 GetNativeFunction(v8::Handle<v8::String> name) {
1966 return v8::Handle<v8::FunctionTemplate>();
1967 }
1968
1969 const char* name() { return name_; }
1970 const char* source() { return source_; }
1971 int dependency_count() { return dep_count_; }
1972 const char** dependencies() { return deps_; }
1973 void set_auto_enable(bool value) { auto_enable_ = value; }
1974 bool auto_enable() { return auto_enable_; }
1975
1976 private:
1977 const char* name_;
1978 const char* source_;
1979 int dep_count_;
1980 const char** deps_;
1981 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001982
1983 // Disallow copying and assigning.
1984 Extension(const Extension&);
1985 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986};
1987
1988
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001989void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990
1991
1992/**
1993 * Ignore
1994 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001995class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 public:
1997 inline DeclareExtension(Extension* extension) {
1998 RegisterExtension(extension);
1999 }
2000};
2001
2002
2003// --- S t a t i c s ---
2004
2005
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002006Handle<Primitive> V8EXPORT Undefined();
2007Handle<Primitive> V8EXPORT Null();
2008Handle<Boolean> V8EXPORT True();
2009Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
2011
2012/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002013 * A set of constraints that specifies the limits of the runtime's memory use.
2014 * You must set the heap size before initializing the VM - the size cannot be
2015 * adjusted after the VM is initialized.
2016 *
2017 * If you are using threads then you should hold the V8::Locker lock while
2018 * setting the stack limit and you must set a non-default stack limit separately
2019 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002021class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 public:
2023 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002024 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002026 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002028 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002029 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2031 private:
2032 int max_young_space_size_;
2033 int max_old_space_size_;
2034 uint32_t* stack_limit_;
2035};
2036
2037
2038bool SetResourceConstraints(ResourceConstraints* constraints);
2039
2040
2041// --- E x c e p t i o n s ---
2042
2043
2044typedef void (*FatalErrorCallback)(const char* location, const char* message);
2045
2046
2047typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2048
2049
2050/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002051 * Schedules an exception to be thrown when returning to JavaScript. When an
2052 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002054 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002056Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057
2058/**
2059 * Create new error objects by calling the corresponding error object
2060 * constructor with the message.
2061 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002062class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 public:
2064 static Local<Value> RangeError(Handle<String> message);
2065 static Local<Value> ReferenceError(Handle<String> message);
2066 static Local<Value> SyntaxError(Handle<String> message);
2067 static Local<Value> TypeError(Handle<String> message);
2068 static Local<Value> Error(Handle<String> message);
2069};
2070
2071
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002072// --- 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 +00002073
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002074typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002076typedef void* (*CreateHistogramCallback)(const char* name,
2077 int min,
2078 int max,
2079 size_t buckets);
2080
2081typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2082
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083// --- 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 ---
2084typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2085 AccessType type,
2086 Local<Value> data);
2087
2088// --- 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
2089
2090/**
2091 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002092 * before and after a major garbage collection. Allocations are not
2093 * allowed in the callback function, you therefore cannot manipulate
2094 * objects (set or delete properties for example) since it is possible
2095 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 */
2097typedef void (*GCCallback)();
2098
2099
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002100// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101
2102/**
2103 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002104 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106typedef Persistent<Context> (*ContextGenerator)();
2107
2108
2109/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002110 * Profiler modules.
2111 *
2112 * In V8, profiler consists of several modules: CPU profiler, and different
2113 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002114 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2115 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002116 */
2117enum ProfilerModules {
2118 PROFILER_MODULE_NONE = 0,
2119 PROFILER_MODULE_CPU = 1,
2120 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002121 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2122 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002123};
2124
2125
2126/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002127 * Collection of V8 heap information.
2128 *
2129 * Instances of this class can be passed to v8::V8::HeapStatistics to
2130 * get heap statistics from V8.
2131 */
2132class V8EXPORT HeapStatistics {
2133 public:
2134 HeapStatistics();
2135 size_t total_heap_size() { return total_heap_size_; }
2136 size_t used_heap_size() { return used_heap_size_; }
2137
2138 private:
2139 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2140 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2141
2142 size_t total_heap_size_;
2143 size_t used_heap_size_;
2144
2145 friend class V8;
2146};
2147
2148
2149/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 * Container class for static utility functions.
2151 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002152class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002154 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 static void SetFatalErrorHandler(FatalErrorCallback that);
2156
v8.team.kasperl727e9952008-09-02 14:56:44 +00002157 /**
2158 * Ignore out-of-memory exceptions.
2159 *
2160 * V8 running out of memory is treated as a fatal error by default.
2161 * This means that the fatal error handler is called and that V8 is
2162 * terminated.
2163 *
2164 * IgnoreOutOfMemoryException can be used to not treat a
2165 * out-of-memory situation as a fatal error. This way, the contexts
2166 * that did not cause the out of memory problem might be able to
2167 * continue execution.
2168 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 static void IgnoreOutOfMemoryException();
2170
v8.team.kasperl727e9952008-09-02 14:56:44 +00002171 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002172 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002173 * fatal errors such as out-of-memory situations.
2174 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 static bool IsDead();
2176
2177 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 * Adds a message listener.
2179 *
2180 * The same message listener can be added more than once and it that
2181 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 */
2183 static bool AddMessageListener(MessageCallback that,
2184 Handle<Value> data = Handle<Value>());
2185
2186 /**
2187 * Remove all message listeners from the specified callback function.
2188 */
2189 static void RemoveMessageListeners(MessageCallback that);
2190
2191 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002192 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 */
2194 static void SetFlagsFromString(const char* str, int length);
2195
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002196 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002198 */
2199 static void SetFlagsFromCommandLine(int* argc,
2200 char** argv,
2201 bool remove_flags);
2202
kasper.lund7276f142008-07-30 08:49:36 +00002203 /** Get the version string. */
2204 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205
2206 /**
2207 * Enables the host application to provide a mechanism for recording
2208 * statistics counters.
2209 */
2210 static void SetCounterFunction(CounterLookupCallback);
2211
2212 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002213 * Enables the host application to provide a mechanism for recording
2214 * histograms. The CreateHistogram function returns a
2215 * histogram which will later be passed to the AddHistogramSample
2216 * function.
2217 */
2218 static void SetCreateHistogramFunction(CreateHistogramCallback);
2219 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2220
2221 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 * Enables the computation of a sliding window of states. The sliding
2223 * window information is recorded in statistics counters.
2224 */
2225 static void EnableSlidingStateWindow();
2226
2227 /** Callback function for reporting failed access checks.*/
2228 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2229
2230 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002231 * Enables the host application to receive a notification before a
2232 * major garbage colletion. Allocations are not allowed in the
2233 * callback function, you therefore cannot manipulate objects (set
2234 * or delete properties for example) since it is possible such
2235 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 */
2237 static void SetGlobalGCPrologueCallback(GCCallback);
2238
2239 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * Enables the host application to receive a notification after a
2241 * major garbage collection. Allocations are not allowed in the
2242 * callback function, you therefore cannot manipulate objects (set
2243 * or delete properties for example) since it is possible such
2244 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 */
2246 static void SetGlobalGCEpilogueCallback(GCCallback);
2247
2248 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002249 * Allows the host application to group objects together. If one
2250 * object in the group is alive, all objects in the group are alive.
2251 * After each garbage collection, object groups are removed. It is
2252 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002253 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002254 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002256 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257
2258 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002259 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002260 * initialize from scratch. This function is called implicitly if
2261 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 */
2263 static bool Initialize();
2264
kasper.lund7276f142008-07-30 08:49:36 +00002265 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002266 * Adjusts the amount of registered external memory. Used to give
2267 * V8 an indication of the amount of externally allocated memory
2268 * that is kept alive by JavaScript objects. V8 uses this to decide
2269 * when to perform global garbage collections. Registering
2270 * externally allocated memory will trigger global garbage
2271 * collections more often than otherwise in an attempt to garbage
2272 * collect the JavaScript objects keeping the externally allocated
2273 * memory alive.
2274 *
2275 * \param change_in_bytes the change in externally allocated memory
2276 * that is kept alive by JavaScript objects.
2277 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002278 */
2279 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2280
iposva@chromium.org245aa852009-02-10 00:49:54 +00002281 /**
2282 * Suspends recording of tick samples in the profiler.
2283 * When the V8 profiling mode is enabled (usually via command line
2284 * switches) this function suspends recording of tick samples.
2285 * Profiling ticks are discarded until ResumeProfiler() is called.
2286 *
2287 * See also the --prof and --prof_auto command line switches to
2288 * enable V8 profiling.
2289 */
2290 static void PauseProfiler();
2291
2292 /**
2293 * Resumes recording of tick samples in the profiler.
2294 * See also PauseProfiler().
2295 */
2296 static void ResumeProfiler();
2297
ager@chromium.org41826e72009-03-30 13:30:57 +00002298 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002299 * Return whether profiler is currently paused.
2300 */
2301 static bool IsProfilerPaused();
2302
2303 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002304 * Resumes specified profiler modules.
2305 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2306 * See ProfilerModules enum.
2307 *
2308 * \param flags Flags specifying profiler modules.
2309 */
2310 static void ResumeProfilerEx(int flags);
2311
2312 /**
2313 * Pauses specified profiler modules.
2314 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2315 * See ProfilerModules enum.
2316 *
2317 * \param flags Flags specifying profiler modules.
2318 */
2319 static void PauseProfilerEx(int flags);
2320
2321 /**
2322 * Returns active (resumed) profiler modules.
2323 * See ProfilerModules enum.
2324 *
2325 * \returns active profiler modules.
2326 */
2327 static int GetActiveProfilerModules();
2328
2329 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002330 * If logging is performed into a memory buffer (via --logfile=*), allows to
2331 * retrieve previously written messages. This can be used for retrieving
2332 * profiler log data in the application. This function is thread-safe.
2333 *
2334 * Caller provides a destination buffer that must exist during GetLogLines
2335 * call. Only whole log lines are copied into the buffer.
2336 *
2337 * \param from_pos specified a point in a buffer to read from, 0 is the
2338 * beginning of a buffer. It is assumed that caller updates its current
2339 * position using returned size value from the previous call.
2340 * \param dest_buf destination buffer for log data.
2341 * \param max_size size of the destination buffer.
2342 * \returns actual size of log data copied into buffer.
2343 */
2344 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2345
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002346 /**
2347 * Retrieve the V8 thread id of the calling thread.
2348 *
2349 * The thread id for a thread should only be retrieved after the V8
2350 * lock has been acquired with a Locker object with that thread.
2351 */
2352 static int GetCurrentThreadId();
2353
2354 /**
2355 * Forcefully terminate execution of a JavaScript thread. This can
2356 * be used to terminate long-running scripts.
2357 *
2358 * TerminateExecution should only be called when then V8 lock has
2359 * been acquired with a Locker object. Therefore, in order to be
2360 * able to terminate long-running threads, preemption must be
2361 * enabled to allow the user of TerminateExecution to acquire the
2362 * lock.
2363 *
2364 * The termination is achieved by throwing an exception that is
2365 * uncatchable by JavaScript exception handlers. Termination
2366 * exceptions act as if they were caught by a C++ TryCatch exception
2367 * handlers. If forceful termination is used, any C++ TryCatch
2368 * exception handler that catches an exception should check if that
2369 * exception is a termination exception and immediately return if
2370 * that is the case. Returning immediately in that case will
2371 * continue the propagation of the termination exception if needed.
2372 *
2373 * The thread id passed to TerminateExecution must have been
2374 * obtained by calling GetCurrentThreadId on the thread in question.
2375 *
2376 * \param thread_id The thread id of the thread to terminate.
2377 */
2378 static void TerminateExecution(int thread_id);
2379
2380 /**
2381 * Forcefully terminate the current thread of JavaScript execution.
2382 *
2383 * This method can be used by any thread even if that thread has not
2384 * acquired the V8 lock with a Locker object.
2385 */
2386 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002387
2388 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002389 * Releases any resources used by v8 and stops any utility threads
2390 * that may be running. Note that disposing v8 is permanent, it
2391 * cannot be reinitialized.
2392 *
2393 * It should generally not be necessary to dispose v8 before exiting
2394 * a process, this should happen automatically. It is only necessary
2395 * to use if the process needs the resources taken up by v8.
2396 */
2397 static bool Dispose();
2398
ager@chromium.org3811b432009-10-28 14:53:37 +00002399 /**
2400 * Get statistics about the heap memory usage.
2401 */
2402 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002403
2404 /**
2405 * Optional notification that the embedder is idle.
2406 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002407 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002408 * Returns true if the embedder should stop calling IdleNotification
2409 * until real work has been done. This indicates that V8 has done
2410 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002411 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002412 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002413
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002414 /**
2415 * Optional notification that the system is running low on memory.
2416 * V8 uses these notifications to attempt to free memory.
2417 */
2418 static void LowMemoryNotification();
2419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002420 private:
2421 V8();
2422
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002423 static internal::Object** GlobalizeReference(internal::Object** handle);
2424 static void DisposeGlobal(internal::Object** global_handle);
2425 static void MakeWeak(internal::Object** global_handle,
2426 void* data,
2427 WeakReferenceCallback);
2428 static void ClearWeak(internal::Object** global_handle);
2429 static bool IsGlobalNearDeath(internal::Object** global_handle);
2430 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431
2432 template <class T> friend class Handle;
2433 template <class T> friend class Local;
2434 template <class T> friend class Persistent;
2435 friend class Context;
2436};
2437
2438
2439/**
2440 * An external exception handler.
2441 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002442class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 public:
2444
2445 /**
2446 * Creates a new try/catch block and registers it with v8.
2447 */
2448 TryCatch();
2449
2450 /**
2451 * Unregisters and deletes this try/catch block.
2452 */
2453 ~TryCatch();
2454
2455 /**
2456 * Returns true if an exception has been caught by this try/catch block.
2457 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002458 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459
2460 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002461 * For certain types of exceptions, it makes no sense to continue
2462 * execution.
2463 *
2464 * Currently, the only type of exception that can be caught by a
2465 * TryCatch handler and for which it does not make sense to continue
2466 * is termination exception. Such exceptions are thrown when the
2467 * TerminateExecution methods are called to terminate a long-running
2468 * script.
2469 *
2470 * If CanContinue returns false, the correct action is to perform
2471 * any C++ cleanup needed and then return.
2472 */
2473 bool CanContinue() const;
2474
2475 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002476 * Throws the exception caught by this TryCatch in a way that avoids
2477 * it being caught again by this same TryCatch. As with ThrowException
2478 * it is illegal to execute any JavaScript operations after calling
2479 * ReThrow; the caller must return immediately to where the exception
2480 * is caught.
2481 */
2482 Handle<Value> ReThrow();
2483
2484 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 * Returns the exception caught by this try/catch block. If no exception has
2486 * been caught an empty handle is returned.
2487 *
2488 * The returned handle is valid until this TryCatch block has been destroyed.
2489 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002490 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491
2492 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002493 * Returns the .stack property of the thrown object. If no .stack
2494 * property is present an empty handle is returned.
2495 */
2496 Local<Value> StackTrace() const;
2497
2498 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002499 * Returns the message associated with this exception. If there is
2500 * no message associated an empty handle is returned.
2501 *
2502 * The returned handle is valid until this TryCatch block has been
2503 * destroyed.
2504 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002505 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002506
2507 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 * Clears any exceptions that may have been caught by this try/catch block.
2509 * After this method has been called, HasCaught() will return false.
2510 *
2511 * It is not necessary to clear a try/catch block before using it again; if
2512 * another exception is thrown the previously caught exception will just be
2513 * overwritten. However, it is often a good idea since it makes it easier
2514 * to determine which operation threw a given exception.
2515 */
2516 void Reset();
2517
v8.team.kasperl727e9952008-09-02 14:56:44 +00002518 /**
2519 * Set verbosity of the external exception handler.
2520 *
2521 * By default, exceptions that are caught by an external exception
2522 * handler are not reported. Call SetVerbose with true on an
2523 * external exception handler to have exceptions caught by the
2524 * handler reported as if they were not caught.
2525 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526 void SetVerbose(bool value);
2527
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002528 /**
2529 * Set whether or not this TryCatch should capture a Message object
2530 * which holds source information about where the exception
2531 * occurred. True by default.
2532 */
2533 void SetCaptureMessage(bool value);
2534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 public:
2536 TryCatch* next_;
2537 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002538 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002539 bool is_verbose_ : 1;
2540 bool can_continue_ : 1;
2541 bool capture_message_ : 1;
2542 bool rethrow_ : 1;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002543 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544};
2545
2546
2547// --- C o n t e x t ---
2548
2549
2550/**
2551 * Ignore
2552 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002553class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554 public:
2555 ExtensionConfiguration(int name_count, const char* names[])
2556 : name_count_(name_count), names_(names) { }
2557 private:
2558 friend class ImplementationUtilities;
2559 int name_count_;
2560 const char** names_;
2561};
2562
2563
2564/**
2565 * A sandboxed execution context with its own set of built-in objects
2566 * and functions.
2567 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002568class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002569 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002570 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 Local<Object> Global();
2572
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002573 /**
2574 * Detaches the global object from its context before
2575 * the global object can be reused to create a new context.
2576 */
2577 void DetachGlobal();
2578
v8.team.kasperl727e9952008-09-02 14:56:44 +00002579 /** Creates a new context. */
2580 static Persistent<Context> New(
2581 ExtensionConfiguration* extensions = 0,
2582 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2583 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002584
kasper.lund44510672008-07-25 07:37:58 +00002585 /** Returns the last entered context. */
2586 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587
kasper.lund44510672008-07-25 07:37:58 +00002588 /** Returns the context that is on the top of the stack. */
2589 static Local<Context> GetCurrent();
2590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002592 * Returns the context of the calling JavaScript code. That is the
2593 * context of the top-most JavaScript frame. If there are no
2594 * JavaScript frames an empty handle is returned.
2595 */
2596 static Local<Context> GetCalling();
2597
2598 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599 * Sets the security token for the context. To access an object in
2600 * another context, the security tokens must match.
2601 */
2602 void SetSecurityToken(Handle<Value> token);
2603
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002604 /** Restores the security token to the default value. */
2605 void UseDefaultSecurityToken();
2606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607 /** Returns the security token of this context.*/
2608 Handle<Value> GetSecurityToken();
2609
v8.team.kasperl727e9952008-09-02 14:56:44 +00002610 /**
2611 * Enter this context. After entering a context, all code compiled
2612 * and run is compiled and run in this context. If another context
2613 * is already entered, this old context is saved so it can be
2614 * restored when the new context is exited.
2615 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002617
2618 /**
2619 * Exit this context. Exiting the current context restores the
2620 * context that was in place when entering the current context.
2621 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622 void Exit();
2623
v8.team.kasperl727e9952008-09-02 14:56:44 +00002624 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 bool HasOutOfMemoryException();
2626
v8.team.kasperl727e9952008-09-02 14:56:44 +00002627 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 static bool InContext();
2629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002631 * Associate an additional data object with the context. This is mainly used
2632 * with the debugger to provide additional information on the context through
2633 * the debugger API.
2634 */
2635 void SetData(Handle<Value> data);
2636 Local<Value> GetData();
2637
2638 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002639 * Stack-allocated class which sets the execution context for all
2640 * operations executed within a local scope.
2641 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002642 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643 public:
2644 inline Scope(Handle<Context> context) : context_(context) {
2645 context_->Enter();
2646 }
2647 inline ~Scope() { context_->Exit(); }
2648 private:
2649 Handle<Context> context_;
2650 };
2651
2652 private:
2653 friend class Value;
2654 friend class Script;
2655 friend class Object;
2656 friend class Function;
2657};
2658
2659
2660/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002661 * Multiple threads in V8 are allowed, but only one thread at a time
2662 * is allowed to use V8. The definition of 'using V8' includes
2663 * accessing handles or holding onto object pointers obtained from V8
2664 * handles. It is up to the user of V8 to ensure (perhaps with
2665 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 *
2667 * If you wish to start using V8 in a thread you can do this by constructing
2668 * a v8::Locker object. After the code using V8 has completed for the
2669 * current thread you can call the destructor. This can be combined
2670 * with C++ scope-based construction as follows:
2671 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002672 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 * ...
2674 * {
2675 * v8::Locker locker;
2676 * ...
2677 * // Code using V8 goes here.
2678 * ...
2679 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002680 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 *
2682 * If you wish to stop using V8 in a thread A you can do this by either
2683 * by destroying the v8::Locker object as above or by constructing a
2684 * v8::Unlocker object:
2685 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002686 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 * {
2688 * v8::Unlocker unlocker;
2689 * ...
2690 * // Code not using V8 goes here while V8 can run in another thread.
2691 * ...
2692 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002693 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 *
2695 * The Unlocker object is intended for use in a long-running callback
2696 * from V8, where you want to release the V8 lock for other threads to
2697 * use.
2698 *
2699 * The v8::Locker is a recursive lock. That is, you can lock more than
2700 * once in a given thread. This can be useful if you have code that can
2701 * be called either from code that holds the lock or from code that does
2702 * not. The Unlocker is not recursive so you can not have several
2703 * Unlockers on the stack at once, and you can not use an Unlocker in a
2704 * thread that is not inside a Locker's scope.
2705 *
2706 * An unlocker will unlock several lockers if it has to and reinstate
2707 * the correct depth of locking on its destruction. eg.:
2708 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002709 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 * // V8 not locked.
2711 * {
2712 * v8::Locker locker;
2713 * // V8 locked.
2714 * {
2715 * v8::Locker another_locker;
2716 * // V8 still locked (2 levels).
2717 * {
2718 * v8::Unlocker unlocker;
2719 * // V8 not locked.
2720 * }
2721 * // V8 locked again (2 levels).
2722 * }
2723 * // V8 still locked (1 level).
2724 * }
2725 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002726 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002728class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 public:
2730 Unlocker();
2731 ~Unlocker();
2732};
2733
2734
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002735class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002736 public:
2737 Locker();
2738 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002739
2740 /**
2741 * Start preemption.
2742 *
2743 * When preemption is started, a timer is fired every n milli seconds
2744 * that will switch between multiple threads that are in contention
2745 * for the V8 lock.
2746 */
2747 static void StartPreemption(int every_n_ms);
2748
2749 /**
2750 * Stop preemption.
2751 */
2752 static void StopPreemption();
2753
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002754 /**
2755 * Returns whether or not the locker is locked by the current thread.
2756 */
2757 static bool IsLocked();
2758
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002759 /**
2760 * Returns whether v8::Locker is being used by this V8 instance.
2761 */
2762 static bool IsActive() { return active_; }
2763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764 private:
2765 bool has_lock_;
2766 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002767
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002768 static bool active_;
2769
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002770 // Disallow copying and assigning.
2771 Locker(const Locker&);
2772 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773};
2774
2775
2776
2777// --- I m p l e m e n t a t i o n ---
2778
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002779
2780namespace internal {
2781
2782
2783// Tag information for HeapObject.
2784const int kHeapObjectTag = 1;
2785const int kHeapObjectTagSize = 2;
2786const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2787
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002788// Tag information for Smi.
2789const int kSmiTag = 0;
2790const int kSmiTagSize = 1;
2791const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2792
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002793template <size_t ptr_size> struct SmiConstants;
2794
2795// Smi constants for 32-bit systems.
2796template <> struct SmiConstants<4> {
2797 static const int kSmiShiftSize = 0;
2798 static const int kSmiValueSize = 31;
2799 static inline int SmiToInt(internal::Object* value) {
2800 int shift_bits = kSmiTagSize + kSmiShiftSize;
2801 // Throw away top 32 bits and shift down (requires >> to be sign extending).
2802 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
2803 }
2804};
2805
2806// Smi constants for 64-bit systems.
2807template <> struct SmiConstants<8> {
2808 static const int kSmiShiftSize = 31;
2809 static const int kSmiValueSize = 32;
2810 static inline int SmiToInt(internal::Object* value) {
2811 int shift_bits = kSmiTagSize + kSmiShiftSize;
2812 // Shift down and throw away top 32 bits.
2813 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
2814 }
2815};
2816
2817const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
2818const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002819
2820/**
2821 * This class exports constants and functionality from within v8 that
2822 * is necessary to implement inline functions in the v8 api. Don't
2823 * depend on functions and constants defined here.
2824 */
2825class Internals {
2826 public:
2827
2828 // These values match non-compiler-dependent values defined within
2829 // the implementation of v8.
2830 static const int kHeapObjectMapOffset = 0;
2831 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
2832 static const int kStringResourceOffset = 2 * sizeof(void*);
2833 static const int kProxyProxyOffset = sizeof(void*);
2834 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2835 static const int kFullStringRepresentationMask = 0x07;
2836 static const int kExternalTwoByteRepresentationTag = 0x03;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002837
2838 // These constants are compiler dependent so their values must be
2839 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002840 V8EXPORT static int kJSObjectType;
2841 V8EXPORT static int kFirstNonstringType;
2842 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002843
2844 static inline bool HasHeapObjectTag(internal::Object* value) {
2845 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2846 kHeapObjectTag);
2847 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002848
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002849 static inline bool HasSmiTag(internal::Object* value) {
2850 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2851 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002852
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002853 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002854 return SmiConstants<sizeof(void*)>::SmiToInt(value);
2855 }
2856
2857 static inline int GetInstanceType(internal::Object* obj) {
2858 typedef internal::Object O;
2859 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
2860 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
2861 }
2862
2863 static inline void* GetExternalPointer(internal::Object* obj) {
2864 if (HasSmiTag(obj)) {
2865 return obj;
2866 } else if (GetInstanceType(obj) == kProxyType) {
2867 return ReadField<void*>(obj, kProxyProxyOffset);
2868 } else {
2869 return NULL;
2870 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002871 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002872
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002873 static inline bool IsExternalTwoByteString(int instance_type) {
2874 int representation = (instance_type & kFullStringRepresentationMask);
2875 return representation == kExternalTwoByteRepresentationTag;
2876 }
2877
2878 template <typename T>
2879 static inline T ReadField(Object* ptr, int offset) {
2880 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2881 return *reinterpret_cast<T*>(addr);
2882 }
2883
2884};
2885
2886}
2887
2888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889template <class T>
2890Handle<T>::Handle() : val_(0) { }
2891
2892
2893template <class T>
2894Local<T>::Local() : Handle<T>() { }
2895
2896
2897template <class T>
2898Local<T> Local<T>::New(Handle<T> that) {
2899 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002900 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2902}
2903
2904
2905template <class T>
2906Persistent<T> Persistent<T>::New(Handle<T> that) {
2907 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002908 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2910}
2911
2912
2913template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002914bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002916 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917}
2918
2919
2920template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002921bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002923 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002924}
2925
2926
2927template <class T>
2928void Persistent<T>::Dispose() {
2929 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002930 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931}
2932
2933
2934template <class T>
2935Persistent<T>::Persistent() : Handle<T>() { }
2936
2937template <class T>
2938void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002939 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
2940 parameters,
2941 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002942}
2943
2944template <class T>
2945void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002946 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947}
2948
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949Local<Value> Arguments::operator[](int i) const {
2950 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2951 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2952}
2953
2954
2955Local<Function> Arguments::Callee() const {
2956 return callee_;
2957}
2958
2959
2960Local<Object> Arguments::This() const {
2961 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2962}
2963
2964
2965Local<Object> Arguments::Holder() const {
2966 return holder_;
2967}
2968
2969
2970Local<Value> Arguments::Data() const {
2971 return data_;
2972}
2973
2974
2975bool Arguments::IsConstructCall() const {
2976 return is_construct_call_;
2977}
2978
2979
2980int Arguments::Length() const {
2981 return length_;
2982}
2983
2984
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985template <class T>
2986Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002987 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
2988 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989 return Local<T>(reinterpret_cast<T*>(after));
2990}
2991
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002992Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 return resource_name_;
2994}
2995
2996
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002997Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002998 return resource_line_offset_;
2999}
3000
3001
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003002Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 return resource_column_offset_;
3004}
3005
3006
3007Handle<Boolean> Boolean::New(bool value) {
3008 return value ? True() : False();
3009}
3010
3011
3012void Template::Set(const char* name, v8::Handle<Data> value) {
3013 Set(v8::String::New(name), value);
3014}
3015
3016
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003017Local<Value> Object::GetInternalField(int index) {
3018#ifndef V8_ENABLE_CHECKS
3019 Local<Value> quick_result = UncheckedGetInternalField(index);
3020 if (!quick_result.IsEmpty()) return quick_result;
3021#endif
3022 return CheckedGetInternalField(index);
3023}
3024
3025
3026Local<Value> Object::UncheckedGetInternalField(int index) {
3027 typedef internal::Object O;
3028 typedef internal::Internals I;
3029 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003030 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003031 // If the object is a plain JSObject, which is the common case,
3032 // we know where to find the internal fields and can return the
3033 // value directly.
3034 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3035 O* value = I::ReadField<O*>(obj, offset);
3036 O** result = HandleScope::CreateHandle(value);
3037 return Local<Value>(reinterpret_cast<Value*>(result));
3038 } else {
3039 return Local<Value>();
3040 }
3041}
3042
3043
3044void* External::Unwrap(Handle<v8::Value> obj) {
3045#ifdef V8_ENABLE_CHECKS
3046 return FullUnwrap(obj);
3047#else
3048 return QuickUnwrap(obj);
3049#endif
3050}
3051
3052
3053void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3054 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003055 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003056 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003057}
3058
3059
3060void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003061 typedef internal::Object O;
3062 typedef internal::Internals I;
3063
3064 O* obj = *reinterpret_cast<O**>(this);
3065
3066 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3067 // If the object is a plain JSObject, which is the common case,
3068 // we know where to find the internal fields and can return the
3069 // value directly.
3070 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3071 O* value = I::ReadField<O*>(obj, offset);
3072 return I::GetExternalPointer(value);
3073 }
3074
3075 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003076}
3077
3078
3079String* String::Cast(v8::Value* value) {
3080#ifdef V8_ENABLE_CHECKS
3081 CheckCast(value);
3082#endif
3083 return static_cast<String*>(value);
3084}
3085
3086
3087String::ExternalStringResource* String::GetExternalStringResource() const {
3088 typedef internal::Object O;
3089 typedef internal::Internals I;
3090 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003091 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003092 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003093 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3094 result = reinterpret_cast<String::ExternalStringResource*>(value);
3095 } else {
3096 result = NULL;
3097 }
3098#ifdef V8_ENABLE_CHECKS
3099 VerifyExternalStringResource(result);
3100#endif
3101 return result;
3102}
3103
3104
3105bool Value::IsString() const {
3106#ifdef V8_ENABLE_CHECKS
3107 return FullIsString();
3108#else
3109 return QuickIsString();
3110#endif
3111}
3112
3113bool Value::QuickIsString() const {
3114 typedef internal::Object O;
3115 typedef internal::Internals I;
3116 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3117 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003118 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003119}
3120
3121
3122Number* Number::Cast(v8::Value* value) {
3123#ifdef V8_ENABLE_CHECKS
3124 CheckCast(value);
3125#endif
3126 return static_cast<Number*>(value);
3127}
3128
3129
3130Integer* Integer::Cast(v8::Value* value) {
3131#ifdef V8_ENABLE_CHECKS
3132 CheckCast(value);
3133#endif
3134 return static_cast<Integer*>(value);
3135}
3136
3137
3138Date* Date::Cast(v8::Value* value) {
3139#ifdef V8_ENABLE_CHECKS
3140 CheckCast(value);
3141#endif
3142 return static_cast<Date*>(value);
3143}
3144
3145
3146Object* Object::Cast(v8::Value* value) {
3147#ifdef V8_ENABLE_CHECKS
3148 CheckCast(value);
3149#endif
3150 return static_cast<Object*>(value);
3151}
3152
3153
3154Array* Array::Cast(v8::Value* value) {
3155#ifdef V8_ENABLE_CHECKS
3156 CheckCast(value);
3157#endif
3158 return static_cast<Array*>(value);
3159}
3160
3161
3162Function* Function::Cast(v8::Value* value) {
3163#ifdef V8_ENABLE_CHECKS
3164 CheckCast(value);
3165#endif
3166 return static_cast<Function*>(value);
3167}
3168
3169
3170External* External::Cast(v8::Value* value) {
3171#ifdef V8_ENABLE_CHECKS
3172 CheckCast(value);
3173#endif
3174 return static_cast<External*>(value);
3175}
3176
3177
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003178Local<Value> AccessorInfo::Data() const {
3179 return Local<Value>(reinterpret_cast<Value*>(&args_[-3]));
3180}
3181
3182
3183Local<Object> AccessorInfo::This() const {
3184 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3185}
3186
3187
3188Local<Object> AccessorInfo::Holder() const {
3189 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3190}
3191
3192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003193/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003194 * \example shell.cc
3195 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003196 * command-line and executes them.
3197 */
3198
3199
3200/**
3201 * \example process.cc
3202 */
3203
3204
3205} // namespace v8
3206
3207
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003208#undef V8EXPORT
3209#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003210#undef TYPE_CHECK
3211
3212
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003213#endif // V8_H_