blob: 24b4cbe37dfd807ca5b257f8965df5c25577f097 [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;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000129class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000130class StackTrace;
131class StackFrame;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000133namespace internal {
134
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000135class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000136class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000137class Heap;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000138class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000139
140}
141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
143// --- W e a k H a n d l e s
144
145
146/**
147 * A weak reference callback function.
148 *
149 * \param object the weak global object to be reclaimed by the garbage collector
150 * \param parameter the value passed in when making the weak global object
151 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000152typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 void* parameter);
154
155
156// --- H a n d l e s ---
157
158#define TYPE_CHECK(T, S) \
159 while (false) { \
160 *(static_cast<T**>(0)) = static_cast<S*>(0); \
161 }
162
163/**
164 * An object reference managed by the v8 garbage collector.
165 *
166 * All objects returned from v8 have to be tracked by the garbage
167 * collector so that it knows that the objects are still alive. Also,
168 * because the garbage collector may move objects, it is unsafe to
169 * point directly to an object. Instead, all objects are stored in
170 * handles which are known by the garbage collector and updated
171 * whenever an object moves. Handles should always be passed by value
172 * (except in cases like out-parameters) and they should never be
173 * allocated on the heap.
174 *
175 * There are two types of handles: local and persistent handles.
176 * Local handles are light-weight and transient and typically used in
177 * local operations. They are managed by HandleScopes. Persistent
178 * handles can be used when storing objects across several independent
179 * operations and have to be explicitly deallocated when they're no
180 * longer used.
181 *
182 * It is safe to extract the object stored in the handle by
183 * dereferencing the handle (for instance, to extract the Object* from
184 * an Handle<Object>); the value will still be governed by a handle
185 * behind the scenes and the same rules apply to these values as to
186 * their handles.
187 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000188template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 public:
190
191 /**
192 * Creates an empty handle.
193 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000194 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195
196 /**
197 * Creates a new handle for the specified value.
198 */
199 explicit Handle(T* val) : val_(val) { }
200
201 /**
202 * Creates a handle for the contents of the specified handle. This
203 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000204 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 * incompatible handles, for instance from a Handle<String> to a
206 * Handle<Number> it will cause a compiletime error. Assigning
207 * between compatible handles, for instance assigning a
208 * Handle<String> to a variable declared as Handle<Value>, is legal
209 * because String is a subclass of Value.
210 */
211 template <class S> inline Handle(Handle<S> that)
212 : val_(reinterpret_cast<T*>(*that)) {
213 /**
214 * This check fails when trying to convert between incompatible
215 * handles. For example, converting from a Handle<String> to a
216 * Handle<Number>.
217 */
218 TYPE_CHECK(T, S);
219 }
220
221 /**
222 * Returns true if the handle is empty.
223 */
ager@chromium.org32912102009-01-16 10:38:43 +0000224 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000226 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000228 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229
230 /**
231 * Sets the handle to be empty. IsEmpty() will then return true.
232 */
233 void Clear() { this->val_ = 0; }
234
235 /**
236 * Checks whether two handles are the same.
237 * Returns true if both are empty, or if the objects
238 * to which they refer are identical.
239 * The handles' references are not checked.
240 */
ager@chromium.org32912102009-01-16 10:38:43 +0000241 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000242 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
243 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 if (a == 0) return b == 0;
245 if (b == 0) return false;
246 return *a == *b;
247 }
248
249 /**
250 * Checks whether two handles are different.
251 * Returns true if only one of the handles is empty, or if
252 * the objects to which they refer are different.
253 * The handles' references are not checked.
254 */
ager@chromium.org32912102009-01-16 10:38:43 +0000255 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 return !operator==(that);
257 }
258
259 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000260#ifdef V8_ENABLE_CHECKS
261 // If we're going to perform the type check then we have to check
262 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000264#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 return Handle<T>(T::Cast(*that));
266 }
267
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000268 template <class S> inline Handle<S> As() {
269 return Handle<S>::Cast(*this);
270 }
271
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272 private:
273 T* val_;
274};
275
276
277/**
278 * A light-weight stack-allocated object handle. All operations
279 * that return objects from within v8 return them in local handles. They
280 * are created within HandleScopes, and all local handles allocated within a
281 * handle scope are destroyed when the handle scope is destroyed. Hence it
282 * is not necessary to explicitly deallocate local handles.
283 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000284template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000286 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 template <class S> inline Local(Local<S> that)
288 : Handle<T>(reinterpret_cast<T*>(*that)) {
289 /**
290 * This check fails when trying to convert between incompatible
291 * handles. For example, converting from a Handle<String> to a
292 * Handle<Number>.
293 */
294 TYPE_CHECK(T, S);
295 }
296 template <class S> inline Local(S* that) : Handle<T>(that) { }
297 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000298#ifdef V8_ENABLE_CHECKS
299 // If we're going to perform the type check then we have to check
300 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000302#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 return Local<T>(T::Cast(*that));
304 }
305
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000306 template <class S> inline Local<S> As() {
307 return Local<S>::Cast(*this);
308 }
309
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 /** Create a local handle for the content of another handle.
311 * The referee is kept alive by the local handle even when
312 * the original handle is destroyed/disposed.
313 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000314 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315};
316
317
318/**
319 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000320 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 * allocated, a Persistent handle remains valid until it is explicitly
322 * disposed.
323 *
324 * A persistent handle contains a reference to a storage cell within
325 * the v8 engine which holds an object value and which is updated by
326 * the garbage collector whenever the object is moved. A new storage
327 * cell can be created using Persistent::New and existing handles can
328 * be disposed using Persistent::Dispose. Since persistent handles
329 * are passed by value you may have many persistent handle objects
330 * that point to the same storage cell. For instance, if you pass a
331 * persistent handle as an argument to a function you will not get two
332 * different storage cells but rather two references to the same
333 * storage cell.
334 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000335template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 public:
337
338 /**
339 * Creates an empty persistent handle that doesn't point to any
340 * storage cell.
341 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000342 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343
344 /**
345 * Creates a persistent handle for the same storage cell as the
346 * specified handle. This constructor allows you to pass persistent
347 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000348 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000350 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 * between compatible persistent handles, for instance assigning a
352 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000353 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 */
355 template <class S> inline Persistent(Persistent<S> that)
356 : Handle<T>(reinterpret_cast<T*>(*that)) {
357 /**
358 * This check fails when trying to convert between incompatible
359 * handles. For example, converting from a Handle<String> to a
360 * Handle<Number>.
361 */
362 TYPE_CHECK(T, S);
363 }
364
365 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
366
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000367 /**
368 * "Casts" a plain handle which is known to be a persistent handle
369 * to a persistent handle.
370 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 template <class S> explicit inline Persistent(Handle<S> that)
372 : Handle<T>(*that) { }
373
374 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000375#ifdef V8_ENABLE_CHECKS
376 // If we're going to perform the type check then we have to check
377 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000379#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 return Persistent<T>(T::Cast(*that));
381 }
382
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000383 template <class S> inline Persistent<S> As() {
384 return Persistent<S>::Cast(*this);
385 }
386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000388 * Creates a new persistent handle for an existing local or
389 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392
393 /**
394 * Releases the storage cell referenced by this persistent handle.
395 * Does not remove the reference to the cell from any handles.
396 * This handle's reference, and any any other references to the storage
397 * cell remain and IsEmpty will still return false.
398 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000399 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400
401 /**
402 * Make the reference to this object weak. When only weak handles
403 * refer to the object, the garbage collector will perform a
404 * callback to the given V8::WeakReferenceCallback function, passing
405 * it the object reference and the given parameters.
406 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000407 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408
409 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000410 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412 /**
413 *Checks if the handle holds the only reference to an object.
414 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000415 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416
417 /**
418 * Returns true if the handle's reference is weak.
419 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000420 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421
422 private:
423 friend class ImplementationUtilities;
424 friend class ObjectTemplate;
425};
426
427
v8.team.kasperl727e9952008-09-02 14:56:44 +0000428 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 * A stack-allocated class that governs a number of local handles.
430 * After a handle scope has been created, all local handles will be
431 * allocated within that handle scope until either the handle scope is
432 * deleted or another handle scope is created. If there is already a
433 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000434 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 * new handles will again be allocated in the original handle scope.
436 *
437 * After the handle scope of a local handle has been deleted the
438 * garbage collector will no longer track the object stored in the
439 * handle and may deallocate it. The behavior of accessing a handle
440 * for which the handle scope has been deleted is undefined.
441 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000442class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000444 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000446 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447
448 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449 * Closes the handle scope and returns the value as a handle in the
450 * previous scope, which is the new current scope after the call.
451 */
452 template <class T> Local<T> Close(Handle<T> value);
453
454 /**
455 * Counts the number of allocated handles.
456 */
457 static int NumberOfHandles();
458
459 /**
460 * Creates a new handle with the given value.
461 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000462 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
464 private:
465 // Make it impossible to create heap-allocated or illegal handle
466 // scopes by disallowing certain operations.
467 HandleScope(const HandleScope&);
468 void operator=(const HandleScope&);
469 void* operator new(size_t size);
470 void operator delete(void*, size_t);
471
ager@chromium.org3811b432009-10-28 14:53:37 +0000472 // This Data class is accessible internally as HandleScopeData through a
473 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000474 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 public:
476 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000477 internal::Object** next;
478 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 inline void Initialize() {
480 extensions = -1;
481 next = limit = NULL;
482 }
483 };
484
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000485 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000487 // Allow for the active closing of HandleScopes which allows to pass a handle
488 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000490 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 friend class ImplementationUtilities;
493};
494
495
496// --- S p e c i a l o b j e c t s ---
497
498
499/**
500 * The superclass of values and API object templates.
501 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000502class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 private:
504 Data();
505};
506
507
508/**
509 * Pre-compilation data that can be associated with a script. This
510 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000511 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000512 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000514class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 public:
516 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000517
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000518 /**
519 * Pre-compiles the specified script (context-independent).
520 *
521 * \param input Pointer to UTF-8 script source code.
522 * \param length Length of UTF-8 script source code.
523 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000526 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000527 * Pre-compiles the specified script (context-independent).
528 *
529 * NOTE: Pre-compilation using this method cannot happen on another thread
530 * without using Lockers.
531 *
532 * \param source Script source code.
533 */
534 static ScriptData* PreCompile(Handle<String> source);
535
536 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000537 * Load previous pre-compilation data.
538 *
539 * \param data Pointer to data returned by a call to Data() of a previous
540 * ScriptData. Ownership is not transferred.
541 * \param length Length of data.
542 */
543 static ScriptData* New(const char* data, int length);
544
545 /**
546 * Returns the length of Data().
547 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000549
550 /**
551 * Returns a serialized representation of this ScriptData that can later be
552 * passed to New(). NOTE: Serialized data is platform-dependent.
553 */
554 virtual const char* Data() = 0;
555
556 /**
557 * Returns true if the source code could not be parsed.
558 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000559 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560};
561
562
563/**
564 * The origin, within a file, of a script.
565 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000566class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000568 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 Handle<Integer> resource_line_offset = Handle<Integer>(),
570 Handle<Integer> resource_column_offset = Handle<Integer>())
571 : resource_name_(resource_name),
572 resource_line_offset_(resource_line_offset),
573 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000574 inline Handle<Value> ResourceName() const;
575 inline Handle<Integer> ResourceLineOffset() const;
576 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000578 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 Handle<Integer> resource_line_offset_;
580 Handle<Integer> resource_column_offset_;
581};
582
583
584/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000585 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000587class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 public:
589
590 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000591 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000592 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000593 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000594 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000595 * when New() returns
596 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
597 * using pre_data speeds compilation if it's done multiple times.
598 * Owned by caller, no references are kept when New() returns.
599 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000600 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 * available to compile event handlers.
602 * \return Compiled script object (context independent; when run it
603 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 static Local<Script> New(Handle<String> source,
606 ScriptOrigin* origin = NULL,
607 ScriptData* pre_data = NULL,
608 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
mads.s.agercbaa0602008-08-14 13:41:48 +0000610 /**
611 * Compiles the specified script using the specified file name
612 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000613 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000614 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000615 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000616 * as the script's origin.
617 * \return Compiled script object (context independent; when run it
618 * will use the currently entered context).
619 */
620 static Local<Script> New(Handle<String> source,
621 Handle<Value> file_name);
622
623 /**
624 * Compiles the specified script (bound to current context).
625 *
626 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000627 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 * when Compile() returns
629 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
630 * using pre_data speeds compilation if it's done multiple times.
631 * Owned by caller, no references are kept when Compile() returns.
632 * \param script_data Arbitrary data associated with script. Using
633 * this has same effect as calling SetData(), but makes data available
634 * earlier (i.e. to compile event handlers).
635 * \return Compiled script object, bound to the context that was active
636 * when this function was called. When run it will always use this
637 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000638 */
639 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 ScriptOrigin* origin = NULL,
641 ScriptData* pre_data = NULL,
642 Handle<String> script_data = Handle<String>());
643
644 /**
645 * Compiles the specified script using the specified file name
646 * object (typically a string) as the script's origin.
647 *
648 * \param source Script source code.
649 * \param file_name File name to use as script's origin
650 * \param script_data Arbitrary data associated with script. Using
651 * this has same effect as calling SetData(), but makes data available
652 * earlier (i.e. to compile event handlers).
653 * \return Compiled script object, bound to the context that was active
654 * when this function was called. When run it will always use this
655 * context.
656 */
657 static Local<Script> Compile(Handle<String> source,
658 Handle<Value> file_name,
659 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000660
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000662 * Runs the script returning the resulting value. If the script is
663 * context independent (created using ::New) it will be run in the
664 * currently entered context. If it is context specific (created
665 * using ::Compile) it will be run in the context in which it was
666 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000667 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669
670 /**
671 * Returns the script id value.
672 */
673 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000674
675 /**
676 * Associate an additional data object with the script. This is mainly used
677 * with the debugger as this data object is only available through the
678 * debugger API.
679 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000680 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681};
682
683
684/**
685 * An error message.
686 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000687class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000689 Local<String> Get() const;
690 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000692 /**
693 * Returns the resource name for the script from where the function causing
694 * the error originates.
695 */
ager@chromium.org32912102009-01-16 10:38:43 +0000696 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000698 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000699 * Returns the resource data for the script from where the function causing
700 * the error originates.
701 */
702 Handle<Value> GetScriptData() const;
703
704 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000705 * Returns the number, 1-based, of the line where the error occurred.
706 */
ager@chromium.org32912102009-01-16 10:38:43 +0000707 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000709 /**
710 * Returns the index within the script of the first character where
711 * the error occurred.
712 */
ager@chromium.org32912102009-01-16 10:38:43 +0000713 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000714
715 /**
716 * Returns the index within the script of the last character where
717 * the error occurred.
718 */
ager@chromium.org32912102009-01-16 10:38:43 +0000719 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000720
721 /**
722 * Returns the index within the line of the first character where
723 * the error occurred.
724 */
ager@chromium.org32912102009-01-16 10:38:43 +0000725 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000726
727 /**
728 * Returns the index within the line of the last character where
729 * the error occurred.
730 */
ager@chromium.org32912102009-01-16 10:38:43 +0000731 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000732
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 // TODO(1245381): Print to a string instead of on a FILE.
734 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000735
736 static const int kNoLineNumberInfo = 0;
737 static const int kNoColumnInfo = 0;
738};
739
740
741/**
742 * Representation of a JavaScript stack trace. The information collected is a
743 * snapshot of the execution stack and the information remains valid after
744 * execution continues.
745 */
746class V8EXPORT StackTrace {
747 public:
748 /**
749 * Flags that determine what information is placed captured for each
750 * StackFrame when grabbing the current stack trace.
751 */
752 enum StackTraceOptions {
753 kLineNumber = 1,
754 kColumnOffset = 1 << 1 | kLineNumber,
755 kScriptName = 1 << 2,
756 kFunctionName = 1 << 3,
757 kIsEval = 1 << 4,
758 kIsConstructor = 1 << 5,
759 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
760 kDetailed = kOverview | kIsEval | kIsConstructor
761 };
762
763 /**
764 * Returns a StackFrame at a particular index.
765 */
766 Local<StackFrame> GetFrame(uint32_t index) const;
767
768 /**
769 * Returns the number of StackFrames.
770 */
771 int GetFrameCount() const;
772
773 /**
774 * Returns StackTrace as a v8::Array that contains StackFrame objects.
775 */
776 Local<Array> AsArray();
777
778 /**
779 * Grab a snapshot of the the current JavaScript execution stack.
780 *
781 * \param frame_limit The maximum number of stack frames we want to capture.
782 * \param options Enumerates the set of things we will capture for each
783 * StackFrame.
784 */
785 static Local<StackTrace> CurrentStackTrace(
786 int frame_limit,
787 StackTraceOptions options = kOverview);
788};
789
790
791/**
792 * A single JavaScript stack frame.
793 */
794class V8EXPORT StackFrame {
795 public:
796 /**
797 * Returns the number, 1-based, of the line for the associate function call.
798 * This method will return Message::kNoLineNumberInfo if it is unable to
799 * retrieve the line number, or if kLineNumber was not passed as an option
800 * when capturing the StackTrace.
801 */
802 int GetLineNumber() const;
803
804 /**
805 * Returns the 1-based column offset on the line for the associated function
806 * call.
807 * This method will return Message::kNoColumnInfo if it is unable to retrieve
808 * the column number, or if kColumnOffset was not passed as an option when
809 * capturing the StackTrace.
810 */
811 int GetColumn() const;
812
813 /**
814 * Returns the name of the resource that contains the script for the
815 * function for this StackFrame.
816 */
817 Local<String> GetScriptName() const;
818
819 /**
820 * Returns the name of the function associated with this stack frame.
821 */
822 Local<String> GetFunctionName() const;
823
824 /**
825 * Returns whether or not the associated function is compiled via a call to
826 * eval().
827 */
828 bool IsEval() const;
829
830 /**
831 * Returns whther or not the associated function is called as a
832 * constructor via "new".
833 */
834 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000835};
836
837
838// --- V a l u e ---
839
840
841/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000842 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000844class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 public:
846
847 /**
848 * Returns true if this value is the undefined value. See ECMA-262
849 * 4.3.10.
850 */
ager@chromium.org32912102009-01-16 10:38:43 +0000851 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852
853 /**
854 * Returns true if this value is the null value. See ECMA-262
855 * 4.3.11.
856 */
ager@chromium.org32912102009-01-16 10:38:43 +0000857 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858
859 /**
860 * Returns true if this value is true.
861 */
ager@chromium.org32912102009-01-16 10:38:43 +0000862 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
864 /**
865 * Returns true if this value is false.
866 */
ager@chromium.org32912102009-01-16 10:38:43 +0000867 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868
869 /**
870 * Returns true if this value is an instance of the String type.
871 * See ECMA-262 8.4.
872 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000873 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875 /**
876 * Returns true if this value is a function.
877 */
ager@chromium.org32912102009-01-16 10:38:43 +0000878 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
880 /**
881 * Returns true if this value is an array.
882 */
ager@chromium.org32912102009-01-16 10:38:43 +0000883 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
v8.team.kasperl727e9952008-09-02 14:56:44 +0000885 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 * Returns true if this value is an object.
887 */
ager@chromium.org32912102009-01-16 10:38:43 +0000888 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
v8.team.kasperl727e9952008-09-02 14:56:44 +0000890 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 * Returns true if this value is boolean.
892 */
ager@chromium.org32912102009-01-16 10:38:43 +0000893 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000894
v8.team.kasperl727e9952008-09-02 14:56:44 +0000895 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 * Returns true if this value is a number.
897 */
ager@chromium.org32912102009-01-16 10:38:43 +0000898 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
v8.team.kasperl727e9952008-09-02 14:56:44 +0000900 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 * Returns true if this value is external.
902 */
ager@chromium.org32912102009-01-16 10:38:43 +0000903 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
v8.team.kasperl727e9952008-09-02 14:56:44 +0000905 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 * Returns true if this value is a 32-bit signed integer.
907 */
ager@chromium.org32912102009-01-16 10:38:43 +0000908 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000910 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000911 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000912 */
913 bool IsUint32() const;
914
915 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000916 * Returns true if this value is a Date.
917 */
ager@chromium.org32912102009-01-16 10:38:43 +0000918 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000919
ager@chromium.org32912102009-01-16 10:38:43 +0000920 Local<Boolean> ToBoolean() const;
921 Local<Number> ToNumber() const;
922 Local<String> ToString() const;
923 Local<String> ToDetailString() const;
924 Local<Object> ToObject() const;
925 Local<Integer> ToInteger() const;
926 Local<Uint32> ToUint32() const;
927 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
929 /**
930 * Attempts to convert a string to an array index.
931 * Returns an empty handle if the conversion fails.
932 */
ager@chromium.org32912102009-01-16 10:38:43 +0000933 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934
ager@chromium.org32912102009-01-16 10:38:43 +0000935 bool BooleanValue() const;
936 double NumberValue() const;
937 int64_t IntegerValue() const;
938 uint32_t Uint32Value() const;
939 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940
941 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000942 bool Equals(Handle<Value> that) const;
943 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000944
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000945 private:
946 inline bool QuickIsString() const;
947 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948};
949
950
951/**
952 * The superclass of primitive values. See ECMA-262 4.3.2.
953 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000954class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955
956
957/**
958 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
959 * or false value.
960 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000961class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000963 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 static inline Handle<Boolean> New(bool value);
965};
966
967
968/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000969 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000971class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000973
974 /**
975 * Returns the number of characters in this string.
976 */
ager@chromium.org32912102009-01-16 10:38:43 +0000977 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
v8.team.kasperl727e9952008-09-02 14:56:44 +0000979 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000980 * Returns the number of bytes in the UTF-8 encoded
981 * representation of this string.
982 */
ager@chromium.org32912102009-01-16 10:38:43 +0000983 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000984
985 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000986 * Write the contents of the string to an external buffer.
987 * If no arguments are given, expects the buffer to be large
988 * enough to hold the entire string and NULL terminator. Copies
989 * the contents of the string and the NULL terminator into the
990 * buffer.
991 *
992 * Copies up to length characters into the output buffer.
993 * Only null-terminates if there is enough space in the buffer.
994 *
995 * \param buffer The buffer into which the string will be copied.
996 * \param start The starting position within the string at which
997 * copying begins.
998 * \param length The number of bytes to copy from the string.
ager@chromium.org357bf652010-04-12 11:30:10 +0000999 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001000 * \param hints Various hints that might affect performance of this or
1001 * subsequent operations.
ager@chromium.org357bf652010-04-12 11:30:10 +00001002 * \return The number of bytes copied to the buffer
v8.team.kasperl727e9952008-09-02 14:56:44 +00001003 * excluding the NULL terminator.
1004 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001005 enum WriteHints {
1006 NO_HINTS = 0,
1007 HINT_MANY_WRITES_EXPECTED = 1
1008 };
1009
1010 int Write(uint16_t* buffer,
1011 int start = 0,
1012 int length = -1,
1013 WriteHints hints = NO_HINTS) const; // UTF-16
1014 int WriteAscii(char* buffer,
1015 int start = 0,
1016 int length = -1,
1017 WriteHints hints = NO_HINTS) const; // ASCII
ager@chromium.org357bf652010-04-12 11:30:10 +00001018 int WriteUtf8(char* buffer,
1019 int length = -1,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001020 int* nchars_ref = NULL,
1021 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
v8.team.kasperl727e9952008-09-02 14:56:44 +00001023 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001024 * A zero length string.
1025 */
1026 static v8::Local<v8::String> Empty();
1027
1028 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001029 * Returns true if the string is external
1030 */
ager@chromium.org32912102009-01-16 10:38:43 +00001031 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032
v8.team.kasperl727e9952008-09-02 14:56:44 +00001033 /**
1034 * Returns true if the string is both external and ascii
1035 */
ager@chromium.org32912102009-01-16 10:38:43 +00001036 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001037
1038 class V8EXPORT ExternalStringResourceBase {
1039 public:
1040 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001041
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001042 protected:
1043 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001044
1045 /**
1046 * Internally V8 will call this Dispose method when the external string
1047 * resource is no longer needed. The default implementation will use the
1048 * delete operator. This method can be overridden in subclasses to
1049 * control how allocated external string resources are disposed.
1050 */
1051 virtual void Dispose() { delete this; }
1052
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001053 private:
1054 // Disallow copying and assigning.
1055 ExternalStringResourceBase(const ExternalStringResourceBase&);
1056 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001057
1058 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001059 };
1060
v8.team.kasperl727e9952008-09-02 14:56:44 +00001061 /**
1062 * An ExternalStringResource is a wrapper around a two-byte string
1063 * buffer that resides outside V8's heap. Implement an
1064 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001065 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001066 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001067 class V8EXPORT ExternalStringResource
1068 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069 public:
1070 /**
1071 * Override the destructor to manage the life cycle of the underlying
1072 * buffer.
1073 */
1074 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001075
1076 /**
1077 * The string data from the underlying buffer.
1078 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001080
1081 /**
1082 * The length of the string. That is, the number of two-byte characters.
1083 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 protected:
1087 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 };
1089
1090 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001091 * An ExternalAsciiStringResource is a wrapper around an ascii
1092 * string buffer that resides outside V8's heap. Implement an
1093 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001094 * underlying buffer. Note that the string data must be immutable
1095 * and that the data must be strict 7-bit ASCII, not Latin1 or
1096 * UTF-8, which would require special treatment internally in the
1097 * engine and, in the case of UTF-8, do not allow efficient indexing.
1098 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001099 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001101 class V8EXPORT ExternalAsciiStringResource
1102 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 public:
1104 /**
1105 * Override the destructor to manage the life cycle of the underlying
1106 * buffer.
1107 */
1108 virtual ~ExternalAsciiStringResource() {}
1109 /** The string data from the underlying buffer.*/
1110 virtual const char* data() const = 0;
1111 /** The number of ascii characters in the string.*/
1112 virtual size_t length() const = 0;
1113 protected:
1114 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 };
1116
1117 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001118 * Get the ExternalStringResource for an external string. Returns
1119 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001121 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122
1123 /**
1124 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001125 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 */
ager@chromium.org32912102009-01-16 10:38:43 +00001127 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001129 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130
1131 /**
1132 * Allocates a new string from either utf-8 encoded or ascii data.
1133 * The second parameter 'length' gives the buffer length.
1134 * If the data is utf-8 encoded, the caller must
1135 * be careful to supply the length parameter.
1136 * If it is not given, the function calls
1137 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001138 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 */
1140 static Local<String> New(const char* data, int length = -1);
1141
1142 /** Allocates a new string from utf16 data.*/
1143 static Local<String> New(const uint16_t* data, int length = -1);
1144
1145 /** Creates a symbol. Returns one if it exists already.*/
1146 static Local<String> NewSymbol(const char* data, int length = -1);
1147
v8.team.kasperl727e9952008-09-02 14:56:44 +00001148 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001149 * Creates a new string by concatenating the left and the right strings
1150 * passed in as parameters.
1151 */
1152 static Local<String> Concat(Handle<String> left, Handle<String>right);
1153
1154 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001155 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001156 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001157 * resource will be disposed by calling its Dispose method. The caller of
1158 * this function should not otherwise delete or modify the resource. Neither
1159 * should the underlying buffer be deallocated or modified except through the
1160 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001161 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001163
ager@chromium.org6f10e412009-02-13 10:11:16 +00001164 /**
1165 * Associate an external string resource with this string by transforming it
1166 * in place so that existing references to this string in the JavaScript heap
1167 * will use the external string resource. The external string resource's
1168 * character contents needs to be equivalent to this string.
1169 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001170 * The string is not modified if the operation fails. See NewExternal for
1171 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001172 */
1173 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174
v8.team.kasperl727e9952008-09-02 14:56:44 +00001175 /**
1176 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001177 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001178 * resource will be disposed by calling its Dispose method. The caller of
1179 * this function should not otherwise delete or modify the resource. Neither
1180 * should the underlying buffer be deallocated or modified except through the
1181 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001182 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001184
ager@chromium.org6f10e412009-02-13 10:11:16 +00001185 /**
1186 * Associate an external string resource with this string by transforming it
1187 * in place so that existing references to this string in the JavaScript heap
1188 * will use the external string resource. The external string resource's
1189 * character contents needs to be equivalent to this string.
1190 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001191 * The string is not modified if the operation fails. See NewExternal for
1192 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001193 */
1194 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001196 /**
1197 * Returns true if this string can be made external.
1198 */
1199 bool CanMakeExternal();
1200
kasper.lund7276f142008-07-30 08:49:36 +00001201 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202 static Local<String> NewUndetectable(const char* data, int length = -1);
1203
kasper.lund7276f142008-07-30 08:49:36 +00001204 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
1206
1207 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001208 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001209 * you want to print the object. If conversion to a string fails
1210 * (eg. due to an exception in the toString() method of the object)
1211 * then the length() method returns 0 and the * operator returns
1212 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001213 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001214 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001215 public:
1216 explicit Utf8Value(Handle<v8::Value> obj);
1217 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001218 char* operator*() { return str_; }
1219 const char* operator*() const { return str_; }
1220 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001221 private:
1222 char* str_;
1223 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001224
1225 // Disallow copying and assigning.
1226 Utf8Value(const Utf8Value&);
1227 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001228 };
1229
1230 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 * Converts an object to an ascii string.
1232 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001233 * If conversion to a string fails (eg. due to an exception in the toString()
1234 * method of the object) then the length() method returns 0 and the * operator
1235 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001237 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 public:
1239 explicit AsciiValue(Handle<v8::Value> obj);
1240 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001241 char* operator*() { return str_; }
1242 const char* operator*() const { return str_; }
1243 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 private:
1245 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001246 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001247
1248 // Disallow copying and assigning.
1249 AsciiValue(const AsciiValue&);
1250 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 };
1252
1253 /**
1254 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001255 * If conversion to a string fails (eg. due to an exception in the toString()
1256 * method of the object) then the length() method returns 0 and the * operator
1257 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001259 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 public:
1261 explicit Value(Handle<v8::Value> obj);
1262 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001263 uint16_t* operator*() { return str_; }
1264 const uint16_t* operator*() const { return str_; }
1265 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 private:
1267 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001268 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001269
1270 // Disallow copying and assigning.
1271 Value(const Value&);
1272 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001274
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001275 private:
1276 void VerifyExternalStringResource(ExternalStringResource* val) const;
1277 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278};
1279
1280
1281/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001282 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001284class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001286 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001288 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 private:
1290 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001291 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292};
1293
1294
1295/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001296 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001298class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 public:
1300 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001301 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001302 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001303 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 private:
1305 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001306 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307};
1308
1309
1310/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001311 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001313class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001315 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 private:
1317 Int32();
1318};
1319
1320
1321/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001322 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001324class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001326 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 private:
1328 Uint32();
1329};
1330
1331
1332/**
1333 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1334 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001335class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 public:
1337 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001338
1339 /**
1340 * A specialization of Value::NumberValue that is more efficient
1341 * because we know the structure of this object.
1342 */
ager@chromium.org32912102009-01-16 10:38:43 +00001343 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001344
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001345 static inline Date* Cast(v8::Value* obj);
1346 private:
1347 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348};
1349
1350
1351enum PropertyAttribute {
1352 None = 0,
1353 ReadOnly = 1 << 0,
1354 DontEnum = 1 << 1,
1355 DontDelete = 1 << 2
1356};
1357
ager@chromium.org3811b432009-10-28 14:53:37 +00001358enum ExternalArrayType {
1359 kExternalByteArray = 1,
1360 kExternalUnsignedByteArray,
1361 kExternalShortArray,
1362 kExternalUnsignedShortArray,
1363 kExternalIntArray,
1364 kExternalUnsignedIntArray,
1365 kExternalFloatArray
1366};
1367
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001369 * Accessor[Getter|Setter] are used as callback functions when
1370 * setting|getting a particular property. See Object and ObjectTemplate's
1371 * method SetAccessor.
1372 */
1373typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1374 const AccessorInfo& info);
1375
1376
1377typedef void (*AccessorSetter)(Local<String> property,
1378 Local<Value> value,
1379 const AccessorInfo& info);
1380
1381
1382/**
1383 * Access control specifications.
1384 *
1385 * Some accessors should be accessible across contexts. These
1386 * accessors have an explicit access control parameter which specifies
1387 * the kind of cross-context access that should be allowed.
1388 *
1389 * Additionally, for security, accessors can prohibit overwriting by
1390 * accessors defined in JavaScript. For objects that have such
1391 * accessors either locally or in their prototype chain it is not
1392 * possible to overwrite the accessor by using __defineGetter__ or
1393 * __defineSetter__ from JavaScript code.
1394 */
1395enum AccessControl {
1396 DEFAULT = 0,
1397 ALL_CAN_READ = 1,
1398 ALL_CAN_WRITE = 1 << 1,
1399 PROHIBITS_OVERWRITING = 1 << 2
1400};
1401
1402
1403/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001404 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001406class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 public:
1408 bool Set(Handle<Value> key,
1409 Handle<Value> value,
1410 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001411
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001412 bool Set(uint32_t index,
1413 Handle<Value> value);
1414
ager@chromium.orge2902be2009-06-08 12:21:35 +00001415 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001416 // overriding accessors or read-only properties.
1417 //
1418 // Note that if the object has an interceptor the property will be set
1419 // locally, but since the interceptor takes precedence the local property
1420 // will only be returned if the interceptor doesn't return a value.
1421 //
1422 // Note also that this only works for named properties.
1423 bool ForceSet(Handle<Value> key,
1424 Handle<Value> value,
1425 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001426
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427 Local<Value> Get(Handle<Value> key);
1428
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001429 Local<Value> Get(uint32_t index);
1430
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 // TODO(1245389): Replace the type-specific versions of these
1432 // functions with generic ones that accept a Handle<Value> key.
1433 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001434
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001436
1437 // Delete a property on this object bypassing interceptors and
1438 // ignoring dont-delete attributes.
1439 bool ForceDelete(Handle<Value> key);
1440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 bool Delete(uint32_t index);
1444
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001445 bool SetAccessor(Handle<String> name,
1446 AccessorGetter getter,
1447 AccessorSetter setter = 0,
1448 Handle<Value> data = Handle<Value>(),
1449 AccessControl settings = DEFAULT,
1450 PropertyAttribute attribute = None);
1451
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001453 * Returns an array containing the names of the enumerable properties
1454 * of this object, including properties from prototype objects. The
1455 * array returned by this method contains the same values as would
1456 * be enumerated by a for-in statement over this object.
1457 */
1458 Local<Array> GetPropertyNames();
1459
1460 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 * Get the prototype object. This does not skip objects marked to
1462 * be skipped by __proto__ and it does not consult the security
1463 * handler.
1464 */
1465 Local<Value> GetPrototype();
1466
1467 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001468 * Set the prototype object. This does not skip objects marked to
1469 * be skipped by __proto__ and it does not consult the security
1470 * handler.
1471 */
1472 bool SetPrototype(Handle<Value> prototype);
1473
1474 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001475 * Finds an instance of the given function template in the prototype
1476 * chain.
1477 */
1478 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1479
1480 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 * Call builtin Object.prototype.toString on this object.
1482 * This is different from Value::ToString() that may call
1483 * user-defined toString function. This one does not.
1484 */
1485 Local<String> ObjectProtoToString();
1486
kasper.lund212ac232008-07-16 07:07:30 +00001487 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001489 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001490 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001491 /** Sets the value in an internal field. */
1492 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001494 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001495 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001496
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001497 /** Sets a native pointer in an internal field. */
1498 void SetPointerInInternalField(int index, void* value);
1499
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500 // Testers for local properties.
1501 bool HasRealNamedProperty(Handle<String> key);
1502 bool HasRealIndexedProperty(uint32_t index);
1503 bool HasRealNamedCallbackProperty(Handle<String> key);
1504
1505 /**
1506 * If result.IsEmpty() no real property was located in the prototype chain.
1507 * This means interceptors in the prototype chain are not called.
1508 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001509 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1510
1511 /**
1512 * If result.IsEmpty() no real property was located on the object or
1513 * in the prototype chain.
1514 * This means interceptors in the prototype chain are not called.
1515 */
1516 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517
1518 /** Tests for a named lookup interceptor.*/
1519 bool HasNamedLookupInterceptor();
1520
kasper.lund212ac232008-07-16 07:07:30 +00001521 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522 bool HasIndexedLookupInterceptor();
1523
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001524 /**
1525 * Turns on access check on the object if the object is an instance of
1526 * a template that has access check callbacks. If an object has no
1527 * access check info, the object cannot be accessed by anyone.
1528 */
1529 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001530
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001531 /**
1532 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001533 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001534 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001535 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001536 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001537 */
1538 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001539
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001540 /**
1541 * Access hidden properties on JavaScript objects. These properties are
1542 * hidden from the executing JavaScript and only accessible through the V8
1543 * C++ API. Hidden properties introduced by V8 internally (for example the
1544 * identity hash) are prefixed with "v8::".
1545 */
1546 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1547 Local<Value> GetHiddenValue(Handle<String> key);
1548 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001549
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001550 /**
1551 * Returns true if this is an instance of an api function (one
1552 * created from a function created from a function template) and has
1553 * been modified since it was created. Note that this method is
1554 * conservative and may return true for objects that haven't actually
1555 * been modified.
1556 */
1557 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001558
1559 /**
1560 * Clone this object with a fast but shallow copy. Values will point
1561 * to the same values as the original object.
1562 */
1563 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001565 /**
1566 * Set the backing store of the indexed properties to be managed by the
1567 * embedding layer. Access to the indexed properties will follow the rules
1568 * spelled out in CanvasPixelArray.
1569 * Note: The embedding program still owns the data and needs to ensure that
1570 * the backing store is preserved while V8 has a reference.
1571 */
1572 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1573
ager@chromium.org3811b432009-10-28 14:53:37 +00001574 /**
1575 * Set the backing store of the indexed properties to be managed by the
1576 * embedding layer. Access to the indexed properties will follow the rules
1577 * spelled out for the CanvasArray subtypes in the WebGL specification.
1578 * Note: The embedding program still owns the data and needs to ensure that
1579 * the backing store is preserved while V8 has a reference.
1580 */
1581 void SetIndexedPropertiesToExternalArrayData(void* data,
1582 ExternalArrayType array_type,
1583 int number_of_elements);
1584
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001586 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 private:
1588 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001589 static void CheckCast(Value* obj);
1590 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001591 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001592
1593 /**
1594 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001595 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001596 */
1597 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598};
1599
1600
1601/**
1602 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1603 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001604class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001606 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607
ager@chromium.org3e875802009-06-29 08:26:34 +00001608 /**
1609 * Clones an element at index |index|. Returns an empty
1610 * handle if cloning fails (for any reason).
1611 */
1612 Local<Object> CloneElementAt(uint32_t index);
1613
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001615 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 private:
1617 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001618 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619};
1620
1621
1622/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001623 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001625class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001627 Local<Object> NewInstance() const;
1628 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1630 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001631 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001632
1633 /**
1634 * Returns zero based line number of function body and
1635 * kLineOffsetNotFound if no information available.
1636 */
1637 int GetScriptLineNumber() const;
1638 ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001639 static inline Function* Cast(Value* obj);
ager@chromium.org5c838252010-02-19 08:53:10 +00001640 static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 private:
1642 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001643 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644};
1645
1646
1647/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001648 * A JavaScript value that wraps a C++ void*. This type of value is
1649 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001651 *
1652 * The Wrap function V8 will return the most optimal Value object wrapping the
1653 * C++ void*. The type of the value is not guaranteed to be an External object
1654 * and no assumptions about its type should be made. To access the wrapped
1655 * value Unwrap should be used, all other operations on that object will lead
1656 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001658class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001660 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001661 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001664 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001665 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666 private:
1667 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001668 static void CheckCast(v8::Value* obj);
1669 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1670 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671};
1672
1673
1674// --- T e m p l a t e s ---
1675
1676
1677/**
1678 * The superclass of object and function templates.
1679 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001680class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 public:
1682 /** Adds a property to each instance created by this template.*/
1683 void Set(Handle<String> name, Handle<Data> value,
1684 PropertyAttribute attributes = None);
1685 inline void Set(const char* name, Handle<Data> value);
1686 private:
1687 Template();
1688
1689 friend class ObjectTemplate;
1690 friend class FunctionTemplate;
1691};
1692
1693
1694/**
1695 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001696 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 * including the receiver, the number and values of arguments, and
1698 * the holder of the function.
1699 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001700class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 public:
1702 inline int Length() const;
1703 inline Local<Value> operator[](int i) const;
1704 inline Local<Function> Callee() const;
1705 inline Local<Object> This() const;
1706 inline Local<Object> Holder() const;
1707 inline bool IsConstructCall() const;
1708 inline Local<Value> Data() const;
1709 private:
1710 Arguments();
1711 friend class ImplementationUtilities;
1712 inline Arguments(Local<Value> data,
1713 Local<Object> holder,
1714 Local<Function> callee,
1715 bool is_construct_call,
1716 void** values, int length);
1717 Local<Value> data_;
1718 Local<Object> holder_;
1719 Local<Function> callee_;
1720 bool is_construct_call_;
1721 void** values_;
1722 int length_;
1723};
1724
1725
1726/**
1727 * The information passed to an accessor callback about the context
1728 * of the property access.
1729 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001730class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001732 inline AccessorInfo(internal::Object** args)
1733 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734 inline Local<Value> Data() const;
1735 inline Local<Object> This() const;
1736 inline Local<Object> Holder() const;
1737 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001738 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739};
1740
1741
1742typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1743
1744typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1745
1746/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 * NamedProperty[Getter|Setter] are used as interceptors on object.
1748 * See ObjectTemplate::SetNamedPropertyHandler.
1749 */
1750typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1751 const AccessorInfo& info);
1752
1753
1754/**
1755 * Returns the value if the setter intercepts the request.
1756 * Otherwise, returns an empty handle.
1757 */
1758typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1759 Local<Value> value,
1760 const AccessorInfo& info);
1761
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762/**
1763 * Returns a non-empty handle if the interceptor intercepts the request.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001764 * The result is either boolean (true if property exists and false
1765 * otherwise) or an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001767#ifdef USE_NEW_QUERY_CALLBACKS
1768typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1769 const AccessorInfo& info);
1770#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1772 const AccessorInfo& info);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001773#endif
1774
1775typedef Handle<Value> (*NamedPropertyQueryImpl)(Local<String> property,
1776 const AccessorInfo& info);
1777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778
1779
1780/**
1781 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001782 * The return value is true if the property could be deleted and false
1783 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 */
1785typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1786 const AccessorInfo& info);
1787
1788/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001789 * Returns an array containing the names of the properties the named
1790 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 */
1792typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1793
v8.team.kasperl727e9952008-09-02 14:56:44 +00001794
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001796 * Returns the value of the property if the getter intercepts the
1797 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 */
1799typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1800 const AccessorInfo& info);
1801
1802
1803/**
1804 * Returns the value if the setter intercepts the request.
1805 * Otherwise, returns an empty handle.
1806 */
1807typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1808 Local<Value> value,
1809 const AccessorInfo& info);
1810
1811
1812/**
1813 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001814 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 */
1816typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1817 const AccessorInfo& info);
1818
1819/**
1820 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001821 * The return value is true if the property could be deleted and false
1822 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 */
1824typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1825 const AccessorInfo& info);
1826
v8.team.kasperl727e9952008-09-02 14:56:44 +00001827/**
1828 * Returns an array containing the indices of the properties the
1829 * indexed property getter intercepts.
1830 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1832
1833
1834/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001835 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 */
1837enum AccessType {
1838 ACCESS_GET,
1839 ACCESS_SET,
1840 ACCESS_HAS,
1841 ACCESS_DELETE,
1842 ACCESS_KEYS
1843};
1844
v8.team.kasperl727e9952008-09-02 14:56:44 +00001845
1846/**
1847 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001848 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001849 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001850typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 Local<Value> key,
1852 AccessType type,
1853 Local<Value> data);
1854
v8.team.kasperl727e9952008-09-02 14:56:44 +00001855
1856/**
1857 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001858 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001859 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001860typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 uint32_t index,
1862 AccessType type,
1863 Local<Value> data);
1864
1865
1866/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001867 * A FunctionTemplate is used to create functions at runtime. There
1868 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001869 * context. The lifetime of the created function is equal to the
1870 * lifetime of the context. So in case the embedder needs to create
1871 * temporary functions that can be collected using Scripts is
1872 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 *
1874 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001875 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001877 * A FunctionTemplate has a corresponding instance template which is
1878 * used to create object instances when the function is used as a
1879 * constructor. Properties added to the instance template are added to
1880 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 *
1882 * A FunctionTemplate can have a prototype template. The prototype template
1883 * is used to create the prototype object of the function.
1884 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001885 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001887 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1889 * t->Set("func_property", v8::Number::New(1));
1890 *
1891 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1892 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1893 * proto_t->Set("proto_const", v8::Number::New(2));
1894 *
1895 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1896 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1897 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1898 * instance_t->Set("instance_property", Number::New(3));
1899 *
1900 * v8::Local<v8::Function> function = t->GetFunction();
1901 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001902 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 *
1904 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001905 * and "instance" for the instance object created above. The function
1906 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001908 * \code
1909 * func_property in function == true;
1910 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001912 * function.prototype.proto_method() invokes 'InvokeCallback'
1913 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001915 * instance instanceof function == true;
1916 * instance.instance_accessor calls 'InstanceAccessorCallback'
1917 * instance.instance_property == 3;
1918 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001920 * A FunctionTemplate can inherit from another one by calling the
1921 * FunctionTemplate::Inherit method. The following graph illustrates
1922 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001924 * \code
1925 * FunctionTemplate Parent -> Parent() . prototype -> { }
1926 * ^ ^
1927 * | Inherit(Parent) | .__proto__
1928 * | |
1929 * FunctionTemplate Child -> Child() . prototype -> { }
1930 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001932 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1933 * object of the Child() function has __proto__ pointing to the
1934 * Parent() function's prototype object. An instance of the Child
1935 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001937 * Let Parent be the FunctionTemplate initialized in the previous
1938 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001940 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 * Local<FunctionTemplate> parent = t;
1942 * Local<FunctionTemplate> child = FunctionTemplate::New();
1943 * child->Inherit(parent);
1944 *
1945 * Local<Function> child_function = child->GetFunction();
1946 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001947 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001949 * The Child function and Child instance will have the following
1950 * properties:
1951 *
1952 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001954 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001956 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001958class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 public:
1960 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001961 static Local<FunctionTemplate> New(
1962 InvocationCallback callback = 0,
1963 Handle<Value> data = Handle<Value>(),
1964 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 /** Returns the unique function instance in the current execution context.*/
1966 Local<Function> GetFunction();
1967
v8.team.kasperl727e9952008-09-02 14:56:44 +00001968 /**
1969 * Set the call-handler callback for a FunctionTemplate. This
1970 * callback is called whenever the function created from this
1971 * FunctionTemplate is called.
1972 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 void SetCallHandler(InvocationCallback callback,
1974 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975
v8.team.kasperl727e9952008-09-02 14:56:44 +00001976 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 Local<ObjectTemplate> InstanceTemplate();
1978
1979 /** Causes the function template to inherit from a parent function template.*/
1980 void Inherit(Handle<FunctionTemplate> parent);
1981
1982 /**
1983 * A PrototypeTemplate is the template used to create the prototype object
1984 * of the function created by this template.
1985 */
1986 Local<ObjectTemplate> PrototypeTemplate();
1987
v8.team.kasperl727e9952008-09-02 14:56:44 +00001988
1989 /**
1990 * Set the class name of the FunctionTemplate. This is used for
1991 * printing objects created with the function created from the
1992 * FunctionTemplate as its constructor.
1993 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 void SetClassName(Handle<String> name);
1995
1996 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001997 * Determines whether the __proto__ accessor ignores instances of
1998 * the function template. If instances of the function template are
1999 * ignored, __proto__ skips all instances and instead returns the
2000 * next object in the prototype chain.
2001 *
2002 * Call with a value of true to make the __proto__ accessor ignore
2003 * instances of the function template. Call with a value of false
2004 * to make the __proto__ accessor not ignore instances of the
2005 * function template. By default, instances of a function template
2006 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 */
2008 void SetHiddenPrototype(bool value);
2009
2010 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002011 * Returns true if the given object is an instance of this function
2012 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013 */
2014 bool HasInstance(Handle<Value> object);
2015
2016 private:
2017 FunctionTemplate();
2018 void AddInstancePropertyAccessor(Handle<String> name,
2019 AccessorGetter getter,
2020 AccessorSetter setter,
2021 Handle<Value> data,
2022 AccessControl settings,
2023 PropertyAttribute attributes);
2024 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2025 NamedPropertySetter setter,
2026 NamedPropertyQuery query,
2027 NamedPropertyDeleter remover,
2028 NamedPropertyEnumerator enumerator,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002029 Handle<Value> data) {
2030 NamedPropertyQueryImpl casted =
2031 reinterpret_cast<NamedPropertyQueryImpl>(query);
2032 SetNamedInstancePropertyHandlerImpl(getter,
2033 setter,
2034 casted,
2035 remover,
2036 enumerator,
2037 data);
2038 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2040 IndexedPropertySetter setter,
2041 IndexedPropertyQuery query,
2042 IndexedPropertyDeleter remover,
2043 IndexedPropertyEnumerator enumerator,
2044 Handle<Value> data);
2045 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2046 Handle<Value> data);
2047
2048 friend class Context;
2049 friend class ObjectTemplate;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002050 private:
2051 void SetNamedInstancePropertyHandlerImpl(NamedPropertyGetter getter,
2052 NamedPropertySetter setter,
2053 NamedPropertyQueryImpl query,
2054 NamedPropertyDeleter remover,
2055 NamedPropertyEnumerator enumerator,
2056 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057};
2058
2059
2060/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002061 * An ObjectTemplate is used to create objects at runtime.
2062 *
2063 * Properties added to an ObjectTemplate are added to each object
2064 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002066class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002068 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002070
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 /** Creates a new instance of this template.*/
2072 Local<Object> NewInstance();
2073
2074 /**
2075 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002076 *
2077 * Whenever the property with the given name is accessed on objects
2078 * created from this ObjectTemplate the getter and setter callbacks
2079 * are called instead of getting and setting the property directly
2080 * on the JavaScript object.
2081 *
2082 * \param name The name of the property for which an accessor is added.
2083 * \param getter The callback to invoke when getting the property.
2084 * \param setter The callback to invoke when setting the property.
2085 * \param data A piece of data that will be passed to the getter and setter
2086 * callbacks whenever they are invoked.
2087 * \param settings Access control settings for the accessor. This is a bit
2088 * field consisting of one of more of
2089 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2090 * The default is to not allow cross-context access.
2091 * ALL_CAN_READ means that all cross-context reads are allowed.
2092 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2093 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2094 * cross-context access.
2095 * \param attribute The attributes of the property for which an accessor
2096 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 */
2098 void SetAccessor(Handle<String> name,
2099 AccessorGetter getter,
2100 AccessorSetter setter = 0,
2101 Handle<Value> data = Handle<Value>(),
2102 AccessControl settings = DEFAULT,
2103 PropertyAttribute attribute = None);
2104
2105 /**
2106 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002107 *
2108 * Whenever a named property is accessed on objects created from
2109 * this object template, the provided callback is invoked instead of
2110 * accessing the property directly on the JavaScript object.
2111 *
2112 * \param getter The callback to invoke when getting a property.
2113 * \param setter The callback to invoke when setting a property.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002114 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115 * \param deleter The callback to invoke when deleting a property.
2116 * \param enumerator The callback to invoke to enumerate all the named
2117 * properties of an object.
2118 * \param data A piece of data that will be passed to the callbacks
2119 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120 */
2121 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2122 NamedPropertySetter setter = 0,
2123 NamedPropertyQuery query = 0,
2124 NamedPropertyDeleter deleter = 0,
2125 NamedPropertyEnumerator enumerator = 0,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002126 Handle<Value> data = Handle<Value>()) {
2127 NamedPropertyQueryImpl casted =
2128 reinterpret_cast<NamedPropertyQueryImpl>(query);
2129 SetNamedPropertyHandlerImpl(getter,
2130 setter,
2131 casted,
2132 deleter,
2133 enumerator,
2134 data);
2135 }
2136
2137 private:
2138 void SetNamedPropertyHandlerImpl(NamedPropertyGetter getter,
2139 NamedPropertySetter setter,
2140 NamedPropertyQueryImpl query,
2141 NamedPropertyDeleter deleter,
2142 NamedPropertyEnumerator enumerator,
2143 Handle<Value> data);
2144
2145 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146
2147 /**
2148 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149 *
2150 * Whenever an indexed property is accessed on objects created from
2151 * this object template, the provided callback is invoked instead of
2152 * accessing the property directly on the JavaScript object.
2153 *
2154 * \param getter The callback to invoke when getting a property.
2155 * \param setter The callback to invoke when setting a property.
2156 * \param query The callback to invoke to check is an object has a property.
2157 * \param deleter The callback to invoke when deleting a property.
2158 * \param enumerator The callback to invoke to enumerate all the indexed
2159 * properties of an object.
2160 * \param data A piece of data that will be passed to the callbacks
2161 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 */
2163 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2164 IndexedPropertySetter setter = 0,
2165 IndexedPropertyQuery query = 0,
2166 IndexedPropertyDeleter deleter = 0,
2167 IndexedPropertyEnumerator enumerator = 0,
2168 Handle<Value> data = Handle<Value>());
2169 /**
2170 * Sets the callback to be used when calling instances created from
2171 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 * function.
2174 */
2175 void SetCallAsFunctionHandler(InvocationCallback callback,
2176 Handle<Value> data = Handle<Value>());
2177
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 /**
2179 * Mark object instances of the template as undetectable.
2180 *
2181 * In many ways, undetectable objects behave as though they are not
2182 * there. They behave like 'undefined' in conditionals and when
2183 * printed. However, properties can be accessed and called as on
2184 * normal objects.
2185 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 void MarkAsUndetectable();
2187
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 /**
2189 * Sets access check callbacks on the object template.
2190 *
2191 * When accessing properties on instances of this object template,
2192 * the access check callback will be called to determine whether or
2193 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002194 * The last parameter specifies whether access checks are turned
2195 * on by default on instances. If access checks are off by default,
2196 * they can be turned on on individual instances by calling
2197 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2200 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002201 Handle<Value> data = Handle<Value>(),
2202 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203
kasper.lund212ac232008-07-16 07:07:30 +00002204 /**
2205 * Gets the number of internal fields for objects generated from
2206 * this template.
2207 */
2208 int InternalFieldCount();
2209
2210 /**
2211 * Sets the number of internal fields for objects generated from
2212 * this template.
2213 */
2214 void SetInternalFieldCount(int value);
2215
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 private:
2217 ObjectTemplate();
2218 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2219 friend class FunctionTemplate;
2220};
2221
2222
2223/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 * A Signature specifies which receivers and arguments a function can
2225 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002227class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 public:
2229 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2230 Handle<FunctionTemplate>(),
2231 int argc = 0,
2232 Handle<FunctionTemplate> argv[] = 0);
2233 private:
2234 Signature();
2235};
2236
2237
2238/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002239 * A utility for determining the type of objects based on the template
2240 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002242class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 public:
2244 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2245 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2246 int match(Handle<Value> value);
2247 private:
2248 TypeSwitch();
2249};
2250
2251
2252// --- E x t e n s i o n s ---
2253
2254
2255/**
2256 * Ignore
2257 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002258class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 public:
2260 Extension(const char* name,
2261 const char* source = 0,
2262 int dep_count = 0,
2263 const char** deps = 0);
2264 virtual ~Extension() { }
2265 virtual v8::Handle<v8::FunctionTemplate>
2266 GetNativeFunction(v8::Handle<v8::String> name) {
2267 return v8::Handle<v8::FunctionTemplate>();
2268 }
2269
2270 const char* name() { return name_; }
2271 const char* source() { return source_; }
2272 int dependency_count() { return dep_count_; }
2273 const char** dependencies() { return deps_; }
2274 void set_auto_enable(bool value) { auto_enable_ = value; }
2275 bool auto_enable() { return auto_enable_; }
2276
2277 private:
2278 const char* name_;
2279 const char* source_;
2280 int dep_count_;
2281 const char** deps_;
2282 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002283
2284 // Disallow copying and assigning.
2285 Extension(const Extension&);
2286 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287};
2288
2289
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002290void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291
2292
2293/**
2294 * Ignore
2295 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002296class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 public:
2298 inline DeclareExtension(Extension* extension) {
2299 RegisterExtension(extension);
2300 }
2301};
2302
2303
2304// --- S t a t i c s ---
2305
2306
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002307Handle<Primitive> V8EXPORT Undefined();
2308Handle<Primitive> V8EXPORT Null();
2309Handle<Boolean> V8EXPORT True();
2310Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311
2312
2313/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002314 * A set of constraints that specifies the limits of the runtime's memory use.
2315 * You must set the heap size before initializing the VM - the size cannot be
2316 * adjusted after the VM is initialized.
2317 *
2318 * If you are using threads then you should hold the V8::Locker lock while
2319 * setting the stack limit and you must set a non-default stack limit separately
2320 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002322class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 public:
2324 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002325 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002327 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002329 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002330 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2332 private:
2333 int max_young_space_size_;
2334 int max_old_space_size_;
2335 uint32_t* stack_limit_;
2336};
2337
2338
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002339bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340
2341
2342// --- E x c e p t i o n s ---
2343
2344
2345typedef void (*FatalErrorCallback)(const char* location, const char* message);
2346
2347
2348typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2349
2350
2351/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002352 * Schedules an exception to be thrown when returning to JavaScript. When an
2353 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002355 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002356 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002357Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358
2359/**
2360 * Create new error objects by calling the corresponding error object
2361 * constructor with the message.
2362 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002363class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364 public:
2365 static Local<Value> RangeError(Handle<String> message);
2366 static Local<Value> ReferenceError(Handle<String> message);
2367 static Local<Value> SyntaxError(Handle<String> message);
2368 static Local<Value> TypeError(Handle<String> message);
2369 static Local<Value> Error(Handle<String> message);
2370};
2371
2372
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002373// --- 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 +00002374
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002375typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002377typedef void* (*CreateHistogramCallback)(const char* name,
2378 int min,
2379 int max,
2380 size_t buckets);
2381
2382typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2383
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384// --- 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 ---
2385typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2386 AccessType type,
2387 Local<Value> data);
2388
2389// --- 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
2390
2391/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002392 * Applications can register callback functions which will be called
2393 * before and after a garbage collection. Allocations are not
2394 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002395 * objects (set or delete properties for example) since it is possible
2396 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002398enum GCType {
2399 kGCTypeScavenge = 1 << 0,
2400 kGCTypeMarkSweepCompact = 1 << 1,
2401 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2402};
2403
2404enum GCCallbackFlags {
2405 kNoGCCallbackFlags = 0,
2406 kGCCallbackFlagCompacted = 1 << 0
2407};
2408
2409typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2410typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2411
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412typedef void (*GCCallback)();
2413
2414
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002416 * Profiler modules.
2417 *
2418 * In V8, profiler consists of several modules: CPU profiler, and different
2419 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002420 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2421 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002422 */
2423enum ProfilerModules {
2424 PROFILER_MODULE_NONE = 0,
2425 PROFILER_MODULE_CPU = 1,
2426 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002427 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2428 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002429};
2430
2431
2432/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002433 * Collection of V8 heap information.
2434 *
2435 * Instances of this class can be passed to v8::V8::HeapStatistics to
2436 * get heap statistics from V8.
2437 */
2438class V8EXPORT HeapStatistics {
2439 public:
2440 HeapStatistics();
2441 size_t total_heap_size() { return total_heap_size_; }
2442 size_t used_heap_size() { return used_heap_size_; }
2443
2444 private:
2445 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2446 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2447
2448 size_t total_heap_size_;
2449 size_t used_heap_size_;
2450
2451 friend class V8;
2452};
2453
2454
2455/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002456 * Container class for static utility functions.
2457 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002458class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002460 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 static void SetFatalErrorHandler(FatalErrorCallback that);
2462
v8.team.kasperl727e9952008-09-02 14:56:44 +00002463 /**
2464 * Ignore out-of-memory exceptions.
2465 *
2466 * V8 running out of memory is treated as a fatal error by default.
2467 * This means that the fatal error handler is called and that V8 is
2468 * terminated.
2469 *
2470 * IgnoreOutOfMemoryException can be used to not treat a
2471 * out-of-memory situation as a fatal error. This way, the contexts
2472 * that did not cause the out of memory problem might be able to
2473 * continue execution.
2474 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475 static void IgnoreOutOfMemoryException();
2476
v8.team.kasperl727e9952008-09-02 14:56:44 +00002477 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002478 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002479 * fatal errors such as out-of-memory situations.
2480 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 static bool IsDead();
2482
2483 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002484 * Adds a message listener.
2485 *
2486 * The same message listener can be added more than once and it that
2487 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 */
2489 static bool AddMessageListener(MessageCallback that,
2490 Handle<Value> data = Handle<Value>());
2491
2492 /**
2493 * Remove all message listeners from the specified callback function.
2494 */
2495 static void RemoveMessageListeners(MessageCallback that);
2496
2497 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002498 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 */
2500 static void SetFlagsFromString(const char* str, int length);
2501
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002502 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002503 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002504 */
2505 static void SetFlagsFromCommandLine(int* argc,
2506 char** argv,
2507 bool remove_flags);
2508
kasper.lund7276f142008-07-30 08:49:36 +00002509 /** Get the version string. */
2510 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511
2512 /**
2513 * Enables the host application to provide a mechanism for recording
2514 * statistics counters.
2515 */
2516 static void SetCounterFunction(CounterLookupCallback);
2517
2518 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002519 * Enables the host application to provide a mechanism for recording
2520 * histograms. The CreateHistogram function returns a
2521 * histogram which will later be passed to the AddHistogramSample
2522 * function.
2523 */
2524 static void SetCreateHistogramFunction(CreateHistogramCallback);
2525 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2526
2527 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 * Enables the computation of a sliding window of states. The sliding
2529 * window information is recorded in statistics counters.
2530 */
2531 static void EnableSlidingStateWindow();
2532
2533 /** Callback function for reporting failed access checks.*/
2534 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2535
2536 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002537 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002538 * garbage collection. Allocations are not allowed in the
2539 * callback function, you therefore cannot manipulate objects (set
2540 * or delete properties for example) since it is possible such
2541 * operations will result in the allocation of objects. It is possible
2542 * to specify the GCType filter for your callback. But it is not possible to
2543 * register the same callback function two times with different
2544 * GCType filters.
2545 */
2546 static void AddGCPrologueCallback(
2547 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2548
2549 /**
2550 * This function removes callback which was installed by
2551 * AddGCPrologueCallback function.
2552 */
2553 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2554
2555 /**
2556 * The function is deprecated. Please use AddGCPrologueCallback instead.
2557 * Enables the host application to receive a notification before a
2558 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002559 * callback function, you therefore cannot manipulate objects (set
2560 * or delete properties for example) since it is possible such
2561 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 */
2563 static void SetGlobalGCPrologueCallback(GCCallback);
2564
2565 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002566 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002567 * garbage collection. Allocations are not allowed in the
2568 * callback function, you therefore cannot manipulate objects (set
2569 * or delete properties for example) since it is possible such
2570 * operations will result in the allocation of objects. It is possible
2571 * to specify the GCType filter for your callback. But it is not possible to
2572 * register the same callback function two times with different
2573 * GCType filters.
2574 */
2575 static void AddGCEpilogueCallback(
2576 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2577
2578 /**
2579 * This function removes callback which was installed by
2580 * AddGCEpilogueCallback function.
2581 */
2582 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2583
2584 /**
2585 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2586 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002587 * major garbage collection. Allocations are not allowed in the
2588 * callback function, you therefore cannot manipulate objects (set
2589 * or delete properties for example) since it is possible such
2590 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 */
2592 static void SetGlobalGCEpilogueCallback(GCCallback);
2593
2594 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002595 * Allows the host application to group objects together. If one
2596 * object in the group is alive, all objects in the group are alive.
2597 * After each garbage collection, object groups are removed. It is
2598 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002599 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002600 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002602 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
2604 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002605 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002606 * initialize from scratch. This function is called implicitly if
2607 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 */
2609 static bool Initialize();
2610
kasper.lund7276f142008-07-30 08:49:36 +00002611 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002612 * Adjusts the amount of registered external memory. Used to give
2613 * V8 an indication of the amount of externally allocated memory
2614 * that is kept alive by JavaScript objects. V8 uses this to decide
2615 * when to perform global garbage collections. Registering
2616 * externally allocated memory will trigger global garbage
2617 * collections more often than otherwise in an attempt to garbage
2618 * collect the JavaScript objects keeping the externally allocated
2619 * memory alive.
2620 *
2621 * \param change_in_bytes the change in externally allocated memory
2622 * that is kept alive by JavaScript objects.
2623 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002624 */
2625 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2626
iposva@chromium.org245aa852009-02-10 00:49:54 +00002627 /**
2628 * Suspends recording of tick samples in the profiler.
2629 * When the V8 profiling mode is enabled (usually via command line
2630 * switches) this function suspends recording of tick samples.
2631 * Profiling ticks are discarded until ResumeProfiler() is called.
2632 *
2633 * See also the --prof and --prof_auto command line switches to
2634 * enable V8 profiling.
2635 */
2636 static void PauseProfiler();
2637
2638 /**
2639 * Resumes recording of tick samples in the profiler.
2640 * See also PauseProfiler().
2641 */
2642 static void ResumeProfiler();
2643
ager@chromium.org41826e72009-03-30 13:30:57 +00002644 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002645 * Return whether profiler is currently paused.
2646 */
2647 static bool IsProfilerPaused();
2648
2649 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002650 * Resumes specified profiler modules. Can be called several times to
2651 * mark the opening of a profiler events block with the given tag.
2652 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002653 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2654 * See ProfilerModules enum.
2655 *
2656 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002657 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002658 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002659 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002660
2661 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002662 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2663 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2664 * same tag value. There is no need for blocks to be properly nested.
2665 * The profiler is paused when the last opened block is closed.
2666 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002667 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2668 * See ProfilerModules enum.
2669 *
2670 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002671 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002672 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002673 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002674
2675 /**
2676 * Returns active (resumed) profiler modules.
2677 * See ProfilerModules enum.
2678 *
2679 * \returns active profiler modules.
2680 */
2681 static int GetActiveProfilerModules();
2682
2683 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002684 * If logging is performed into a memory buffer (via --logfile=*), allows to
2685 * retrieve previously written messages. This can be used for retrieving
2686 * profiler log data in the application. This function is thread-safe.
2687 *
2688 * Caller provides a destination buffer that must exist during GetLogLines
2689 * call. Only whole log lines are copied into the buffer.
2690 *
2691 * \param from_pos specified a point in a buffer to read from, 0 is the
2692 * beginning of a buffer. It is assumed that caller updates its current
2693 * position using returned size value from the previous call.
2694 * \param dest_buf destination buffer for log data.
2695 * \param max_size size of the destination buffer.
2696 * \returns actual size of log data copied into buffer.
2697 */
2698 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2699
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002700 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002701 * The minimum allowed size for a log lines buffer. If the size of
2702 * the buffer given will not be enough to hold a line of the maximum
2703 * length, an attempt to find a log line end in GetLogLines will
2704 * fail, and an empty result will be returned.
2705 */
2706 static const int kMinimumSizeForLogLinesBuffer = 2048;
2707
2708 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002709 * Retrieve the V8 thread id of the calling thread.
2710 *
2711 * The thread id for a thread should only be retrieved after the V8
2712 * lock has been acquired with a Locker object with that thread.
2713 */
2714 static int GetCurrentThreadId();
2715
2716 /**
2717 * Forcefully terminate execution of a JavaScript thread. This can
2718 * be used to terminate long-running scripts.
2719 *
2720 * TerminateExecution should only be called when then V8 lock has
2721 * been acquired with a Locker object. Therefore, in order to be
2722 * able to terminate long-running threads, preemption must be
2723 * enabled to allow the user of TerminateExecution to acquire the
2724 * lock.
2725 *
2726 * The termination is achieved by throwing an exception that is
2727 * uncatchable by JavaScript exception handlers. Termination
2728 * exceptions act as if they were caught by a C++ TryCatch exception
2729 * handlers. If forceful termination is used, any C++ TryCatch
2730 * exception handler that catches an exception should check if that
2731 * exception is a termination exception and immediately return if
2732 * that is the case. Returning immediately in that case will
2733 * continue the propagation of the termination exception if needed.
2734 *
2735 * The thread id passed to TerminateExecution must have been
2736 * obtained by calling GetCurrentThreadId on the thread in question.
2737 *
2738 * \param thread_id The thread id of the thread to terminate.
2739 */
2740 static void TerminateExecution(int thread_id);
2741
2742 /**
2743 * Forcefully terminate the current thread of JavaScript execution.
2744 *
2745 * This method can be used by any thread even if that thread has not
2746 * acquired the V8 lock with a Locker object.
2747 */
2748 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002749
2750 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00002751 * Is V8 terminating JavaScript execution.
2752 *
2753 * Returns true if JavaScript execution is currently terminating
2754 * because of a call to TerminateExecution. In that case there are
2755 * still JavaScript frames on the stack and the termination
2756 * exception is still active.
2757 */
2758 static bool IsExecutionTerminating();
2759
2760 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002761 * Releases any resources used by v8 and stops any utility threads
2762 * that may be running. Note that disposing v8 is permanent, it
2763 * cannot be reinitialized.
2764 *
2765 * It should generally not be necessary to dispose v8 before exiting
2766 * a process, this should happen automatically. It is only necessary
2767 * to use if the process needs the resources taken up by v8.
2768 */
2769 static bool Dispose();
2770
ager@chromium.org3811b432009-10-28 14:53:37 +00002771 /**
2772 * Get statistics about the heap memory usage.
2773 */
2774 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002775
2776 /**
2777 * Optional notification that the embedder is idle.
2778 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002779 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002780 * Returns true if the embedder should stop calling IdleNotification
2781 * until real work has been done. This indicates that V8 has done
2782 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002783 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002784 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002785
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002786 /**
2787 * Optional notification that the system is running low on memory.
2788 * V8 uses these notifications to attempt to free memory.
2789 */
2790 static void LowMemoryNotification();
2791
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002792 /**
2793 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002794 * these notifications to guide the GC heuristic. Returns the number
2795 * of context disposals - including this one - since the last time
2796 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002797 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002798 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002799
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 private:
2801 V8();
2802
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002803 static internal::Object** GlobalizeReference(internal::Object** handle);
2804 static void DisposeGlobal(internal::Object** global_handle);
2805 static void MakeWeak(internal::Object** global_handle,
2806 void* data,
2807 WeakReferenceCallback);
2808 static void ClearWeak(internal::Object** global_handle);
2809 static bool IsGlobalNearDeath(internal::Object** global_handle);
2810 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811
2812 template <class T> friend class Handle;
2813 template <class T> friend class Local;
2814 template <class T> friend class Persistent;
2815 friend class Context;
2816};
2817
2818
2819/**
2820 * An external exception handler.
2821 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002822class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002823 public:
2824
2825 /**
2826 * Creates a new try/catch block and registers it with v8.
2827 */
2828 TryCatch();
2829
2830 /**
2831 * Unregisters and deletes this try/catch block.
2832 */
2833 ~TryCatch();
2834
2835 /**
2836 * Returns true if an exception has been caught by this try/catch block.
2837 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002838 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839
2840 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002841 * For certain types of exceptions, it makes no sense to continue
2842 * execution.
2843 *
2844 * Currently, the only type of exception that can be caught by a
2845 * TryCatch handler and for which it does not make sense to continue
2846 * is termination exception. Such exceptions are thrown when the
2847 * TerminateExecution methods are called to terminate a long-running
2848 * script.
2849 *
2850 * If CanContinue returns false, the correct action is to perform
2851 * any C++ cleanup needed and then return.
2852 */
2853 bool CanContinue() const;
2854
2855 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002856 * Throws the exception caught by this TryCatch in a way that avoids
2857 * it being caught again by this same TryCatch. As with ThrowException
2858 * it is illegal to execute any JavaScript operations after calling
2859 * ReThrow; the caller must return immediately to where the exception
2860 * is caught.
2861 */
2862 Handle<Value> ReThrow();
2863
2864 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002865 * Returns the exception caught by this try/catch block. If no exception has
2866 * been caught an empty handle is returned.
2867 *
2868 * The returned handle is valid until this TryCatch block has been destroyed.
2869 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002870 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871
2872 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002873 * Returns the .stack property of the thrown object. If no .stack
2874 * property is present an empty handle is returned.
2875 */
2876 Local<Value> StackTrace() const;
2877
2878 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002879 * Returns the message associated with this exception. If there is
2880 * no message associated an empty handle is returned.
2881 *
2882 * The returned handle is valid until this TryCatch block has been
2883 * destroyed.
2884 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002885 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002886
2887 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002888 * Clears any exceptions that may have been caught by this try/catch block.
2889 * After this method has been called, HasCaught() will return false.
2890 *
2891 * It is not necessary to clear a try/catch block before using it again; if
2892 * another exception is thrown the previously caught exception will just be
2893 * overwritten. However, it is often a good idea since it makes it easier
2894 * to determine which operation threw a given exception.
2895 */
2896 void Reset();
2897
v8.team.kasperl727e9952008-09-02 14:56:44 +00002898 /**
2899 * Set verbosity of the external exception handler.
2900 *
2901 * By default, exceptions that are caught by an external exception
2902 * handler are not reported. Call SetVerbose with true on an
2903 * external exception handler to have exceptions caught by the
2904 * handler reported as if they were not caught.
2905 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 void SetVerbose(bool value);
2907
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002908 /**
2909 * Set whether or not this TryCatch should capture a Message object
2910 * which holds source information about where the exception
2911 * occurred. True by default.
2912 */
2913 void SetCaptureMessage(bool value);
2914
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002915 private:
2916 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002918 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002919 bool is_verbose_ : 1;
2920 bool can_continue_ : 1;
2921 bool capture_message_ : 1;
2922 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002923
2924 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925};
2926
2927
2928// --- C o n t e x t ---
2929
2930
2931/**
2932 * Ignore
2933 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002934class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002935 public:
2936 ExtensionConfiguration(int name_count, const char* names[])
2937 : name_count_(name_count), names_(names) { }
2938 private:
2939 friend class ImplementationUtilities;
2940 int name_count_;
2941 const char** names_;
2942};
2943
2944
2945/**
2946 * A sandboxed execution context with its own set of built-in objects
2947 * and functions.
2948 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002949class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002951 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 Local<Object> Global();
2953
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002954 /**
2955 * Detaches the global object from its context before
2956 * the global object can be reused to create a new context.
2957 */
2958 void DetachGlobal();
2959
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002960 /**
2961 * Reattaches a global object to a context. This can be used to
2962 * restore the connection between a global object and a context
2963 * after DetachGlobal has been called.
2964 *
2965 * \param global_object The global object to reattach to the
2966 * context. For this to work, the global object must be the global
2967 * object that was associated with this context before a call to
2968 * DetachGlobal.
2969 */
2970 void ReattachGlobal(Handle<Object> global_object);
2971
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002972 /** Creates a new context.
2973 *
2974 * Returns a persistent handle to the newly allocated context. This
2975 * persistent handle has to be disposed when the context is no
2976 * longer used so the context can be garbage collected.
2977 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00002978 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002979 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00002980 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2981 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982
kasper.lund44510672008-07-25 07:37:58 +00002983 /** Returns the last entered context. */
2984 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985
kasper.lund44510672008-07-25 07:37:58 +00002986 /** Returns the context that is on the top of the stack. */
2987 static Local<Context> GetCurrent();
2988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002989 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002990 * Returns the context of the calling JavaScript code. That is the
2991 * context of the top-most JavaScript frame. If there are no
2992 * JavaScript frames an empty handle is returned.
2993 */
2994 static Local<Context> GetCalling();
2995
2996 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997 * Sets the security token for the context. To access an object in
2998 * another context, the security tokens must match.
2999 */
3000 void SetSecurityToken(Handle<Value> token);
3001
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003002 /** Restores the security token to the default value. */
3003 void UseDefaultSecurityToken();
3004
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003005 /** Returns the security token of this context.*/
3006 Handle<Value> GetSecurityToken();
3007
v8.team.kasperl727e9952008-09-02 14:56:44 +00003008 /**
3009 * Enter this context. After entering a context, all code compiled
3010 * and run is compiled and run in this context. If another context
3011 * is already entered, this old context is saved so it can be
3012 * restored when the new context is exited.
3013 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003014 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003015
3016 /**
3017 * Exit this context. Exiting the current context restores the
3018 * context that was in place when entering the current context.
3019 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003020 void Exit();
3021
v8.team.kasperl727e9952008-09-02 14:56:44 +00003022 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 bool HasOutOfMemoryException();
3024
v8.team.kasperl727e9952008-09-02 14:56:44 +00003025 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003026 static bool InContext();
3027
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003028 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003029 * Associate an additional data object with the context. This is mainly used
3030 * with the debugger to provide additional information on the context through
3031 * the debugger API.
3032 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003033 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003034 Local<Value> GetData();
3035
3036 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 * Stack-allocated class which sets the execution context for all
3038 * operations executed within a local scope.
3039 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003040 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003041 public:
3042 inline Scope(Handle<Context> context) : context_(context) {
3043 context_->Enter();
3044 }
3045 inline ~Scope() { context_->Exit(); }
3046 private:
3047 Handle<Context> context_;
3048 };
3049
3050 private:
3051 friend class Value;
3052 friend class Script;
3053 friend class Object;
3054 friend class Function;
3055};
3056
3057
3058/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003059 * Multiple threads in V8 are allowed, but only one thread at a time
3060 * is allowed to use V8. The definition of 'using V8' includes
3061 * accessing handles or holding onto object pointers obtained from V8
3062 * handles. It is up to the user of V8 to ensure (perhaps with
3063 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064 *
3065 * If you wish to start using V8 in a thread you can do this by constructing
3066 * a v8::Locker object. After the code using V8 has completed for the
3067 * current thread you can call the destructor. This can be combined
3068 * with C++ scope-based construction as follows:
3069 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003070 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 * ...
3072 * {
3073 * v8::Locker locker;
3074 * ...
3075 * // Code using V8 goes here.
3076 * ...
3077 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003078 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 *
3080 * If you wish to stop using V8 in a thread A you can do this by either
3081 * by destroying the v8::Locker object as above or by constructing a
3082 * v8::Unlocker object:
3083 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003084 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 * {
3086 * v8::Unlocker unlocker;
3087 * ...
3088 * // Code not using V8 goes here while V8 can run in another thread.
3089 * ...
3090 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003091 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003092 *
3093 * The Unlocker object is intended for use in a long-running callback
3094 * from V8, where you want to release the V8 lock for other threads to
3095 * use.
3096 *
3097 * The v8::Locker is a recursive lock. That is, you can lock more than
3098 * once in a given thread. This can be useful if you have code that can
3099 * be called either from code that holds the lock or from code that does
3100 * not. The Unlocker is not recursive so you can not have several
3101 * Unlockers on the stack at once, and you can not use an Unlocker in a
3102 * thread that is not inside a Locker's scope.
3103 *
3104 * An unlocker will unlock several lockers if it has to and reinstate
3105 * the correct depth of locking on its destruction. eg.:
3106 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003107 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003108 * // V8 not locked.
3109 * {
3110 * v8::Locker locker;
3111 * // V8 locked.
3112 * {
3113 * v8::Locker another_locker;
3114 * // V8 still locked (2 levels).
3115 * {
3116 * v8::Unlocker unlocker;
3117 * // V8 not locked.
3118 * }
3119 * // V8 locked again (2 levels).
3120 * }
3121 * // V8 still locked (1 level).
3122 * }
3123 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003124 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003125 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003126class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003127 public:
3128 Unlocker();
3129 ~Unlocker();
3130};
3131
3132
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003133class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003134 public:
3135 Locker();
3136 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003137
3138 /**
3139 * Start preemption.
3140 *
3141 * When preemption is started, a timer is fired every n milli seconds
3142 * that will switch between multiple threads that are in contention
3143 * for the V8 lock.
3144 */
3145 static void StartPreemption(int every_n_ms);
3146
3147 /**
3148 * Stop preemption.
3149 */
3150 static void StopPreemption();
3151
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003152 /**
3153 * Returns whether or not the locker is locked by the current thread.
3154 */
3155 static bool IsLocked();
3156
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003157 /**
3158 * Returns whether v8::Locker is being used by this V8 instance.
3159 */
3160 static bool IsActive() { return active_; }
3161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162 private:
3163 bool has_lock_;
3164 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003165
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003166 static bool active_;
3167
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003168 // Disallow copying and assigning.
3169 Locker(const Locker&);
3170 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003171};
3172
3173
3174
3175// --- I m p l e m e n t a t i o n ---
3176
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003177
3178namespace internal {
3179
3180
3181// Tag information for HeapObject.
3182const int kHeapObjectTag = 1;
3183const int kHeapObjectTagSize = 2;
3184const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3185
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003186// Tag information for Smi.
3187const int kSmiTag = 0;
3188const int kSmiTagSize = 1;
3189const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3190
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003191template <size_t ptr_size> struct SmiConstants;
3192
3193// Smi constants for 32-bit systems.
3194template <> struct SmiConstants<4> {
3195 static const int kSmiShiftSize = 0;
3196 static const int kSmiValueSize = 31;
3197 static inline int SmiToInt(internal::Object* value) {
3198 int shift_bits = kSmiTagSize + kSmiShiftSize;
3199 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3200 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3201 }
3202};
3203
3204// Smi constants for 64-bit systems.
3205template <> struct SmiConstants<8> {
3206 static const int kSmiShiftSize = 31;
3207 static const int kSmiValueSize = 32;
3208 static inline int SmiToInt(internal::Object* value) {
3209 int shift_bits = kSmiTagSize + kSmiShiftSize;
3210 // Shift down and throw away top 32 bits.
3211 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3212 }
3213};
3214
3215const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
3216const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003217
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003218template <size_t ptr_size> struct InternalConstants;
3219
3220// Internal constants for 32-bit systems.
3221template <> struct InternalConstants<4> {
3222 static const int kStringResourceOffset = 3 * sizeof(void*);
3223};
3224
3225// Internal constants for 64-bit systems.
3226template <> struct InternalConstants<8> {
ager@chromium.orgac091b72010-05-05 07:34:42 +00003227 static const int kStringResourceOffset = 3 * sizeof(void*);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003228};
3229
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003230/**
3231 * This class exports constants and functionality from within v8 that
3232 * is necessary to implement inline functions in the v8 api. Don't
3233 * depend on functions and constants defined here.
3234 */
3235class Internals {
3236 public:
3237
3238 // These values match non-compiler-dependent values defined within
3239 // the implementation of v8.
3240 static const int kHeapObjectMapOffset = 0;
3241 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003242 static const int kStringResourceOffset =
3243 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3244
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003245 static const int kProxyProxyOffset = sizeof(void*);
3246 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3247 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003248 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003249
3250 // These constants are compiler dependent so their values must be
3251 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00003252 V8EXPORT static int kJSObjectType;
3253 V8EXPORT static int kFirstNonstringType;
3254 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003255
3256 static inline bool HasHeapObjectTag(internal::Object* value) {
3257 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3258 kHeapObjectTag);
3259 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003260
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003261 static inline bool HasSmiTag(internal::Object* value) {
3262 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3263 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003264
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003265 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003266 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3267 }
3268
3269 static inline int GetInstanceType(internal::Object* obj) {
3270 typedef internal::Object O;
3271 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3272 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3273 }
3274
3275 static inline void* GetExternalPointer(internal::Object* obj) {
3276 if (HasSmiTag(obj)) {
3277 return obj;
3278 } else if (GetInstanceType(obj) == kProxyType) {
3279 return ReadField<void*>(obj, kProxyProxyOffset);
3280 } else {
3281 return NULL;
3282 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003283 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003284
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003285 static inline bool IsExternalTwoByteString(int instance_type) {
3286 int representation = (instance_type & kFullStringRepresentationMask);
3287 return representation == kExternalTwoByteRepresentationTag;
3288 }
3289
3290 template <typename T>
3291 static inline T ReadField(Object* ptr, int offset) {
3292 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3293 return *reinterpret_cast<T*>(addr);
3294 }
3295
3296};
3297
3298}
3299
3300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003301template <class T>
3302Handle<T>::Handle() : val_(0) { }
3303
3304
3305template <class T>
3306Local<T>::Local() : Handle<T>() { }
3307
3308
3309template <class T>
3310Local<T> Local<T>::New(Handle<T> that) {
3311 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003312 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3314}
3315
3316
3317template <class T>
3318Persistent<T> Persistent<T>::New(Handle<T> that) {
3319 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003320 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3322}
3323
3324
3325template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003326bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003328 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329}
3330
3331
3332template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003333bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003334 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003335 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003336}
3337
3338
3339template <class T>
3340void Persistent<T>::Dispose() {
3341 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003342 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003343}
3344
3345
3346template <class T>
3347Persistent<T>::Persistent() : Handle<T>() { }
3348
3349template <class T>
3350void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003351 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3352 parameters,
3353 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003354}
3355
3356template <class T>
3357void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003358 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359}
3360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361Local<Value> Arguments::operator[](int i) const {
3362 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3363 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3364}
3365
3366
3367Local<Function> Arguments::Callee() const {
3368 return callee_;
3369}
3370
3371
3372Local<Object> Arguments::This() const {
3373 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3374}
3375
3376
3377Local<Object> Arguments::Holder() const {
3378 return holder_;
3379}
3380
3381
3382Local<Value> Arguments::Data() const {
3383 return data_;
3384}
3385
3386
3387bool Arguments::IsConstructCall() const {
3388 return is_construct_call_;
3389}
3390
3391
3392int Arguments::Length() const {
3393 return length_;
3394}
3395
3396
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003397template <class T>
3398Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003399 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3400 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003401 return Local<T>(reinterpret_cast<T*>(after));
3402}
3403
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003404Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 return resource_name_;
3406}
3407
3408
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003409Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410 return resource_line_offset_;
3411}
3412
3413
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003414Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415 return resource_column_offset_;
3416}
3417
3418
3419Handle<Boolean> Boolean::New(bool value) {
3420 return value ? True() : False();
3421}
3422
3423
3424void Template::Set(const char* name, v8::Handle<Data> value) {
3425 Set(v8::String::New(name), value);
3426}
3427
3428
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003429Local<Value> Object::GetInternalField(int index) {
3430#ifndef V8_ENABLE_CHECKS
3431 Local<Value> quick_result = UncheckedGetInternalField(index);
3432 if (!quick_result.IsEmpty()) return quick_result;
3433#endif
3434 return CheckedGetInternalField(index);
3435}
3436
3437
3438Local<Value> Object::UncheckedGetInternalField(int index) {
3439 typedef internal::Object O;
3440 typedef internal::Internals I;
3441 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003442 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003443 // If the object is a plain JSObject, which is the common case,
3444 // we know where to find the internal fields and can return the
3445 // value directly.
3446 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3447 O* value = I::ReadField<O*>(obj, offset);
3448 O** result = HandleScope::CreateHandle(value);
3449 return Local<Value>(reinterpret_cast<Value*>(result));
3450 } else {
3451 return Local<Value>();
3452 }
3453}
3454
3455
3456void* External::Unwrap(Handle<v8::Value> obj) {
3457#ifdef V8_ENABLE_CHECKS
3458 return FullUnwrap(obj);
3459#else
3460 return QuickUnwrap(obj);
3461#endif
3462}
3463
3464
3465void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3466 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003467 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003468 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003469}
3470
3471
3472void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003473 typedef internal::Object O;
3474 typedef internal::Internals I;
3475
3476 O* obj = *reinterpret_cast<O**>(this);
3477
3478 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3479 // If the object is a plain JSObject, which is the common case,
3480 // we know where to find the internal fields and can return the
3481 // value directly.
3482 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3483 O* value = I::ReadField<O*>(obj, offset);
3484 return I::GetExternalPointer(value);
3485 }
3486
3487 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003488}
3489
3490
3491String* String::Cast(v8::Value* value) {
3492#ifdef V8_ENABLE_CHECKS
3493 CheckCast(value);
3494#endif
3495 return static_cast<String*>(value);
3496}
3497
3498
3499String::ExternalStringResource* String::GetExternalStringResource() const {
3500 typedef internal::Object O;
3501 typedef internal::Internals I;
3502 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003503 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003504 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003505 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3506 result = reinterpret_cast<String::ExternalStringResource*>(value);
3507 } else {
3508 result = NULL;
3509 }
3510#ifdef V8_ENABLE_CHECKS
3511 VerifyExternalStringResource(result);
3512#endif
3513 return result;
3514}
3515
3516
3517bool Value::IsString() const {
3518#ifdef V8_ENABLE_CHECKS
3519 return FullIsString();
3520#else
3521 return QuickIsString();
3522#endif
3523}
3524
3525bool Value::QuickIsString() const {
3526 typedef internal::Object O;
3527 typedef internal::Internals I;
3528 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3529 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003530 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003531}
3532
3533
3534Number* Number::Cast(v8::Value* value) {
3535#ifdef V8_ENABLE_CHECKS
3536 CheckCast(value);
3537#endif
3538 return static_cast<Number*>(value);
3539}
3540
3541
3542Integer* Integer::Cast(v8::Value* value) {
3543#ifdef V8_ENABLE_CHECKS
3544 CheckCast(value);
3545#endif
3546 return static_cast<Integer*>(value);
3547}
3548
3549
3550Date* Date::Cast(v8::Value* value) {
3551#ifdef V8_ENABLE_CHECKS
3552 CheckCast(value);
3553#endif
3554 return static_cast<Date*>(value);
3555}
3556
3557
3558Object* Object::Cast(v8::Value* value) {
3559#ifdef V8_ENABLE_CHECKS
3560 CheckCast(value);
3561#endif
3562 return static_cast<Object*>(value);
3563}
3564
3565
3566Array* Array::Cast(v8::Value* value) {
3567#ifdef V8_ENABLE_CHECKS
3568 CheckCast(value);
3569#endif
3570 return static_cast<Array*>(value);
3571}
3572
3573
3574Function* Function::Cast(v8::Value* value) {
3575#ifdef V8_ENABLE_CHECKS
3576 CheckCast(value);
3577#endif
3578 return static_cast<Function*>(value);
3579}
3580
3581
3582External* External::Cast(v8::Value* value) {
3583#ifdef V8_ENABLE_CHECKS
3584 CheckCast(value);
3585#endif
3586 return static_cast<External*>(value);
3587}
3588
3589
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003590Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003591 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003592}
3593
3594
3595Local<Object> AccessorInfo::This() const {
3596 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3597}
3598
3599
3600Local<Object> AccessorInfo::Holder() const {
3601 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3602}
3603
3604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003606 * \example shell.cc
3607 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 * command-line and executes them.
3609 */
3610
3611
3612/**
3613 * \example process.cc
3614 */
3615
3616
3617} // namespace v8
3618
3619
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003620#undef V8EXPORT
3621#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622#undef TYPE_CHECK
3623
3624
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003625#endif // V8_H_