blob: fe01e30cf6eb4b9ec160148a421a3d9ddac88fe3 [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;
137class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000138
139}
140
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141
142// --- W e a k H a n d l e s
143
144
145/**
146 * A weak reference callback function.
147 *
148 * \param object the weak global object to be reclaimed by the garbage collector
149 * \param parameter the value passed in when making the weak global object
150 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000151typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 void* parameter);
153
154
155// --- H a n d l e s ---
156
157#define TYPE_CHECK(T, S) \
158 while (false) { \
159 *(static_cast<T**>(0)) = static_cast<S*>(0); \
160 }
161
162/**
163 * An object reference managed by the v8 garbage collector.
164 *
165 * All objects returned from v8 have to be tracked by the garbage
166 * collector so that it knows that the objects are still alive. Also,
167 * because the garbage collector may move objects, it is unsafe to
168 * point directly to an object. Instead, all objects are stored in
169 * handles which are known by the garbage collector and updated
170 * whenever an object moves. Handles should always be passed by value
171 * (except in cases like out-parameters) and they should never be
172 * allocated on the heap.
173 *
174 * There are two types of handles: local and persistent handles.
175 * Local handles are light-weight and transient and typically used in
176 * local operations. They are managed by HandleScopes. Persistent
177 * handles can be used when storing objects across several independent
178 * operations and have to be explicitly deallocated when they're no
179 * longer used.
180 *
181 * It is safe to extract the object stored in the handle by
182 * dereferencing the handle (for instance, to extract the Object* from
183 * an Handle<Object>); the value will still be governed by a handle
184 * behind the scenes and the same rules apply to these values as to
185 * their handles.
186 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000187template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 public:
189
190 /**
191 * Creates an empty handle.
192 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000193 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194
195 /**
196 * Creates a new handle for the specified value.
197 */
198 explicit Handle(T* val) : val_(val) { }
199
200 /**
201 * Creates a handle for the contents of the specified handle. This
202 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000203 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 * incompatible handles, for instance from a Handle<String> to a
205 * Handle<Number> it will cause a compiletime error. Assigning
206 * between compatible handles, for instance assigning a
207 * Handle<String> to a variable declared as Handle<Value>, is legal
208 * because String is a subclass of Value.
209 */
210 template <class S> inline Handle(Handle<S> that)
211 : val_(reinterpret_cast<T*>(*that)) {
212 /**
213 * This check fails when trying to convert between incompatible
214 * handles. For example, converting from a Handle<String> to a
215 * Handle<Number>.
216 */
217 TYPE_CHECK(T, S);
218 }
219
220 /**
221 * Returns true if the handle is empty.
222 */
ager@chromium.org32912102009-01-16 10:38:43 +0000223 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000225 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000227 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228
229 /**
230 * Sets the handle to be empty. IsEmpty() will then return true.
231 */
232 void Clear() { this->val_ = 0; }
233
234 /**
235 * Checks whether two handles are the same.
236 * Returns true if both are empty, or if the objects
237 * to which they refer are identical.
238 * The handles' references are not checked.
239 */
ager@chromium.org32912102009-01-16 10:38:43 +0000240 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000241 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
242 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 if (a == 0) return b == 0;
244 if (b == 0) return false;
245 return *a == *b;
246 }
247
248 /**
249 * Checks whether two handles are different.
250 * Returns true if only one of the handles is empty, or if
251 * the objects to which they refer are different.
252 * The handles' references are not checked.
253 */
ager@chromium.org32912102009-01-16 10:38:43 +0000254 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 return !operator==(that);
256 }
257
258 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000259#ifdef V8_ENABLE_CHECKS
260 // If we're going to perform the type check then we have to check
261 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000263#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 return Handle<T>(T::Cast(*that));
265 }
266
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000267 template <class S> inline Handle<S> As() {
268 return Handle<S>::Cast(*this);
269 }
270
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 private:
272 T* val_;
273};
274
275
276/**
277 * A light-weight stack-allocated object handle. All operations
278 * that return objects from within v8 return them in local handles. They
279 * are created within HandleScopes, and all local handles allocated within a
280 * handle scope are destroyed when the handle scope is destroyed. Hence it
281 * is not necessary to explicitly deallocate local handles.
282 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000283template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000285 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 template <class S> inline Local(Local<S> that)
287 : Handle<T>(reinterpret_cast<T*>(*that)) {
288 /**
289 * This check fails when trying to convert between incompatible
290 * handles. For example, converting from a Handle<String> to a
291 * Handle<Number>.
292 */
293 TYPE_CHECK(T, S);
294 }
295 template <class S> inline Local(S* that) : Handle<T>(that) { }
296 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000297#ifdef V8_ENABLE_CHECKS
298 // If we're going to perform the type check then we have to check
299 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000301#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 return Local<T>(T::Cast(*that));
303 }
304
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000305 template <class S> inline Local<S> As() {
306 return Local<S>::Cast(*this);
307 }
308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 /** Create a local handle for the content of another handle.
310 * The referee is kept alive by the local handle even when
311 * the original handle is destroyed/disposed.
312 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000313 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314};
315
316
317/**
318 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000319 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 * allocated, a Persistent handle remains valid until it is explicitly
321 * disposed.
322 *
323 * A persistent handle contains a reference to a storage cell within
324 * the v8 engine which holds an object value and which is updated by
325 * the garbage collector whenever the object is moved. A new storage
326 * cell can be created using Persistent::New and existing handles can
327 * be disposed using Persistent::Dispose. Since persistent handles
328 * are passed by value you may have many persistent handle objects
329 * that point to the same storage cell. For instance, if you pass a
330 * persistent handle as an argument to a function you will not get two
331 * different storage cells but rather two references to the same
332 * storage cell.
333 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000334template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 public:
336
337 /**
338 * Creates an empty persistent handle that doesn't point to any
339 * storage cell.
340 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000341 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342
343 /**
344 * Creates a persistent handle for the same storage cell as the
345 * specified handle. This constructor allows you to pass persistent
346 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000347 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000349 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 * between compatible persistent handles, for instance assigning a
351 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000352 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 */
354 template <class S> inline Persistent(Persistent<S> that)
355 : Handle<T>(reinterpret_cast<T*>(*that)) {
356 /**
357 * This check fails when trying to convert between incompatible
358 * handles. For example, converting from a Handle<String> to a
359 * Handle<Number>.
360 */
361 TYPE_CHECK(T, S);
362 }
363
364 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
365
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000366 /**
367 * "Casts" a plain handle which is known to be a persistent handle
368 * to a persistent handle.
369 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 template <class S> explicit inline Persistent(Handle<S> that)
371 : Handle<T>(*that) { }
372
373 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000374#ifdef V8_ENABLE_CHECKS
375 // If we're going to perform the type check then we have to check
376 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000378#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 return Persistent<T>(T::Cast(*that));
380 }
381
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000382 template <class S> inline Persistent<S> As() {
383 return Persistent<S>::Cast(*this);
384 }
385
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000387 * Creates a new persistent handle for an existing local or
388 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000390 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391
392 /**
393 * Releases the storage cell referenced by this persistent handle.
394 * Does not remove the reference to the cell from any handles.
395 * This handle's reference, and any any other references to the storage
396 * cell remain and IsEmpty will still return false.
397 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000398 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399
400 /**
401 * Make the reference to this object weak. When only weak handles
402 * refer to the object, the garbage collector will perform a
403 * callback to the given V8::WeakReferenceCallback function, passing
404 * it the object reference and the given parameters.
405 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000406 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000409 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410
411 /**
412 *Checks if the handle holds the only reference to an object.
413 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000414 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415
416 /**
417 * Returns true if the handle's reference is weak.
418 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000419 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420
421 private:
422 friend class ImplementationUtilities;
423 friend class ObjectTemplate;
424};
425
426
v8.team.kasperl727e9952008-09-02 14:56:44 +0000427 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 * A stack-allocated class that governs a number of local handles.
429 * After a handle scope has been created, all local handles will be
430 * allocated within that handle scope until either the handle scope is
431 * deleted or another handle scope is created. If there is already a
432 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000433 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 * new handles will again be allocated in the original handle scope.
435 *
436 * After the handle scope of a local handle has been deleted the
437 * garbage collector will no longer track the object stored in the
438 * handle and may deallocate it. The behavior of accessing a handle
439 * for which the handle scope has been deleted is undefined.
440 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000441class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000443 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000445 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446
447 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 * Closes the handle scope and returns the value as a handle in the
449 * previous scope, which is the new current scope after the call.
450 */
451 template <class T> Local<T> Close(Handle<T> value);
452
453 /**
454 * Counts the number of allocated handles.
455 */
456 static int NumberOfHandles();
457
458 /**
459 * Creates a new handle with the given value.
460 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000461 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 private:
464 // Make it impossible to create heap-allocated or illegal handle
465 // scopes by disallowing certain operations.
466 HandleScope(const HandleScope&);
467 void operator=(const HandleScope&);
468 void* operator new(size_t size);
469 void operator delete(void*, size_t);
470
ager@chromium.org3811b432009-10-28 14:53:37 +0000471 // This Data class is accessible internally as HandleScopeData through a
472 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000473 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 public:
475 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000476 internal::Object** next;
477 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 inline void Initialize() {
479 extensions = -1;
480 next = limit = NULL;
481 }
482 };
483
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000484 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000486 // Allow for the active closing of HandleScopes which allows to pass a handle
487 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000489 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 friend class ImplementationUtilities;
492};
493
494
495// --- S p e c i a l o b j e c t s ---
496
497
498/**
499 * The superclass of values and API object templates.
500 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000501class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 private:
503 Data();
504};
505
506
507/**
508 * Pre-compilation data that can be associated with a script. This
509 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000510 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000511 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000513class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 public:
515 virtual ~ScriptData() { }
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000516 /**
517 * Pre-compiles the specified script (context-independent).
518 *
519 * \param input Pointer to UTF-8 script source code.
520 * \param length Length of UTF-8 script source code.
521 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000524 /**
525 * Load previous pre-compilation data.
526 *
527 * \param data Pointer to data returned by a call to Data() of a previous
528 * ScriptData. Ownership is not transferred.
529 * \param length Length of data.
530 */
531 static ScriptData* New(const char* data, int length);
532
533 /**
534 * Returns the length of Data().
535 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000537
538 /**
539 * Returns a serialized representation of this ScriptData that can later be
540 * passed to New(). NOTE: Serialized data is platform-dependent.
541 */
542 virtual const char* Data() = 0;
543
544 /**
545 * Returns true if the source code could not be parsed.
546 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000547 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548};
549
550
551/**
552 * The origin, within a file, of a script.
553 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000554class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000556 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 Handle<Integer> resource_line_offset = Handle<Integer>(),
558 Handle<Integer> resource_column_offset = Handle<Integer>())
559 : resource_name_(resource_name),
560 resource_line_offset_(resource_line_offset),
561 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000562 inline Handle<Value> ResourceName() const;
563 inline Handle<Integer> ResourceLineOffset() const;
564 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000566 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 Handle<Integer> resource_line_offset_;
568 Handle<Integer> resource_column_offset_;
569};
570
571
572/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000573 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000575class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 public:
577
578 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000579 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000580 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000581 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000582 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000583 * when New() returns
584 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
585 * using pre_data speeds compilation if it's done multiple times.
586 * Owned by caller, no references are kept when New() returns.
587 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000588 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000589 * available to compile event handlers.
590 * \return Compiled script object (context independent; when run it
591 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000593 static Local<Script> New(Handle<String> source,
594 ScriptOrigin* origin = NULL,
595 ScriptData* pre_data = NULL,
596 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597
mads.s.agercbaa0602008-08-14 13:41:48 +0000598 /**
599 * Compiles the specified script using the specified file name
600 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000601 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000603 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000604 * as the script's origin.
605 * \return Compiled script object (context independent; when run it
606 * will use the currently entered context).
607 */
608 static Local<Script> New(Handle<String> source,
609 Handle<Value> file_name);
610
611 /**
612 * Compiles the specified script (bound to current context).
613 *
614 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000615 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000616 * when Compile() returns
617 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
618 * using pre_data speeds compilation if it's done multiple times.
619 * Owned by caller, no references are kept when Compile() returns.
620 * \param script_data Arbitrary data associated with script. Using
621 * this has same effect as calling SetData(), but makes data available
622 * earlier (i.e. to compile event handlers).
623 * \return Compiled script object, bound to the context that was active
624 * when this function was called. When run it will always use this
625 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000626 */
627 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 ScriptOrigin* origin = NULL,
629 ScriptData* pre_data = NULL,
630 Handle<String> script_data = Handle<String>());
631
632 /**
633 * Compiles the specified script using the specified file name
634 * object (typically a string) as the script's origin.
635 *
636 * \param source Script source code.
637 * \param file_name File name to use as script's origin
638 * \param script_data Arbitrary data associated with script. Using
639 * this has same effect as calling SetData(), but makes data available
640 * earlier (i.e. to compile event handlers).
641 * \return Compiled script object, bound to the context that was active
642 * when this function was called. When run it will always use this
643 * context.
644 */
645 static Local<Script> Compile(Handle<String> source,
646 Handle<Value> file_name,
647 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000648
v8.team.kasperl727e9952008-09-02 14:56:44 +0000649 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000650 * Runs the script returning the resulting value. If the script is
651 * context independent (created using ::New) it will be run in the
652 * currently entered context. If it is context specific (created
653 * using ::Compile) it will be run in the context in which it was
654 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000655 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000657
658 /**
659 * Returns the script id value.
660 */
661 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000662
663 /**
664 * Associate an additional data object with the script. This is mainly used
665 * with the debugger as this data object is only available through the
666 * debugger API.
667 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000668 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669};
670
671
672/**
673 * An error message.
674 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000675class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000677 Local<String> Get() const;
678 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000680 /**
681 * Returns the resource name for the script from where the function causing
682 * the error originates.
683 */
ager@chromium.org32912102009-01-16 10:38:43 +0000684 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000686 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000687 * Returns the resource data for the script from where the function causing
688 * the error originates.
689 */
690 Handle<Value> GetScriptData() const;
691
692 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000693 * Returns the number, 1-based, of the line where the error occurred.
694 */
ager@chromium.org32912102009-01-16 10:38:43 +0000695 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000697 /**
698 * Returns the index within the script of the first character where
699 * the error occurred.
700 */
ager@chromium.org32912102009-01-16 10:38:43 +0000701 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000702
703 /**
704 * Returns the index within the script of the last character where
705 * the error occurred.
706 */
ager@chromium.org32912102009-01-16 10:38:43 +0000707 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000708
709 /**
710 * Returns the index within the line of the first character where
711 * the error occurred.
712 */
ager@chromium.org32912102009-01-16 10:38:43 +0000713 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000714
715 /**
716 * Returns the index within the line of the last character where
717 * the error occurred.
718 */
ager@chromium.org32912102009-01-16 10:38:43 +0000719 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000720
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000721 // TODO(1245381): Print to a string instead of on a FILE.
722 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000723
724 static const int kNoLineNumberInfo = 0;
725 static const int kNoColumnInfo = 0;
726};
727
728
729/**
730 * Representation of a JavaScript stack trace. The information collected is a
731 * snapshot of the execution stack and the information remains valid after
732 * execution continues.
733 */
734class V8EXPORT StackTrace {
735 public:
736 /**
737 * Flags that determine what information is placed captured for each
738 * StackFrame when grabbing the current stack trace.
739 */
740 enum StackTraceOptions {
741 kLineNumber = 1,
742 kColumnOffset = 1 << 1 | kLineNumber,
743 kScriptName = 1 << 2,
744 kFunctionName = 1 << 3,
745 kIsEval = 1 << 4,
746 kIsConstructor = 1 << 5,
747 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
748 kDetailed = kOverview | kIsEval | kIsConstructor
749 };
750
751 /**
752 * Returns a StackFrame at a particular index.
753 */
754 Local<StackFrame> GetFrame(uint32_t index) const;
755
756 /**
757 * Returns the number of StackFrames.
758 */
759 int GetFrameCount() const;
760
761 /**
762 * Returns StackTrace as a v8::Array that contains StackFrame objects.
763 */
764 Local<Array> AsArray();
765
766 /**
767 * Grab a snapshot of the the current JavaScript execution stack.
768 *
769 * \param frame_limit The maximum number of stack frames we want to capture.
770 * \param options Enumerates the set of things we will capture for each
771 * StackFrame.
772 */
773 static Local<StackTrace> CurrentStackTrace(
774 int frame_limit,
775 StackTraceOptions options = kOverview);
776};
777
778
779/**
780 * A single JavaScript stack frame.
781 */
782class V8EXPORT StackFrame {
783 public:
784 /**
785 * Returns the number, 1-based, of the line for the associate function call.
786 * This method will return Message::kNoLineNumberInfo if it is unable to
787 * retrieve the line number, or if kLineNumber was not passed as an option
788 * when capturing the StackTrace.
789 */
790 int GetLineNumber() const;
791
792 /**
793 * Returns the 1-based column offset on the line for the associated function
794 * call.
795 * This method will return Message::kNoColumnInfo if it is unable to retrieve
796 * the column number, or if kColumnOffset was not passed as an option when
797 * capturing the StackTrace.
798 */
799 int GetColumn() const;
800
801 /**
802 * Returns the name of the resource that contains the script for the
803 * function for this StackFrame.
804 */
805 Local<String> GetScriptName() const;
806
807 /**
808 * Returns the name of the function associated with this stack frame.
809 */
810 Local<String> GetFunctionName() const;
811
812 /**
813 * Returns whether or not the associated function is compiled via a call to
814 * eval().
815 */
816 bool IsEval() const;
817
818 /**
819 * Returns whther or not the associated function is called as a
820 * constructor via "new".
821 */
822 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823};
824
825
826// --- V a l u e ---
827
828
829/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000830 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000832class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833 public:
834
835 /**
836 * Returns true if this value is the undefined value. See ECMA-262
837 * 4.3.10.
838 */
ager@chromium.org32912102009-01-16 10:38:43 +0000839 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
841 /**
842 * Returns true if this value is the null value. See ECMA-262
843 * 4.3.11.
844 */
ager@chromium.org32912102009-01-16 10:38:43 +0000845 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846
847 /**
848 * Returns true if this value is true.
849 */
ager@chromium.org32912102009-01-16 10:38:43 +0000850 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
852 /**
853 * Returns true if this value is false.
854 */
ager@chromium.org32912102009-01-16 10:38:43 +0000855 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856
857 /**
858 * Returns true if this value is an instance of the String type.
859 * See ECMA-262 8.4.
860 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000861 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
863 /**
864 * Returns true if this value is a function.
865 */
ager@chromium.org32912102009-01-16 10:38:43 +0000866 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 /**
869 * Returns true if this value is an array.
870 */
ager@chromium.org32912102009-01-16 10:38:43 +0000871 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
v8.team.kasperl727e9952008-09-02 14:56:44 +0000873 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 * Returns true if this value is an object.
875 */
ager@chromium.org32912102009-01-16 10:38:43 +0000876 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
v8.team.kasperl727e9952008-09-02 14:56:44 +0000878 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 * Returns true if this value is boolean.
880 */
ager@chromium.org32912102009-01-16 10:38:43 +0000881 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000882
v8.team.kasperl727e9952008-09-02 14:56:44 +0000883 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 * Returns true if this value is a number.
885 */
ager@chromium.org32912102009-01-16 10:38:43 +0000886 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
v8.team.kasperl727e9952008-09-02 14:56:44 +0000888 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 * Returns true if this value is external.
890 */
ager@chromium.org32912102009-01-16 10:38:43 +0000891 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892
v8.team.kasperl727e9952008-09-02 14:56:44 +0000893 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 * Returns true if this value is a 32-bit signed integer.
895 */
ager@chromium.org32912102009-01-16 10:38:43 +0000896 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000898 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000899 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000900 */
901 bool IsUint32() const;
902
903 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000904 * Returns true if this value is a Date.
905 */
ager@chromium.org32912102009-01-16 10:38:43 +0000906 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000907
ager@chromium.org32912102009-01-16 10:38:43 +0000908 Local<Boolean> ToBoolean() const;
909 Local<Number> ToNumber() const;
910 Local<String> ToString() const;
911 Local<String> ToDetailString() const;
912 Local<Object> ToObject() const;
913 Local<Integer> ToInteger() const;
914 Local<Uint32> ToUint32() const;
915 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916
917 /**
918 * Attempts to convert a string to an array index.
919 * Returns an empty handle if the conversion fails.
920 */
ager@chromium.org32912102009-01-16 10:38:43 +0000921 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922
ager@chromium.org32912102009-01-16 10:38:43 +0000923 bool BooleanValue() const;
924 double NumberValue() const;
925 int64_t IntegerValue() const;
926 uint32_t Uint32Value() const;
927 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
929 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000930 bool Equals(Handle<Value> that) const;
931 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000932
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000933 private:
934 inline bool QuickIsString() const;
935 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936};
937
938
939/**
940 * The superclass of primitive values. See ECMA-262 4.3.2.
941 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000942class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943
944
945/**
946 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
947 * or false value.
948 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000949class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000951 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 static inline Handle<Boolean> New(bool value);
953};
954
955
956/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000957 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000959class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000961
962 /**
963 * Returns the number of characters in this string.
964 */
ager@chromium.org32912102009-01-16 10:38:43 +0000965 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966
v8.team.kasperl727e9952008-09-02 14:56:44 +0000967 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000968 * Returns the number of bytes in the UTF-8 encoded
969 * representation of this string.
970 */
ager@chromium.org32912102009-01-16 10:38:43 +0000971 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000972
973 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000974 * Write the contents of the string to an external buffer.
975 * If no arguments are given, expects the buffer to be large
976 * enough to hold the entire string and NULL terminator. Copies
977 * the contents of the string and the NULL terminator into the
978 * buffer.
979 *
980 * Copies up to length characters into the output buffer.
981 * Only null-terminates if there is enough space in the buffer.
982 *
983 * \param buffer The buffer into which the string will be copied.
984 * \param start The starting position within the string at which
985 * copying begins.
986 * \param length The number of bytes to copy from the string.
ager@chromium.org357bf652010-04-12 11:30:10 +0000987 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000988 * \param hints Various hints that might affect performance of this or
989 * subsequent operations.
ager@chromium.org357bf652010-04-12 11:30:10 +0000990 * \return The number of bytes copied to the buffer
v8.team.kasperl727e9952008-09-02 14:56:44 +0000991 * excluding the NULL terminator.
992 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000993 enum WriteHints {
994 NO_HINTS = 0,
995 HINT_MANY_WRITES_EXPECTED = 1
996 };
997
998 int Write(uint16_t* buffer,
999 int start = 0,
1000 int length = -1,
1001 WriteHints hints = NO_HINTS) const; // UTF-16
1002 int WriteAscii(char* buffer,
1003 int start = 0,
1004 int length = -1,
1005 WriteHints hints = NO_HINTS) const; // ASCII
ager@chromium.org357bf652010-04-12 11:30:10 +00001006 int WriteUtf8(char* buffer,
1007 int length = -1,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001008 int* nchars_ref = NULL,
1009 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010
v8.team.kasperl727e9952008-09-02 14:56:44 +00001011 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001012 * A zero length string.
1013 */
1014 static v8::Local<v8::String> Empty();
1015
1016 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001017 * Returns true if the string is external
1018 */
ager@chromium.org32912102009-01-16 10:38:43 +00001019 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020
v8.team.kasperl727e9952008-09-02 14:56:44 +00001021 /**
1022 * Returns true if the string is both external and ascii
1023 */
ager@chromium.org32912102009-01-16 10:38:43 +00001024 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001025
1026 class V8EXPORT ExternalStringResourceBase {
1027 public:
1028 virtual ~ExternalStringResourceBase() {}
1029 protected:
1030 ExternalStringResourceBase() {}
1031 private:
1032 // Disallow copying and assigning.
1033 ExternalStringResourceBase(const ExternalStringResourceBase&);
1034 void operator=(const ExternalStringResourceBase&);
1035 };
1036
v8.team.kasperl727e9952008-09-02 14:56:44 +00001037 /**
1038 * An ExternalStringResource is a wrapper around a two-byte string
1039 * buffer that resides outside V8's heap. Implement an
1040 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001041 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001042 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001043 class V8EXPORT ExternalStringResource
1044 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 public:
1046 /**
1047 * Override the destructor to manage the life cycle of the underlying
1048 * buffer.
1049 */
1050 virtual ~ExternalStringResource() {}
1051 /** The string data from the underlying buffer.*/
1052 virtual const uint16_t* data() const = 0;
1053 /** The length of the string. That is, the number of two-byte characters.*/
1054 virtual size_t length() const = 0;
1055 protected:
1056 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 };
1058
1059 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001060 * An ExternalAsciiStringResource is a wrapper around an ascii
1061 * string buffer that resides outside V8's heap. Implement an
1062 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001063 * underlying buffer. Note that the string data must be immutable
1064 * and that the data must be strict 7-bit ASCII, not Latin1 or
1065 * UTF-8, which would require special treatment internally in the
1066 * engine and, in the case of UTF-8, do not allow efficient indexing.
1067 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001068 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001070 class V8EXPORT ExternalAsciiStringResource
1071 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 public:
1073 /**
1074 * Override the destructor to manage the life cycle of the underlying
1075 * buffer.
1076 */
1077 virtual ~ExternalAsciiStringResource() {}
1078 /** The string data from the underlying buffer.*/
1079 virtual const char* data() const = 0;
1080 /** The number of ascii characters in the string.*/
1081 virtual size_t length() const = 0;
1082 protected:
1083 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 };
1085
1086 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001087 * Get the ExternalStringResource for an external string. Returns
1088 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001090 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091
1092 /**
1093 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001094 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 */
ager@chromium.org32912102009-01-16 10:38:43 +00001096 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001098 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099
1100 /**
1101 * Allocates a new string from either utf-8 encoded or ascii data.
1102 * The second parameter 'length' gives the buffer length.
1103 * If the data is utf-8 encoded, the caller must
1104 * be careful to supply the length parameter.
1105 * If it is not given, the function calls
1106 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001107 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 */
1109 static Local<String> New(const char* data, int length = -1);
1110
1111 /** Allocates a new string from utf16 data.*/
1112 static Local<String> New(const uint16_t* data, int length = -1);
1113
1114 /** Creates a symbol. Returns one if it exists already.*/
1115 static Local<String> NewSymbol(const char* data, int length = -1);
1116
v8.team.kasperl727e9952008-09-02 14:56:44 +00001117 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001118 * Creates a new string by concatenating the left and the right strings
1119 * passed in as parameters.
1120 */
1121 static Local<String> Concat(Handle<String> left, Handle<String>right);
1122
1123 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001124 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001125 * resource. When the external string is no longer live on V8's heap the
1126 * resource will be disposed. If a disposal callback has been set using
1127 * SetExternalStringDiposeCallback this callback will be called to dispose
1128 * the resource. Otherwise, V8 will dispose the resource using the C++ delete
1129 * operator. The caller of this function should not otherwise delete or
1130 * modify the resource. Neither should the underlying buffer be deallocated
1131 * or modified except through the destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001132 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001134
ager@chromium.org6f10e412009-02-13 10:11:16 +00001135 /**
1136 * Associate an external string resource with this string by transforming it
1137 * in place so that existing references to this string in the JavaScript heap
1138 * will use the external string resource. The external string resource's
1139 * character contents needs to be equivalent to this string.
1140 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001141 * The string is not modified if the operation fails. See NewExternal for
1142 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001143 */
1144 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145
v8.team.kasperl727e9952008-09-02 14:56:44 +00001146 /**
1147 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001148 * resource. When the external string is no longer live on V8's heap the
1149 * resource will be disposed. If a disposal callback has been set using
1150 * SetExternalStringDiposeCallback this callback will be called to dispose
1151 * the resource. Otherwise, V8 will dispose the resource using the C++ delete
1152 * operator. The caller of this function should not otherwise delete or
1153 * modify the resource. Neither should the underlying buffer be deallocated
1154 * or modified except through the destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001155 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001157
ager@chromium.org6f10e412009-02-13 10:11:16 +00001158 /**
1159 * Associate an external string resource with this string by transforming it
1160 * in place so that existing references to this string in the JavaScript heap
1161 * will use the external string resource. The external string resource's
1162 * character contents needs to be equivalent to this string.
1163 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001164 * The string is not modified if the operation fails. See NewExternal for
1165 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001166 */
1167 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001169 /**
1170 * Returns true if this string can be made external.
1171 */
1172 bool CanMakeExternal();
1173
kasper.lund7276f142008-07-30 08:49:36 +00001174 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 static Local<String> NewUndetectable(const char* data, int length = -1);
1176
kasper.lund7276f142008-07-30 08:49:36 +00001177 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
1179
1180 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001181 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001182 * you want to print the object. If conversion to a string fails
1183 * (eg. due to an exception in the toString() method of the object)
1184 * then the length() method returns 0 and the * operator returns
1185 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001186 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001187 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001188 public:
1189 explicit Utf8Value(Handle<v8::Value> obj);
1190 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001191 char* operator*() { return str_; }
1192 const char* operator*() const { return str_; }
1193 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001194 private:
1195 char* str_;
1196 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001197
1198 // Disallow copying and assigning.
1199 Utf8Value(const Utf8Value&);
1200 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001201 };
1202
1203 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 * Converts an object to an ascii string.
1205 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001206 * If conversion to a string fails (eg. due to an exception in the toString()
1207 * method of the object) then the length() method returns 0 and the * operator
1208 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001210 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 public:
1212 explicit AsciiValue(Handle<v8::Value> obj);
1213 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001214 char* operator*() { return str_; }
1215 const char* operator*() const { return str_; }
1216 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 private:
1218 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001219 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001220
1221 // Disallow copying and assigning.
1222 AsciiValue(const AsciiValue&);
1223 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 };
1225
1226 /**
1227 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001228 * If conversion to a string fails (eg. due to an exception in the toString()
1229 * method of the object) then the length() method returns 0 and the * operator
1230 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001232 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233 public:
1234 explicit Value(Handle<v8::Value> obj);
1235 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001236 uint16_t* operator*() { return str_; }
1237 const uint16_t* operator*() const { return str_; }
1238 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 private:
1240 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001241 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001242
1243 // Disallow copying and assigning.
1244 Value(const Value&);
1245 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001247
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001248 private:
1249 void VerifyExternalStringResource(ExternalStringResource* val) const;
1250 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251};
1252
1253
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001254typedef void (*ExternalStringDiposeCallback)
1255 (String::ExternalStringResourceBase* resource);
1256
1257
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001259 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001261class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001263 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001265 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 private:
1267 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001268 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269};
1270
1271
1272/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001273 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001275class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 public:
1277 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001278 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001279 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001280 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 private:
1282 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001283 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284};
1285
1286
1287/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001288 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001290class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001292 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 private:
1294 Int32();
1295};
1296
1297
1298/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001299 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001301class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001303 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 private:
1305 Uint32();
1306};
1307
1308
1309/**
1310 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1311 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001312class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 public:
1314 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001315
1316 /**
1317 * A specialization of Value::NumberValue that is more efficient
1318 * because we know the structure of this object.
1319 */
ager@chromium.org32912102009-01-16 10:38:43 +00001320 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001321
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001322 static inline Date* Cast(v8::Value* obj);
1323 private:
1324 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325};
1326
1327
1328enum PropertyAttribute {
1329 None = 0,
1330 ReadOnly = 1 << 0,
1331 DontEnum = 1 << 1,
1332 DontDelete = 1 << 2
1333};
1334
ager@chromium.org3811b432009-10-28 14:53:37 +00001335enum ExternalArrayType {
1336 kExternalByteArray = 1,
1337 kExternalUnsignedByteArray,
1338 kExternalShortArray,
1339 kExternalUnsignedShortArray,
1340 kExternalIntArray,
1341 kExternalUnsignedIntArray,
1342 kExternalFloatArray
1343};
1344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001346 * Accessor[Getter|Setter] are used as callback functions when
1347 * setting|getting a particular property. See Object and ObjectTemplate's
1348 * method SetAccessor.
1349 */
1350typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1351 const AccessorInfo& info);
1352
1353
1354typedef void (*AccessorSetter)(Local<String> property,
1355 Local<Value> value,
1356 const AccessorInfo& info);
1357
1358
1359/**
1360 * Access control specifications.
1361 *
1362 * Some accessors should be accessible across contexts. These
1363 * accessors have an explicit access control parameter which specifies
1364 * the kind of cross-context access that should be allowed.
1365 *
1366 * Additionally, for security, accessors can prohibit overwriting by
1367 * accessors defined in JavaScript. For objects that have such
1368 * accessors either locally or in their prototype chain it is not
1369 * possible to overwrite the accessor by using __defineGetter__ or
1370 * __defineSetter__ from JavaScript code.
1371 */
1372enum AccessControl {
1373 DEFAULT = 0,
1374 ALL_CAN_READ = 1,
1375 ALL_CAN_WRITE = 1 << 1,
1376 PROHIBITS_OVERWRITING = 1 << 2
1377};
1378
1379
1380/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001381 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001383class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384 public:
1385 bool Set(Handle<Value> key,
1386 Handle<Value> value,
1387 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001388
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001389 bool Set(uint32_t index,
1390 Handle<Value> value);
1391
ager@chromium.orge2902be2009-06-08 12:21:35 +00001392 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001393 // overriding accessors or read-only properties.
1394 //
1395 // Note that if the object has an interceptor the property will be set
1396 // locally, but since the interceptor takes precedence the local property
1397 // will only be returned if the interceptor doesn't return a value.
1398 //
1399 // Note also that this only works for named properties.
1400 bool ForceSet(Handle<Value> key,
1401 Handle<Value> value,
1402 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001403
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 Local<Value> Get(Handle<Value> key);
1405
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001406 Local<Value> Get(uint32_t index);
1407
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 // TODO(1245389): Replace the type-specific versions of these
1409 // functions with generic ones that accept a Handle<Value> key.
1410 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001411
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001413
1414 // Delete a property on this object bypassing interceptors and
1415 // ignoring dont-delete attributes.
1416 bool ForceDelete(Handle<Value> key);
1417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001419
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 bool Delete(uint32_t index);
1421
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001422 bool SetAccessor(Handle<String> name,
1423 AccessorGetter getter,
1424 AccessorSetter setter = 0,
1425 Handle<Value> data = Handle<Value>(),
1426 AccessControl settings = DEFAULT,
1427 PropertyAttribute attribute = None);
1428
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001430 * Returns an array containing the names of the enumerable properties
1431 * of this object, including properties from prototype objects. The
1432 * array returned by this method contains the same values as would
1433 * be enumerated by a for-in statement over this object.
1434 */
1435 Local<Array> GetPropertyNames();
1436
1437 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 * Get the prototype object. This does not skip objects marked to
1439 * be skipped by __proto__ and it does not consult the security
1440 * handler.
1441 */
1442 Local<Value> GetPrototype();
1443
1444 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001445 * Set the prototype object. This does not skip objects marked to
1446 * be skipped by __proto__ and it does not consult the security
1447 * handler.
1448 */
1449 bool SetPrototype(Handle<Value> prototype);
1450
1451 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001452 * Finds an instance of the given function template in the prototype
1453 * chain.
1454 */
1455 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1456
1457 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 * Call builtin Object.prototype.toString on this object.
1459 * This is different from Value::ToString() that may call
1460 * user-defined toString function. This one does not.
1461 */
1462 Local<String> ObjectProtoToString();
1463
kasper.lund212ac232008-07-16 07:07:30 +00001464 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001466 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001467 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001468 /** Sets the value in an internal field. */
1469 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001471 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001472 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001473
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001474 /** Sets a native pointer in an internal field. */
1475 void SetPointerInInternalField(int index, void* value);
1476
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 // Testers for local properties.
1478 bool HasRealNamedProperty(Handle<String> key);
1479 bool HasRealIndexedProperty(uint32_t index);
1480 bool HasRealNamedCallbackProperty(Handle<String> key);
1481
1482 /**
1483 * If result.IsEmpty() no real property was located in the prototype chain.
1484 * This means interceptors in the prototype chain are not called.
1485 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001486 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1487
1488 /**
1489 * If result.IsEmpty() no real property was located on the object or
1490 * in the prototype chain.
1491 * This means interceptors in the prototype chain are not called.
1492 */
1493 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494
1495 /** Tests for a named lookup interceptor.*/
1496 bool HasNamedLookupInterceptor();
1497
kasper.lund212ac232008-07-16 07:07:30 +00001498 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 bool HasIndexedLookupInterceptor();
1500
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001501 /**
1502 * Turns on access check on the object if the object is an instance of
1503 * a template that has access check callbacks. If an object has no
1504 * access check info, the object cannot be accessed by anyone.
1505 */
1506 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001507
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001508 /**
1509 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001510 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001511 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001512 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001513 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001514 */
1515 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001516
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001517 /**
1518 * Access hidden properties on JavaScript objects. These properties are
1519 * hidden from the executing JavaScript and only accessible through the V8
1520 * C++ API. Hidden properties introduced by V8 internally (for example the
1521 * identity hash) are prefixed with "v8::".
1522 */
1523 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1524 Local<Value> GetHiddenValue(Handle<String> key);
1525 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001526
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001527 /**
1528 * Returns true if this is an instance of an api function (one
1529 * created from a function created from a function template) and has
1530 * been modified since it was created. Note that this method is
1531 * conservative and may return true for objects that haven't actually
1532 * been modified.
1533 */
1534 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001535
1536 /**
1537 * Clone this object with a fast but shallow copy. Values will point
1538 * to the same values as the original object.
1539 */
1540 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001542 /**
1543 * Set the backing store of the indexed properties to be managed by the
1544 * embedding layer. Access to the indexed properties will follow the rules
1545 * spelled out in CanvasPixelArray.
1546 * Note: The embedding program still owns the data and needs to ensure that
1547 * the backing store is preserved while V8 has a reference.
1548 */
1549 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1550
ager@chromium.org3811b432009-10-28 14:53:37 +00001551 /**
1552 * Set the backing store of the indexed properties to be managed by the
1553 * embedding layer. Access to the indexed properties will follow the rules
1554 * spelled out for the CanvasArray subtypes in the WebGL specification.
1555 * Note: The embedding program still owns the data and needs to ensure that
1556 * the backing store is preserved while V8 has a reference.
1557 */
1558 void SetIndexedPropertiesToExternalArrayData(void* data,
1559 ExternalArrayType array_type,
1560 int number_of_elements);
1561
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001563 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 private:
1565 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001566 static void CheckCast(Value* obj);
1567 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001568 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001569
1570 /**
1571 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001572 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001573 */
1574 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575};
1576
1577
1578/**
1579 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1580 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001581class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001583 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584
ager@chromium.org3e875802009-06-29 08:26:34 +00001585 /**
1586 * Clones an element at index |index|. Returns an empty
1587 * handle if cloning fails (for any reason).
1588 */
1589 Local<Object> CloneElementAt(uint32_t index);
1590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001592 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 private:
1594 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001595 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596};
1597
1598
1599/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001600 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001602class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001604 Local<Object> NewInstance() const;
1605 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1607 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001608 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001609
1610 /**
1611 * Returns zero based line number of function body and
1612 * kLineOffsetNotFound if no information available.
1613 */
1614 int GetScriptLineNumber() const;
1615 ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001616 static inline Function* Cast(Value* obj);
ager@chromium.org5c838252010-02-19 08:53:10 +00001617 static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 private:
1619 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001620 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621};
1622
1623
1624/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001625 * A JavaScript value that wraps a C++ void*. This type of value is
1626 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001628 *
1629 * The Wrap function V8 will return the most optimal Value object wrapping the
1630 * C++ void*. The type of the value is not guaranteed to be an External object
1631 * and no assumptions about its type should be made. To access the wrapped
1632 * value Unwrap should be used, all other operations on that object will lead
1633 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001635class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001637 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001638 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001641 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001642 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 private:
1644 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001645 static void CheckCast(v8::Value* obj);
1646 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1647 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648};
1649
1650
1651// --- T e m p l a t e s ---
1652
1653
1654/**
1655 * The superclass of object and function templates.
1656 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001657class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658 public:
1659 /** Adds a property to each instance created by this template.*/
1660 void Set(Handle<String> name, Handle<Data> value,
1661 PropertyAttribute attributes = None);
1662 inline void Set(const char* name, Handle<Data> value);
1663 private:
1664 Template();
1665
1666 friend class ObjectTemplate;
1667 friend class FunctionTemplate;
1668};
1669
1670
1671/**
1672 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001673 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674 * including the receiver, the number and values of arguments, and
1675 * the holder of the function.
1676 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001677class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 public:
1679 inline int Length() const;
1680 inline Local<Value> operator[](int i) const;
1681 inline Local<Function> Callee() const;
1682 inline Local<Object> This() const;
1683 inline Local<Object> Holder() const;
1684 inline bool IsConstructCall() const;
1685 inline Local<Value> Data() const;
1686 private:
1687 Arguments();
1688 friend class ImplementationUtilities;
1689 inline Arguments(Local<Value> data,
1690 Local<Object> holder,
1691 Local<Function> callee,
1692 bool is_construct_call,
1693 void** values, int length);
1694 Local<Value> data_;
1695 Local<Object> holder_;
1696 Local<Function> callee_;
1697 bool is_construct_call_;
1698 void** values_;
1699 int length_;
1700};
1701
1702
1703/**
1704 * The information passed to an accessor callback about the context
1705 * of the property access.
1706 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001707class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001709 inline AccessorInfo(internal::Object** args)
1710 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 inline Local<Value> Data() const;
1712 inline Local<Object> This() const;
1713 inline Local<Object> Holder() const;
1714 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001715 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716};
1717
1718
1719typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1720
1721typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1722
1723/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724 * NamedProperty[Getter|Setter] are used as interceptors on object.
1725 * See ObjectTemplate::SetNamedPropertyHandler.
1726 */
1727typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1728 const AccessorInfo& info);
1729
1730
1731/**
1732 * Returns the value if the setter intercepts the request.
1733 * Otherwise, returns an empty handle.
1734 */
1735typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1736 Local<Value> value,
1737 const AccessorInfo& info);
1738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739/**
1740 * Returns a non-empty handle if the interceptor intercepts the request.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001741 * The result is either boolean (true if property exists and false
1742 * otherwise) or an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001744#ifdef USE_NEW_QUERY_CALLBACKS
1745typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1746 const AccessorInfo& info);
1747#else
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1749 const AccessorInfo& info);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001750#endif
1751
1752typedef Handle<Value> (*NamedPropertyQueryImpl)(Local<String> property,
1753 const AccessorInfo& info);
1754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755
1756
1757/**
1758 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001759 * The return value is true if the property could be deleted and false
1760 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 */
1762typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1763 const AccessorInfo& info);
1764
1765/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001766 * Returns an array containing the names of the properties the named
1767 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768 */
1769typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1770
v8.team.kasperl727e9952008-09-02 14:56:44 +00001771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001773 * Returns the value of the property if the getter intercepts the
1774 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 */
1776typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1777 const AccessorInfo& info);
1778
1779
1780/**
1781 * Returns the value if the setter intercepts the request.
1782 * Otherwise, returns an empty handle.
1783 */
1784typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1785 Local<Value> value,
1786 const AccessorInfo& info);
1787
1788
1789/**
1790 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001791 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 */
1793typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1794 const AccessorInfo& info);
1795
1796/**
1797 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001798 * The return value is true if the property could be deleted and false
1799 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 */
1801typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1802 const AccessorInfo& info);
1803
v8.team.kasperl727e9952008-09-02 14:56:44 +00001804/**
1805 * Returns an array containing the indices of the properties the
1806 * indexed property getter intercepts.
1807 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1809
1810
1811/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001812 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 */
1814enum AccessType {
1815 ACCESS_GET,
1816 ACCESS_SET,
1817 ACCESS_HAS,
1818 ACCESS_DELETE,
1819 ACCESS_KEYS
1820};
1821
v8.team.kasperl727e9952008-09-02 14:56:44 +00001822
1823/**
1824 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001825 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001826 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001827typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 Local<Value> key,
1829 AccessType type,
1830 Local<Value> data);
1831
v8.team.kasperl727e9952008-09-02 14:56:44 +00001832
1833/**
1834 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001835 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001836 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001837typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 uint32_t index,
1839 AccessType type,
1840 Local<Value> data);
1841
1842
1843/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001844 * A FunctionTemplate is used to create functions at runtime. There
1845 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001846 * context. The lifetime of the created function is equal to the
1847 * lifetime of the context. So in case the embedder needs to create
1848 * temporary functions that can be collected using Scripts is
1849 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 *
1851 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001852 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001854 * A FunctionTemplate has a corresponding instance template which is
1855 * used to create object instances when the function is used as a
1856 * constructor. Properties added to the instance template are added to
1857 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 *
1859 * A FunctionTemplate can have a prototype template. The prototype template
1860 * is used to create the prototype object of the function.
1861 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001862 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001864 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1866 * t->Set("func_property", v8::Number::New(1));
1867 *
1868 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1869 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1870 * proto_t->Set("proto_const", v8::Number::New(2));
1871 *
1872 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1873 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1874 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1875 * instance_t->Set("instance_property", Number::New(3));
1876 *
1877 * v8::Local<v8::Function> function = t->GetFunction();
1878 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001879 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 *
1881 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001882 * and "instance" for the instance object created above. The function
1883 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001885 * \code
1886 * func_property in function == true;
1887 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001889 * function.prototype.proto_method() invokes 'InvokeCallback'
1890 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001892 * instance instanceof function == true;
1893 * instance.instance_accessor calls 'InstanceAccessorCallback'
1894 * instance.instance_property == 3;
1895 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001897 * A FunctionTemplate can inherit from another one by calling the
1898 * FunctionTemplate::Inherit method. The following graph illustrates
1899 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001901 * \code
1902 * FunctionTemplate Parent -> Parent() . prototype -> { }
1903 * ^ ^
1904 * | Inherit(Parent) | .__proto__
1905 * | |
1906 * FunctionTemplate Child -> Child() . prototype -> { }
1907 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001909 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1910 * object of the Child() function has __proto__ pointing to the
1911 * Parent() function's prototype object. An instance of the Child
1912 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001914 * Let Parent be the FunctionTemplate initialized in the previous
1915 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001917 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 * Local<FunctionTemplate> parent = t;
1919 * Local<FunctionTemplate> child = FunctionTemplate::New();
1920 * child->Inherit(parent);
1921 *
1922 * Local<Function> child_function = child->GetFunction();
1923 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001924 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001926 * The Child function and Child instance will have the following
1927 * properties:
1928 *
1929 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001931 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001933 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001935class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 public:
1937 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001938 static Local<FunctionTemplate> New(
1939 InvocationCallback callback = 0,
1940 Handle<Value> data = Handle<Value>(),
1941 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 /** Returns the unique function instance in the current execution context.*/
1943 Local<Function> GetFunction();
1944
v8.team.kasperl727e9952008-09-02 14:56:44 +00001945 /**
1946 * Set the call-handler callback for a FunctionTemplate. This
1947 * callback is called whenever the function created from this
1948 * FunctionTemplate is called.
1949 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 void SetCallHandler(InvocationCallback callback,
1951 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952
v8.team.kasperl727e9952008-09-02 14:56:44 +00001953 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 Local<ObjectTemplate> InstanceTemplate();
1955
1956 /** Causes the function template to inherit from a parent function template.*/
1957 void Inherit(Handle<FunctionTemplate> parent);
1958
1959 /**
1960 * A PrototypeTemplate is the template used to create the prototype object
1961 * of the function created by this template.
1962 */
1963 Local<ObjectTemplate> PrototypeTemplate();
1964
v8.team.kasperl727e9952008-09-02 14:56:44 +00001965
1966 /**
1967 * Set the class name of the FunctionTemplate. This is used for
1968 * printing objects created with the function created from the
1969 * FunctionTemplate as its constructor.
1970 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 void SetClassName(Handle<String> name);
1972
1973 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001974 * Determines whether the __proto__ accessor ignores instances of
1975 * the function template. If instances of the function template are
1976 * ignored, __proto__ skips all instances and instead returns the
1977 * next object in the prototype chain.
1978 *
1979 * Call with a value of true to make the __proto__ accessor ignore
1980 * instances of the function template. Call with a value of false
1981 * to make the __proto__ accessor not ignore instances of the
1982 * function template. By default, instances of a function template
1983 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 */
1985 void SetHiddenPrototype(bool value);
1986
1987 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001988 * Returns true if the given object is an instance of this function
1989 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 */
1991 bool HasInstance(Handle<Value> object);
1992
1993 private:
1994 FunctionTemplate();
1995 void AddInstancePropertyAccessor(Handle<String> name,
1996 AccessorGetter getter,
1997 AccessorSetter setter,
1998 Handle<Value> data,
1999 AccessControl settings,
2000 PropertyAttribute attributes);
2001 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2002 NamedPropertySetter setter,
2003 NamedPropertyQuery query,
2004 NamedPropertyDeleter remover,
2005 NamedPropertyEnumerator enumerator,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002006 Handle<Value> data) {
2007 NamedPropertyQueryImpl casted =
2008 reinterpret_cast<NamedPropertyQueryImpl>(query);
2009 SetNamedInstancePropertyHandlerImpl(getter,
2010 setter,
2011 casted,
2012 remover,
2013 enumerator,
2014 data);
2015 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2017 IndexedPropertySetter setter,
2018 IndexedPropertyQuery query,
2019 IndexedPropertyDeleter remover,
2020 IndexedPropertyEnumerator enumerator,
2021 Handle<Value> data);
2022 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2023 Handle<Value> data);
2024
2025 friend class Context;
2026 friend class ObjectTemplate;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002027 private:
2028 void SetNamedInstancePropertyHandlerImpl(NamedPropertyGetter getter,
2029 NamedPropertySetter setter,
2030 NamedPropertyQueryImpl query,
2031 NamedPropertyDeleter remover,
2032 NamedPropertyEnumerator enumerator,
2033 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034};
2035
2036
2037/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 * An ObjectTemplate is used to create objects at runtime.
2039 *
2040 * Properties added to an ObjectTemplate are added to each object
2041 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002043class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002045 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002047
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 /** Creates a new instance of this template.*/
2049 Local<Object> NewInstance();
2050
2051 /**
2052 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002053 *
2054 * Whenever the property with the given name is accessed on objects
2055 * created from this ObjectTemplate the getter and setter callbacks
2056 * are called instead of getting and setting the property directly
2057 * on the JavaScript object.
2058 *
2059 * \param name The name of the property for which an accessor is added.
2060 * \param getter The callback to invoke when getting the property.
2061 * \param setter The callback to invoke when setting the property.
2062 * \param data A piece of data that will be passed to the getter and setter
2063 * callbacks whenever they are invoked.
2064 * \param settings Access control settings for the accessor. This is a bit
2065 * field consisting of one of more of
2066 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2067 * The default is to not allow cross-context access.
2068 * ALL_CAN_READ means that all cross-context reads are allowed.
2069 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2070 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2071 * cross-context access.
2072 * \param attribute The attributes of the property for which an accessor
2073 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 */
2075 void SetAccessor(Handle<String> name,
2076 AccessorGetter getter,
2077 AccessorSetter setter = 0,
2078 Handle<Value> data = Handle<Value>(),
2079 AccessControl settings = DEFAULT,
2080 PropertyAttribute attribute = None);
2081
2082 /**
2083 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002084 *
2085 * Whenever a named property is accessed on objects created from
2086 * this object template, the provided callback is invoked instead of
2087 * accessing the property directly on the JavaScript object.
2088 *
2089 * \param getter The callback to invoke when getting a property.
2090 * \param setter The callback to invoke when setting a property.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002091 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002092 * \param deleter The callback to invoke when deleting a property.
2093 * \param enumerator The callback to invoke to enumerate all the named
2094 * properties of an object.
2095 * \param data A piece of data that will be passed to the callbacks
2096 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 */
2098 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2099 NamedPropertySetter setter = 0,
2100 NamedPropertyQuery query = 0,
2101 NamedPropertyDeleter deleter = 0,
2102 NamedPropertyEnumerator enumerator = 0,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002103 Handle<Value> data = Handle<Value>()) {
2104 NamedPropertyQueryImpl casted =
2105 reinterpret_cast<NamedPropertyQueryImpl>(query);
2106 SetNamedPropertyHandlerImpl(getter,
2107 setter,
2108 casted,
2109 deleter,
2110 enumerator,
2111 data);
2112 }
2113
2114 private:
2115 void SetNamedPropertyHandlerImpl(NamedPropertyGetter getter,
2116 NamedPropertySetter setter,
2117 NamedPropertyQueryImpl query,
2118 NamedPropertyDeleter deleter,
2119 NamedPropertyEnumerator enumerator,
2120 Handle<Value> data);
2121
2122 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123
2124 /**
2125 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002126 *
2127 * Whenever an indexed property is accessed on objects created from
2128 * this object template, the provided callback is invoked instead of
2129 * accessing the property directly on the JavaScript object.
2130 *
2131 * \param getter The callback to invoke when getting a property.
2132 * \param setter The callback to invoke when setting a property.
2133 * \param query The callback to invoke to check is an object has a property.
2134 * \param deleter The callback to invoke when deleting a property.
2135 * \param enumerator The callback to invoke to enumerate all the indexed
2136 * properties of an object.
2137 * \param data A piece of data that will be passed to the callbacks
2138 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139 */
2140 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2141 IndexedPropertySetter setter = 0,
2142 IndexedPropertyQuery query = 0,
2143 IndexedPropertyDeleter deleter = 0,
2144 IndexedPropertyEnumerator enumerator = 0,
2145 Handle<Value> data = Handle<Value>());
2146 /**
2147 * Sets the callback to be used when calling instances created from
2148 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 * function.
2151 */
2152 void SetCallAsFunctionHandler(InvocationCallback callback,
2153 Handle<Value> data = Handle<Value>());
2154
v8.team.kasperl727e9952008-09-02 14:56:44 +00002155 /**
2156 * Mark object instances of the template as undetectable.
2157 *
2158 * In many ways, undetectable objects behave as though they are not
2159 * there. They behave like 'undefined' in conditionals and when
2160 * printed. However, properties can be accessed and called as on
2161 * normal objects.
2162 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 void MarkAsUndetectable();
2164
v8.team.kasperl727e9952008-09-02 14:56:44 +00002165 /**
2166 * Sets access check callbacks on the object template.
2167 *
2168 * When accessing properties on instances of this object template,
2169 * the access check callback will be called to determine whether or
2170 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002171 * The last parameter specifies whether access checks are turned
2172 * on by default on instances. If access checks are off by default,
2173 * they can be turned on on individual instances by calling
2174 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002175 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2177 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002178 Handle<Value> data = Handle<Value>(),
2179 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180
kasper.lund212ac232008-07-16 07:07:30 +00002181 /**
2182 * Gets the number of internal fields for objects generated from
2183 * this template.
2184 */
2185 int InternalFieldCount();
2186
2187 /**
2188 * Sets the number of internal fields for objects generated from
2189 * this template.
2190 */
2191 void SetInternalFieldCount(int value);
2192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 private:
2194 ObjectTemplate();
2195 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2196 friend class FunctionTemplate;
2197};
2198
2199
2200/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002201 * A Signature specifies which receivers and arguments a function can
2202 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002204class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 public:
2206 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2207 Handle<FunctionTemplate>(),
2208 int argc = 0,
2209 Handle<FunctionTemplate> argv[] = 0);
2210 private:
2211 Signature();
2212};
2213
2214
2215/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002216 * A utility for determining the type of objects based on the template
2217 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002219class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 public:
2221 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2222 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2223 int match(Handle<Value> value);
2224 private:
2225 TypeSwitch();
2226};
2227
2228
2229// --- E x t e n s i o n s ---
2230
2231
2232/**
2233 * Ignore
2234 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002235class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 public:
2237 Extension(const char* name,
2238 const char* source = 0,
2239 int dep_count = 0,
2240 const char** deps = 0);
2241 virtual ~Extension() { }
2242 virtual v8::Handle<v8::FunctionTemplate>
2243 GetNativeFunction(v8::Handle<v8::String> name) {
2244 return v8::Handle<v8::FunctionTemplate>();
2245 }
2246
2247 const char* name() { return name_; }
2248 const char* source() { return source_; }
2249 int dependency_count() { return dep_count_; }
2250 const char** dependencies() { return deps_; }
2251 void set_auto_enable(bool value) { auto_enable_ = value; }
2252 bool auto_enable() { return auto_enable_; }
2253
2254 private:
2255 const char* name_;
2256 const char* source_;
2257 int dep_count_;
2258 const char** deps_;
2259 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002260
2261 // Disallow copying and assigning.
2262 Extension(const Extension&);
2263 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264};
2265
2266
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002267void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268
2269
2270/**
2271 * Ignore
2272 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002273class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 public:
2275 inline DeclareExtension(Extension* extension) {
2276 RegisterExtension(extension);
2277 }
2278};
2279
2280
2281// --- S t a t i c s ---
2282
2283
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002284Handle<Primitive> V8EXPORT Undefined();
2285Handle<Primitive> V8EXPORT Null();
2286Handle<Boolean> V8EXPORT True();
2287Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288
2289
2290/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002291 * A set of constraints that specifies the limits of the runtime's memory use.
2292 * You must set the heap size before initializing the VM - the size cannot be
2293 * adjusted after the VM is initialized.
2294 *
2295 * If you are using threads then you should hold the V8::Locker lock while
2296 * setting the stack limit and you must set a non-default stack limit separately
2297 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002299class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 public:
2301 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002302 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002304 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002306 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002307 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2309 private:
2310 int max_young_space_size_;
2311 int max_old_space_size_;
2312 uint32_t* stack_limit_;
2313};
2314
2315
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002316bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317
2318
2319// --- E x c e p t i o n s ---
2320
2321
2322typedef void (*FatalErrorCallback)(const char* location, const char* message);
2323
2324
2325typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2326
2327
2328/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002329 * Schedules an exception to be thrown when returning to JavaScript. When an
2330 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002332 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002334Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335
2336/**
2337 * Create new error objects by calling the corresponding error object
2338 * constructor with the message.
2339 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002340class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341 public:
2342 static Local<Value> RangeError(Handle<String> message);
2343 static Local<Value> ReferenceError(Handle<String> message);
2344 static Local<Value> SyntaxError(Handle<String> message);
2345 static Local<Value> TypeError(Handle<String> message);
2346 static Local<Value> Error(Handle<String> message);
2347};
2348
2349
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002350// --- 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 +00002351
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002352typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002354typedef void* (*CreateHistogramCallback)(const char* name,
2355 int min,
2356 int max,
2357 size_t buckets);
2358
2359typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002361// --- 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 ---
2362typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2363 AccessType type,
2364 Local<Value> data);
2365
2366// --- 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
2367
2368/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002369 * Applications can register callback functions which will be called
2370 * before and after a garbage collection. Allocations are not
2371 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002372 * objects (set or delete properties for example) since it is possible
2373 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002375enum GCType {
2376 kGCTypeScavenge = 1 << 0,
2377 kGCTypeMarkSweepCompact = 1 << 1,
2378 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2379};
2380
2381enum GCCallbackFlags {
2382 kNoGCCallbackFlags = 0,
2383 kGCCallbackFlagCompacted = 1 << 0
2384};
2385
2386typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2387typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389typedef void (*GCCallback)();
2390
2391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002393 * Profiler modules.
2394 *
2395 * In V8, profiler consists of several modules: CPU profiler, and different
2396 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002397 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2398 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002399 */
2400enum ProfilerModules {
2401 PROFILER_MODULE_NONE = 0,
2402 PROFILER_MODULE_CPU = 1,
2403 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002404 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2405 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002406};
2407
2408
2409/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002410 * Collection of V8 heap information.
2411 *
2412 * Instances of this class can be passed to v8::V8::HeapStatistics to
2413 * get heap statistics from V8.
2414 */
2415class V8EXPORT HeapStatistics {
2416 public:
2417 HeapStatistics();
2418 size_t total_heap_size() { return total_heap_size_; }
2419 size_t used_heap_size() { return used_heap_size_; }
2420
2421 private:
2422 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2423 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2424
2425 size_t total_heap_size_;
2426 size_t used_heap_size_;
2427
2428 friend class V8;
2429};
2430
2431
2432/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 * Container class for static utility functions.
2434 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002435class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002437 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 static void SetFatalErrorHandler(FatalErrorCallback that);
2439
v8.team.kasperl727e9952008-09-02 14:56:44 +00002440 /**
2441 * Ignore out-of-memory exceptions.
2442 *
2443 * V8 running out of memory is treated as a fatal error by default.
2444 * This means that the fatal error handler is called and that V8 is
2445 * terminated.
2446 *
2447 * IgnoreOutOfMemoryException can be used to not treat a
2448 * out-of-memory situation as a fatal error. This way, the contexts
2449 * that did not cause the out of memory problem might be able to
2450 * continue execution.
2451 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 static void IgnoreOutOfMemoryException();
2453
v8.team.kasperl727e9952008-09-02 14:56:44 +00002454 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002455 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002456 * fatal errors such as out-of-memory situations.
2457 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458 static bool IsDead();
2459
2460 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002461 * Adds a message listener.
2462 *
2463 * The same message listener can be added more than once and it that
2464 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002465 */
2466 static bool AddMessageListener(MessageCallback that,
2467 Handle<Value> data = Handle<Value>());
2468
2469 /**
2470 * Remove all message listeners from the specified callback function.
2471 */
2472 static void RemoveMessageListeners(MessageCallback that);
2473
2474 /**
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002475 * Set a callback to be called when an external string is no longer live on
2476 * V8's heap. The resource will no longer be needed by V8 and the embedder
2477 * can dispose of if. If this callback is not set V8 will free the resource
2478 * using the C++ delete operator.
2479 */
2480 static void SetExternalStringDiposeCallback(
2481 ExternalStringDiposeCallback that);
2482
2483 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002484 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 */
2486 static void SetFlagsFromString(const char* str, int length);
2487
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002488 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002489 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002490 */
2491 static void SetFlagsFromCommandLine(int* argc,
2492 char** argv,
2493 bool remove_flags);
2494
kasper.lund7276f142008-07-30 08:49:36 +00002495 /** Get the version string. */
2496 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497
2498 /**
2499 * Enables the host application to provide a mechanism for recording
2500 * statistics counters.
2501 */
2502 static void SetCounterFunction(CounterLookupCallback);
2503
2504 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002505 * Enables the host application to provide a mechanism for recording
2506 * histograms. The CreateHistogram function returns a
2507 * histogram which will later be passed to the AddHistogramSample
2508 * function.
2509 */
2510 static void SetCreateHistogramFunction(CreateHistogramCallback);
2511 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2512
2513 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002514 * Enables the computation of a sliding window of states. The sliding
2515 * window information is recorded in statistics counters.
2516 */
2517 static void EnableSlidingStateWindow();
2518
2519 /** Callback function for reporting failed access checks.*/
2520 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2521
2522 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002523 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002524 * garbage collection. Allocations are not allowed in the
2525 * callback function, you therefore cannot manipulate objects (set
2526 * or delete properties for example) since it is possible such
2527 * operations will result in the allocation of objects. It is possible
2528 * to specify the GCType filter for your callback. But it is not possible to
2529 * register the same callback function two times with different
2530 * GCType filters.
2531 */
2532 static void AddGCPrologueCallback(
2533 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2534
2535 /**
2536 * This function removes callback which was installed by
2537 * AddGCPrologueCallback function.
2538 */
2539 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2540
2541 /**
2542 * The function is deprecated. Please use AddGCPrologueCallback instead.
2543 * Enables the host application to receive a notification before a
2544 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002545 * callback function, you therefore cannot manipulate objects (set
2546 * or delete properties for example) since it is possible such
2547 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 */
2549 static void SetGlobalGCPrologueCallback(GCCallback);
2550
2551 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002552 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002553 * garbage collection. Allocations are not allowed in the
2554 * callback function, you therefore cannot manipulate objects (set
2555 * or delete properties for example) since it is possible such
2556 * operations will result in the allocation of objects. It is possible
2557 * to specify the GCType filter for your callback. But it is not possible to
2558 * register the same callback function two times with different
2559 * GCType filters.
2560 */
2561 static void AddGCEpilogueCallback(
2562 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2563
2564 /**
2565 * This function removes callback which was installed by
2566 * AddGCEpilogueCallback function.
2567 */
2568 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2569
2570 /**
2571 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2572 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002573 * major garbage collection. Allocations are not allowed in the
2574 * callback function, you therefore cannot manipulate objects (set
2575 * or delete properties for example) since it is possible such
2576 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 */
2578 static void SetGlobalGCEpilogueCallback(GCCallback);
2579
2580 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002581 * Allows the host application to group objects together. If one
2582 * object in the group is alive, all objects in the group are alive.
2583 * After each garbage collection, object groups are removed. It is
2584 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002585 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002586 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002588 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
2590 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002591 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002592 * initialize from scratch. This function is called implicitly if
2593 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594 */
2595 static bool Initialize();
2596
kasper.lund7276f142008-07-30 08:49:36 +00002597 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002598 * Adjusts the amount of registered external memory. Used to give
2599 * V8 an indication of the amount of externally allocated memory
2600 * that is kept alive by JavaScript objects. V8 uses this to decide
2601 * when to perform global garbage collections. Registering
2602 * externally allocated memory will trigger global garbage
2603 * collections more often than otherwise in an attempt to garbage
2604 * collect the JavaScript objects keeping the externally allocated
2605 * memory alive.
2606 *
2607 * \param change_in_bytes the change in externally allocated memory
2608 * that is kept alive by JavaScript objects.
2609 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002610 */
2611 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2612
iposva@chromium.org245aa852009-02-10 00:49:54 +00002613 /**
2614 * Suspends recording of tick samples in the profiler.
2615 * When the V8 profiling mode is enabled (usually via command line
2616 * switches) this function suspends recording of tick samples.
2617 * Profiling ticks are discarded until ResumeProfiler() is called.
2618 *
2619 * See also the --prof and --prof_auto command line switches to
2620 * enable V8 profiling.
2621 */
2622 static void PauseProfiler();
2623
2624 /**
2625 * Resumes recording of tick samples in the profiler.
2626 * See also PauseProfiler().
2627 */
2628 static void ResumeProfiler();
2629
ager@chromium.org41826e72009-03-30 13:30:57 +00002630 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002631 * Return whether profiler is currently paused.
2632 */
2633 static bool IsProfilerPaused();
2634
2635 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002636 * Resumes specified profiler modules. Can be called several times to
2637 * mark the opening of a profiler events block with the given tag.
2638 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002639 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2640 * See ProfilerModules enum.
2641 *
2642 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002643 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002644 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002645 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002646
2647 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002648 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2649 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2650 * same tag value. There is no need for blocks to be properly nested.
2651 * The profiler is paused when the last opened block is closed.
2652 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002653 * "PauseProfiler" is equivalent to "PauseProfilerEx(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 PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002660
2661 /**
2662 * Returns active (resumed) profiler modules.
2663 * See ProfilerModules enum.
2664 *
2665 * \returns active profiler modules.
2666 */
2667 static int GetActiveProfilerModules();
2668
2669 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002670 * If logging is performed into a memory buffer (via --logfile=*), allows to
2671 * retrieve previously written messages. This can be used for retrieving
2672 * profiler log data in the application. This function is thread-safe.
2673 *
2674 * Caller provides a destination buffer that must exist during GetLogLines
2675 * call. Only whole log lines are copied into the buffer.
2676 *
2677 * \param from_pos specified a point in a buffer to read from, 0 is the
2678 * beginning of a buffer. It is assumed that caller updates its current
2679 * position using returned size value from the previous call.
2680 * \param dest_buf destination buffer for log data.
2681 * \param max_size size of the destination buffer.
2682 * \returns actual size of log data copied into buffer.
2683 */
2684 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2685
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002686 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002687 * The minimum allowed size for a log lines buffer. If the size of
2688 * the buffer given will not be enough to hold a line of the maximum
2689 * length, an attempt to find a log line end in GetLogLines will
2690 * fail, and an empty result will be returned.
2691 */
2692 static const int kMinimumSizeForLogLinesBuffer = 2048;
2693
2694 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002695 * Retrieve the V8 thread id of the calling thread.
2696 *
2697 * The thread id for a thread should only be retrieved after the V8
2698 * lock has been acquired with a Locker object with that thread.
2699 */
2700 static int GetCurrentThreadId();
2701
2702 /**
2703 * Forcefully terminate execution of a JavaScript thread. This can
2704 * be used to terminate long-running scripts.
2705 *
2706 * TerminateExecution should only be called when then V8 lock has
2707 * been acquired with a Locker object. Therefore, in order to be
2708 * able to terminate long-running threads, preemption must be
2709 * enabled to allow the user of TerminateExecution to acquire the
2710 * lock.
2711 *
2712 * The termination is achieved by throwing an exception that is
2713 * uncatchable by JavaScript exception handlers. Termination
2714 * exceptions act as if they were caught by a C++ TryCatch exception
2715 * handlers. If forceful termination is used, any C++ TryCatch
2716 * exception handler that catches an exception should check if that
2717 * exception is a termination exception and immediately return if
2718 * that is the case. Returning immediately in that case will
2719 * continue the propagation of the termination exception if needed.
2720 *
2721 * The thread id passed to TerminateExecution must have been
2722 * obtained by calling GetCurrentThreadId on the thread in question.
2723 *
2724 * \param thread_id The thread id of the thread to terminate.
2725 */
2726 static void TerminateExecution(int thread_id);
2727
2728 /**
2729 * Forcefully terminate the current thread of JavaScript execution.
2730 *
2731 * This method can be used by any thread even if that thread has not
2732 * acquired the V8 lock with a Locker object.
2733 */
2734 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002735
2736 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00002737 * Is V8 terminating JavaScript execution.
2738 *
2739 * Returns true if JavaScript execution is currently terminating
2740 * because of a call to TerminateExecution. In that case there are
2741 * still JavaScript frames on the stack and the termination
2742 * exception is still active.
2743 */
2744 static bool IsExecutionTerminating();
2745
2746 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002747 * Releases any resources used by v8 and stops any utility threads
2748 * that may be running. Note that disposing v8 is permanent, it
2749 * cannot be reinitialized.
2750 *
2751 * It should generally not be necessary to dispose v8 before exiting
2752 * a process, this should happen automatically. It is only necessary
2753 * to use if the process needs the resources taken up by v8.
2754 */
2755 static bool Dispose();
2756
ager@chromium.org3811b432009-10-28 14:53:37 +00002757 /**
2758 * Get statistics about the heap memory usage.
2759 */
2760 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002761
2762 /**
2763 * Optional notification that the embedder is idle.
2764 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002765 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002766 * Returns true if the embedder should stop calling IdleNotification
2767 * until real work has been done. This indicates that V8 has done
2768 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002769 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002770 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002771
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002772 /**
2773 * Optional notification that the system is running low on memory.
2774 * V8 uses these notifications to attempt to free memory.
2775 */
2776 static void LowMemoryNotification();
2777
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002778 /**
2779 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002780 * these notifications to guide the GC heuristic. Returns the number
2781 * of context disposals - including this one - since the last time
2782 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002783 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002784 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 private:
2787 V8();
2788
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002789 static internal::Object** GlobalizeReference(internal::Object** handle);
2790 static void DisposeGlobal(internal::Object** global_handle);
2791 static void MakeWeak(internal::Object** global_handle,
2792 void* data,
2793 WeakReferenceCallback);
2794 static void ClearWeak(internal::Object** global_handle);
2795 static bool IsGlobalNearDeath(internal::Object** global_handle);
2796 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797
2798 template <class T> friend class Handle;
2799 template <class T> friend class Local;
2800 template <class T> friend class Persistent;
2801 friend class Context;
2802};
2803
2804
2805/**
2806 * An external exception handler.
2807 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002808class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 public:
2810
2811 /**
2812 * Creates a new try/catch block and registers it with v8.
2813 */
2814 TryCatch();
2815
2816 /**
2817 * Unregisters and deletes this try/catch block.
2818 */
2819 ~TryCatch();
2820
2821 /**
2822 * Returns true if an exception has been caught by this try/catch block.
2823 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002824 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
2826 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002827 * For certain types of exceptions, it makes no sense to continue
2828 * execution.
2829 *
2830 * Currently, the only type of exception that can be caught by a
2831 * TryCatch handler and for which it does not make sense to continue
2832 * is termination exception. Such exceptions are thrown when the
2833 * TerminateExecution methods are called to terminate a long-running
2834 * script.
2835 *
2836 * If CanContinue returns false, the correct action is to perform
2837 * any C++ cleanup needed and then return.
2838 */
2839 bool CanContinue() const;
2840
2841 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002842 * Throws the exception caught by this TryCatch in a way that avoids
2843 * it being caught again by this same TryCatch. As with ThrowException
2844 * it is illegal to execute any JavaScript operations after calling
2845 * ReThrow; the caller must return immediately to where the exception
2846 * is caught.
2847 */
2848 Handle<Value> ReThrow();
2849
2850 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851 * Returns the exception caught by this try/catch block. If no exception has
2852 * been caught an empty handle is returned.
2853 *
2854 * The returned handle is valid until this TryCatch block has been destroyed.
2855 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002856 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002857
2858 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002859 * Returns the .stack property of the thrown object. If no .stack
2860 * property is present an empty handle is returned.
2861 */
2862 Local<Value> StackTrace() const;
2863
2864 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002865 * Returns the message associated with this exception. If there is
2866 * no message associated an empty handle is returned.
2867 *
2868 * The returned handle is valid until this TryCatch block has been
2869 * destroyed.
2870 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002871 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002872
2873 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 * Clears any exceptions that may have been caught by this try/catch block.
2875 * After this method has been called, HasCaught() will return false.
2876 *
2877 * It is not necessary to clear a try/catch block before using it again; if
2878 * another exception is thrown the previously caught exception will just be
2879 * overwritten. However, it is often a good idea since it makes it easier
2880 * to determine which operation threw a given exception.
2881 */
2882 void Reset();
2883
v8.team.kasperl727e9952008-09-02 14:56:44 +00002884 /**
2885 * Set verbosity of the external exception handler.
2886 *
2887 * By default, exceptions that are caught by an external exception
2888 * handler are not reported. Call SetVerbose with true on an
2889 * external exception handler to have exceptions caught by the
2890 * handler reported as if they were not caught.
2891 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892 void SetVerbose(bool value);
2893
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002894 /**
2895 * Set whether or not this TryCatch should capture a Message object
2896 * which holds source information about where the exception
2897 * occurred. True by default.
2898 */
2899 void SetCaptureMessage(bool value);
2900
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002901 private:
2902 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002903 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002904 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002905 bool is_verbose_ : 1;
2906 bool can_continue_ : 1;
2907 bool capture_message_ : 1;
2908 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002909
2910 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002911};
2912
2913
2914// --- C o n t e x t ---
2915
2916
2917/**
2918 * Ignore
2919 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002920class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921 public:
2922 ExtensionConfiguration(int name_count, const char* names[])
2923 : name_count_(name_count), names_(names) { }
2924 private:
2925 friend class ImplementationUtilities;
2926 int name_count_;
2927 const char** names_;
2928};
2929
2930
2931/**
2932 * A sandboxed execution context with its own set of built-in objects
2933 * and functions.
2934 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002935class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002937 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 Local<Object> Global();
2939
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002940 /**
2941 * Detaches the global object from its context before
2942 * the global object can be reused to create a new context.
2943 */
2944 void DetachGlobal();
2945
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002946 /**
2947 * Reattaches a global object to a context. This can be used to
2948 * restore the connection between a global object and a context
2949 * after DetachGlobal has been called.
2950 *
2951 * \param global_object The global object to reattach to the
2952 * context. For this to work, the global object must be the global
2953 * object that was associated with this context before a call to
2954 * DetachGlobal.
2955 */
2956 void ReattachGlobal(Handle<Object> global_object);
2957
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002958 /** Creates a new context.
2959 *
2960 * Returns a persistent handle to the newly allocated context. This
2961 * persistent handle has to be disposed when the context is no
2962 * longer used so the context can be garbage collected.
2963 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00002964 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002965 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00002966 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2967 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968
kasper.lund44510672008-07-25 07:37:58 +00002969 /** Returns the last entered context. */
2970 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971
kasper.lund44510672008-07-25 07:37:58 +00002972 /** Returns the context that is on the top of the stack. */
2973 static Local<Context> GetCurrent();
2974
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002976 * Returns the context of the calling JavaScript code. That is the
2977 * context of the top-most JavaScript frame. If there are no
2978 * JavaScript frames an empty handle is returned.
2979 */
2980 static Local<Context> GetCalling();
2981
2982 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002983 * Sets the security token for the context. To access an object in
2984 * another context, the security tokens must match.
2985 */
2986 void SetSecurityToken(Handle<Value> token);
2987
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002988 /** Restores the security token to the default value. */
2989 void UseDefaultSecurityToken();
2990
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991 /** Returns the security token of this context.*/
2992 Handle<Value> GetSecurityToken();
2993
v8.team.kasperl727e9952008-09-02 14:56:44 +00002994 /**
2995 * Enter this context. After entering a context, all code compiled
2996 * and run is compiled and run in this context. If another context
2997 * is already entered, this old context is saved so it can be
2998 * restored when the new context is exited.
2999 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003001
3002 /**
3003 * Exit this context. Exiting the current context restores the
3004 * context that was in place when entering the current context.
3005 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006 void Exit();
3007
v8.team.kasperl727e9952008-09-02 14:56:44 +00003008 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009 bool HasOutOfMemoryException();
3010
v8.team.kasperl727e9952008-09-02 14:56:44 +00003011 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003012 static bool InContext();
3013
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003014 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003015 * Associate an additional data object with the context. This is mainly used
3016 * with the debugger to provide additional information on the context through
3017 * the debugger API.
3018 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003019 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003020 Local<Value> GetData();
3021
3022 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 * Stack-allocated class which sets the execution context for all
3024 * operations executed within a local scope.
3025 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003026 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 public:
3028 inline Scope(Handle<Context> context) : context_(context) {
3029 context_->Enter();
3030 }
3031 inline ~Scope() { context_->Exit(); }
3032 private:
3033 Handle<Context> context_;
3034 };
3035
3036 private:
3037 friend class Value;
3038 friend class Script;
3039 friend class Object;
3040 friend class Function;
3041};
3042
3043
3044/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003045 * Multiple threads in V8 are allowed, but only one thread at a time
3046 * is allowed to use V8. The definition of 'using V8' includes
3047 * accessing handles or holding onto object pointers obtained from V8
3048 * handles. It is up to the user of V8 to ensure (perhaps with
3049 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050 *
3051 * If you wish to start using V8 in a thread you can do this by constructing
3052 * a v8::Locker object. After the code using V8 has completed for the
3053 * current thread you can call the destructor. This can be combined
3054 * with C++ scope-based construction as follows:
3055 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003056 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003057 * ...
3058 * {
3059 * v8::Locker locker;
3060 * ...
3061 * // Code using V8 goes here.
3062 * ...
3063 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003064 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065 *
3066 * If you wish to stop using V8 in a thread A you can do this by either
3067 * by destroying the v8::Locker object as above or by constructing a
3068 * v8::Unlocker object:
3069 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003070 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 * {
3072 * v8::Unlocker unlocker;
3073 * ...
3074 * // Code not using V8 goes here while V8 can run in another thread.
3075 * ...
3076 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003077 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 *
3079 * The Unlocker object is intended for use in a long-running callback
3080 * from V8, where you want to release the V8 lock for other threads to
3081 * use.
3082 *
3083 * The v8::Locker is a recursive lock. That is, you can lock more than
3084 * once in a given thread. This can be useful if you have code that can
3085 * be called either from code that holds the lock or from code that does
3086 * not. The Unlocker is not recursive so you can not have several
3087 * Unlockers on the stack at once, and you can not use an Unlocker in a
3088 * thread that is not inside a Locker's scope.
3089 *
3090 * An unlocker will unlock several lockers if it has to and reinstate
3091 * the correct depth of locking on its destruction. eg.:
3092 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003093 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094 * // V8 not locked.
3095 * {
3096 * v8::Locker locker;
3097 * // V8 locked.
3098 * {
3099 * v8::Locker another_locker;
3100 * // V8 still locked (2 levels).
3101 * {
3102 * v8::Unlocker unlocker;
3103 * // V8 not locked.
3104 * }
3105 * // V8 locked again (2 levels).
3106 * }
3107 * // V8 still locked (1 level).
3108 * }
3109 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003110 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003112class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113 public:
3114 Unlocker();
3115 ~Unlocker();
3116};
3117
3118
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003119class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 public:
3121 Locker();
3122 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003123
3124 /**
3125 * Start preemption.
3126 *
3127 * When preemption is started, a timer is fired every n milli seconds
3128 * that will switch between multiple threads that are in contention
3129 * for the V8 lock.
3130 */
3131 static void StartPreemption(int every_n_ms);
3132
3133 /**
3134 * Stop preemption.
3135 */
3136 static void StopPreemption();
3137
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003138 /**
3139 * Returns whether or not the locker is locked by the current thread.
3140 */
3141 static bool IsLocked();
3142
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003143 /**
3144 * Returns whether v8::Locker is being used by this V8 instance.
3145 */
3146 static bool IsActive() { return active_; }
3147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003148 private:
3149 bool has_lock_;
3150 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003151
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003152 static bool active_;
3153
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003154 // Disallow copying and assigning.
3155 Locker(const Locker&);
3156 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157};
3158
3159
3160
3161// --- I m p l e m e n t a t i o n ---
3162
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003163
3164namespace internal {
3165
3166
3167// Tag information for HeapObject.
3168const int kHeapObjectTag = 1;
3169const int kHeapObjectTagSize = 2;
3170const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3171
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003172// Tag information for Smi.
3173const int kSmiTag = 0;
3174const int kSmiTagSize = 1;
3175const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3176
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003177template <size_t ptr_size> struct SmiConstants;
3178
3179// Smi constants for 32-bit systems.
3180template <> struct SmiConstants<4> {
3181 static const int kSmiShiftSize = 0;
3182 static const int kSmiValueSize = 31;
3183 static inline int SmiToInt(internal::Object* value) {
3184 int shift_bits = kSmiTagSize + kSmiShiftSize;
3185 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3186 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3187 }
3188};
3189
3190// Smi constants for 64-bit systems.
3191template <> struct SmiConstants<8> {
3192 static const int kSmiShiftSize = 31;
3193 static const int kSmiValueSize = 32;
3194 static inline int SmiToInt(internal::Object* value) {
3195 int shift_bits = kSmiTagSize + kSmiShiftSize;
3196 // Shift down and throw away top 32 bits.
3197 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3198 }
3199};
3200
3201const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
3202const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003203
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003204template <size_t ptr_size> struct InternalConstants;
3205
3206// Internal constants for 32-bit systems.
3207template <> struct InternalConstants<4> {
3208 static const int kStringResourceOffset = 3 * sizeof(void*);
3209};
3210
3211// Internal constants for 64-bit systems.
3212template <> struct InternalConstants<8> {
ager@chromium.orgac091b72010-05-05 07:34:42 +00003213 static const int kStringResourceOffset = 3 * sizeof(void*);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003214};
3215
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003216/**
3217 * This class exports constants and functionality from within v8 that
3218 * is necessary to implement inline functions in the v8 api. Don't
3219 * depend on functions and constants defined here.
3220 */
3221class Internals {
3222 public:
3223
3224 // These values match non-compiler-dependent values defined within
3225 // the implementation of v8.
3226 static const int kHeapObjectMapOffset = 0;
3227 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003228 static const int kStringResourceOffset =
3229 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3230
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003231 static const int kProxyProxyOffset = sizeof(void*);
3232 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3233 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003234 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003235
3236 // These constants are compiler dependent so their values must be
3237 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00003238 V8EXPORT static int kJSObjectType;
3239 V8EXPORT static int kFirstNonstringType;
3240 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003241
3242 static inline bool HasHeapObjectTag(internal::Object* value) {
3243 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3244 kHeapObjectTag);
3245 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003246
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003247 static inline bool HasSmiTag(internal::Object* value) {
3248 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3249 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003250
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003251 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003252 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3253 }
3254
3255 static inline int GetInstanceType(internal::Object* obj) {
3256 typedef internal::Object O;
3257 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3258 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3259 }
3260
3261 static inline void* GetExternalPointer(internal::Object* obj) {
3262 if (HasSmiTag(obj)) {
3263 return obj;
3264 } else if (GetInstanceType(obj) == kProxyType) {
3265 return ReadField<void*>(obj, kProxyProxyOffset);
3266 } else {
3267 return NULL;
3268 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003269 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003270
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003271 static inline bool IsExternalTwoByteString(int instance_type) {
3272 int representation = (instance_type & kFullStringRepresentationMask);
3273 return representation == kExternalTwoByteRepresentationTag;
3274 }
3275
3276 template <typename T>
3277 static inline T ReadField(Object* ptr, int offset) {
3278 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3279 return *reinterpret_cast<T*>(addr);
3280 }
3281
3282};
3283
3284}
3285
3286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287template <class T>
3288Handle<T>::Handle() : val_(0) { }
3289
3290
3291template <class T>
3292Local<T>::Local() : Handle<T>() { }
3293
3294
3295template <class T>
3296Local<T> Local<T>::New(Handle<T> that) {
3297 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003298 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3300}
3301
3302
3303template <class T>
3304Persistent<T> Persistent<T>::New(Handle<T> that) {
3305 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003306 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3308}
3309
3310
3311template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003312bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003314 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003315}
3316
3317
3318template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003319bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003321 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322}
3323
3324
3325template <class T>
3326void Persistent<T>::Dispose() {
3327 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003328 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329}
3330
3331
3332template <class T>
3333Persistent<T>::Persistent() : Handle<T>() { }
3334
3335template <class T>
3336void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003337 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3338 parameters,
3339 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340}
3341
3342template <class T>
3343void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003344 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345}
3346
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003347Local<Value> Arguments::operator[](int i) const {
3348 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3349 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3350}
3351
3352
3353Local<Function> Arguments::Callee() const {
3354 return callee_;
3355}
3356
3357
3358Local<Object> Arguments::This() const {
3359 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3360}
3361
3362
3363Local<Object> Arguments::Holder() const {
3364 return holder_;
3365}
3366
3367
3368Local<Value> Arguments::Data() const {
3369 return data_;
3370}
3371
3372
3373bool Arguments::IsConstructCall() const {
3374 return is_construct_call_;
3375}
3376
3377
3378int Arguments::Length() const {
3379 return length_;
3380}
3381
3382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383template <class T>
3384Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003385 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3386 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003387 return Local<T>(reinterpret_cast<T*>(after));
3388}
3389
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003390Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003391 return resource_name_;
3392}
3393
3394
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003395Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396 return resource_line_offset_;
3397}
3398
3399
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003400Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003401 return resource_column_offset_;
3402}
3403
3404
3405Handle<Boolean> Boolean::New(bool value) {
3406 return value ? True() : False();
3407}
3408
3409
3410void Template::Set(const char* name, v8::Handle<Data> value) {
3411 Set(v8::String::New(name), value);
3412}
3413
3414
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003415Local<Value> Object::GetInternalField(int index) {
3416#ifndef V8_ENABLE_CHECKS
3417 Local<Value> quick_result = UncheckedGetInternalField(index);
3418 if (!quick_result.IsEmpty()) return quick_result;
3419#endif
3420 return CheckedGetInternalField(index);
3421}
3422
3423
3424Local<Value> Object::UncheckedGetInternalField(int index) {
3425 typedef internal::Object O;
3426 typedef internal::Internals I;
3427 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003428 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003429 // If the object is a plain JSObject, which is the common case,
3430 // we know where to find the internal fields and can return the
3431 // value directly.
3432 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3433 O* value = I::ReadField<O*>(obj, offset);
3434 O** result = HandleScope::CreateHandle(value);
3435 return Local<Value>(reinterpret_cast<Value*>(result));
3436 } else {
3437 return Local<Value>();
3438 }
3439}
3440
3441
3442void* External::Unwrap(Handle<v8::Value> obj) {
3443#ifdef V8_ENABLE_CHECKS
3444 return FullUnwrap(obj);
3445#else
3446 return QuickUnwrap(obj);
3447#endif
3448}
3449
3450
3451void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3452 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003453 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003454 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003455}
3456
3457
3458void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003459 typedef internal::Object O;
3460 typedef internal::Internals I;
3461
3462 O* obj = *reinterpret_cast<O**>(this);
3463
3464 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3465 // If the object is a plain JSObject, which is the common case,
3466 // we know where to find the internal fields and can return the
3467 // value directly.
3468 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3469 O* value = I::ReadField<O*>(obj, offset);
3470 return I::GetExternalPointer(value);
3471 }
3472
3473 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003474}
3475
3476
3477String* String::Cast(v8::Value* value) {
3478#ifdef V8_ENABLE_CHECKS
3479 CheckCast(value);
3480#endif
3481 return static_cast<String*>(value);
3482}
3483
3484
3485String::ExternalStringResource* String::GetExternalStringResource() const {
3486 typedef internal::Object O;
3487 typedef internal::Internals I;
3488 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003489 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003490 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003491 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3492 result = reinterpret_cast<String::ExternalStringResource*>(value);
3493 } else {
3494 result = NULL;
3495 }
3496#ifdef V8_ENABLE_CHECKS
3497 VerifyExternalStringResource(result);
3498#endif
3499 return result;
3500}
3501
3502
3503bool Value::IsString() const {
3504#ifdef V8_ENABLE_CHECKS
3505 return FullIsString();
3506#else
3507 return QuickIsString();
3508#endif
3509}
3510
3511bool Value::QuickIsString() const {
3512 typedef internal::Object O;
3513 typedef internal::Internals I;
3514 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3515 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003516 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003517}
3518
3519
3520Number* Number::Cast(v8::Value* value) {
3521#ifdef V8_ENABLE_CHECKS
3522 CheckCast(value);
3523#endif
3524 return static_cast<Number*>(value);
3525}
3526
3527
3528Integer* Integer::Cast(v8::Value* value) {
3529#ifdef V8_ENABLE_CHECKS
3530 CheckCast(value);
3531#endif
3532 return static_cast<Integer*>(value);
3533}
3534
3535
3536Date* Date::Cast(v8::Value* value) {
3537#ifdef V8_ENABLE_CHECKS
3538 CheckCast(value);
3539#endif
3540 return static_cast<Date*>(value);
3541}
3542
3543
3544Object* Object::Cast(v8::Value* value) {
3545#ifdef V8_ENABLE_CHECKS
3546 CheckCast(value);
3547#endif
3548 return static_cast<Object*>(value);
3549}
3550
3551
3552Array* Array::Cast(v8::Value* value) {
3553#ifdef V8_ENABLE_CHECKS
3554 CheckCast(value);
3555#endif
3556 return static_cast<Array*>(value);
3557}
3558
3559
3560Function* Function::Cast(v8::Value* value) {
3561#ifdef V8_ENABLE_CHECKS
3562 CheckCast(value);
3563#endif
3564 return static_cast<Function*>(value);
3565}
3566
3567
3568External* External::Cast(v8::Value* value) {
3569#ifdef V8_ENABLE_CHECKS
3570 CheckCast(value);
3571#endif
3572 return static_cast<External*>(value);
3573}
3574
3575
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003576Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003577 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003578}
3579
3580
3581Local<Object> AccessorInfo::This() const {
3582 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3583}
3584
3585
3586Local<Object> AccessorInfo::Holder() const {
3587 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3588}
3589
3590
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003592 * \example shell.cc
3593 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594 * command-line and executes them.
3595 */
3596
3597
3598/**
3599 * \example process.cc
3600 */
3601
3602
3603} // namespace v8
3604
3605
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003606#undef V8EXPORT
3607#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608#undef TYPE_CHECK
3609
3610
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003611#endif // V8_H_