blob: 13f819170376fe7c3c5bf0c4c5e7fd3c4ee219ae [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;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000506 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507};
508
509
510/**
511 * The origin, within a file, of a script.
512 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000513class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000515 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 Handle<Integer> resource_line_offset = Handle<Integer>(),
517 Handle<Integer> resource_column_offset = Handle<Integer>())
518 : resource_name_(resource_name),
519 resource_line_offset_(resource_line_offset),
520 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000521 inline Handle<Value> ResourceName() const;
522 inline Handle<Integer> ResourceLineOffset() const;
523 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000525 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 Handle<Integer> resource_line_offset_;
527 Handle<Integer> resource_column_offset_;
528};
529
530
531/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000532 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000534class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 public:
536
537 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000538 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000539 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000540 * \param source Script source code.
541 * \param origin Script origin, owned by caller, no references are kept
542 * when New() returns
543 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
544 * using pre_data speeds compilation if it's done multiple times.
545 * Owned by caller, no references are kept when New() returns.
546 * \param script_data Arbitrary data associated with script. Using
547 * this has same effect as calling SetData(), but allows data to be
548 * available to compile event handlers.
549 * \return Compiled script object (context independent; when run it
550 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000552 static Local<Script> New(Handle<String> source,
553 ScriptOrigin* origin = NULL,
554 ScriptData* pre_data = NULL,
555 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556
mads.s.agercbaa0602008-08-14 13:41:48 +0000557 /**
558 * Compiles the specified script using the specified file name
559 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000560 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000561 * \param source Script source code.
562 * \patam file_name file name object (typically a string) to be used
563 * as the script's origin.
564 * \return Compiled script object (context independent; when run it
565 * will use the currently entered context).
566 */
567 static Local<Script> New(Handle<String> source,
568 Handle<Value> file_name);
569
570 /**
571 * Compiles the specified script (bound to current context).
572 *
573 * \param source Script source code.
574 * \param origin Script origin, owned by caller, no references are kept
575 * when Compile() returns
576 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
577 * using pre_data speeds compilation if it's done multiple times.
578 * Owned by caller, no references are kept when Compile() returns.
579 * \param script_data Arbitrary data associated with script. Using
580 * this has same effect as calling SetData(), but makes data available
581 * earlier (i.e. to compile event handlers).
582 * \return Compiled script object, bound to the context that was active
583 * when this function was called. When run it will always use this
584 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000585 */
586 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000587 ScriptOrigin* origin = NULL,
588 ScriptData* pre_data = NULL,
589 Handle<String> script_data = Handle<String>());
590
591 /**
592 * Compiles the specified script using the specified file name
593 * object (typically a string) as the script's origin.
594 *
595 * \param source Script source code.
596 * \param file_name File name to use as script's origin
597 * \param script_data Arbitrary data associated with script. Using
598 * this has same effect as calling SetData(), but makes data available
599 * earlier (i.e. to compile event handlers).
600 * \return Compiled script object, bound to the context that was active
601 * when this function was called. When run it will always use this
602 * context.
603 */
604 static Local<Script> Compile(Handle<String> source,
605 Handle<Value> file_name,
606 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000607
v8.team.kasperl727e9952008-09-02 14:56:44 +0000608 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000609 * Runs the script returning the resulting value. If the script is
610 * context independent (created using ::New) it will be run in the
611 * currently entered context. If it is context specific (created
612 * using ::Compile) it will be run in the context in which it was
613 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000614 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000616
617 /**
618 * Returns the script id value.
619 */
620 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000621
622 /**
623 * Associate an additional data object with the script. This is mainly used
624 * with the debugger as this data object is only available through the
625 * debugger API.
626 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000627 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628};
629
630
631/**
632 * An error message.
633 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000634class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000636 Local<String> Get() const;
637 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000639 /**
640 * Returns the resource name for the script from where the function causing
641 * the error originates.
642 */
ager@chromium.org32912102009-01-16 10:38:43 +0000643 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000645 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000646 * Returns the resource data for the script from where the function causing
647 * the error originates.
648 */
649 Handle<Value> GetScriptData() const;
650
651 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000652 * Returns the number, 1-based, of the line where the error occurred.
653 */
ager@chromium.org32912102009-01-16 10:38:43 +0000654 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000656 /**
657 * Returns the index within the script of the first character where
658 * the error occurred.
659 */
ager@chromium.org32912102009-01-16 10:38:43 +0000660 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000661
662 /**
663 * Returns the index within the script of the last character where
664 * the error occurred.
665 */
ager@chromium.org32912102009-01-16 10:38:43 +0000666 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000667
668 /**
669 * Returns the index within the line of the first character where
670 * the error occurred.
671 */
ager@chromium.org32912102009-01-16 10:38:43 +0000672 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000673
674 /**
675 * Returns the index within the line of the last character where
676 * the error occurred.
677 */
ager@chromium.org32912102009-01-16 10:38:43 +0000678 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000679
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680 // TODO(1245381): Print to a string instead of on a FILE.
681 static void PrintCurrentStackTrace(FILE* out);
682};
683
684
685// --- V a l u e ---
686
687
688/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000689 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000691class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 public:
693
694 /**
695 * Returns true if this value is the undefined value. See ECMA-262
696 * 4.3.10.
697 */
ager@chromium.org32912102009-01-16 10:38:43 +0000698 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
700 /**
701 * Returns true if this value is the null value. See ECMA-262
702 * 4.3.11.
703 */
ager@chromium.org32912102009-01-16 10:38:43 +0000704 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
706 /**
707 * Returns true if this value is true.
708 */
ager@chromium.org32912102009-01-16 10:38:43 +0000709 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
711 /**
712 * Returns true if this value is false.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
716 /**
717 * Returns true if this value is an instance of the String type.
718 * See ECMA-262 8.4.
719 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000720 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721
722 /**
723 * Returns true if this value is a function.
724 */
ager@chromium.org32912102009-01-16 10:38:43 +0000725 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726
727 /**
728 * Returns true if this value is an array.
729 */
ager@chromium.org32912102009-01-16 10:38:43 +0000730 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731
v8.team.kasperl727e9952008-09-02 14:56:44 +0000732 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 * Returns true if this value is an object.
734 */
ager@chromium.org32912102009-01-16 10:38:43 +0000735 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736
v8.team.kasperl727e9952008-09-02 14:56:44 +0000737 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738 * Returns true if this value is boolean.
739 */
ager@chromium.org32912102009-01-16 10:38:43 +0000740 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000741
v8.team.kasperl727e9952008-09-02 14:56:44 +0000742 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 * Returns true if this value is a number.
744 */
ager@chromium.org32912102009-01-16 10:38:43 +0000745 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746
v8.team.kasperl727e9952008-09-02 14:56:44 +0000747 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 * Returns true if this value is external.
749 */
ager@chromium.org32912102009-01-16 10:38:43 +0000750 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000751
v8.team.kasperl727e9952008-09-02 14:56:44 +0000752 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 * Returns true if this value is a 32-bit signed integer.
754 */
ager@chromium.org32912102009-01-16 10:38:43 +0000755 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000757 /**
758 * Returns true if this value is a Date.
759 */
ager@chromium.org32912102009-01-16 10:38:43 +0000760 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000761
ager@chromium.org32912102009-01-16 10:38:43 +0000762 Local<Boolean> ToBoolean() const;
763 Local<Number> ToNumber() const;
764 Local<String> ToString() const;
765 Local<String> ToDetailString() const;
766 Local<Object> ToObject() const;
767 Local<Integer> ToInteger() const;
768 Local<Uint32> ToUint32() const;
769 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770
771 /**
772 * Attempts to convert a string to an array index.
773 * Returns an empty handle if the conversion fails.
774 */
ager@chromium.org32912102009-01-16 10:38:43 +0000775 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776
ager@chromium.org32912102009-01-16 10:38:43 +0000777 bool BooleanValue() const;
778 double NumberValue() const;
779 int64_t IntegerValue() const;
780 uint32_t Uint32Value() const;
781 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782
783 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000784 bool Equals(Handle<Value> that) const;
785 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000786
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000787 private:
788 inline bool QuickIsString() const;
789 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790};
791
792
793/**
794 * The superclass of primitive values. See ECMA-262 4.3.2.
795 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000796class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
798
799/**
800 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
801 * or false value.
802 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000803class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000805 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 static inline Handle<Boolean> New(bool value);
807};
808
809
810/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000811 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000813class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000815
816 /**
817 * Returns the number of characters in this string.
818 */
ager@chromium.org32912102009-01-16 10:38:43 +0000819 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820
v8.team.kasperl727e9952008-09-02 14:56:44 +0000821 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000822 * Returns the number of bytes in the UTF-8 encoded
823 * representation of this string.
824 */
ager@chromium.org32912102009-01-16 10:38:43 +0000825 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000826
827 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000828 * Write the contents of the string to an external buffer.
829 * If no arguments are given, expects the buffer to be large
830 * enough to hold the entire string and NULL terminator. Copies
831 * the contents of the string and the NULL terminator into the
832 * buffer.
833 *
834 * Copies up to length characters into the output buffer.
835 * Only null-terminates if there is enough space in the buffer.
836 *
837 * \param buffer The buffer into which the string will be copied.
838 * \param start The starting position within the string at which
839 * copying begins.
840 * \param length The number of bytes to copy from the string.
841 * \return The number of characters copied to the buffer
842 * excluding the NULL terminator.
843 */
ager@chromium.org32912102009-01-16 10:38:43 +0000844 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
845 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
846 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847
v8.team.kasperl727e9952008-09-02 14:56:44 +0000848 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000849 * A zero length string.
850 */
851 static v8::Local<v8::String> Empty();
852
853 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000854 * Returns true if the string is external
855 */
ager@chromium.org32912102009-01-16 10:38:43 +0000856 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857
v8.team.kasperl727e9952008-09-02 14:56:44 +0000858 /**
859 * Returns true if the string is both external and ascii
860 */
ager@chromium.org32912102009-01-16 10:38:43 +0000861 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000862
863 class V8EXPORT ExternalStringResourceBase {
864 public:
865 virtual ~ExternalStringResourceBase() {}
866 protected:
867 ExternalStringResourceBase() {}
868 private:
869 // Disallow copying and assigning.
870 ExternalStringResourceBase(const ExternalStringResourceBase&);
871 void operator=(const ExternalStringResourceBase&);
872 };
873
v8.team.kasperl727e9952008-09-02 14:56:44 +0000874 /**
875 * An ExternalStringResource is a wrapper around a two-byte string
876 * buffer that resides outside V8's heap. Implement an
877 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000878 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000879 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000880 class V8EXPORT ExternalStringResource
881 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 public:
883 /**
884 * Override the destructor to manage the life cycle of the underlying
885 * buffer.
886 */
887 virtual ~ExternalStringResource() {}
888 /** The string data from the underlying buffer.*/
889 virtual const uint16_t* data() const = 0;
890 /** The length of the string. That is, the number of two-byte characters.*/
891 virtual size_t length() const = 0;
892 protected:
893 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 };
895
896 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000897 * An ExternalAsciiStringResource is a wrapper around an ascii
898 * string buffer that resides outside V8's heap. Implement an
899 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000900 * underlying buffer. Note that the string data must be immutable
901 * and that the data must be strict 7-bit ASCII, not Latin1 or
902 * UTF-8, which would require special treatment internally in the
903 * engine and, in the case of UTF-8, do not allow efficient indexing.
904 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000905 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000907 class V8EXPORT ExternalAsciiStringResource
908 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 public:
910 /**
911 * Override the destructor to manage the life cycle of the underlying
912 * buffer.
913 */
914 virtual ~ExternalAsciiStringResource() {}
915 /** The string data from the underlying buffer.*/
916 virtual const char* data() const = 0;
917 /** The number of ascii characters in the string.*/
918 virtual size_t length() const = 0;
919 protected:
920 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 };
922
923 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000924 * Get the ExternalStringResource for an external string. Returns
925 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000927 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
929 /**
930 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000931 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932 */
ager@chromium.org32912102009-01-16 10:38:43 +0000933 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000935 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936
937 /**
938 * Allocates a new string from either utf-8 encoded or ascii data.
939 * The second parameter 'length' gives the buffer length.
940 * If the data is utf-8 encoded, the caller must
941 * be careful to supply the length parameter.
942 * If it is not given, the function calls
943 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000944 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 */
946 static Local<String> New(const char* data, int length = -1);
947
948 /** Allocates a new string from utf16 data.*/
949 static Local<String> New(const uint16_t* data, int length = -1);
950
951 /** Creates a symbol. Returns one if it exists already.*/
952 static Local<String> NewSymbol(const char* data, int length = -1);
953
v8.team.kasperl727e9952008-09-02 14:56:44 +0000954 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000955 * Creates a new string by concatenating the left and the right strings
956 * passed in as parameters.
957 */
958 static Local<String> Concat(Handle<String> left, Handle<String>right);
959
960 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000961 * Creates a new external string using the data defined in the given
962 * resource. The resource is deleted when the external string is no
963 * longer live on V8's heap. The caller of this function should not
964 * delete or modify the resource. Neither should the underlying buffer be
965 * deallocated or modified except through the destructor of the
966 * external string resource.
967 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000969
ager@chromium.org6f10e412009-02-13 10:11:16 +0000970 /**
971 * Associate an external string resource with this string by transforming it
972 * in place so that existing references to this string in the JavaScript heap
973 * will use the external string resource. The external string resource's
974 * character contents needs to be equivalent to this string.
975 * Returns true if the string has been changed to be an external string.
976 * The string is not modified if the operation fails.
977 */
978 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979
v8.team.kasperl727e9952008-09-02 14:56:44 +0000980 /**
981 * Creates a new external string using the ascii data defined in the given
982 * resource. The resource is deleted when the external string is no
983 * longer live on V8's heap. The caller of this function should not
984 * delete or modify the resource. Neither should the underlying buffer be
985 * deallocated or modified except through the destructor of the
986 * external string resource.
987 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000989
ager@chromium.org6f10e412009-02-13 10:11:16 +0000990 /**
991 * Associate an external string resource with this string by transforming it
992 * in place so that existing references to this string in the JavaScript heap
993 * will use the external string resource. The external string resource's
994 * character contents needs to be equivalent to this string.
995 * Returns true if the string has been changed to be an external string.
996 * The string is not modified if the operation fails.
997 */
998 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001000 /**
1001 * Returns true if this string can be made external.
1002 */
1003 bool CanMakeExternal();
1004
kasper.lund7276f142008-07-30 08:49:36 +00001005 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 static Local<String> NewUndetectable(const char* data, int length = -1);
1007
kasper.lund7276f142008-07-30 08:49:36 +00001008 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
1010
1011 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001012 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001013 * you want to print the object. If conversion to a string fails
1014 * (eg. due to an exception in the toString() method of the object)
1015 * then the length() method returns 0 and the * operator returns
1016 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001017 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001018 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001019 public:
1020 explicit Utf8Value(Handle<v8::Value> obj);
1021 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001022 char* operator*() { return str_; }
1023 const char* operator*() const { return str_; }
1024 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001025 private:
1026 char* str_;
1027 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001028
1029 // Disallow copying and assigning.
1030 Utf8Value(const Utf8Value&);
1031 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001032 };
1033
1034 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035 * Converts an object to an ascii string.
1036 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001037 * If conversion to a string fails (eg. due to an exception in the toString()
1038 * method of the object) then the length() method returns 0 and the * operator
1039 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001041 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 public:
1043 explicit AsciiValue(Handle<v8::Value> obj);
1044 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001045 char* operator*() { return str_; }
1046 const char* operator*() const { return str_; }
1047 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 private:
1049 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001050 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001051
1052 // Disallow copying and assigning.
1053 AsciiValue(const AsciiValue&);
1054 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 };
1056
1057 /**
1058 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001059 * If conversion to a string fails (eg. due to an exception in the toString()
1060 * method of the object) then the length() method returns 0 and the * operator
1061 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001063 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 public:
1065 explicit Value(Handle<v8::Value> obj);
1066 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001067 uint16_t* operator*() { return str_; }
1068 const uint16_t* operator*() const { return str_; }
1069 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 private:
1071 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001072 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001073
1074 // Disallow copying and assigning.
1075 Value(const Value&);
1076 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001078
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001079 private:
1080 void VerifyExternalStringResource(ExternalStringResource* val) const;
1081 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082};
1083
1084
1085/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001088class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001090 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001092 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 private:
1094 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001095 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096};
1097
1098
1099/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001100 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001102class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 public:
1104 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001105 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001106 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001107 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 private:
1109 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001110 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111};
1112
1113
1114/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001115 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001117class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001119 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 private:
1121 Int32();
1122};
1123
1124
1125/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001126 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001128class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001130 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 private:
1132 Uint32();
1133};
1134
1135
1136/**
1137 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1138 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001139class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 public:
1141 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001142
1143 /**
1144 * A specialization of Value::NumberValue that is more efficient
1145 * because we know the structure of this object.
1146 */
ager@chromium.org32912102009-01-16 10:38:43 +00001147 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001148
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001149 static inline Date* Cast(v8::Value* obj);
1150 private:
1151 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152};
1153
1154
1155enum PropertyAttribute {
1156 None = 0,
1157 ReadOnly = 1 << 0,
1158 DontEnum = 1 << 1,
1159 DontDelete = 1 << 2
1160};
1161
ager@chromium.org3811b432009-10-28 14:53:37 +00001162enum ExternalArrayType {
1163 kExternalByteArray = 1,
1164 kExternalUnsignedByteArray,
1165 kExternalShortArray,
1166 kExternalUnsignedShortArray,
1167 kExternalIntArray,
1168 kExternalUnsignedIntArray,
1169 kExternalFloatArray
1170};
1171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001173 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001175class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176 public:
1177 bool Set(Handle<Value> key,
1178 Handle<Value> value,
1179 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001180
ager@chromium.orge2902be2009-06-08 12:21:35 +00001181 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001182 // overriding accessors or read-only properties.
1183 //
1184 // Note that if the object has an interceptor the property will be set
1185 // locally, but since the interceptor takes precedence the local property
1186 // will only be returned if the interceptor doesn't return a value.
1187 //
1188 // Note also that this only works for named properties.
1189 bool ForceSet(Handle<Value> key,
1190 Handle<Value> value,
1191 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193 Local<Value> Get(Handle<Value> key);
1194
1195 // TODO(1245389): Replace the type-specific versions of these
1196 // functions with generic ones that accept a Handle<Value> key.
1197 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001200
1201 // Delete a property on this object bypassing interceptors and
1202 // ignoring dont-delete attributes.
1203 bool ForceDelete(Handle<Value> key);
1204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001206
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 bool Delete(uint32_t index);
1208
1209 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001210 * Returns an array containing the names of the enumerable properties
1211 * of this object, including properties from prototype objects. The
1212 * array returned by this method contains the same values as would
1213 * be enumerated by a for-in statement over this object.
1214 */
1215 Local<Array> GetPropertyNames();
1216
1217 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 * Get the prototype object. This does not skip objects marked to
1219 * be skipped by __proto__ and it does not consult the security
1220 * handler.
1221 */
1222 Local<Value> GetPrototype();
1223
1224 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001225 * Set the prototype object. This does not skip objects marked to
1226 * be skipped by __proto__ and it does not consult the security
1227 * handler.
1228 */
1229 bool SetPrototype(Handle<Value> prototype);
1230
1231 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001232 * Finds an instance of the given function template in the prototype
1233 * chain.
1234 */
1235 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1236
1237 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 * Call builtin Object.prototype.toString on this object.
1239 * This is different from Value::ToString() that may call
1240 * user-defined toString function. This one does not.
1241 */
1242 Local<String> ObjectProtoToString();
1243
kasper.lund212ac232008-07-16 07:07:30 +00001244 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001246 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001247 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001248 /** Sets the value in an internal field. */
1249 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001251 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001252 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001253
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001254 /** Sets a native pointer in an internal field. */
1255 void SetPointerInInternalField(int index, void* value);
1256
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 // Testers for local properties.
1258 bool HasRealNamedProperty(Handle<String> key);
1259 bool HasRealIndexedProperty(uint32_t index);
1260 bool HasRealNamedCallbackProperty(Handle<String> key);
1261
1262 /**
1263 * If result.IsEmpty() no real property was located in the prototype chain.
1264 * This means interceptors in the prototype chain are not called.
1265 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001266 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1267
1268 /**
1269 * If result.IsEmpty() no real property was located on the object or
1270 * in the prototype chain.
1271 * This means interceptors in the prototype chain are not called.
1272 */
1273 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274
1275 /** Tests for a named lookup interceptor.*/
1276 bool HasNamedLookupInterceptor();
1277
kasper.lund212ac232008-07-16 07:07:30 +00001278 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 bool HasIndexedLookupInterceptor();
1280
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001281 /**
1282 * Turns on access check on the object if the object is an instance of
1283 * a template that has access check callbacks. If an object has no
1284 * access check info, the object cannot be accessed by anyone.
1285 */
1286 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001287
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001288 /**
1289 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001290 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001291 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001292 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001293 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001294 */
1295 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001296
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001297 /**
1298 * Access hidden properties on JavaScript objects. These properties are
1299 * hidden from the executing JavaScript and only accessible through the V8
1300 * C++ API. Hidden properties introduced by V8 internally (for example the
1301 * identity hash) are prefixed with "v8::".
1302 */
1303 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1304 Local<Value> GetHiddenValue(Handle<String> key);
1305 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001306
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001307 /**
1308 * Returns true if this is an instance of an api function (one
1309 * created from a function created from a function template) and has
1310 * been modified since it was created. Note that this method is
1311 * conservative and may return true for objects that haven't actually
1312 * been modified.
1313 */
1314 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001315
1316 /**
1317 * Clone this object with a fast but shallow copy. Values will point
1318 * to the same values as the original object.
1319 */
1320 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001322 /**
1323 * Set the backing store of the indexed properties to be managed by the
1324 * embedding layer. Access to the indexed properties will follow the rules
1325 * spelled out in CanvasPixelArray.
1326 * Note: The embedding program still owns the data and needs to ensure that
1327 * the backing store is preserved while V8 has a reference.
1328 */
1329 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1330
ager@chromium.org3811b432009-10-28 14:53:37 +00001331 /**
1332 * Set the backing store of the indexed properties to be managed by the
1333 * embedding layer. Access to the indexed properties will follow the rules
1334 * spelled out for the CanvasArray subtypes in the WebGL specification.
1335 * Note: The embedding program still owns the data and needs to ensure that
1336 * the backing store is preserved while V8 has a reference.
1337 */
1338 void SetIndexedPropertiesToExternalArrayData(void* data,
1339 ExternalArrayType array_type,
1340 int number_of_elements);
1341
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001343 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 private:
1345 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001346 static void CheckCast(Value* obj);
1347 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001348 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001349
1350 /**
1351 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001352 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001353 */
1354 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355};
1356
1357
1358/**
1359 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1360 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001361class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001363 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364
ager@chromium.org3e875802009-06-29 08:26:34 +00001365 /**
1366 * Clones an element at index |index|. Returns an empty
1367 * handle if cloning fails (for any reason).
1368 */
1369 Local<Object> CloneElementAt(uint32_t index);
1370
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001372 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 private:
1374 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001375 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376};
1377
1378
1379/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001380 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001382class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001384 Local<Object> NewInstance() const;
1385 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1387 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001388 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001389
1390 /**
1391 * Returns zero based line number of function body and
1392 * kLineOffsetNotFound if no information available.
1393 */
1394 int GetScriptLineNumber() const;
1395 ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001396 static inline Function* Cast(Value* obj);
ager@chromium.org5c838252010-02-19 08:53:10 +00001397 static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398 private:
1399 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001400 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401};
1402
1403
1404/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001405 * A JavaScript value that wraps a C++ void*. This type of value is
1406 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001408 *
1409 * The Wrap function V8 will return the most optimal Value object wrapping the
1410 * C++ void*. The type of the value is not guaranteed to be an External object
1411 * and no assumptions about its type should be made. To access the wrapped
1412 * value Unwrap should be used, all other operations on that object will lead
1413 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001415class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001417 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001418 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001421 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001422 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 private:
1424 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001425 static void CheckCast(v8::Value* obj);
1426 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1427 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428};
1429
1430
1431// --- T e m p l a t e s ---
1432
1433
1434/**
1435 * The superclass of object and function templates.
1436 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001437class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 public:
1439 /** Adds a property to each instance created by this template.*/
1440 void Set(Handle<String> name, Handle<Data> value,
1441 PropertyAttribute attributes = None);
1442 inline void Set(const char* name, Handle<Data> value);
1443 private:
1444 Template();
1445
1446 friend class ObjectTemplate;
1447 friend class FunctionTemplate;
1448};
1449
1450
1451/**
1452 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001453 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 * including the receiver, the number and values of arguments, and
1455 * the holder of the function.
1456 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001457class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 public:
1459 inline int Length() const;
1460 inline Local<Value> operator[](int i) const;
1461 inline Local<Function> Callee() const;
1462 inline Local<Object> This() const;
1463 inline Local<Object> Holder() const;
1464 inline bool IsConstructCall() const;
1465 inline Local<Value> Data() const;
1466 private:
1467 Arguments();
1468 friend class ImplementationUtilities;
1469 inline Arguments(Local<Value> data,
1470 Local<Object> holder,
1471 Local<Function> callee,
1472 bool is_construct_call,
1473 void** values, int length);
1474 Local<Value> data_;
1475 Local<Object> holder_;
1476 Local<Function> callee_;
1477 bool is_construct_call_;
1478 void** values_;
1479 int length_;
1480};
1481
1482
1483/**
1484 * The information passed to an accessor callback about the context
1485 * of the property access.
1486 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001487class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001489 inline AccessorInfo(internal::Object** args)
1490 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 inline Local<Value> Data() const;
1492 inline Local<Object> This() const;
1493 inline Local<Object> Holder() const;
1494 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001495 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496};
1497
1498
1499typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1500
1501typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1502
1503/**
1504 * Accessor[Getter|Setter] are used as callback functions when
1505 * setting|getting a particular property. See objectTemplate::SetAccessor.
1506 */
1507typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1508 const AccessorInfo& info);
1509
1510
1511typedef void (*AccessorSetter)(Local<String> property,
1512 Local<Value> value,
1513 const AccessorInfo& info);
1514
1515
1516/**
1517 * NamedProperty[Getter|Setter] are used as interceptors on object.
1518 * See ObjectTemplate::SetNamedPropertyHandler.
1519 */
1520typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1521 const AccessorInfo& info);
1522
1523
1524/**
1525 * Returns the value if the setter intercepts the request.
1526 * Otherwise, returns an empty handle.
1527 */
1528typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1529 Local<Value> value,
1530 const AccessorInfo& info);
1531
1532
1533/**
1534 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001535 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001536 */
1537typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1538 const AccessorInfo& info);
1539
1540
1541/**
1542 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001543 * The return value is true if the property could be deleted and false
1544 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545 */
1546typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1547 const AccessorInfo& info);
1548
1549/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001550 * Returns an array containing the names of the properties the named
1551 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552 */
1553typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1554
v8.team.kasperl727e9952008-09-02 14:56:44 +00001555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001557 * Returns the value of the property if the getter intercepts the
1558 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 */
1560typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1561 const AccessorInfo& info);
1562
1563
1564/**
1565 * Returns the value if the setter intercepts the request.
1566 * Otherwise, returns an empty handle.
1567 */
1568typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1569 Local<Value> value,
1570 const AccessorInfo& info);
1571
1572
1573/**
1574 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001575 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576 */
1577typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1578 const AccessorInfo& info);
1579
1580/**
1581 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001582 * The return value is true if the property could be deleted and false
1583 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 */
1585typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1586 const AccessorInfo& info);
1587
v8.team.kasperl727e9952008-09-02 14:56:44 +00001588/**
1589 * Returns an array containing the indices of the properties the
1590 * indexed property getter intercepts.
1591 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1593
1594
1595/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001596 * Access control specifications.
1597 *
1598 * Some accessors should be accessible across contexts. These
1599 * accessors have an explicit access control parameter which specifies
1600 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001601 *
1602 * Additionally, for security, accessors can prohibit overwriting by
1603 * accessors defined in JavaScript. For objects that have such
1604 * accessors either locally or in their prototype chain it is not
1605 * possible to overwrite the accessor by using __defineGetter__ or
1606 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 */
1608enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001609 DEFAULT = 0,
1610 ALL_CAN_READ = 1,
1611 ALL_CAN_WRITE = 1 << 1,
1612 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613};
1614
1615
1616/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001617 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 */
1619enum AccessType {
1620 ACCESS_GET,
1621 ACCESS_SET,
1622 ACCESS_HAS,
1623 ACCESS_DELETE,
1624 ACCESS_KEYS
1625};
1626
v8.team.kasperl727e9952008-09-02 14:56:44 +00001627
1628/**
1629 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001630 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001631 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001632typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 Local<Value> key,
1634 AccessType type,
1635 Local<Value> data);
1636
v8.team.kasperl727e9952008-09-02 14:56:44 +00001637
1638/**
1639 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001640 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001641 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001642typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 uint32_t index,
1644 AccessType type,
1645 Local<Value> data);
1646
1647
1648/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001649 * A FunctionTemplate is used to create functions at runtime. There
1650 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001651 * context. The lifetime of the created function is equal to the
1652 * lifetime of the context. So in case the embedder needs to create
1653 * temporary functions that can be collected using Scripts is
1654 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 *
1656 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001657 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001659 * A FunctionTemplate has a corresponding instance template which is
1660 * used to create object instances when the function is used as a
1661 * constructor. Properties added to the instance template are added to
1662 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 *
1664 * A FunctionTemplate can have a prototype template. The prototype template
1665 * is used to create the prototype object of the function.
1666 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001667 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001669 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1671 * t->Set("func_property", v8::Number::New(1));
1672 *
1673 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1674 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1675 * proto_t->Set("proto_const", v8::Number::New(2));
1676 *
1677 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1678 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1679 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1680 * instance_t->Set("instance_property", Number::New(3));
1681 *
1682 * v8::Local<v8::Function> function = t->GetFunction();
1683 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001684 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 *
1686 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001687 * and "instance" for the instance object created above. The function
1688 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001690 * \code
1691 * func_property in function == true;
1692 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001694 * function.prototype.proto_method() invokes 'InvokeCallback'
1695 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001697 * instance instanceof function == true;
1698 * instance.instance_accessor calls 'InstanceAccessorCallback'
1699 * instance.instance_property == 3;
1700 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001702 * A FunctionTemplate can inherit from another one by calling the
1703 * FunctionTemplate::Inherit method. The following graph illustrates
1704 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001706 * \code
1707 * FunctionTemplate Parent -> Parent() . prototype -> { }
1708 * ^ ^
1709 * | Inherit(Parent) | .__proto__
1710 * | |
1711 * FunctionTemplate Child -> Child() . prototype -> { }
1712 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001714 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1715 * object of the Child() function has __proto__ pointing to the
1716 * Parent() function's prototype object. An instance of the Child
1717 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001719 * Let Parent be the FunctionTemplate initialized in the previous
1720 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001722 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 * Local<FunctionTemplate> parent = t;
1724 * Local<FunctionTemplate> child = FunctionTemplate::New();
1725 * child->Inherit(parent);
1726 *
1727 * Local<Function> child_function = child->GetFunction();
1728 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001729 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001731 * The Child function and Child instance will have the following
1732 * properties:
1733 *
1734 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001736 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001738 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001740class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 public:
1742 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001743 static Local<FunctionTemplate> New(
1744 InvocationCallback callback = 0,
1745 Handle<Value> data = Handle<Value>(),
1746 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 /** Returns the unique function instance in the current execution context.*/
1748 Local<Function> GetFunction();
1749
v8.team.kasperl727e9952008-09-02 14:56:44 +00001750 /**
1751 * Set the call-handler callback for a FunctionTemplate. This
1752 * callback is called whenever the function created from this
1753 * FunctionTemplate is called.
1754 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 void SetCallHandler(InvocationCallback callback,
1756 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757
v8.team.kasperl727e9952008-09-02 14:56:44 +00001758 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 Local<ObjectTemplate> InstanceTemplate();
1760
1761 /** Causes the function template to inherit from a parent function template.*/
1762 void Inherit(Handle<FunctionTemplate> parent);
1763
1764 /**
1765 * A PrototypeTemplate is the template used to create the prototype object
1766 * of the function created by this template.
1767 */
1768 Local<ObjectTemplate> PrototypeTemplate();
1769
v8.team.kasperl727e9952008-09-02 14:56:44 +00001770
1771 /**
1772 * Set the class name of the FunctionTemplate. This is used for
1773 * printing objects created with the function created from the
1774 * FunctionTemplate as its constructor.
1775 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 void SetClassName(Handle<String> name);
1777
1778 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001779 * Determines whether the __proto__ accessor ignores instances of
1780 * the function template. If instances of the function template are
1781 * ignored, __proto__ skips all instances and instead returns the
1782 * next object in the prototype chain.
1783 *
1784 * Call with a value of true to make the __proto__ accessor ignore
1785 * instances of the function template. Call with a value of false
1786 * to make the __proto__ accessor not ignore instances of the
1787 * function template. By default, instances of a function template
1788 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 */
1790 void SetHiddenPrototype(bool value);
1791
1792 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001793 * Returns true if the given object is an instance of this function
1794 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 */
1796 bool HasInstance(Handle<Value> object);
1797
1798 private:
1799 FunctionTemplate();
1800 void AddInstancePropertyAccessor(Handle<String> name,
1801 AccessorGetter getter,
1802 AccessorSetter setter,
1803 Handle<Value> data,
1804 AccessControl settings,
1805 PropertyAttribute attributes);
1806 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1807 NamedPropertySetter setter,
1808 NamedPropertyQuery query,
1809 NamedPropertyDeleter remover,
1810 NamedPropertyEnumerator enumerator,
1811 Handle<Value> data);
1812 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1813 IndexedPropertySetter setter,
1814 IndexedPropertyQuery query,
1815 IndexedPropertyDeleter remover,
1816 IndexedPropertyEnumerator enumerator,
1817 Handle<Value> data);
1818 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1819 Handle<Value> data);
1820
1821 friend class Context;
1822 friend class ObjectTemplate;
1823};
1824
1825
1826/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001827 * An ObjectTemplate is used to create objects at runtime.
1828 *
1829 * Properties added to an ObjectTemplate are added to each object
1830 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001832class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001834 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001836
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 /** Creates a new instance of this template.*/
1838 Local<Object> NewInstance();
1839
1840 /**
1841 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001842 *
1843 * Whenever the property with the given name is accessed on objects
1844 * created from this ObjectTemplate the getter and setter callbacks
1845 * are called instead of getting and setting the property directly
1846 * on the JavaScript object.
1847 *
1848 * \param name The name of the property for which an accessor is added.
1849 * \param getter The callback to invoke when getting the property.
1850 * \param setter The callback to invoke when setting the property.
1851 * \param data A piece of data that will be passed to the getter and setter
1852 * callbacks whenever they are invoked.
1853 * \param settings Access control settings for the accessor. This is a bit
1854 * field consisting of one of more of
1855 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1856 * The default is to not allow cross-context access.
1857 * ALL_CAN_READ means that all cross-context reads are allowed.
1858 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1859 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1860 * cross-context access.
1861 * \param attribute The attributes of the property for which an accessor
1862 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 */
1864 void SetAccessor(Handle<String> name,
1865 AccessorGetter getter,
1866 AccessorSetter setter = 0,
1867 Handle<Value> data = Handle<Value>(),
1868 AccessControl settings = DEFAULT,
1869 PropertyAttribute attribute = None);
1870
1871 /**
1872 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001873 *
1874 * Whenever a named property is accessed on objects created from
1875 * this object template, the provided callback is invoked instead of
1876 * accessing the property directly on the JavaScript object.
1877 *
1878 * \param getter The callback to invoke when getting a property.
1879 * \param setter The callback to invoke when setting a property.
1880 * \param query The callback to invoke to check is an object has a property.
1881 * \param deleter The callback to invoke when deleting a property.
1882 * \param enumerator The callback to invoke to enumerate all the named
1883 * properties of an object.
1884 * \param data A piece of data that will be passed to the callbacks
1885 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 */
1887 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1888 NamedPropertySetter setter = 0,
1889 NamedPropertyQuery query = 0,
1890 NamedPropertyDeleter deleter = 0,
1891 NamedPropertyEnumerator enumerator = 0,
1892 Handle<Value> data = Handle<Value>());
1893
1894 /**
1895 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001896 *
1897 * Whenever an indexed property is accessed on objects created from
1898 * this object template, the provided callback is invoked instead of
1899 * accessing the property directly on the JavaScript object.
1900 *
1901 * \param getter The callback to invoke when getting a property.
1902 * \param setter The callback to invoke when setting a property.
1903 * \param query The callback to invoke to check is an object has a property.
1904 * \param deleter The callback to invoke when deleting a property.
1905 * \param enumerator The callback to invoke to enumerate all the indexed
1906 * properties of an object.
1907 * \param data A piece of data that will be passed to the callbacks
1908 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 */
1910 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1911 IndexedPropertySetter setter = 0,
1912 IndexedPropertyQuery query = 0,
1913 IndexedPropertyDeleter deleter = 0,
1914 IndexedPropertyEnumerator enumerator = 0,
1915 Handle<Value> data = Handle<Value>());
1916 /**
1917 * Sets the callback to be used when calling instances created from
1918 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001919 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 * function.
1921 */
1922 void SetCallAsFunctionHandler(InvocationCallback callback,
1923 Handle<Value> data = Handle<Value>());
1924
v8.team.kasperl727e9952008-09-02 14:56:44 +00001925 /**
1926 * Mark object instances of the template as undetectable.
1927 *
1928 * In many ways, undetectable objects behave as though they are not
1929 * there. They behave like 'undefined' in conditionals and when
1930 * printed. However, properties can be accessed and called as on
1931 * normal objects.
1932 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 void MarkAsUndetectable();
1934
v8.team.kasperl727e9952008-09-02 14:56:44 +00001935 /**
1936 * Sets access check callbacks on the object template.
1937 *
1938 * When accessing properties on instances of this object template,
1939 * the access check callback will be called to determine whether or
1940 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001941 * The last parameter specifies whether access checks are turned
1942 * on by default on instances. If access checks are off by default,
1943 * they can be turned on on individual instances by calling
1944 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001945 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1947 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001948 Handle<Value> data = Handle<Value>(),
1949 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950
kasper.lund212ac232008-07-16 07:07:30 +00001951 /**
1952 * Gets the number of internal fields for objects generated from
1953 * this template.
1954 */
1955 int InternalFieldCount();
1956
1957 /**
1958 * Sets the number of internal fields for objects generated from
1959 * this template.
1960 */
1961 void SetInternalFieldCount(int value);
1962
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 private:
1964 ObjectTemplate();
1965 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1966 friend class FunctionTemplate;
1967};
1968
1969
1970/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001971 * A Signature specifies which receivers and arguments a function can
1972 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001974class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 public:
1976 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1977 Handle<FunctionTemplate>(),
1978 int argc = 0,
1979 Handle<FunctionTemplate> argv[] = 0);
1980 private:
1981 Signature();
1982};
1983
1984
1985/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001986 * A utility for determining the type of objects based on the template
1987 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001989class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 public:
1991 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1992 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1993 int match(Handle<Value> value);
1994 private:
1995 TypeSwitch();
1996};
1997
1998
1999// --- E x t e n s i o n s ---
2000
2001
2002/**
2003 * Ignore
2004 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002005class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 public:
2007 Extension(const char* name,
2008 const char* source = 0,
2009 int dep_count = 0,
2010 const char** deps = 0);
2011 virtual ~Extension() { }
2012 virtual v8::Handle<v8::FunctionTemplate>
2013 GetNativeFunction(v8::Handle<v8::String> name) {
2014 return v8::Handle<v8::FunctionTemplate>();
2015 }
2016
2017 const char* name() { return name_; }
2018 const char* source() { return source_; }
2019 int dependency_count() { return dep_count_; }
2020 const char** dependencies() { return deps_; }
2021 void set_auto_enable(bool value) { auto_enable_ = value; }
2022 bool auto_enable() { return auto_enable_; }
2023
2024 private:
2025 const char* name_;
2026 const char* source_;
2027 int dep_count_;
2028 const char** deps_;
2029 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002030
2031 // Disallow copying and assigning.
2032 Extension(const Extension&);
2033 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034};
2035
2036
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002037void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038
2039
2040/**
2041 * Ignore
2042 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002043class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 public:
2045 inline DeclareExtension(Extension* extension) {
2046 RegisterExtension(extension);
2047 }
2048};
2049
2050
2051// --- S t a t i c s ---
2052
2053
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002054Handle<Primitive> V8EXPORT Undefined();
2055Handle<Primitive> V8EXPORT Null();
2056Handle<Boolean> V8EXPORT True();
2057Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058
2059
2060/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002061 * A set of constraints that specifies the limits of the runtime's memory use.
2062 * You must set the heap size before initializing the VM - the size cannot be
2063 * adjusted after the VM is initialized.
2064 *
2065 * If you are using threads then you should hold the V8::Locker lock while
2066 * setting the stack limit and you must set a non-default stack limit separately
2067 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002069class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 public:
2071 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002072 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002074 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002076 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002077 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2079 private:
2080 int max_young_space_size_;
2081 int max_old_space_size_;
2082 uint32_t* stack_limit_;
2083};
2084
2085
2086bool SetResourceConstraints(ResourceConstraints* constraints);
2087
2088
2089// --- E x c e p t i o n s ---
2090
2091
2092typedef void (*FatalErrorCallback)(const char* location, const char* message);
2093
2094
2095typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2096
2097
2098/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002099 * Schedules an exception to be thrown when returning to JavaScript. When an
2100 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002102 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002104Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105
2106/**
2107 * Create new error objects by calling the corresponding error object
2108 * constructor with the message.
2109 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002110class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 public:
2112 static Local<Value> RangeError(Handle<String> message);
2113 static Local<Value> ReferenceError(Handle<String> message);
2114 static Local<Value> SyntaxError(Handle<String> message);
2115 static Local<Value> TypeError(Handle<String> message);
2116 static Local<Value> Error(Handle<String> message);
2117};
2118
2119
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002120// --- 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 +00002121
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002122typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002124typedef void* (*CreateHistogramCallback)(const char* name,
2125 int min,
2126 int max,
2127 size_t buckets);
2128
2129typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131// --- 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 ---
2132typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2133 AccessType type,
2134 Local<Value> data);
2135
2136// --- 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
2137
2138/**
2139 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002140 * before and after a major garbage collection. Allocations are not
2141 * allowed in the callback function, you therefore cannot manipulate
2142 * objects (set or delete properties for example) since it is possible
2143 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144 */
2145typedef void (*GCCallback)();
2146
2147
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002148// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149
2150/**
2151 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002152 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154typedef Persistent<Context> (*ContextGenerator)();
2155
2156
2157/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002158 * Profiler modules.
2159 *
2160 * In V8, profiler consists of several modules: CPU profiler, and different
2161 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002162 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2163 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002164 */
2165enum ProfilerModules {
2166 PROFILER_MODULE_NONE = 0,
2167 PROFILER_MODULE_CPU = 1,
2168 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002169 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2170 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002171};
2172
2173
2174/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002175 * Collection of V8 heap information.
2176 *
2177 * Instances of this class can be passed to v8::V8::HeapStatistics to
2178 * get heap statistics from V8.
2179 */
2180class V8EXPORT HeapStatistics {
2181 public:
2182 HeapStatistics();
2183 size_t total_heap_size() { return total_heap_size_; }
2184 size_t used_heap_size() { return used_heap_size_; }
2185
2186 private:
2187 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2188 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2189
2190 size_t total_heap_size_;
2191 size_t used_heap_size_;
2192
2193 friend class V8;
2194};
2195
2196
2197/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 * Container class for static utility functions.
2199 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002200class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002202 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 static void SetFatalErrorHandler(FatalErrorCallback that);
2204
v8.team.kasperl727e9952008-09-02 14:56:44 +00002205 /**
2206 * Ignore out-of-memory exceptions.
2207 *
2208 * V8 running out of memory is treated as a fatal error by default.
2209 * This means that the fatal error handler is called and that V8 is
2210 * terminated.
2211 *
2212 * IgnoreOutOfMemoryException can be used to not treat a
2213 * out-of-memory situation as a fatal error. This way, the contexts
2214 * that did not cause the out of memory problem might be able to
2215 * continue execution.
2216 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 static void IgnoreOutOfMemoryException();
2218
v8.team.kasperl727e9952008-09-02 14:56:44 +00002219 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002220 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002221 * fatal errors such as out-of-memory situations.
2222 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223 static bool IsDead();
2224
2225 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002226 * Adds a message listener.
2227 *
2228 * The same message listener can be added more than once and it that
2229 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 */
2231 static bool AddMessageListener(MessageCallback that,
2232 Handle<Value> data = Handle<Value>());
2233
2234 /**
2235 * Remove all message listeners from the specified callback function.
2236 */
2237 static void RemoveMessageListeners(MessageCallback that);
2238
2239 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 */
2242 static void SetFlagsFromString(const char* str, int length);
2243
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002244 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002245 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002246 */
2247 static void SetFlagsFromCommandLine(int* argc,
2248 char** argv,
2249 bool remove_flags);
2250
kasper.lund7276f142008-07-30 08:49:36 +00002251 /** Get the version string. */
2252 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253
2254 /**
2255 * Enables the host application to provide a mechanism for recording
2256 * statistics counters.
2257 */
2258 static void SetCounterFunction(CounterLookupCallback);
2259
2260 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002261 * Enables the host application to provide a mechanism for recording
2262 * histograms. The CreateHistogram function returns a
2263 * histogram which will later be passed to the AddHistogramSample
2264 * function.
2265 */
2266 static void SetCreateHistogramFunction(CreateHistogramCallback);
2267 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2268
2269 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 * Enables the computation of a sliding window of states. The sliding
2271 * window information is recorded in statistics counters.
2272 */
2273 static void EnableSlidingStateWindow();
2274
2275 /** Callback function for reporting failed access checks.*/
2276 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2277
2278 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002279 * Enables the host application to receive a notification before a
2280 * major garbage colletion. Allocations are not allowed in the
2281 * callback function, you therefore cannot manipulate objects (set
2282 * or delete properties for example) since it is possible such
2283 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 */
2285 static void SetGlobalGCPrologueCallback(GCCallback);
2286
2287 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002288 * Enables the host application to receive a notification after a
2289 * major garbage collection. Allocations are not allowed in the
2290 * callback function, you therefore cannot manipulate objects (set
2291 * or delete properties for example) since it is possible such
2292 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 */
2294 static void SetGlobalGCEpilogueCallback(GCCallback);
2295
2296 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002297 * Allows the host application to group objects together. If one
2298 * object in the group is alive, all objects in the group are alive.
2299 * After each garbage collection, object groups are removed. It is
2300 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002301 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002302 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002304 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305
2306 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002307 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002308 * initialize from scratch. This function is called implicitly if
2309 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 */
2311 static bool Initialize();
2312
kasper.lund7276f142008-07-30 08:49:36 +00002313 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002314 * Adjusts the amount of registered external memory. Used to give
2315 * V8 an indication of the amount of externally allocated memory
2316 * that is kept alive by JavaScript objects. V8 uses this to decide
2317 * when to perform global garbage collections. Registering
2318 * externally allocated memory will trigger global garbage
2319 * collections more often than otherwise in an attempt to garbage
2320 * collect the JavaScript objects keeping the externally allocated
2321 * memory alive.
2322 *
2323 * \param change_in_bytes the change in externally allocated memory
2324 * that is kept alive by JavaScript objects.
2325 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002326 */
2327 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2328
iposva@chromium.org245aa852009-02-10 00:49:54 +00002329 /**
2330 * Suspends recording of tick samples in the profiler.
2331 * When the V8 profiling mode is enabled (usually via command line
2332 * switches) this function suspends recording of tick samples.
2333 * Profiling ticks are discarded until ResumeProfiler() is called.
2334 *
2335 * See also the --prof and --prof_auto command line switches to
2336 * enable V8 profiling.
2337 */
2338 static void PauseProfiler();
2339
2340 /**
2341 * Resumes recording of tick samples in the profiler.
2342 * See also PauseProfiler().
2343 */
2344 static void ResumeProfiler();
2345
ager@chromium.org41826e72009-03-30 13:30:57 +00002346 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002347 * Return whether profiler is currently paused.
2348 */
2349 static bool IsProfilerPaused();
2350
2351 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002352 * Resumes specified profiler modules. Can be called several times to
2353 * mark the opening of a profiler events block with the given tag.
2354 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002355 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2356 * See ProfilerModules enum.
2357 *
2358 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002359 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002360 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002361 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002362
2363 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002364 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2365 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2366 * same tag value. There is no need for blocks to be properly nested.
2367 * The profiler is paused when the last opened block is closed.
2368 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002369 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2370 * See ProfilerModules enum.
2371 *
2372 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002373 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002374 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002375 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002376
2377 /**
2378 * Returns active (resumed) profiler modules.
2379 * See ProfilerModules enum.
2380 *
2381 * \returns active profiler modules.
2382 */
2383 static int GetActiveProfilerModules();
2384
2385 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002386 * If logging is performed into a memory buffer (via --logfile=*), allows to
2387 * retrieve previously written messages. This can be used for retrieving
2388 * profiler log data in the application. This function is thread-safe.
2389 *
2390 * Caller provides a destination buffer that must exist during GetLogLines
2391 * call. Only whole log lines are copied into the buffer.
2392 *
2393 * \param from_pos specified a point in a buffer to read from, 0 is the
2394 * beginning of a buffer. It is assumed that caller updates its current
2395 * position using returned size value from the previous call.
2396 * \param dest_buf destination buffer for log data.
2397 * \param max_size size of the destination buffer.
2398 * \returns actual size of log data copied into buffer.
2399 */
2400 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2401
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002402 /**
2403 * Retrieve the V8 thread id of the calling thread.
2404 *
2405 * The thread id for a thread should only be retrieved after the V8
2406 * lock has been acquired with a Locker object with that thread.
2407 */
2408 static int GetCurrentThreadId();
2409
2410 /**
2411 * Forcefully terminate execution of a JavaScript thread. This can
2412 * be used to terminate long-running scripts.
2413 *
2414 * TerminateExecution should only be called when then V8 lock has
2415 * been acquired with a Locker object. Therefore, in order to be
2416 * able to terminate long-running threads, preemption must be
2417 * enabled to allow the user of TerminateExecution to acquire the
2418 * lock.
2419 *
2420 * The termination is achieved by throwing an exception that is
2421 * uncatchable by JavaScript exception handlers. Termination
2422 * exceptions act as if they were caught by a C++ TryCatch exception
2423 * handlers. If forceful termination is used, any C++ TryCatch
2424 * exception handler that catches an exception should check if that
2425 * exception is a termination exception and immediately return if
2426 * that is the case. Returning immediately in that case will
2427 * continue the propagation of the termination exception if needed.
2428 *
2429 * The thread id passed to TerminateExecution must have been
2430 * obtained by calling GetCurrentThreadId on the thread in question.
2431 *
2432 * \param thread_id The thread id of the thread to terminate.
2433 */
2434 static void TerminateExecution(int thread_id);
2435
2436 /**
2437 * Forcefully terminate the current thread of JavaScript execution.
2438 *
2439 * This method can be used by any thread even if that thread has not
2440 * acquired the V8 lock with a Locker object.
2441 */
2442 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002443
2444 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002445 * Releases any resources used by v8 and stops any utility threads
2446 * that may be running. Note that disposing v8 is permanent, it
2447 * cannot be reinitialized.
2448 *
2449 * It should generally not be necessary to dispose v8 before exiting
2450 * a process, this should happen automatically. It is only necessary
2451 * to use if the process needs the resources taken up by v8.
2452 */
2453 static bool Dispose();
2454
ager@chromium.org3811b432009-10-28 14:53:37 +00002455 /**
2456 * Get statistics about the heap memory usage.
2457 */
2458 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002459
2460 /**
2461 * Optional notification that the embedder is idle.
2462 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002463 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002464 * Returns true if the embedder should stop calling IdleNotification
2465 * until real work has been done. This indicates that V8 has done
2466 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002467 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002468 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002469
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002470 /**
2471 * Optional notification that the system is running low on memory.
2472 * V8 uses these notifications to attempt to free memory.
2473 */
2474 static void LowMemoryNotification();
2475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476 private:
2477 V8();
2478
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002479 static internal::Object** GlobalizeReference(internal::Object** handle);
2480 static void DisposeGlobal(internal::Object** global_handle);
2481 static void MakeWeak(internal::Object** global_handle,
2482 void* data,
2483 WeakReferenceCallback);
2484 static void ClearWeak(internal::Object** global_handle);
2485 static bool IsGlobalNearDeath(internal::Object** global_handle);
2486 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487
2488 template <class T> friend class Handle;
2489 template <class T> friend class Local;
2490 template <class T> friend class Persistent;
2491 friend class Context;
2492};
2493
2494
2495/**
2496 * An external exception handler.
2497 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002498class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 public:
2500
2501 /**
2502 * Creates a new try/catch block and registers it with v8.
2503 */
2504 TryCatch();
2505
2506 /**
2507 * Unregisters and deletes this try/catch block.
2508 */
2509 ~TryCatch();
2510
2511 /**
2512 * Returns true if an exception has been caught by this try/catch block.
2513 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002514 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
2516 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002517 * For certain types of exceptions, it makes no sense to continue
2518 * execution.
2519 *
2520 * Currently, the only type of exception that can be caught by a
2521 * TryCatch handler and for which it does not make sense to continue
2522 * is termination exception. Such exceptions are thrown when the
2523 * TerminateExecution methods are called to terminate a long-running
2524 * script.
2525 *
2526 * If CanContinue returns false, the correct action is to perform
2527 * any C++ cleanup needed and then return.
2528 */
2529 bool CanContinue() const;
2530
2531 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002532 * Throws the exception caught by this TryCatch in a way that avoids
2533 * it being caught again by this same TryCatch. As with ThrowException
2534 * it is illegal to execute any JavaScript operations after calling
2535 * ReThrow; the caller must return immediately to where the exception
2536 * is caught.
2537 */
2538 Handle<Value> ReThrow();
2539
2540 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541 * Returns the exception caught by this try/catch block. If no exception has
2542 * been caught an empty handle is returned.
2543 *
2544 * The returned handle is valid until this TryCatch block has been destroyed.
2545 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002546 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547
2548 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002549 * Returns the .stack property of the thrown object. If no .stack
2550 * property is present an empty handle is returned.
2551 */
2552 Local<Value> StackTrace() const;
2553
2554 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002555 * Returns the message associated with this exception. If there is
2556 * no message associated an empty handle is returned.
2557 *
2558 * The returned handle is valid until this TryCatch block has been
2559 * destroyed.
2560 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002561 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002562
2563 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 * Clears any exceptions that may have been caught by this try/catch block.
2565 * After this method has been called, HasCaught() will return false.
2566 *
2567 * It is not necessary to clear a try/catch block before using it again; if
2568 * another exception is thrown the previously caught exception will just be
2569 * overwritten. However, it is often a good idea since it makes it easier
2570 * to determine which operation threw a given exception.
2571 */
2572 void Reset();
2573
v8.team.kasperl727e9952008-09-02 14:56:44 +00002574 /**
2575 * Set verbosity of the external exception handler.
2576 *
2577 * By default, exceptions that are caught by an external exception
2578 * handler are not reported. Call SetVerbose with true on an
2579 * external exception handler to have exceptions caught by the
2580 * handler reported as if they were not caught.
2581 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 void SetVerbose(bool value);
2583
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002584 /**
2585 * Set whether or not this TryCatch should capture a Message object
2586 * which holds source information about where the exception
2587 * occurred. True by default.
2588 */
2589 void SetCaptureMessage(bool value);
2590
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002591 private:
2592 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002594 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002595 bool is_verbose_ : 1;
2596 bool can_continue_ : 1;
2597 bool capture_message_ : 1;
2598 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002599
2600 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601};
2602
2603
2604// --- C o n t e x t ---
2605
2606
2607/**
2608 * Ignore
2609 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002610class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 public:
2612 ExtensionConfiguration(int name_count, const char* names[])
2613 : name_count_(name_count), names_(names) { }
2614 private:
2615 friend class ImplementationUtilities;
2616 int name_count_;
2617 const char** names_;
2618};
2619
2620
2621/**
2622 * A sandboxed execution context with its own set of built-in objects
2623 * and functions.
2624 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002625class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002626 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002627 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 Local<Object> Global();
2629
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002630 /**
2631 * Detaches the global object from its context before
2632 * the global object can be reused to create a new context.
2633 */
2634 void DetachGlobal();
2635
v8.team.kasperl727e9952008-09-02 14:56:44 +00002636 /** Creates a new context. */
2637 static Persistent<Context> New(
2638 ExtensionConfiguration* extensions = 0,
2639 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2640 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641
kasper.lund44510672008-07-25 07:37:58 +00002642 /** Returns the last entered context. */
2643 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644
kasper.lund44510672008-07-25 07:37:58 +00002645 /** Returns the context that is on the top of the stack. */
2646 static Local<Context> GetCurrent();
2647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002649 * Returns the context of the calling JavaScript code. That is the
2650 * context of the top-most JavaScript frame. If there are no
2651 * JavaScript frames an empty handle is returned.
2652 */
2653 static Local<Context> GetCalling();
2654
2655 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 * Sets the security token for the context. To access an object in
2657 * another context, the security tokens must match.
2658 */
2659 void SetSecurityToken(Handle<Value> token);
2660
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002661 /** Restores the security token to the default value. */
2662 void UseDefaultSecurityToken();
2663
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 /** Returns the security token of this context.*/
2665 Handle<Value> GetSecurityToken();
2666
v8.team.kasperl727e9952008-09-02 14:56:44 +00002667 /**
2668 * Enter this context. After entering a context, all code compiled
2669 * and run is compiled and run in this context. If another context
2670 * is already entered, this old context is saved so it can be
2671 * restored when the new context is exited.
2672 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002674
2675 /**
2676 * Exit this context. Exiting the current context restores the
2677 * context that was in place when entering the current context.
2678 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 void Exit();
2680
v8.team.kasperl727e9952008-09-02 14:56:44 +00002681 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 bool HasOutOfMemoryException();
2683
v8.team.kasperl727e9952008-09-02 14:56:44 +00002684 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685 static bool InContext();
2686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002688 * Associate an additional data object with the context. This is mainly used
2689 * with the debugger to provide additional information on the context through
2690 * the debugger API.
2691 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00002692 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00002693 Local<Value> GetData();
2694
2695 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 * Stack-allocated class which sets the execution context for all
2697 * operations executed within a local scope.
2698 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002699 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700 public:
2701 inline Scope(Handle<Context> context) : context_(context) {
2702 context_->Enter();
2703 }
2704 inline ~Scope() { context_->Exit(); }
2705 private:
2706 Handle<Context> context_;
2707 };
2708
2709 private:
2710 friend class Value;
2711 friend class Script;
2712 friend class Object;
2713 friend class Function;
2714};
2715
2716
2717/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002718 * Multiple threads in V8 are allowed, but only one thread at a time
2719 * is allowed to use V8. The definition of 'using V8' includes
2720 * accessing handles or holding onto object pointers obtained from V8
2721 * handles. It is up to the user of V8 to ensure (perhaps with
2722 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 *
2724 * If you wish to start using V8 in a thread you can do this by constructing
2725 * a v8::Locker object. After the code using V8 has completed for the
2726 * current thread you can call the destructor. This can be combined
2727 * with C++ scope-based construction as follows:
2728 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002729 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 * ...
2731 * {
2732 * v8::Locker locker;
2733 * ...
2734 * // Code using V8 goes here.
2735 * ...
2736 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002737 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 *
2739 * If you wish to stop using V8 in a thread A you can do this by either
2740 * by destroying the v8::Locker object as above or by constructing a
2741 * v8::Unlocker object:
2742 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002743 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 * {
2745 * v8::Unlocker unlocker;
2746 * ...
2747 * // Code not using V8 goes here while V8 can run in another thread.
2748 * ...
2749 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002750 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 *
2752 * The Unlocker object is intended for use in a long-running callback
2753 * from V8, where you want to release the V8 lock for other threads to
2754 * use.
2755 *
2756 * The v8::Locker is a recursive lock. That is, you can lock more than
2757 * once in a given thread. This can be useful if you have code that can
2758 * be called either from code that holds the lock or from code that does
2759 * not. The Unlocker is not recursive so you can not have several
2760 * Unlockers on the stack at once, and you can not use an Unlocker in a
2761 * thread that is not inside a Locker's scope.
2762 *
2763 * An unlocker will unlock several lockers if it has to and reinstate
2764 * the correct depth of locking on its destruction. eg.:
2765 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002766 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 * // V8 not locked.
2768 * {
2769 * v8::Locker locker;
2770 * // V8 locked.
2771 * {
2772 * v8::Locker another_locker;
2773 * // V8 still locked (2 levels).
2774 * {
2775 * v8::Unlocker unlocker;
2776 * // V8 not locked.
2777 * }
2778 * // V8 locked again (2 levels).
2779 * }
2780 * // V8 still locked (1 level).
2781 * }
2782 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002783 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002785class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 public:
2787 Unlocker();
2788 ~Unlocker();
2789};
2790
2791
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002792class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 public:
2794 Locker();
2795 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002796
2797 /**
2798 * Start preemption.
2799 *
2800 * When preemption is started, a timer is fired every n milli seconds
2801 * that will switch between multiple threads that are in contention
2802 * for the V8 lock.
2803 */
2804 static void StartPreemption(int every_n_ms);
2805
2806 /**
2807 * Stop preemption.
2808 */
2809 static void StopPreemption();
2810
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002811 /**
2812 * Returns whether or not the locker is locked by the current thread.
2813 */
2814 static bool IsLocked();
2815
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002816 /**
2817 * Returns whether v8::Locker is being used by this V8 instance.
2818 */
2819 static bool IsActive() { return active_; }
2820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821 private:
2822 bool has_lock_;
2823 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002824
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002825 static bool active_;
2826
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002827 // Disallow copying and assigning.
2828 Locker(const Locker&);
2829 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830};
2831
2832
2833
2834// --- I m p l e m e n t a t i o n ---
2835
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002836
2837namespace internal {
2838
2839
2840// Tag information for HeapObject.
2841const int kHeapObjectTag = 1;
2842const int kHeapObjectTagSize = 2;
2843const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2844
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002845// Tag information for Smi.
2846const int kSmiTag = 0;
2847const int kSmiTagSize = 1;
2848const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2849
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002850template <size_t ptr_size> struct SmiConstants;
2851
2852// Smi constants for 32-bit systems.
2853template <> struct SmiConstants<4> {
2854 static const int kSmiShiftSize = 0;
2855 static const int kSmiValueSize = 31;
2856 static inline int SmiToInt(internal::Object* value) {
2857 int shift_bits = kSmiTagSize + kSmiShiftSize;
2858 // Throw away top 32 bits and shift down (requires >> to be sign extending).
2859 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
2860 }
2861};
2862
2863// Smi constants for 64-bit systems.
2864template <> struct SmiConstants<8> {
2865 static const int kSmiShiftSize = 31;
2866 static const int kSmiValueSize = 32;
2867 static inline int SmiToInt(internal::Object* value) {
2868 int shift_bits = kSmiTagSize + kSmiShiftSize;
2869 // Shift down and throw away top 32 bits.
2870 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
2871 }
2872};
2873
2874const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
2875const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002876
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002877template <size_t ptr_size> struct InternalConstants;
2878
2879// Internal constants for 32-bit systems.
2880template <> struct InternalConstants<4> {
2881 static const int kStringResourceOffset = 3 * sizeof(void*);
2882};
2883
2884// Internal constants for 64-bit systems.
2885template <> struct InternalConstants<8> {
2886 static const int kStringResourceOffset = 2 * sizeof(void*);
2887};
2888
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002889/**
2890 * This class exports constants and functionality from within v8 that
2891 * is necessary to implement inline functions in the v8 api. Don't
2892 * depend on functions and constants defined here.
2893 */
2894class Internals {
2895 public:
2896
2897 // These values match non-compiler-dependent values defined within
2898 // the implementation of v8.
2899 static const int kHeapObjectMapOffset = 0;
2900 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002901 static const int kStringResourceOffset =
2902 InternalConstants<sizeof(void*)>::kStringResourceOffset;
2903
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002904 static const int kProxyProxyOffset = sizeof(void*);
2905 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2906 static const int kFullStringRepresentationMask = 0x07;
2907 static const int kExternalTwoByteRepresentationTag = 0x03;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002908
2909 // These constants are compiler dependent so their values must be
2910 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00002911 V8EXPORT static int kJSObjectType;
2912 V8EXPORT static int kFirstNonstringType;
2913 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002914
2915 static inline bool HasHeapObjectTag(internal::Object* value) {
2916 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2917 kHeapObjectTag);
2918 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002919
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002920 static inline bool HasSmiTag(internal::Object* value) {
2921 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2922 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002923
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002924 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002925 return SmiConstants<sizeof(void*)>::SmiToInt(value);
2926 }
2927
2928 static inline int GetInstanceType(internal::Object* obj) {
2929 typedef internal::Object O;
2930 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
2931 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
2932 }
2933
2934 static inline void* GetExternalPointer(internal::Object* obj) {
2935 if (HasSmiTag(obj)) {
2936 return obj;
2937 } else if (GetInstanceType(obj) == kProxyType) {
2938 return ReadField<void*>(obj, kProxyProxyOffset);
2939 } else {
2940 return NULL;
2941 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002942 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002943
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002944 static inline bool IsExternalTwoByteString(int instance_type) {
2945 int representation = (instance_type & kFullStringRepresentationMask);
2946 return representation == kExternalTwoByteRepresentationTag;
2947 }
2948
2949 template <typename T>
2950 static inline T ReadField(Object* ptr, int offset) {
2951 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2952 return *reinterpret_cast<T*>(addr);
2953 }
2954
2955};
2956
2957}
2958
2959
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960template <class T>
2961Handle<T>::Handle() : val_(0) { }
2962
2963
2964template <class T>
2965Local<T>::Local() : Handle<T>() { }
2966
2967
2968template <class T>
2969Local<T> Local<T>::New(Handle<T> that) {
2970 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002971 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002972 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2973}
2974
2975
2976template <class T>
2977Persistent<T> Persistent<T>::New(Handle<T> that) {
2978 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002979 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002980 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2981}
2982
2983
2984template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002985bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002987 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988}
2989
2990
2991template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002992bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002994 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995}
2996
2997
2998template <class T>
2999void Persistent<T>::Dispose() {
3000 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003001 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002}
3003
3004
3005template <class T>
3006Persistent<T>::Persistent() : Handle<T>() { }
3007
3008template <class T>
3009void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003010 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3011 parameters,
3012 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003013}
3014
3015template <class T>
3016void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003017 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003018}
3019
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003020Local<Value> Arguments::operator[](int i) const {
3021 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3022 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3023}
3024
3025
3026Local<Function> Arguments::Callee() const {
3027 return callee_;
3028}
3029
3030
3031Local<Object> Arguments::This() const {
3032 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3033}
3034
3035
3036Local<Object> Arguments::Holder() const {
3037 return holder_;
3038}
3039
3040
3041Local<Value> Arguments::Data() const {
3042 return data_;
3043}
3044
3045
3046bool Arguments::IsConstructCall() const {
3047 return is_construct_call_;
3048}
3049
3050
3051int Arguments::Length() const {
3052 return length_;
3053}
3054
3055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056template <class T>
3057Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003058 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3059 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003060 return Local<T>(reinterpret_cast<T*>(after));
3061}
3062
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003063Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064 return resource_name_;
3065}
3066
3067
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003068Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069 return resource_line_offset_;
3070}
3071
3072
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003073Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003074 return resource_column_offset_;
3075}
3076
3077
3078Handle<Boolean> Boolean::New(bool value) {
3079 return value ? True() : False();
3080}
3081
3082
3083void Template::Set(const char* name, v8::Handle<Data> value) {
3084 Set(v8::String::New(name), value);
3085}
3086
3087
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003088Local<Value> Object::GetInternalField(int index) {
3089#ifndef V8_ENABLE_CHECKS
3090 Local<Value> quick_result = UncheckedGetInternalField(index);
3091 if (!quick_result.IsEmpty()) return quick_result;
3092#endif
3093 return CheckedGetInternalField(index);
3094}
3095
3096
3097Local<Value> Object::UncheckedGetInternalField(int index) {
3098 typedef internal::Object O;
3099 typedef internal::Internals I;
3100 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003101 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003102 // If the object is a plain JSObject, which is the common case,
3103 // we know where to find the internal fields and can return the
3104 // value directly.
3105 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3106 O* value = I::ReadField<O*>(obj, offset);
3107 O** result = HandleScope::CreateHandle(value);
3108 return Local<Value>(reinterpret_cast<Value*>(result));
3109 } else {
3110 return Local<Value>();
3111 }
3112}
3113
3114
3115void* External::Unwrap(Handle<v8::Value> obj) {
3116#ifdef V8_ENABLE_CHECKS
3117 return FullUnwrap(obj);
3118#else
3119 return QuickUnwrap(obj);
3120#endif
3121}
3122
3123
3124void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3125 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003126 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003127 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003128}
3129
3130
3131void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003132 typedef internal::Object O;
3133 typedef internal::Internals I;
3134
3135 O* obj = *reinterpret_cast<O**>(this);
3136
3137 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3138 // If the object is a plain JSObject, which is the common case,
3139 // we know where to find the internal fields and can return the
3140 // value directly.
3141 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3142 O* value = I::ReadField<O*>(obj, offset);
3143 return I::GetExternalPointer(value);
3144 }
3145
3146 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003147}
3148
3149
3150String* String::Cast(v8::Value* value) {
3151#ifdef V8_ENABLE_CHECKS
3152 CheckCast(value);
3153#endif
3154 return static_cast<String*>(value);
3155}
3156
3157
3158String::ExternalStringResource* String::GetExternalStringResource() const {
3159 typedef internal::Object O;
3160 typedef internal::Internals I;
3161 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003162 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003163 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003164 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3165 result = reinterpret_cast<String::ExternalStringResource*>(value);
3166 } else {
3167 result = NULL;
3168 }
3169#ifdef V8_ENABLE_CHECKS
3170 VerifyExternalStringResource(result);
3171#endif
3172 return result;
3173}
3174
3175
3176bool Value::IsString() const {
3177#ifdef V8_ENABLE_CHECKS
3178 return FullIsString();
3179#else
3180 return QuickIsString();
3181#endif
3182}
3183
3184bool Value::QuickIsString() const {
3185 typedef internal::Object O;
3186 typedef internal::Internals I;
3187 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3188 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003189 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003190}
3191
3192
3193Number* Number::Cast(v8::Value* value) {
3194#ifdef V8_ENABLE_CHECKS
3195 CheckCast(value);
3196#endif
3197 return static_cast<Number*>(value);
3198}
3199
3200
3201Integer* Integer::Cast(v8::Value* value) {
3202#ifdef V8_ENABLE_CHECKS
3203 CheckCast(value);
3204#endif
3205 return static_cast<Integer*>(value);
3206}
3207
3208
3209Date* Date::Cast(v8::Value* value) {
3210#ifdef V8_ENABLE_CHECKS
3211 CheckCast(value);
3212#endif
3213 return static_cast<Date*>(value);
3214}
3215
3216
3217Object* Object::Cast(v8::Value* value) {
3218#ifdef V8_ENABLE_CHECKS
3219 CheckCast(value);
3220#endif
3221 return static_cast<Object*>(value);
3222}
3223
3224
3225Array* Array::Cast(v8::Value* value) {
3226#ifdef V8_ENABLE_CHECKS
3227 CheckCast(value);
3228#endif
3229 return static_cast<Array*>(value);
3230}
3231
3232
3233Function* Function::Cast(v8::Value* value) {
3234#ifdef V8_ENABLE_CHECKS
3235 CheckCast(value);
3236#endif
3237 return static_cast<Function*>(value);
3238}
3239
3240
3241External* External::Cast(v8::Value* value) {
3242#ifdef V8_ENABLE_CHECKS
3243 CheckCast(value);
3244#endif
3245 return static_cast<External*>(value);
3246}
3247
3248
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003249Local<Value> AccessorInfo::Data() const {
3250 return Local<Value>(reinterpret_cast<Value*>(&args_[-3]));
3251}
3252
3253
3254Local<Object> AccessorInfo::This() const {
3255 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3256}
3257
3258
3259Local<Object> AccessorInfo::Holder() const {
3260 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3261}
3262
3263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003264/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003265 * \example shell.cc
3266 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267 * command-line and executes them.
3268 */
3269
3270
3271/**
3272 * \example process.cc
3273 */
3274
3275
3276} // namespace v8
3277
3278
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003279#undef V8EXPORT
3280#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281#undef TYPE_CHECK
3282
3283
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003284#endif // V8_H_