blob: 2e30992eddddf7ef471d3679c545d021467d2719 [file] [log] [blame]
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001// Copyright 2007-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41#include <stdio.h>
42
43#ifdef _WIN32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
ager@chromium.org5ec48922009-05-05 07:25:34 +000048typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052typedef int int32_t;
53typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000054typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058
59// Setup for Windows DLL export/import. When building the V8 DLL the
60// BUILDING_V8_SHARED needs to be defined. When building a program which uses
61// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
62// static library or building a program which uses the V8 static library neither
63// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000064// The reason for having both V8EXPORT and V8EXPORT_INLINE is that classes which
65// have their code inside this header file need to have __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066// when building the DLL but cannot have __declspec(dllimport) when building
67// a program which uses the DLL.
68#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
69#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
70 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071#endif
72
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT __declspec(dllexport)
75#define V8EXPORT_INLINE __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT __declspec(dllimport)
78#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000080#define V8EXPORT
81#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#endif // BUILDING_V8_SHARED
83
84#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000085
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086#include <stdint.h>
87
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000088// Setup for Linux shared library export. There is no need to distinguish
89// between building or using the V8 shared library, but we should not
90// export symbols when we are building a static library.
91#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000092#define V8EXPORT __attribute__ ((visibility("default")))
93#define V8EXPORT_INLINE __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000094#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095#define V8EXPORT
96#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000097#endif // defined(__GNUC__) && (__GNUC__ >= 4)
98
99#endif // _WIN32
100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000102 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 */
104namespace v8 {
105
106class Context;
107class String;
108class Value;
109class Utils;
110class Number;
111class Object;
112class Array;
113class Int32;
114class Uint32;
115class External;
116class Primitive;
117class Boolean;
118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
123template <class T> class Handle;
124template <class T> class Local;
125template <class T> class Persistent;
126class FunctionTemplate;
127class ObjectTemplate;
128class Data;
129
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000130namespace internal {
131
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000132class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000133class Object;
134class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000135
136}
137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139// --- W e a k H a n d l e s
140
141
142/**
143 * A weak reference callback function.
144 *
145 * \param object the weak global object to be reclaimed by the garbage collector
146 * \param parameter the value passed in when making the weak global object
147 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000148typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 void* parameter);
150
151
152// --- H a n d l e s ---
153
154#define TYPE_CHECK(T, S) \
155 while (false) { \
156 *(static_cast<T**>(0)) = static_cast<S*>(0); \
157 }
158
159/**
160 * An object reference managed by the v8 garbage collector.
161 *
162 * All objects returned from v8 have to be tracked by the garbage
163 * collector so that it knows that the objects are still alive. Also,
164 * because the garbage collector may move objects, it is unsafe to
165 * point directly to an object. Instead, all objects are stored in
166 * handles which are known by the garbage collector and updated
167 * whenever an object moves. Handles should always be passed by value
168 * (except in cases like out-parameters) and they should never be
169 * allocated on the heap.
170 *
171 * There are two types of handles: local and persistent handles.
172 * Local handles are light-weight and transient and typically used in
173 * local operations. They are managed by HandleScopes. Persistent
174 * handles can be used when storing objects across several independent
175 * operations and have to be explicitly deallocated when they're no
176 * longer used.
177 *
178 * It is safe to extract the object stored in the handle by
179 * dereferencing the handle (for instance, to extract the Object* from
180 * an Handle<Object>); the value will still be governed by a handle
181 * behind the scenes and the same rules apply to these values as to
182 * their handles.
183 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000184template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 public:
186
187 /**
188 * Creates an empty handle.
189 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000190 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
192 /**
193 * Creates a new handle for the specified value.
194 */
195 explicit Handle(T* val) : val_(val) { }
196
197 /**
198 * Creates a handle for the contents of the specified handle. This
199 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000200 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 * incompatible handles, for instance from a Handle<String> to a
202 * Handle<Number> it will cause a compiletime error. Assigning
203 * between compatible handles, for instance assigning a
204 * Handle<String> to a variable declared as Handle<Value>, is legal
205 * because String is a subclass of Value.
206 */
207 template <class S> inline Handle(Handle<S> that)
208 : val_(reinterpret_cast<T*>(*that)) {
209 /**
210 * This check fails when trying to convert between incompatible
211 * handles. For example, converting from a Handle<String> to a
212 * Handle<Number>.
213 */
214 TYPE_CHECK(T, S);
215 }
216
217 /**
218 * Returns true if the handle is empty.
219 */
ager@chromium.org32912102009-01-16 10:38:43 +0000220 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000222 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000224 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
226 /**
227 * Sets the handle to be empty. IsEmpty() will then return true.
228 */
229 void Clear() { this->val_ = 0; }
230
231 /**
232 * Checks whether two handles are the same.
233 * Returns true if both are empty, or if the objects
234 * to which they refer are identical.
235 * The handles' references are not checked.
236 */
ager@chromium.org32912102009-01-16 10:38:43 +0000237 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
239 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 if (a == 0) return b == 0;
241 if (b == 0) return false;
242 return *a == *b;
243 }
244
245 /**
246 * Checks whether two handles are different.
247 * Returns true if only one of the handles is empty, or if
248 * the objects to which they refer are different.
249 * The handles' references are not checked.
250 */
ager@chromium.org32912102009-01-16 10:38:43 +0000251 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 return !operator==(that);
253 }
254
255 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000256#ifdef V8_ENABLE_CHECKS
257 // If we're going to perform the type check then we have to check
258 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000260#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 return Handle<T>(T::Cast(*that));
262 }
263
264 private:
265 T* val_;
266};
267
268
269/**
270 * A light-weight stack-allocated object handle. All operations
271 * that return objects from within v8 return them in local handles. They
272 * are created within HandleScopes, and all local handles allocated within a
273 * handle scope are destroyed when the handle scope is destroyed. Hence it
274 * is not necessary to explicitly deallocate local handles.
275 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000276template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000278 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 template <class S> inline Local(Local<S> that)
280 : Handle<T>(reinterpret_cast<T*>(*that)) {
281 /**
282 * This check fails when trying to convert between incompatible
283 * handles. For example, converting from a Handle<String> to a
284 * Handle<Number>.
285 */
286 TYPE_CHECK(T, S);
287 }
288 template <class S> inline Local(S* that) : Handle<T>(that) { }
289 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000290#ifdef V8_ENABLE_CHECKS
291 // If we're going to perform the type check then we have to check
292 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000294#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 return Local<T>(T::Cast(*that));
296 }
297
298 /** Create a local handle for the content of another handle.
299 * The referee is kept alive by the local handle even when
300 * the original handle is destroyed/disposed.
301 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000302 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303};
304
305
306/**
307 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000308 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 * allocated, a Persistent handle remains valid until it is explicitly
310 * disposed.
311 *
312 * A persistent handle contains a reference to a storage cell within
313 * the v8 engine which holds an object value and which is updated by
314 * the garbage collector whenever the object is moved. A new storage
315 * cell can be created using Persistent::New and existing handles can
316 * be disposed using Persistent::Dispose. Since persistent handles
317 * are passed by value you may have many persistent handle objects
318 * that point to the same storage cell. For instance, if you pass a
319 * persistent handle as an argument to a function you will not get two
320 * different storage cells but rather two references to the same
321 * storage cell.
322 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000323template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 public:
325
326 /**
327 * Creates an empty persistent handle that doesn't point to any
328 * storage cell.
329 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000330 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331
332 /**
333 * Creates a persistent handle for the same storage cell as the
334 * specified handle. This constructor allows you to pass persistent
335 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000336 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000338 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339 * between compatible persistent handles, for instance assigning a
340 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000341 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342 */
343 template <class S> inline Persistent(Persistent<S> that)
344 : Handle<T>(reinterpret_cast<T*>(*that)) {
345 /**
346 * This check fails when trying to convert between incompatible
347 * handles. For example, converting from a Handle<String> to a
348 * Handle<Number>.
349 */
350 TYPE_CHECK(T, S);
351 }
352
353 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
354
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000355 /**
356 * "Casts" a plain handle which is known to be a persistent handle
357 * to a persistent handle.
358 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 template <class S> explicit inline Persistent(Handle<S> that)
360 : Handle<T>(*that) { }
361
362 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000363#ifdef V8_ENABLE_CHECKS
364 // If we're going to perform the type check then we have to check
365 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000367#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 return Persistent<T>(T::Cast(*that));
369 }
370
371 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000372 * Creates a new persistent handle for an existing local or
373 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Releases the storage cell referenced by this persistent handle.
379 * Does not remove the reference to the cell from any handles.
380 * This handle's reference, and any any other references to the storage
381 * cell remain and IsEmpty will still return false.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /**
386 * Make the reference to this object weak. When only weak handles
387 * refer to the object, the garbage collector will perform a
388 * callback to the given V8::WeakReferenceCallback function, passing
389 * it the object reference and the given parameters.
390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
393 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000394 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000395
396 /**
397 *Checks if the handle holds the only reference to an object.
398 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000399 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400
401 /**
402 * Returns true if the handle's reference is weak.
403 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000404 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
406 private:
407 friend class ImplementationUtilities;
408 friend class ObjectTemplate;
409};
410
411
v8.team.kasperl727e9952008-09-02 14:56:44 +0000412 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 * A stack-allocated class that governs a number of local handles.
414 * After a handle scope has been created, all local handles will be
415 * allocated within that handle scope until either the handle scope is
416 * deleted or another handle scope is created. If there is already a
417 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000418 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 * new handles will again be allocated in the original handle scope.
420 *
421 * After the handle scope of a local handle has been deleted the
422 * garbage collector will no longer track the object stored in the
423 * handle and may deallocate it. The behavior of accessing a handle
424 * for which the handle scope has been deleted is undefined.
425 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000426class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000428 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000430 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431
432 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 * Closes the handle scope and returns the value as a handle in the
434 * previous scope, which is the new current scope after the call.
435 */
436 template <class T> Local<T> Close(Handle<T> value);
437
438 /**
439 * Counts the number of allocated handles.
440 */
441 static int NumberOfHandles();
442
443 /**
444 * Creates a new handle with the given value.
445 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000446 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447
448 private:
449 // Make it impossible to create heap-allocated or illegal handle
450 // scopes by disallowing certain operations.
451 HandleScope(const HandleScope&);
452 void operator=(const HandleScope&);
453 void* operator new(size_t size);
454 void operator delete(void*, size_t);
455
ager@chromium.org3811b432009-10-28 14:53:37 +0000456 // This Data class is accessible internally as HandleScopeData through a
457 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000458 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459 public:
460 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000461 internal::Object** next;
462 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463 inline void Initialize() {
464 extensions = -1;
465 next = limit = NULL;
466 }
467 };
468
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000469 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000471 // Allow for the active closing of HandleScopes which allows to pass a handle
472 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000474 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 friend class ImplementationUtilities;
477};
478
479
480// --- S p e c i a l o b j e c t s ---
481
482
483/**
484 * The superclass of values and API object templates.
485 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000486class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487 private:
488 Data();
489};
490
491
492/**
493 * Pre-compilation data that can be associated with a script. This
494 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000495 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000496 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000498class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 public:
500 virtual ~ScriptData() { }
501 static ScriptData* PreCompile(const char* input, int length);
502 static ScriptData* New(unsigned* data, int length);
503
504 virtual int Length() = 0;
505 virtual unsigned* Data() = 0;
506};
507
508
509/**
510 * The origin, within a file, of a script.
511 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000512class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000514 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 Handle<Integer> resource_line_offset = Handle<Integer>(),
516 Handle<Integer> resource_column_offset = Handle<Integer>())
517 : resource_name_(resource_name),
518 resource_line_offset_(resource_line_offset),
519 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000520 inline Handle<Value> ResourceName() const;
521 inline Handle<Integer> ResourceLineOffset() const;
522 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000524 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 Handle<Integer> resource_line_offset_;
526 Handle<Integer> resource_column_offset_;
527};
528
529
530/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000531 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000533class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 public:
535
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000536 /**
537 * Compiles the specified script. The ScriptOrigin* and ScriptData*
538 * parameters are owned by the caller of Script::Compile. No
539 * references to these objects are kept after compilation finishes.
540 *
541 * The script object returned is context independent; when run it
542 * will use the currently entered context.
543 */
544 static Local<Script> New(Handle<String> source,
545 ScriptOrigin* origin = NULL,
546 ScriptData* pre_data = NULL);
547
548 /**
549 * Compiles the specified script using the specified file name
550 * object (typically a string) as the script's origin.
551 *
552 * The script object returned is context independent; when run it
553 * will use the currently entered context.
554 */
555 static Local<Script> New(Handle<String> source,
556 Handle<Value> file_name);
557
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 /**
559 * Compiles the specified script. The ScriptOrigin* and ScriptData*
560 * parameters are owned by the caller of Script::Compile. No
561 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000562 *
563 * The script object returned is bound to the context that was active
564 * when this function was called. When run it will always use this
565 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 */
567 static Local<Script> Compile(Handle<String> source,
568 ScriptOrigin* origin = NULL,
569 ScriptData* pre_data = NULL);
570
mads.s.agercbaa0602008-08-14 13:41:48 +0000571 /**
572 * Compiles the specified script using the specified file name
573 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000574 *
575 * The script object returned is bound to the context that was active
576 * when this function was called. When run it will always use this
577 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000578 */
579 static Local<Script> Compile(Handle<String> source,
580 Handle<Value> file_name);
581
v8.team.kasperl727e9952008-09-02 14:56:44 +0000582 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000583 * Runs the script returning the resulting value. If the script is
584 * context independent (created using ::New) it will be run in the
585 * currently entered context. If it is context specific (created
586 * using ::Compile) it will be run in the context in which it was
587 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000588 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000590
591 /**
592 * Returns the script id value.
593 */
594 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000595
596 /**
597 * Associate an additional data object with the script. This is mainly used
598 * with the debugger as this data object is only available through the
599 * debugger API.
600 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000601 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602};
603
604
605/**
606 * An error message.
607 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000608class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000610 Local<String> Get() const;
611 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000613 /**
614 * Returns the resource name for the script from where the function causing
615 * the error originates.
616 */
ager@chromium.org32912102009-01-16 10:38:43 +0000617 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000619 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000620 * Returns the resource data for the script from where the function causing
621 * the error originates.
622 */
623 Handle<Value> GetScriptData() const;
624
625 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000626 * Returns the number, 1-based, of the line where the error occurred.
627 */
ager@chromium.org32912102009-01-16 10:38:43 +0000628 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000630 /**
631 * Returns the index within the script of the first character where
632 * the error occurred.
633 */
ager@chromium.org32912102009-01-16 10:38:43 +0000634 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000635
636 /**
637 * Returns the index within the script of the last character where
638 * the error occurred.
639 */
ager@chromium.org32912102009-01-16 10:38:43 +0000640 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000641
642 /**
643 * Returns the index within the line of the first character where
644 * the error occurred.
645 */
ager@chromium.org32912102009-01-16 10:38:43 +0000646 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000647
648 /**
649 * Returns the index within the line of the last character where
650 * the error occurred.
651 */
ager@chromium.org32912102009-01-16 10:38:43 +0000652 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 // TODO(1245381): Print to a string instead of on a FILE.
655 static void PrintCurrentStackTrace(FILE* out);
656};
657
658
659// --- V a l u e ---
660
661
662/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000663 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000665class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 public:
667
668 /**
669 * Returns true if this value is the undefined value. See ECMA-262
670 * 4.3.10.
671 */
ager@chromium.org32912102009-01-16 10:38:43 +0000672 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
674 /**
675 * Returns true if this value is the null value. See ECMA-262
676 * 4.3.11.
677 */
ager@chromium.org32912102009-01-16 10:38:43 +0000678 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679
680 /**
681 * Returns true if this value is true.
682 */
ager@chromium.org32912102009-01-16 10:38:43 +0000683 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000684
685 /**
686 * Returns true if this value is false.
687 */
ager@chromium.org32912102009-01-16 10:38:43 +0000688 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
690 /**
691 * Returns true if this value is an instance of the String type.
692 * See ECMA-262 8.4.
693 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000694 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
696 /**
697 * Returns true if this value is a function.
698 */
ager@chromium.org32912102009-01-16 10:38:43 +0000699 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
701 /**
702 * Returns true if this value is an array.
703 */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
v8.team.kasperl727e9952008-09-02 14:56:44 +0000706 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707 * Returns true if this value is an object.
708 */
ager@chromium.org32912102009-01-16 10:38:43 +0000709 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
v8.team.kasperl727e9952008-09-02 14:56:44 +0000711 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712 * Returns true if this value is boolean.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000715
v8.team.kasperl727e9952008-09-02 14:56:44 +0000716 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 * Returns true if this value is a number.
718 */
ager@chromium.org32912102009-01-16 10:38:43 +0000719 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
v8.team.kasperl727e9952008-09-02 14:56:44 +0000721 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 * Returns true if this value is external.
723 */
ager@chromium.org32912102009-01-16 10:38:43 +0000724 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
v8.team.kasperl727e9952008-09-02 14:56:44 +0000726 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 * Returns true if this value is a 32-bit signed integer.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000731 /**
732 * Returns true if this value is a Date.
733 */
ager@chromium.org32912102009-01-16 10:38:43 +0000734 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000735
ager@chromium.org32912102009-01-16 10:38:43 +0000736 Local<Boolean> ToBoolean() const;
737 Local<Number> ToNumber() const;
738 Local<String> ToString() const;
739 Local<String> ToDetailString() const;
740 Local<Object> ToObject() const;
741 Local<Integer> ToInteger() const;
742 Local<Uint32> ToUint32() const;
743 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744
745 /**
746 * Attempts to convert a string to an array index.
747 * Returns an empty handle if the conversion fails.
748 */
ager@chromium.org32912102009-01-16 10:38:43 +0000749 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750
ager@chromium.org32912102009-01-16 10:38:43 +0000751 bool BooleanValue() const;
752 double NumberValue() const;
753 int64_t IntegerValue() const;
754 uint32_t Uint32Value() const;
755 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
757 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000758 bool Equals(Handle<Value> that) const;
759 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000760
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000761 private:
762 inline bool QuickIsString() const;
763 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764};
765
766
767/**
768 * The superclass of primitive values. See ECMA-262 4.3.2.
769 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000770class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771
772
773/**
774 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
775 * or false value.
776 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000777class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000779 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780 static inline Handle<Boolean> New(bool value);
781};
782
783
784/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000785 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000787class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000789
790 /**
791 * Returns the number of characters in this string.
792 */
ager@chromium.org32912102009-01-16 10:38:43 +0000793 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794
v8.team.kasperl727e9952008-09-02 14:56:44 +0000795 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000796 * Returns the number of bytes in the UTF-8 encoded
797 * representation of this string.
798 */
ager@chromium.org32912102009-01-16 10:38:43 +0000799 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000800
801 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000802 * Write the contents of the string to an external buffer.
803 * If no arguments are given, expects the buffer to be large
804 * enough to hold the entire string and NULL terminator. Copies
805 * the contents of the string and the NULL terminator into the
806 * buffer.
807 *
808 * Copies up to length characters into the output buffer.
809 * Only null-terminates if there is enough space in the buffer.
810 *
811 * \param buffer The buffer into which the string will be copied.
812 * \param start The starting position within the string at which
813 * copying begins.
814 * \param length The number of bytes to copy from the string.
815 * \return The number of characters copied to the buffer
816 * excluding the NULL terminator.
817 */
ager@chromium.org32912102009-01-16 10:38:43 +0000818 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
819 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
820 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821
v8.team.kasperl727e9952008-09-02 14:56:44 +0000822 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000823 * A zero length string.
824 */
825 static v8::Local<v8::String> Empty();
826
827 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000828 * Returns true if the string is external
829 */
ager@chromium.org32912102009-01-16 10:38:43 +0000830 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
v8.team.kasperl727e9952008-09-02 14:56:44 +0000832 /**
833 * Returns true if the string is both external and ascii
834 */
ager@chromium.org32912102009-01-16 10:38:43 +0000835 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000836
837 class V8EXPORT ExternalStringResourceBase {
838 public:
839 virtual ~ExternalStringResourceBase() {}
840 protected:
841 ExternalStringResourceBase() {}
842 private:
843 // Disallow copying and assigning.
844 ExternalStringResourceBase(const ExternalStringResourceBase&);
845 void operator=(const ExternalStringResourceBase&);
846 };
847
v8.team.kasperl727e9952008-09-02 14:56:44 +0000848 /**
849 * An ExternalStringResource is a wrapper around a two-byte string
850 * buffer that resides outside V8's heap. Implement an
851 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000852 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000853 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000854 class V8EXPORT ExternalStringResource
855 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856 public:
857 /**
858 * Override the destructor to manage the life cycle of the underlying
859 * buffer.
860 */
861 virtual ~ExternalStringResource() {}
862 /** The string data from the underlying buffer.*/
863 virtual const uint16_t* data() const = 0;
864 /** The length of the string. That is, the number of two-byte characters.*/
865 virtual size_t length() const = 0;
866 protected:
867 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 };
869
870 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000871 * An ExternalAsciiStringResource is a wrapper around an ascii
872 * string buffer that resides outside V8's heap. Implement an
873 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000874 * underlying buffer. Note that the string data must be immutable
875 * and that the data must be strict 7-bit ASCII, not Latin1 or
876 * UTF-8, which would require special treatment internally in the
877 * engine and, in the case of UTF-8, do not allow efficient indexing.
878 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000879 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000881 class V8EXPORT ExternalAsciiStringResource
882 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 public:
884 /**
885 * Override the destructor to manage the life cycle of the underlying
886 * buffer.
887 */
888 virtual ~ExternalAsciiStringResource() {}
889 /** The string data from the underlying buffer.*/
890 virtual const char* data() const = 0;
891 /** The number of ascii characters in the string.*/
892 virtual size_t length() const = 0;
893 protected:
894 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 };
896
897 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000898 * Get the ExternalStringResource for an external string. Returns
899 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000901 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
903 /**
904 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000905 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 */
ager@chromium.org32912102009-01-16 10:38:43 +0000907 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000909 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
911 /**
912 * Allocates a new string from either utf-8 encoded or ascii data.
913 * The second parameter 'length' gives the buffer length.
914 * If the data is utf-8 encoded, the caller must
915 * be careful to supply the length parameter.
916 * If it is not given, the function calls
917 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000918 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 */
920 static Local<String> New(const char* data, int length = -1);
921
922 /** Allocates a new string from utf16 data.*/
923 static Local<String> New(const uint16_t* data, int length = -1);
924
925 /** Creates a symbol. Returns one if it exists already.*/
926 static Local<String> NewSymbol(const char* data, int length = -1);
927
v8.team.kasperl727e9952008-09-02 14:56:44 +0000928 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000929 * Creates a new string by concatenating the left and the right strings
930 * passed in as parameters.
931 */
932 static Local<String> Concat(Handle<String> left, Handle<String>right);
933
934 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000935 * Creates a new external string using the data defined in the given
936 * resource. The resource is deleted when the external string is no
937 * longer live on V8's heap. The caller of this function should not
938 * delete or modify the resource. Neither should the underlying buffer be
939 * deallocated or modified except through the destructor of the
940 * external string resource.
941 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000943
ager@chromium.org6f10e412009-02-13 10:11:16 +0000944 /**
945 * Associate an external string resource with this string by transforming it
946 * in place so that existing references to this string in the JavaScript heap
947 * will use the external string resource. The external string resource's
948 * character contents needs to be equivalent to this string.
949 * Returns true if the string has been changed to be an external string.
950 * The string is not modified if the operation fails.
951 */
952 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953
v8.team.kasperl727e9952008-09-02 14:56:44 +0000954 /**
955 * Creates a new external string using the ascii data defined in the given
956 * resource. The resource is deleted when the external string is no
957 * longer live on V8's heap. The caller of this function should not
958 * delete or modify the resource. Neither should the underlying buffer be
959 * deallocated or modified except through the destructor of the
960 * external string resource.
961 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000963
ager@chromium.org6f10e412009-02-13 10:11:16 +0000964 /**
965 * Associate an external string resource with this string by transforming it
966 * in place so that existing references to this string in the JavaScript heap
967 * will use the external string resource. The external string resource's
968 * character contents needs to be equivalent to this string.
969 * Returns true if the string has been changed to be an external string.
970 * The string is not modified if the operation fails.
971 */
972 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000974 /**
975 * Returns true if this string can be made external.
976 */
977 bool CanMakeExternal();
978
kasper.lund7276f142008-07-30 08:49:36 +0000979 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 static Local<String> NewUndetectable(const char* data, int length = -1);
981
kasper.lund7276f142008-07-30 08:49:36 +0000982 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
984
985 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000986 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000987 * you want to print the object. If conversion to a string fails
988 * (eg. due to an exception in the toString() method of the object)
989 * then the length() method returns 0 and the * operator returns
990 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000991 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000992 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000993 public:
994 explicit Utf8Value(Handle<v8::Value> obj);
995 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +0000996 char* operator*() { return str_; }
997 const char* operator*() const { return str_; }
998 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000999 private:
1000 char* str_;
1001 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001002
1003 // Disallow copying and assigning.
1004 Utf8Value(const Utf8Value&);
1005 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001006 };
1007
1008 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 * Converts an object to an ascii string.
1010 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001011 * If conversion to a string fails (eg. due to an exception in the toString()
1012 * method of the object) then the length() method returns 0 and the * operator
1013 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001015 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016 public:
1017 explicit AsciiValue(Handle<v8::Value> obj);
1018 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001019 char* operator*() { return str_; }
1020 const char* operator*() const { return str_; }
1021 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 private:
1023 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001024 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001025
1026 // Disallow copying and assigning.
1027 AsciiValue(const AsciiValue&);
1028 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 };
1030
1031 /**
1032 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001033 * If conversion to a string fails (eg. due to an exception in the toString()
1034 * method of the object) then the length() method returns 0 and the * operator
1035 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001037 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 public:
1039 explicit Value(Handle<v8::Value> obj);
1040 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001041 uint16_t* operator*() { return str_; }
1042 const uint16_t* operator*() const { return str_; }
1043 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 private:
1045 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001046 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001047
1048 // Disallow copying and assigning.
1049 Value(const Value&);
1050 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001052
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001053 private:
1054 void VerifyExternalStringResource(ExternalStringResource* val) const;
1055 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056};
1057
1058
1059/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001060 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001062class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001064 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001065 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001066 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 private:
1068 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001069 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070};
1071
1072
1073/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001074 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001076class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 public:
1078 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001079 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001080 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001081 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 private:
1083 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001084 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085};
1086
1087
1088/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001089 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001091class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001093 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 private:
1095 Int32();
1096};
1097
1098
1099/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001100 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001102class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001104 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 private:
1106 Uint32();
1107};
1108
1109
1110/**
1111 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1112 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001113class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 public:
1115 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001116
1117 /**
1118 * A specialization of Value::NumberValue that is more efficient
1119 * because we know the structure of this object.
1120 */
ager@chromium.org32912102009-01-16 10:38:43 +00001121 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001122
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001123 static inline Date* Cast(v8::Value* obj);
1124 private:
1125 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126};
1127
1128
1129enum PropertyAttribute {
1130 None = 0,
1131 ReadOnly = 1 << 0,
1132 DontEnum = 1 << 1,
1133 DontDelete = 1 << 2
1134};
1135
ager@chromium.org3811b432009-10-28 14:53:37 +00001136enum ExternalArrayType {
1137 kExternalByteArray = 1,
1138 kExternalUnsignedByteArray,
1139 kExternalShortArray,
1140 kExternalUnsignedShortArray,
1141 kExternalIntArray,
1142 kExternalUnsignedIntArray,
1143 kExternalFloatArray
1144};
1145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001147 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001149class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 public:
1151 bool Set(Handle<Value> key,
1152 Handle<Value> value,
1153 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001154
ager@chromium.orge2902be2009-06-08 12:21:35 +00001155 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001156 // overriding accessors or read-only properties.
1157 //
1158 // Note that if the object has an interceptor the property will be set
1159 // locally, but since the interceptor takes precedence the local property
1160 // will only be returned if the interceptor doesn't return a value.
1161 //
1162 // Note also that this only works for named properties.
1163 bool ForceSet(Handle<Value> key,
1164 Handle<Value> value,
1165 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 Local<Value> Get(Handle<Value> key);
1168
1169 // TODO(1245389): Replace the type-specific versions of these
1170 // functions with generic ones that accept a Handle<Value> key.
1171 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001172
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001174
1175 // Delete a property on this object bypassing interceptors and
1176 // ignoring dont-delete attributes.
1177 bool ForceDelete(Handle<Value> key);
1178
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 bool Delete(uint32_t index);
1182
1183 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001184 * Returns an array containing the names of the enumerable properties
1185 * of this object, including properties from prototype objects. The
1186 * array returned by this method contains the same values as would
1187 * be enumerated by a for-in statement over this object.
1188 */
1189 Local<Array> GetPropertyNames();
1190
1191 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 * Get the prototype object. This does not skip objects marked to
1193 * be skipped by __proto__ and it does not consult the security
1194 * handler.
1195 */
1196 Local<Value> GetPrototype();
1197
1198 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001199 * Finds an instance of the given function template in the prototype
1200 * chain.
1201 */
1202 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1203
1204 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 * Call builtin Object.prototype.toString on this object.
1206 * This is different from Value::ToString() that may call
1207 * user-defined toString function. This one does not.
1208 */
1209 Local<String> ObjectProtoToString();
1210
kasper.lund212ac232008-07-16 07:07:30 +00001211 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001213 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001214 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001215 /** Sets the value in an internal field. */
1216 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001218 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001219 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001220
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001221 /** Sets a native pointer in an internal field. */
1222 void SetPointerInInternalField(int index, void* value);
1223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 // Testers for local properties.
1225 bool HasRealNamedProperty(Handle<String> key);
1226 bool HasRealIndexedProperty(uint32_t index);
1227 bool HasRealNamedCallbackProperty(Handle<String> key);
1228
1229 /**
1230 * If result.IsEmpty() no real property was located in the prototype chain.
1231 * This means interceptors in the prototype chain are not called.
1232 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001233 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1234
1235 /**
1236 * If result.IsEmpty() no real property was located on the object or
1237 * in the prototype chain.
1238 * This means interceptors in the prototype chain are not called.
1239 */
1240 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241
1242 /** Tests for a named lookup interceptor.*/
1243 bool HasNamedLookupInterceptor();
1244
kasper.lund212ac232008-07-16 07:07:30 +00001245 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 bool HasIndexedLookupInterceptor();
1247
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001248 /**
1249 * Turns on access check on the object if the object is an instance of
1250 * a template that has access check callbacks. If an object has no
1251 * access check info, the object cannot be accessed by anyone.
1252 */
1253 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001254
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001255 /**
1256 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001257 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001258 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001259 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001260 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001261 */
1262 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001263
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001264 /**
1265 * Access hidden properties on JavaScript objects. These properties are
1266 * hidden from the executing JavaScript and only accessible through the V8
1267 * C++ API. Hidden properties introduced by V8 internally (for example the
1268 * identity hash) are prefixed with "v8::".
1269 */
1270 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1271 Local<Value> GetHiddenValue(Handle<String> key);
1272 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001273
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001274 /**
1275 * Returns true if this is an instance of an api function (one
1276 * created from a function created from a function template) and has
1277 * been modified since it was created. Note that this method is
1278 * conservative and may return true for objects that haven't actually
1279 * been modified.
1280 */
1281 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001282
1283 /**
1284 * Clone this object with a fast but shallow copy. Values will point
1285 * to the same values as the original object.
1286 */
1287 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001289 /**
1290 * Set the backing store of the indexed properties to be managed by the
1291 * embedding layer. Access to the indexed properties will follow the rules
1292 * spelled out in CanvasPixelArray.
1293 * Note: The embedding program still owns the data and needs to ensure that
1294 * the backing store is preserved while V8 has a reference.
1295 */
1296 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1297
ager@chromium.org3811b432009-10-28 14:53:37 +00001298 /**
1299 * Set the backing store of the indexed properties to be managed by the
1300 * embedding layer. Access to the indexed properties will follow the rules
1301 * spelled out for the CanvasArray subtypes in the WebGL specification.
1302 * Note: The embedding program still owns the data and needs to ensure that
1303 * the backing store is preserved while V8 has a reference.
1304 */
1305 void SetIndexedPropertiesToExternalArrayData(void* data,
1306 ExternalArrayType array_type,
1307 int number_of_elements);
1308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001310 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 private:
1312 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001313 static void CheckCast(Value* obj);
1314 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001315 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001316
1317 /**
1318 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001319 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001320 */
1321 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322};
1323
1324
1325/**
1326 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1327 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001328class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001330 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331
ager@chromium.org3e875802009-06-29 08:26:34 +00001332 /**
1333 * Clones an element at index |index|. Returns an empty
1334 * handle if cloning fails (for any reason).
1335 */
1336 Local<Object> CloneElementAt(uint32_t index);
1337
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001339 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 private:
1341 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001342 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343};
1344
1345
1346/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001347 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001349class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001350 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001351 Local<Object> NewInstance() const;
1352 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1354 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001355 Handle<Value> GetName() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001356 static inline Function* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 private:
1358 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001359 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360};
1361
1362
1363/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001364 * A JavaScript value that wraps a C++ void*. This type of value is
1365 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001367 *
1368 * The Wrap function V8 will return the most optimal Value object wrapping the
1369 * C++ void*. The type of the value is not guaranteed to be an External object
1370 * and no assumptions about its type should be made. To access the wrapped
1371 * value Unwrap should be used, all other operations on that object will lead
1372 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001374class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001376 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001377 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001380 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001381 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 private:
1383 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001384 static void CheckCast(v8::Value* obj);
1385 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1386 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387};
1388
1389
1390// --- T e m p l a t e s ---
1391
1392
1393/**
1394 * The superclass of object and function templates.
1395 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001396class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 public:
1398 /** Adds a property to each instance created by this template.*/
1399 void Set(Handle<String> name, Handle<Data> value,
1400 PropertyAttribute attributes = None);
1401 inline void Set(const char* name, Handle<Data> value);
1402 private:
1403 Template();
1404
1405 friend class ObjectTemplate;
1406 friend class FunctionTemplate;
1407};
1408
1409
1410/**
1411 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001412 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 * including the receiver, the number and values of arguments, and
1414 * the holder of the function.
1415 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001416class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 public:
1418 inline int Length() const;
1419 inline Local<Value> operator[](int i) const;
1420 inline Local<Function> Callee() const;
1421 inline Local<Object> This() const;
1422 inline Local<Object> Holder() const;
1423 inline bool IsConstructCall() const;
1424 inline Local<Value> Data() const;
1425 private:
1426 Arguments();
1427 friend class ImplementationUtilities;
1428 inline Arguments(Local<Value> data,
1429 Local<Object> holder,
1430 Local<Function> callee,
1431 bool is_construct_call,
1432 void** values, int length);
1433 Local<Value> data_;
1434 Local<Object> holder_;
1435 Local<Function> callee_;
1436 bool is_construct_call_;
1437 void** values_;
1438 int length_;
1439};
1440
1441
1442/**
1443 * The information passed to an accessor callback about the context
1444 * of the property access.
1445 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001446class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001448 inline AccessorInfo(internal::Object** args)
1449 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 inline Local<Value> Data() const;
1451 inline Local<Object> This() const;
1452 inline Local<Object> Holder() const;
1453 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001454 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455};
1456
1457
1458typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1459
1460typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1461
1462/**
1463 * Accessor[Getter|Setter] are used as callback functions when
1464 * setting|getting a particular property. See objectTemplate::SetAccessor.
1465 */
1466typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1467 const AccessorInfo& info);
1468
1469
1470typedef void (*AccessorSetter)(Local<String> property,
1471 Local<Value> value,
1472 const AccessorInfo& info);
1473
1474
1475/**
1476 * NamedProperty[Getter|Setter] are used as interceptors on object.
1477 * See ObjectTemplate::SetNamedPropertyHandler.
1478 */
1479typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1480 const AccessorInfo& info);
1481
1482
1483/**
1484 * Returns the value if the setter intercepts the request.
1485 * Otherwise, returns an empty handle.
1486 */
1487typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1488 Local<Value> value,
1489 const AccessorInfo& info);
1490
1491
1492/**
1493 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001494 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 */
1496typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1497 const AccessorInfo& info);
1498
1499
1500/**
1501 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001502 * The return value is true if the property could be deleted and false
1503 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 */
1505typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1506 const AccessorInfo& info);
1507
1508/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001509 * Returns an array containing the names of the properties the named
1510 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511 */
1512typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1513
v8.team.kasperl727e9952008-09-02 14:56:44 +00001514
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001516 * Returns the value of the property if the getter intercepts the
1517 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 */
1519typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1520 const AccessorInfo& info);
1521
1522
1523/**
1524 * Returns the value if the setter intercepts the request.
1525 * Otherwise, returns an empty handle.
1526 */
1527typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1528 Local<Value> value,
1529 const AccessorInfo& info);
1530
1531
1532/**
1533 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001534 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535 */
1536typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1537 const AccessorInfo& info);
1538
1539/**
1540 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001541 * The return value is true if the property could be deleted and false
1542 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 */
1544typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1545 const AccessorInfo& info);
1546
v8.team.kasperl727e9952008-09-02 14:56:44 +00001547/**
1548 * Returns an array containing the indices of the properties the
1549 * indexed property getter intercepts.
1550 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1552
1553
1554/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001555 * Access control specifications.
1556 *
1557 * Some accessors should be accessible across contexts. These
1558 * accessors have an explicit access control parameter which specifies
1559 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001560 *
1561 * Additionally, for security, accessors can prohibit overwriting by
1562 * accessors defined in JavaScript. For objects that have such
1563 * accessors either locally or in their prototype chain it is not
1564 * possible to overwrite the accessor by using __defineGetter__ or
1565 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 */
1567enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001568 DEFAULT = 0,
1569 ALL_CAN_READ = 1,
1570 ALL_CAN_WRITE = 1 << 1,
1571 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572};
1573
1574
1575/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001576 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 */
1578enum AccessType {
1579 ACCESS_GET,
1580 ACCESS_SET,
1581 ACCESS_HAS,
1582 ACCESS_DELETE,
1583 ACCESS_KEYS
1584};
1585
v8.team.kasperl727e9952008-09-02 14:56:44 +00001586
1587/**
1588 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001589 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001590 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001591typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 Local<Value> key,
1593 AccessType type,
1594 Local<Value> data);
1595
v8.team.kasperl727e9952008-09-02 14:56:44 +00001596
1597/**
1598 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001599 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001600 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001601typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 uint32_t index,
1603 AccessType type,
1604 Local<Value> data);
1605
1606
1607/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001608 * A FunctionTemplate is used to create functions at runtime. There
1609 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001610 * context. The lifetime of the created function is equal to the
1611 * lifetime of the context. So in case the embedder needs to create
1612 * temporary functions that can be collected using Scripts is
1613 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 *
1615 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001616 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001618 * A FunctionTemplate has a corresponding instance template which is
1619 * used to create object instances when the function is used as a
1620 * constructor. Properties added to the instance template are added to
1621 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 *
1623 * A FunctionTemplate can have a prototype template. The prototype template
1624 * is used to create the prototype object of the function.
1625 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001626 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001628 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1630 * t->Set("func_property", v8::Number::New(1));
1631 *
1632 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1633 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1634 * proto_t->Set("proto_const", v8::Number::New(2));
1635 *
1636 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1637 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1638 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1639 * instance_t->Set("instance_property", Number::New(3));
1640 *
1641 * v8::Local<v8::Function> function = t->GetFunction();
1642 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001643 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 *
1645 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001646 * and "instance" for the instance object created above. The function
1647 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001649 * \code
1650 * func_property in function == true;
1651 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001653 * function.prototype.proto_method() invokes 'InvokeCallback'
1654 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001656 * instance instanceof function == true;
1657 * instance.instance_accessor calls 'InstanceAccessorCallback'
1658 * instance.instance_property == 3;
1659 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001661 * A FunctionTemplate can inherit from another one by calling the
1662 * FunctionTemplate::Inherit method. The following graph illustrates
1663 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001665 * \code
1666 * FunctionTemplate Parent -> Parent() . prototype -> { }
1667 * ^ ^
1668 * | Inherit(Parent) | .__proto__
1669 * | |
1670 * FunctionTemplate Child -> Child() . prototype -> { }
1671 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001673 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1674 * object of the Child() function has __proto__ pointing to the
1675 * Parent() function's prototype object. An instance of the Child
1676 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001678 * Let Parent be the FunctionTemplate initialized in the previous
1679 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001681 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 * Local<FunctionTemplate> parent = t;
1683 * Local<FunctionTemplate> child = FunctionTemplate::New();
1684 * child->Inherit(parent);
1685 *
1686 * Local<Function> child_function = child->GetFunction();
1687 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001688 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001690 * The Child function and Child instance will have the following
1691 * properties:
1692 *
1693 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001695 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001697 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001699class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 public:
1701 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001702 static Local<FunctionTemplate> New(
1703 InvocationCallback callback = 0,
1704 Handle<Value> data = Handle<Value>(),
1705 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 /** Returns the unique function instance in the current execution context.*/
1707 Local<Function> GetFunction();
1708
v8.team.kasperl727e9952008-09-02 14:56:44 +00001709 /**
1710 * Set the call-handler callback for a FunctionTemplate. This
1711 * callback is called whenever the function created from this
1712 * FunctionTemplate is called.
1713 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 void SetCallHandler(InvocationCallback callback,
1715 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716
v8.team.kasperl727e9952008-09-02 14:56:44 +00001717 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718 Local<ObjectTemplate> InstanceTemplate();
1719
1720 /** Causes the function template to inherit from a parent function template.*/
1721 void Inherit(Handle<FunctionTemplate> parent);
1722
1723 /**
1724 * A PrototypeTemplate is the template used to create the prototype object
1725 * of the function created by this template.
1726 */
1727 Local<ObjectTemplate> PrototypeTemplate();
1728
v8.team.kasperl727e9952008-09-02 14:56:44 +00001729
1730 /**
1731 * Set the class name of the FunctionTemplate. This is used for
1732 * printing objects created with the function created from the
1733 * FunctionTemplate as its constructor.
1734 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 void SetClassName(Handle<String> name);
1736
1737 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001738 * Determines whether the __proto__ accessor ignores instances of
1739 * the function template. If instances of the function template are
1740 * ignored, __proto__ skips all instances and instead returns the
1741 * next object in the prototype chain.
1742 *
1743 * Call with a value of true to make the __proto__ accessor ignore
1744 * instances of the function template. Call with a value of false
1745 * to make the __proto__ accessor not ignore instances of the
1746 * function template. By default, instances of a function template
1747 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 */
1749 void SetHiddenPrototype(bool value);
1750
1751 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001752 * Returns true if the given object is an instance of this function
1753 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754 */
1755 bool HasInstance(Handle<Value> object);
1756
1757 private:
1758 FunctionTemplate();
1759 void AddInstancePropertyAccessor(Handle<String> name,
1760 AccessorGetter getter,
1761 AccessorSetter setter,
1762 Handle<Value> data,
1763 AccessControl settings,
1764 PropertyAttribute attributes);
1765 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1766 NamedPropertySetter setter,
1767 NamedPropertyQuery query,
1768 NamedPropertyDeleter remover,
1769 NamedPropertyEnumerator enumerator,
1770 Handle<Value> data);
1771 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1772 IndexedPropertySetter setter,
1773 IndexedPropertyQuery query,
1774 IndexedPropertyDeleter remover,
1775 IndexedPropertyEnumerator enumerator,
1776 Handle<Value> data);
1777 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1778 Handle<Value> data);
1779
1780 friend class Context;
1781 friend class ObjectTemplate;
1782};
1783
1784
1785/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001786 * An ObjectTemplate is used to create objects at runtime.
1787 *
1788 * Properties added to an ObjectTemplate are added to each object
1789 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001791class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001793 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001795
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 /** Creates a new instance of this template.*/
1797 Local<Object> NewInstance();
1798
1799 /**
1800 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001801 *
1802 * Whenever the property with the given name is accessed on objects
1803 * created from this ObjectTemplate the getter and setter callbacks
1804 * are called instead of getting and setting the property directly
1805 * on the JavaScript object.
1806 *
1807 * \param name The name of the property for which an accessor is added.
1808 * \param getter The callback to invoke when getting the property.
1809 * \param setter The callback to invoke when setting the property.
1810 * \param data A piece of data that will be passed to the getter and setter
1811 * callbacks whenever they are invoked.
1812 * \param settings Access control settings for the accessor. This is a bit
1813 * field consisting of one of more of
1814 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1815 * The default is to not allow cross-context access.
1816 * ALL_CAN_READ means that all cross-context reads are allowed.
1817 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1818 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1819 * cross-context access.
1820 * \param attribute The attributes of the property for which an accessor
1821 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 */
1823 void SetAccessor(Handle<String> name,
1824 AccessorGetter getter,
1825 AccessorSetter setter = 0,
1826 Handle<Value> data = Handle<Value>(),
1827 AccessControl settings = DEFAULT,
1828 PropertyAttribute attribute = None);
1829
1830 /**
1831 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001832 *
1833 * Whenever a named property is accessed on objects created from
1834 * this object template, the provided callback is invoked instead of
1835 * accessing the property directly on the JavaScript object.
1836 *
1837 * \param getter The callback to invoke when getting a property.
1838 * \param setter The callback to invoke when setting a property.
1839 * \param query The callback to invoke to check is an object has a property.
1840 * \param deleter The callback to invoke when deleting a property.
1841 * \param enumerator The callback to invoke to enumerate all the named
1842 * properties of an object.
1843 * \param data A piece of data that will be passed to the callbacks
1844 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 */
1846 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1847 NamedPropertySetter setter = 0,
1848 NamedPropertyQuery query = 0,
1849 NamedPropertyDeleter deleter = 0,
1850 NamedPropertyEnumerator enumerator = 0,
1851 Handle<Value> data = Handle<Value>());
1852
1853 /**
1854 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001855 *
1856 * Whenever an indexed property is accessed on objects created from
1857 * this object template, the provided callback is invoked instead of
1858 * accessing the property directly on the JavaScript object.
1859 *
1860 * \param getter The callback to invoke when getting a property.
1861 * \param setter The callback to invoke when setting a property.
1862 * \param query The callback to invoke to check is an object has a property.
1863 * \param deleter The callback to invoke when deleting a property.
1864 * \param enumerator The callback to invoke to enumerate all the indexed
1865 * properties of an object.
1866 * \param data A piece of data that will be passed to the callbacks
1867 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 */
1869 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1870 IndexedPropertySetter setter = 0,
1871 IndexedPropertyQuery query = 0,
1872 IndexedPropertyDeleter deleter = 0,
1873 IndexedPropertyEnumerator enumerator = 0,
1874 Handle<Value> data = Handle<Value>());
1875 /**
1876 * Sets the callback to be used when calling instances created from
1877 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001878 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 * function.
1880 */
1881 void SetCallAsFunctionHandler(InvocationCallback callback,
1882 Handle<Value> data = Handle<Value>());
1883
v8.team.kasperl727e9952008-09-02 14:56:44 +00001884 /**
1885 * Mark object instances of the template as undetectable.
1886 *
1887 * In many ways, undetectable objects behave as though they are not
1888 * there. They behave like 'undefined' in conditionals and when
1889 * printed. However, properties can be accessed and called as on
1890 * normal objects.
1891 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 void MarkAsUndetectable();
1893
v8.team.kasperl727e9952008-09-02 14:56:44 +00001894 /**
1895 * Sets access check callbacks on the object template.
1896 *
1897 * When accessing properties on instances of this object template,
1898 * the access check callback will be called to determine whether or
1899 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001900 * The last parameter specifies whether access checks are turned
1901 * on by default on instances. If access checks are off by default,
1902 * they can be turned on on individual instances by calling
1903 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001904 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1906 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001907 Handle<Value> data = Handle<Value>(),
1908 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
kasper.lund212ac232008-07-16 07:07:30 +00001910 /**
1911 * Gets the number of internal fields for objects generated from
1912 * this template.
1913 */
1914 int InternalFieldCount();
1915
1916 /**
1917 * Sets the number of internal fields for objects generated from
1918 * this template.
1919 */
1920 void SetInternalFieldCount(int value);
1921
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 private:
1923 ObjectTemplate();
1924 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1925 friend class FunctionTemplate;
1926};
1927
1928
1929/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001930 * A Signature specifies which receivers and arguments a function can
1931 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001933class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 public:
1935 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1936 Handle<FunctionTemplate>(),
1937 int argc = 0,
1938 Handle<FunctionTemplate> argv[] = 0);
1939 private:
1940 Signature();
1941};
1942
1943
1944/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001945 * A utility for determining the type of objects based on the template
1946 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001948class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949 public:
1950 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1951 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1952 int match(Handle<Value> value);
1953 private:
1954 TypeSwitch();
1955};
1956
1957
1958// --- E x t e n s i o n s ---
1959
1960
1961/**
1962 * Ignore
1963 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001964class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 public:
1966 Extension(const char* name,
1967 const char* source = 0,
1968 int dep_count = 0,
1969 const char** deps = 0);
1970 virtual ~Extension() { }
1971 virtual v8::Handle<v8::FunctionTemplate>
1972 GetNativeFunction(v8::Handle<v8::String> name) {
1973 return v8::Handle<v8::FunctionTemplate>();
1974 }
1975
1976 const char* name() { return name_; }
1977 const char* source() { return source_; }
1978 int dependency_count() { return dep_count_; }
1979 const char** dependencies() { return deps_; }
1980 void set_auto_enable(bool value) { auto_enable_ = value; }
1981 bool auto_enable() { return auto_enable_; }
1982
1983 private:
1984 const char* name_;
1985 const char* source_;
1986 int dep_count_;
1987 const char** deps_;
1988 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001989
1990 // Disallow copying and assigning.
1991 Extension(const Extension&);
1992 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993};
1994
1995
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001996void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997
1998
1999/**
2000 * Ignore
2001 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002002class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 public:
2004 inline DeclareExtension(Extension* extension) {
2005 RegisterExtension(extension);
2006 }
2007};
2008
2009
2010// --- S t a t i c s ---
2011
2012
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002013Handle<Primitive> V8EXPORT Undefined();
2014Handle<Primitive> V8EXPORT Null();
2015Handle<Boolean> V8EXPORT True();
2016Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017
2018
2019/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002020 * A set of constraints that specifies the limits of the runtime's memory use.
2021 * You must set the heap size before initializing the VM - the size cannot be
2022 * adjusted after the VM is initialized.
2023 *
2024 * If you are using threads then you should hold the V8::Locker lock while
2025 * setting the stack limit and you must set a non-default stack limit separately
2026 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002028class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 public:
2030 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002031 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002033 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002035 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002036 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2038 private:
2039 int max_young_space_size_;
2040 int max_old_space_size_;
2041 uint32_t* stack_limit_;
2042};
2043
2044
2045bool SetResourceConstraints(ResourceConstraints* constraints);
2046
2047
2048// --- E x c e p t i o n s ---
2049
2050
2051typedef void (*FatalErrorCallback)(const char* location, const char* message);
2052
2053
2054typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2055
2056
2057/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002058 * Schedules an exception to be thrown when returning to JavaScript. When an
2059 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002061 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002063Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064
2065/**
2066 * Create new error objects by calling the corresponding error object
2067 * constructor with the message.
2068 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002069class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 public:
2071 static Local<Value> RangeError(Handle<String> message);
2072 static Local<Value> ReferenceError(Handle<String> message);
2073 static Local<Value> SyntaxError(Handle<String> message);
2074 static Local<Value> TypeError(Handle<String> message);
2075 static Local<Value> Error(Handle<String> message);
2076};
2077
2078
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002079// --- 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 +00002080
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002081typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002083typedef void* (*CreateHistogramCallback)(const char* name,
2084 int min,
2085 int max,
2086 size_t buckets);
2087
2088typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2089
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090// --- 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 ---
2091typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2092 AccessType type,
2093 Local<Value> data);
2094
2095// --- 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
2096
2097/**
2098 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002099 * before and after a major garbage collection. Allocations are not
2100 * allowed in the callback function, you therefore cannot manipulate
2101 * objects (set or delete properties for example) since it is possible
2102 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 */
2104typedef void (*GCCallback)();
2105
2106
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002107// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108
2109/**
2110 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002111 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113typedef Persistent<Context> (*ContextGenerator)();
2114
2115
2116/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002117 * Profiler modules.
2118 *
2119 * In V8, profiler consists of several modules: CPU profiler, and different
2120 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002121 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2122 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002123 */
2124enum ProfilerModules {
2125 PROFILER_MODULE_NONE = 0,
2126 PROFILER_MODULE_CPU = 1,
2127 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002128 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2129 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002130};
2131
2132
2133/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002134 * Collection of V8 heap information.
2135 *
2136 * Instances of this class can be passed to v8::V8::HeapStatistics to
2137 * get heap statistics from V8.
2138 */
2139class V8EXPORT HeapStatistics {
2140 public:
2141 HeapStatistics();
2142 size_t total_heap_size() { return total_heap_size_; }
2143 size_t used_heap_size() { return used_heap_size_; }
2144
2145 private:
2146 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2147 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2148
2149 size_t total_heap_size_;
2150 size_t used_heap_size_;
2151
2152 friend class V8;
2153};
2154
2155
2156/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 * Container class for static utility functions.
2158 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002159class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002161 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 static void SetFatalErrorHandler(FatalErrorCallback that);
2163
v8.team.kasperl727e9952008-09-02 14:56:44 +00002164 /**
2165 * Ignore out-of-memory exceptions.
2166 *
2167 * V8 running out of memory is treated as a fatal error by default.
2168 * This means that the fatal error handler is called and that V8 is
2169 * terminated.
2170 *
2171 * IgnoreOutOfMemoryException can be used to not treat a
2172 * out-of-memory situation as a fatal error. This way, the contexts
2173 * that did not cause the out of memory problem might be able to
2174 * continue execution.
2175 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 static void IgnoreOutOfMemoryException();
2177
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002179 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002180 * fatal errors such as out-of-memory situations.
2181 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 static bool IsDead();
2183
2184 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002185 * Adds a message listener.
2186 *
2187 * The same message listener can be added more than once and it that
2188 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 */
2190 static bool AddMessageListener(MessageCallback that,
2191 Handle<Value> data = Handle<Value>());
2192
2193 /**
2194 * Remove all message listeners from the specified callback function.
2195 */
2196 static void RemoveMessageListeners(MessageCallback that);
2197
2198 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002199 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 */
2201 static void SetFlagsFromString(const char* str, int length);
2202
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002203 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002204 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002205 */
2206 static void SetFlagsFromCommandLine(int* argc,
2207 char** argv,
2208 bool remove_flags);
2209
kasper.lund7276f142008-07-30 08:49:36 +00002210 /** Get the version string. */
2211 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212
2213 /**
2214 * Enables the host application to provide a mechanism for recording
2215 * statistics counters.
2216 */
2217 static void SetCounterFunction(CounterLookupCallback);
2218
2219 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002220 * Enables the host application to provide a mechanism for recording
2221 * histograms. The CreateHistogram function returns a
2222 * histogram which will later be passed to the AddHistogramSample
2223 * function.
2224 */
2225 static void SetCreateHistogramFunction(CreateHistogramCallback);
2226 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2227
2228 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 * Enables the computation of a sliding window of states. The sliding
2230 * window information is recorded in statistics counters.
2231 */
2232 static void EnableSlidingStateWindow();
2233
2234 /** Callback function for reporting failed access checks.*/
2235 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2236
2237 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002238 * Enables the host application to receive a notification before a
2239 * major garbage colletion. Allocations are not allowed in the
2240 * callback function, you therefore cannot manipulate objects (set
2241 * or delete properties for example) since it is possible such
2242 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 */
2244 static void SetGlobalGCPrologueCallback(GCCallback);
2245
2246 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002247 * Enables the host application to receive a notification after a
2248 * major garbage collection. Allocations are not allowed in the
2249 * callback function, you therefore cannot manipulate objects (set
2250 * or delete properties for example) since it is possible such
2251 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 */
2253 static void SetGlobalGCEpilogueCallback(GCCallback);
2254
2255 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002256 * Allows the host application to group objects together. If one
2257 * object in the group is alive, all objects in the group are alive.
2258 * After each garbage collection, object groups are removed. It is
2259 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002260 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002261 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002263 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264
2265 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002266 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002267 * initialize from scratch. This function is called implicitly if
2268 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 */
2270 static bool Initialize();
2271
kasper.lund7276f142008-07-30 08:49:36 +00002272 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002273 * Adjusts the amount of registered external memory. Used to give
2274 * V8 an indication of the amount of externally allocated memory
2275 * that is kept alive by JavaScript objects. V8 uses this to decide
2276 * when to perform global garbage collections. Registering
2277 * externally allocated memory will trigger global garbage
2278 * collections more often than otherwise in an attempt to garbage
2279 * collect the JavaScript objects keeping the externally allocated
2280 * memory alive.
2281 *
2282 * \param change_in_bytes the change in externally allocated memory
2283 * that is kept alive by JavaScript objects.
2284 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002285 */
2286 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2287
iposva@chromium.org245aa852009-02-10 00:49:54 +00002288 /**
2289 * Suspends recording of tick samples in the profiler.
2290 * When the V8 profiling mode is enabled (usually via command line
2291 * switches) this function suspends recording of tick samples.
2292 * Profiling ticks are discarded until ResumeProfiler() is called.
2293 *
2294 * See also the --prof and --prof_auto command line switches to
2295 * enable V8 profiling.
2296 */
2297 static void PauseProfiler();
2298
2299 /**
2300 * Resumes recording of tick samples in the profiler.
2301 * See also PauseProfiler().
2302 */
2303 static void ResumeProfiler();
2304
ager@chromium.org41826e72009-03-30 13:30:57 +00002305 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002306 * Return whether profiler is currently paused.
2307 */
2308 static bool IsProfilerPaused();
2309
2310 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002311 * Resumes specified profiler modules.
2312 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2313 * See ProfilerModules enum.
2314 *
2315 * \param flags Flags specifying profiler modules.
2316 */
2317 static void ResumeProfilerEx(int flags);
2318
2319 /**
2320 * Pauses specified profiler modules.
2321 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2322 * See ProfilerModules enum.
2323 *
2324 * \param flags Flags specifying profiler modules.
2325 */
2326 static void PauseProfilerEx(int flags);
2327
2328 /**
2329 * Returns active (resumed) profiler modules.
2330 * See ProfilerModules enum.
2331 *
2332 * \returns active profiler modules.
2333 */
2334 static int GetActiveProfilerModules();
2335
2336 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002337 * If logging is performed into a memory buffer (via --logfile=*), allows to
2338 * retrieve previously written messages. This can be used for retrieving
2339 * profiler log data in the application. This function is thread-safe.
2340 *
2341 * Caller provides a destination buffer that must exist during GetLogLines
2342 * call. Only whole log lines are copied into the buffer.
2343 *
2344 * \param from_pos specified a point in a buffer to read from, 0 is the
2345 * beginning of a buffer. It is assumed that caller updates its current
2346 * position using returned size value from the previous call.
2347 * \param dest_buf destination buffer for log data.
2348 * \param max_size size of the destination buffer.
2349 * \returns actual size of log data copied into buffer.
2350 */
2351 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2352
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002353 /**
2354 * Retrieve the V8 thread id of the calling thread.
2355 *
2356 * The thread id for a thread should only be retrieved after the V8
2357 * lock has been acquired with a Locker object with that thread.
2358 */
2359 static int GetCurrentThreadId();
2360
2361 /**
2362 * Forcefully terminate execution of a JavaScript thread. This can
2363 * be used to terminate long-running scripts.
2364 *
2365 * TerminateExecution should only be called when then V8 lock has
2366 * been acquired with a Locker object. Therefore, in order to be
2367 * able to terminate long-running threads, preemption must be
2368 * enabled to allow the user of TerminateExecution to acquire the
2369 * lock.
2370 *
2371 * The termination is achieved by throwing an exception that is
2372 * uncatchable by JavaScript exception handlers. Termination
2373 * exceptions act as if they were caught by a C++ TryCatch exception
2374 * handlers. If forceful termination is used, any C++ TryCatch
2375 * exception handler that catches an exception should check if that
2376 * exception is a termination exception and immediately return if
2377 * that is the case. Returning immediately in that case will
2378 * continue the propagation of the termination exception if needed.
2379 *
2380 * The thread id passed to TerminateExecution must have been
2381 * obtained by calling GetCurrentThreadId on the thread in question.
2382 *
2383 * \param thread_id The thread id of the thread to terminate.
2384 */
2385 static void TerminateExecution(int thread_id);
2386
2387 /**
2388 * Forcefully terminate the current thread of JavaScript execution.
2389 *
2390 * This method can be used by any thread even if that thread has not
2391 * acquired the V8 lock with a Locker object.
2392 */
2393 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002394
2395 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002396 * Releases any resources used by v8 and stops any utility threads
2397 * that may be running. Note that disposing v8 is permanent, it
2398 * cannot be reinitialized.
2399 *
2400 * It should generally not be necessary to dispose v8 before exiting
2401 * a process, this should happen automatically. It is only necessary
2402 * to use if the process needs the resources taken up by v8.
2403 */
2404 static bool Dispose();
2405
ager@chromium.org3811b432009-10-28 14:53:37 +00002406 /**
2407 * Get statistics about the heap memory usage.
2408 */
2409 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002410
2411 /**
2412 * Optional notification that the embedder is idle.
2413 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002414 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002415 * Returns true if the embedder should stop calling IdleNotification
2416 * until real work has been done. This indicates that V8 has done
2417 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002418 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002419 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002420
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002421 /**
2422 * Optional notification that the system is running low on memory.
2423 * V8 uses these notifications to attempt to free memory.
2424 */
2425 static void LowMemoryNotification();
2426
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 private:
2428 V8();
2429
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002430 static internal::Object** GlobalizeReference(internal::Object** handle);
2431 static void DisposeGlobal(internal::Object** global_handle);
2432 static void MakeWeak(internal::Object** global_handle,
2433 void* data,
2434 WeakReferenceCallback);
2435 static void ClearWeak(internal::Object** global_handle);
2436 static bool IsGlobalNearDeath(internal::Object** global_handle);
2437 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438
2439 template <class T> friend class Handle;
2440 template <class T> friend class Local;
2441 template <class T> friend class Persistent;
2442 friend class Context;
2443};
2444
2445
2446/**
2447 * An external exception handler.
2448 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002449class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 public:
2451
2452 /**
2453 * Creates a new try/catch block and registers it with v8.
2454 */
2455 TryCatch();
2456
2457 /**
2458 * Unregisters and deletes this try/catch block.
2459 */
2460 ~TryCatch();
2461
2462 /**
2463 * Returns true if an exception has been caught by this try/catch block.
2464 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002465 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466
2467 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002468 * For certain types of exceptions, it makes no sense to continue
2469 * execution.
2470 *
2471 * Currently, the only type of exception that can be caught by a
2472 * TryCatch handler and for which it does not make sense to continue
2473 * is termination exception. Such exceptions are thrown when the
2474 * TerminateExecution methods are called to terminate a long-running
2475 * script.
2476 *
2477 * If CanContinue returns false, the correct action is to perform
2478 * any C++ cleanup needed and then return.
2479 */
2480 bool CanContinue() const;
2481
2482 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002483 * Throws the exception caught by this TryCatch in a way that avoids
2484 * it being caught again by this same TryCatch. As with ThrowException
2485 * it is illegal to execute any JavaScript operations after calling
2486 * ReThrow; the caller must return immediately to where the exception
2487 * is caught.
2488 */
2489 Handle<Value> ReThrow();
2490
2491 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 * Returns the exception caught by this try/catch block. If no exception has
2493 * been caught an empty handle is returned.
2494 *
2495 * The returned handle is valid until this TryCatch block has been destroyed.
2496 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002497 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498
2499 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002500 * Returns the .stack property of the thrown object. If no .stack
2501 * property is present an empty handle is returned.
2502 */
2503 Local<Value> StackTrace() const;
2504
2505 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002506 * Returns the message associated with this exception. If there is
2507 * no message associated an empty handle is returned.
2508 *
2509 * The returned handle is valid until this TryCatch block has been
2510 * destroyed.
2511 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002512 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002513
2514 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515 * Clears any exceptions that may have been caught by this try/catch block.
2516 * After this method has been called, HasCaught() will return false.
2517 *
2518 * It is not necessary to clear a try/catch block before using it again; if
2519 * another exception is thrown the previously caught exception will just be
2520 * overwritten. However, it is often a good idea since it makes it easier
2521 * to determine which operation threw a given exception.
2522 */
2523 void Reset();
2524
v8.team.kasperl727e9952008-09-02 14:56:44 +00002525 /**
2526 * Set verbosity of the external exception handler.
2527 *
2528 * By default, exceptions that are caught by an external exception
2529 * handler are not reported. Call SetVerbose with true on an
2530 * external exception handler to have exceptions caught by the
2531 * handler reported as if they were not caught.
2532 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 void SetVerbose(bool value);
2534
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002535 /**
2536 * Set whether or not this TryCatch should capture a Message object
2537 * which holds source information about where the exception
2538 * occurred. True by default.
2539 */
2540 void SetCaptureMessage(bool value);
2541
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002542 private:
2543 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002545 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002546 bool is_verbose_ : 1;
2547 bool can_continue_ : 1;
2548 bool capture_message_ : 1;
2549 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002550
2551 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552};
2553
2554
2555// --- C o n t e x t ---
2556
2557
2558/**
2559 * Ignore
2560 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002561class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 public:
2563 ExtensionConfiguration(int name_count, const char* names[])
2564 : name_count_(name_count), names_(names) { }
2565 private:
2566 friend class ImplementationUtilities;
2567 int name_count_;
2568 const char** names_;
2569};
2570
2571
2572/**
2573 * A sandboxed execution context with its own set of built-in objects
2574 * and functions.
2575 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002576class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002578 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 Local<Object> Global();
2580
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002581 /**
2582 * Detaches the global object from its context before
2583 * the global object can be reused to create a new context.
2584 */
2585 void DetachGlobal();
2586
v8.team.kasperl727e9952008-09-02 14:56:44 +00002587 /** Creates a new context. */
2588 static Persistent<Context> New(
2589 ExtensionConfiguration* extensions = 0,
2590 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2591 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592
kasper.lund44510672008-07-25 07:37:58 +00002593 /** Returns the last entered context. */
2594 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002595
kasper.lund44510672008-07-25 07:37:58 +00002596 /** Returns the context that is on the top of the stack. */
2597 static Local<Context> GetCurrent();
2598
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002600 * Returns the context of the calling JavaScript code. That is the
2601 * context of the top-most JavaScript frame. If there are no
2602 * JavaScript frames an empty handle is returned.
2603 */
2604 static Local<Context> GetCalling();
2605
2606 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002607 * Sets the security token for the context. To access an object in
2608 * another context, the security tokens must match.
2609 */
2610 void SetSecurityToken(Handle<Value> token);
2611
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002612 /** Restores the security token to the default value. */
2613 void UseDefaultSecurityToken();
2614
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 /** Returns the security token of this context.*/
2616 Handle<Value> GetSecurityToken();
2617
v8.team.kasperl727e9952008-09-02 14:56:44 +00002618 /**
2619 * Enter this context. After entering a context, all code compiled
2620 * and run is compiled and run in this context. If another context
2621 * is already entered, this old context is saved so it can be
2622 * restored when the new context is exited.
2623 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002625
2626 /**
2627 * Exit this context. Exiting the current context restores the
2628 * context that was in place when entering the current context.
2629 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 void Exit();
2631
v8.team.kasperl727e9952008-09-02 14:56:44 +00002632 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 bool HasOutOfMemoryException();
2634
v8.team.kasperl727e9952008-09-02 14:56:44 +00002635 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 static bool InContext();
2637
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002639 * Associate an additional data object with the context. This is mainly used
2640 * with the debugger to provide additional information on the context through
2641 * the debugger API.
2642 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00002643 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00002644 Local<Value> GetData();
2645
2646 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647 * Stack-allocated class which sets the execution context for all
2648 * operations executed within a local scope.
2649 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002650 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 public:
2652 inline Scope(Handle<Context> context) : context_(context) {
2653 context_->Enter();
2654 }
2655 inline ~Scope() { context_->Exit(); }
2656 private:
2657 Handle<Context> context_;
2658 };
2659
2660 private:
2661 friend class Value;
2662 friend class Script;
2663 friend class Object;
2664 friend class Function;
2665};
2666
2667
2668/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002669 * Multiple threads in V8 are allowed, but only one thread at a time
2670 * is allowed to use V8. The definition of 'using V8' includes
2671 * accessing handles or holding onto object pointers obtained from V8
2672 * handles. It is up to the user of V8 to ensure (perhaps with
2673 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 *
2675 * If you wish to start using V8 in a thread you can do this by constructing
2676 * a v8::Locker object. After the code using V8 has completed for the
2677 * current thread you can call the destructor. This can be combined
2678 * with C++ scope-based construction as follows:
2679 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002680 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 * ...
2682 * {
2683 * v8::Locker locker;
2684 * ...
2685 * // Code using V8 goes here.
2686 * ...
2687 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002688 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 *
2690 * If you wish to stop using V8 in a thread A you can do this by either
2691 * by destroying the v8::Locker object as above or by constructing a
2692 * v8::Unlocker object:
2693 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002694 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 * {
2696 * v8::Unlocker unlocker;
2697 * ...
2698 * // Code not using V8 goes here while V8 can run in another thread.
2699 * ...
2700 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002701 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 *
2703 * The Unlocker object is intended for use in a long-running callback
2704 * from V8, where you want to release the V8 lock for other threads to
2705 * use.
2706 *
2707 * The v8::Locker is a recursive lock. That is, you can lock more than
2708 * once in a given thread. This can be useful if you have code that can
2709 * be called either from code that holds the lock or from code that does
2710 * not. The Unlocker is not recursive so you can not have several
2711 * Unlockers on the stack at once, and you can not use an Unlocker in a
2712 * thread that is not inside a Locker's scope.
2713 *
2714 * An unlocker will unlock several lockers if it has to and reinstate
2715 * the correct depth of locking on its destruction. eg.:
2716 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002717 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718 * // V8 not locked.
2719 * {
2720 * v8::Locker locker;
2721 * // V8 locked.
2722 * {
2723 * v8::Locker another_locker;
2724 * // V8 still locked (2 levels).
2725 * {
2726 * v8::Unlocker unlocker;
2727 * // V8 not locked.
2728 * }
2729 * // V8 locked again (2 levels).
2730 * }
2731 * // V8 still locked (1 level).
2732 * }
2733 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002734 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002735 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002736class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737 public:
2738 Unlocker();
2739 ~Unlocker();
2740};
2741
2742
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002743class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 public:
2745 Locker();
2746 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002747
2748 /**
2749 * Start preemption.
2750 *
2751 * When preemption is started, a timer is fired every n milli seconds
2752 * that will switch between multiple threads that are in contention
2753 * for the V8 lock.
2754 */
2755 static void StartPreemption(int every_n_ms);
2756
2757 /**
2758 * Stop preemption.
2759 */
2760 static void StopPreemption();
2761
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002762 /**
2763 * Returns whether or not the locker is locked by the current thread.
2764 */
2765 static bool IsLocked();
2766
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002767 /**
2768 * Returns whether v8::Locker is being used by this V8 instance.
2769 */
2770 static bool IsActive() { return active_; }
2771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772 private:
2773 bool has_lock_;
2774 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002775
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002776 static bool active_;
2777
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002778 // Disallow copying and assigning.
2779 Locker(const Locker&);
2780 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002781};
2782
2783
2784
2785// --- I m p l e m e n t a t i o n ---
2786
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002787
2788namespace internal {
2789
2790
2791// Tag information for HeapObject.
2792const int kHeapObjectTag = 1;
2793const int kHeapObjectTagSize = 2;
2794const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2795
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002796// Tag information for Smi.
2797const int kSmiTag = 0;
2798const int kSmiTagSize = 1;
2799const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2800
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002801template <size_t ptr_size> struct SmiConstants;
2802
2803// Smi constants for 32-bit systems.
2804template <> struct SmiConstants<4> {
2805 static const int kSmiShiftSize = 0;
2806 static const int kSmiValueSize = 31;
2807 static inline int SmiToInt(internal::Object* value) {
2808 int shift_bits = kSmiTagSize + kSmiShiftSize;
2809 // Throw away top 32 bits and shift down (requires >> to be sign extending).
2810 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
2811 }
2812};
2813
2814// Smi constants for 64-bit systems.
2815template <> struct SmiConstants<8> {
2816 static const int kSmiShiftSize = 31;
2817 static const int kSmiValueSize = 32;
2818 static inline int SmiToInt(internal::Object* value) {
2819 int shift_bits = kSmiTagSize + kSmiShiftSize;
2820 // Shift down and throw away top 32 bits.
2821 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
2822 }
2823};
2824
2825const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
2826const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002827
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002828template <size_t ptr_size> struct InternalConstants;
2829
2830// Internal constants for 32-bit systems.
2831template <> struct InternalConstants<4> {
2832 static const int kStringResourceOffset = 3 * sizeof(void*);
2833};
2834
2835// Internal constants for 64-bit systems.
2836template <> struct InternalConstants<8> {
2837 static const int kStringResourceOffset = 2 * sizeof(void*);
2838};
2839
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002840/**
2841 * This class exports constants and functionality from within v8 that
2842 * is necessary to implement inline functions in the v8 api. Don't
2843 * depend on functions and constants defined here.
2844 */
2845class Internals {
2846 public:
2847
2848 // These values match non-compiler-dependent values defined within
2849 // the implementation of v8.
2850 static const int kHeapObjectMapOffset = 0;
2851 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002852 static const int kStringResourceOffset =
2853 InternalConstants<sizeof(void*)>::kStringResourceOffset;
2854
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002855 static const int kProxyProxyOffset = sizeof(void*);
2856 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2857 static const int kFullStringRepresentationMask = 0x07;
2858 static const int kExternalTwoByteRepresentationTag = 0x03;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002859
2860 // These constants are compiler dependent so their values must be
2861 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002862 V8EXPORT static int kJSObjectType;
2863 V8EXPORT static int kFirstNonstringType;
2864 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002865
2866 static inline bool HasHeapObjectTag(internal::Object* value) {
2867 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2868 kHeapObjectTag);
2869 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002870
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002871 static inline bool HasSmiTag(internal::Object* value) {
2872 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2873 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002874
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002875 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002876 return SmiConstants<sizeof(void*)>::SmiToInt(value);
2877 }
2878
2879 static inline int GetInstanceType(internal::Object* obj) {
2880 typedef internal::Object O;
2881 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
2882 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
2883 }
2884
2885 static inline void* GetExternalPointer(internal::Object* obj) {
2886 if (HasSmiTag(obj)) {
2887 return obj;
2888 } else if (GetInstanceType(obj) == kProxyType) {
2889 return ReadField<void*>(obj, kProxyProxyOffset);
2890 } else {
2891 return NULL;
2892 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002893 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002894
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002895 static inline bool IsExternalTwoByteString(int instance_type) {
2896 int representation = (instance_type & kFullStringRepresentationMask);
2897 return representation == kExternalTwoByteRepresentationTag;
2898 }
2899
2900 template <typename T>
2901 static inline T ReadField(Object* ptr, int offset) {
2902 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2903 return *reinterpret_cast<T*>(addr);
2904 }
2905
2906};
2907
2908}
2909
2910
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911template <class T>
2912Handle<T>::Handle() : val_(0) { }
2913
2914
2915template <class T>
2916Local<T>::Local() : Handle<T>() { }
2917
2918
2919template <class T>
2920Local<T> Local<T>::New(Handle<T> that) {
2921 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002922 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2924}
2925
2926
2927template <class T>
2928Persistent<T> Persistent<T>::New(Handle<T> that) {
2929 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002930 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2932}
2933
2934
2935template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002936bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002938 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002939}
2940
2941
2942template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002943bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002945 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946}
2947
2948
2949template <class T>
2950void Persistent<T>::Dispose() {
2951 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002952 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002953}
2954
2955
2956template <class T>
2957Persistent<T>::Persistent() : Handle<T>() { }
2958
2959template <class T>
2960void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002961 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
2962 parameters,
2963 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964}
2965
2966template <class T>
2967void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002968 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969}
2970
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971Local<Value> Arguments::operator[](int i) const {
2972 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2973 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2974}
2975
2976
2977Local<Function> Arguments::Callee() const {
2978 return callee_;
2979}
2980
2981
2982Local<Object> Arguments::This() const {
2983 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2984}
2985
2986
2987Local<Object> Arguments::Holder() const {
2988 return holder_;
2989}
2990
2991
2992Local<Value> Arguments::Data() const {
2993 return data_;
2994}
2995
2996
2997bool Arguments::IsConstructCall() const {
2998 return is_construct_call_;
2999}
3000
3001
3002int Arguments::Length() const {
3003 return length_;
3004}
3005
3006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003007template <class T>
3008Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003009 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3010 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 return Local<T>(reinterpret_cast<T*>(after));
3012}
3013
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003014Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 return resource_name_;
3016}
3017
3018
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003019Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003020 return resource_line_offset_;
3021}
3022
3023
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003024Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003025 return resource_column_offset_;
3026}
3027
3028
3029Handle<Boolean> Boolean::New(bool value) {
3030 return value ? True() : False();
3031}
3032
3033
3034void Template::Set(const char* name, v8::Handle<Data> value) {
3035 Set(v8::String::New(name), value);
3036}
3037
3038
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003039Local<Value> Object::GetInternalField(int index) {
3040#ifndef V8_ENABLE_CHECKS
3041 Local<Value> quick_result = UncheckedGetInternalField(index);
3042 if (!quick_result.IsEmpty()) return quick_result;
3043#endif
3044 return CheckedGetInternalField(index);
3045}
3046
3047
3048Local<Value> Object::UncheckedGetInternalField(int index) {
3049 typedef internal::Object O;
3050 typedef internal::Internals I;
3051 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003052 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003053 // If the object is a plain JSObject, which is the common case,
3054 // we know where to find the internal fields and can return the
3055 // value directly.
3056 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3057 O* value = I::ReadField<O*>(obj, offset);
3058 O** result = HandleScope::CreateHandle(value);
3059 return Local<Value>(reinterpret_cast<Value*>(result));
3060 } else {
3061 return Local<Value>();
3062 }
3063}
3064
3065
3066void* External::Unwrap(Handle<v8::Value> obj) {
3067#ifdef V8_ENABLE_CHECKS
3068 return FullUnwrap(obj);
3069#else
3070 return QuickUnwrap(obj);
3071#endif
3072}
3073
3074
3075void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3076 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003077 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003078 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003079}
3080
3081
3082void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003083 typedef internal::Object O;
3084 typedef internal::Internals I;
3085
3086 O* obj = *reinterpret_cast<O**>(this);
3087
3088 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3089 // If the object is a plain JSObject, which is the common case,
3090 // we know where to find the internal fields and can return the
3091 // value directly.
3092 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3093 O* value = I::ReadField<O*>(obj, offset);
3094 return I::GetExternalPointer(value);
3095 }
3096
3097 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003098}
3099
3100
3101String* String::Cast(v8::Value* value) {
3102#ifdef V8_ENABLE_CHECKS
3103 CheckCast(value);
3104#endif
3105 return static_cast<String*>(value);
3106}
3107
3108
3109String::ExternalStringResource* String::GetExternalStringResource() const {
3110 typedef internal::Object O;
3111 typedef internal::Internals I;
3112 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003113 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003114 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003115 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3116 result = reinterpret_cast<String::ExternalStringResource*>(value);
3117 } else {
3118 result = NULL;
3119 }
3120#ifdef V8_ENABLE_CHECKS
3121 VerifyExternalStringResource(result);
3122#endif
3123 return result;
3124}
3125
3126
3127bool Value::IsString() const {
3128#ifdef V8_ENABLE_CHECKS
3129 return FullIsString();
3130#else
3131 return QuickIsString();
3132#endif
3133}
3134
3135bool Value::QuickIsString() const {
3136 typedef internal::Object O;
3137 typedef internal::Internals I;
3138 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3139 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003140 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003141}
3142
3143
3144Number* Number::Cast(v8::Value* value) {
3145#ifdef V8_ENABLE_CHECKS
3146 CheckCast(value);
3147#endif
3148 return static_cast<Number*>(value);
3149}
3150
3151
3152Integer* Integer::Cast(v8::Value* value) {
3153#ifdef V8_ENABLE_CHECKS
3154 CheckCast(value);
3155#endif
3156 return static_cast<Integer*>(value);
3157}
3158
3159
3160Date* Date::Cast(v8::Value* value) {
3161#ifdef V8_ENABLE_CHECKS
3162 CheckCast(value);
3163#endif
3164 return static_cast<Date*>(value);
3165}
3166
3167
3168Object* Object::Cast(v8::Value* value) {
3169#ifdef V8_ENABLE_CHECKS
3170 CheckCast(value);
3171#endif
3172 return static_cast<Object*>(value);
3173}
3174
3175
3176Array* Array::Cast(v8::Value* value) {
3177#ifdef V8_ENABLE_CHECKS
3178 CheckCast(value);
3179#endif
3180 return static_cast<Array*>(value);
3181}
3182
3183
3184Function* Function::Cast(v8::Value* value) {
3185#ifdef V8_ENABLE_CHECKS
3186 CheckCast(value);
3187#endif
3188 return static_cast<Function*>(value);
3189}
3190
3191
3192External* External::Cast(v8::Value* value) {
3193#ifdef V8_ENABLE_CHECKS
3194 CheckCast(value);
3195#endif
3196 return static_cast<External*>(value);
3197}
3198
3199
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003200Local<Value> AccessorInfo::Data() const {
3201 return Local<Value>(reinterpret_cast<Value*>(&args_[-3]));
3202}
3203
3204
3205Local<Object> AccessorInfo::This() const {
3206 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3207}
3208
3209
3210Local<Object> AccessorInfo::Holder() const {
3211 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3212}
3213
3214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003215/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003216 * \example shell.cc
3217 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003218 * command-line and executes them.
3219 */
3220
3221
3222/**
3223 * \example process.cc
3224 */
3225
3226
3227} // namespace v8
3228
3229
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003230#undef V8EXPORT
3231#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232#undef TYPE_CHECK
3233
3234
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003235#endif // V8_H_