blob: d90289ab99d27a4ae1f0c614dba73842523b26e7 [file] [log] [blame]
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001// Copyright 2007-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41#include <stdio.h>
42
43#ifdef _WIN32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
ager@chromium.org5ec48922009-05-05 07:25:34 +000048typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052typedef int int32_t;
53typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000054typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058
59// Setup for Windows DLL export/import. When building the V8 DLL the
60// BUILDING_V8_SHARED needs to be defined. When building a program which uses
61// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
62// static library or building a program which uses the V8 static library neither
63// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000064// The reason for having both V8EXPORT and V8EXPORT_INLINE is that classes which
65// have their code inside this header file need to have __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000066// when building the DLL but cannot have __declspec(dllimport) when building
67// a program which uses the DLL.
68#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
69#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
70 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071#endif
72
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT __declspec(dllexport)
75#define V8EXPORT_INLINE __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077#define V8EXPORT __declspec(dllimport)
78#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000080#define V8EXPORT
81#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082#endif // BUILDING_V8_SHARED
83
84#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000085
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086#include <stdint.h>
87
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000088// Setup for Linux shared library export. There is no need to distinguish
89// between building or using the V8 shared library, but we should not
90// export symbols when we are building a static library.
91#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000092#define V8EXPORT __attribute__ ((visibility("default")))
93#define V8EXPORT_INLINE __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000094#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000095#define V8EXPORT
96#define V8EXPORT_INLINE
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000097#endif // defined(__GNUC__) && (__GNUC__ >= 4)
98
99#endif // _WIN32
100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000102 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 */
104namespace v8 {
105
106class Context;
107class String;
108class Value;
109class Utils;
110class Number;
111class Object;
112class Array;
113class Int32;
114class Uint32;
115class External;
116class Primitive;
117class Boolean;
118class Integer;
119class Function;
120class Date;
121class ImplementationUtilities;
122class Signature;
123template <class T> class Handle;
124template <class T> class Local;
125template <class T> class Persistent;
126class FunctionTemplate;
127class ObjectTemplate;
128class Data;
129
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000130namespace internal {
131
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000132class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000133class Object;
134class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000135
136}
137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139// --- W e a k H a n d l e s
140
141
142/**
143 * A weak reference callback function.
144 *
145 * \param object the weak global object to be reclaimed by the garbage collector
146 * \param parameter the value passed in when making the weak global object
147 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000148typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 void* parameter);
150
151
152// --- H a n d l e s ---
153
154#define TYPE_CHECK(T, S) \
155 while (false) { \
156 *(static_cast<T**>(0)) = static_cast<S*>(0); \
157 }
158
159/**
160 * An object reference managed by the v8 garbage collector.
161 *
162 * All objects returned from v8 have to be tracked by the garbage
163 * collector so that it knows that the objects are still alive. Also,
164 * because the garbage collector may move objects, it is unsafe to
165 * point directly to an object. Instead, all objects are stored in
166 * handles which are known by the garbage collector and updated
167 * whenever an object moves. Handles should always be passed by value
168 * (except in cases like out-parameters) and they should never be
169 * allocated on the heap.
170 *
171 * There are two types of handles: local and persistent handles.
172 * Local handles are light-weight and transient and typically used in
173 * local operations. They are managed by HandleScopes. Persistent
174 * handles can be used when storing objects across several independent
175 * operations and have to be explicitly deallocated when they're no
176 * longer used.
177 *
178 * It is safe to extract the object stored in the handle by
179 * dereferencing the handle (for instance, to extract the Object* from
180 * an Handle<Object>); the value will still be governed by a handle
181 * behind the scenes and the same rules apply to these values as to
182 * their handles.
183 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000184template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 public:
186
187 /**
188 * Creates an empty handle.
189 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000190 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
192 /**
193 * Creates a new handle for the specified value.
194 */
195 explicit Handle(T* val) : val_(val) { }
196
197 /**
198 * Creates a handle for the contents of the specified handle. This
199 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000200 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 * incompatible handles, for instance from a Handle<String> to a
202 * Handle<Number> it will cause a compiletime error. Assigning
203 * between compatible handles, for instance assigning a
204 * Handle<String> to a variable declared as Handle<Value>, is legal
205 * because String is a subclass of Value.
206 */
207 template <class S> inline Handle(Handle<S> that)
208 : val_(reinterpret_cast<T*>(*that)) {
209 /**
210 * This check fails when trying to convert between incompatible
211 * handles. For example, converting from a Handle<String> to a
212 * Handle<Number>.
213 */
214 TYPE_CHECK(T, S);
215 }
216
217 /**
218 * Returns true if the handle is empty.
219 */
ager@chromium.org32912102009-01-16 10:38:43 +0000220 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000222 T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000224 T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
226 /**
227 * Sets the handle to be empty. IsEmpty() will then return true.
228 */
229 void Clear() { this->val_ = 0; }
230
231 /**
232 * Checks whether two handles are the same.
233 * Returns true if both are empty, or if the objects
234 * to which they refer are identical.
235 * The handles' references are not checked.
236 */
ager@chromium.org32912102009-01-16 10:38:43 +0000237 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
239 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 if (a == 0) return b == 0;
241 if (b == 0) return false;
242 return *a == *b;
243 }
244
245 /**
246 * Checks whether two handles are different.
247 * Returns true if only one of the handles is empty, or if
248 * the objects to which they refer are different.
249 * The handles' references are not checked.
250 */
ager@chromium.org32912102009-01-16 10:38:43 +0000251 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 return !operator==(that);
253 }
254
255 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000256#ifdef V8_ENABLE_CHECKS
257 // If we're going to perform the type check then we have to check
258 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000260#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 return Handle<T>(T::Cast(*that));
262 }
263
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000264 template <class S> inline Handle<S> As() {
265 return Handle<S>::Cast(*this);
266 }
267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 private:
269 T* val_;
270};
271
272
273/**
274 * A light-weight stack-allocated object handle. All operations
275 * that return objects from within v8 return them in local handles. They
276 * are created within HandleScopes, and all local handles allocated within a
277 * handle scope are destroyed when the handle scope is destroyed. Hence it
278 * is not necessary to explicitly deallocate local handles.
279 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000280template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000282 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 template <class S> inline Local(Local<S> that)
284 : Handle<T>(reinterpret_cast<T*>(*that)) {
285 /**
286 * This check fails when trying to convert between incompatible
287 * handles. For example, converting from a Handle<String> to a
288 * Handle<Number>.
289 */
290 TYPE_CHECK(T, S);
291 }
292 template <class S> inline Local(S* that) : Handle<T>(that) { }
293 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000294#ifdef V8_ENABLE_CHECKS
295 // If we're going to perform the type check then we have to check
296 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000298#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 return Local<T>(T::Cast(*that));
300 }
301
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000302 template <class S> inline Local<S> As() {
303 return Local<S>::Cast(*this);
304 }
305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 /** Create a local handle for the content of another handle.
307 * The referee is kept alive by the local handle even when
308 * the original handle is destroyed/disposed.
309 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000310 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311};
312
313
314/**
315 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000316 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 * allocated, a Persistent handle remains valid until it is explicitly
318 * disposed.
319 *
320 * A persistent handle contains a reference to a storage cell within
321 * the v8 engine which holds an object value and which is updated by
322 * the garbage collector whenever the object is moved. A new storage
323 * cell can be created using Persistent::New and existing handles can
324 * be disposed using Persistent::Dispose. Since persistent handles
325 * are passed by value you may have many persistent handle objects
326 * that point to the same storage cell. For instance, if you pass a
327 * persistent handle as an argument to a function you will not get two
328 * different storage cells but rather two references to the same
329 * storage cell.
330 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000331template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 public:
333
334 /**
335 * Creates an empty persistent handle that doesn't point to any
336 * storage cell.
337 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000338 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339
340 /**
341 * Creates a persistent handle for the same storage cell as the
342 * specified handle. This constructor allows you to pass persistent
343 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000344 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000346 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 * between compatible persistent handles, for instance assigning a
348 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000349 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 */
351 template <class S> inline Persistent(Persistent<S> that)
352 : Handle<T>(reinterpret_cast<T*>(*that)) {
353 /**
354 * This check fails when trying to convert between incompatible
355 * handles. For example, converting from a Handle<String> to a
356 * Handle<Number>.
357 */
358 TYPE_CHECK(T, S);
359 }
360
361 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
362
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000363 /**
364 * "Casts" a plain handle which is known to be a persistent handle
365 * to a persistent handle.
366 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 template <class S> explicit inline Persistent(Handle<S> that)
368 : Handle<T>(*that) { }
369
370 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000371#ifdef V8_ENABLE_CHECKS
372 // If we're going to perform the type check then we have to check
373 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000375#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 return Persistent<T>(T::Cast(*that));
377 }
378
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000379 template <class S> inline Persistent<S> As() {
380 return Persistent<S>::Cast(*this);
381 }
382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000384 * Creates a new persistent handle for an existing local or
385 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000387 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000388
389 /**
390 * Releases the storage cell referenced by this persistent handle.
391 * Does not remove the reference to the cell from any handles.
392 * This handle's reference, and any any other references to the storage
393 * cell remain and IsEmpty will still return false.
394 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000395 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396
397 /**
398 * Make the reference to this object weak. When only weak handles
399 * refer to the object, the garbage collector will perform a
400 * callback to the given V8::WeakReferenceCallback function, passing
401 * it the object reference and the given parameters.
402 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000403 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
405 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000406 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408 /**
409 *Checks if the handle holds the only reference to an object.
410 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000411 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412
413 /**
414 * Returns true if the handle's reference is weak.
415 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000416 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417
418 private:
419 friend class ImplementationUtilities;
420 friend class ObjectTemplate;
421};
422
423
v8.team.kasperl727e9952008-09-02 14:56:44 +0000424 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 * A stack-allocated class that governs a number of local handles.
426 * After a handle scope has been created, all local handles will be
427 * allocated within that handle scope until either the handle scope is
428 * deleted or another handle scope is created. If there is already a
429 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000430 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 * new handles will again be allocated in the original handle scope.
432 *
433 * After the handle scope of a local handle has been deleted the
434 * garbage collector will no longer track the object stored in the
435 * handle and may deallocate it. The behavior of accessing a handle
436 * for which the handle scope has been deleted is undefined.
437 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000438class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000440 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000442 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443
444 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 * Closes the handle scope and returns the value as a handle in the
446 * previous scope, which is the new current scope after the call.
447 */
448 template <class T> Local<T> Close(Handle<T> value);
449
450 /**
451 * Counts the number of allocated handles.
452 */
453 static int NumberOfHandles();
454
455 /**
456 * Creates a new handle with the given value.
457 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000458 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 private:
461 // Make it impossible to create heap-allocated or illegal handle
462 // scopes by disallowing certain operations.
463 HandleScope(const HandleScope&);
464 void operator=(const HandleScope&);
465 void* operator new(size_t size);
466 void operator delete(void*, size_t);
467
ager@chromium.org3811b432009-10-28 14:53:37 +0000468 // This Data class is accessible internally as HandleScopeData through a
469 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000470 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 public:
472 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000473 internal::Object** next;
474 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 inline void Initialize() {
476 extensions = -1;
477 next = limit = NULL;
478 }
479 };
480
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000481 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000483 // Allow for the active closing of HandleScopes which allows to pass a handle
484 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000486 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 friend class ImplementationUtilities;
489};
490
491
492// --- S p e c i a l o b j e c t s ---
493
494
495/**
496 * The superclass of values and API object templates.
497 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000498class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 private:
500 Data();
501};
502
503
504/**
505 * Pre-compilation data that can be associated with a script. This
506 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000507 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000508 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000510class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 public:
512 virtual ~ScriptData() { }
513 static ScriptData* PreCompile(const char* input, int length);
514 static ScriptData* New(unsigned* data, int length);
515
516 virtual int Length() = 0;
517 virtual unsigned* Data() = 0;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000518 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519};
520
521
522/**
523 * The origin, within a file, of a script.
524 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000525class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000527 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 Handle<Integer> resource_line_offset = Handle<Integer>(),
529 Handle<Integer> resource_column_offset = Handle<Integer>())
530 : resource_name_(resource_name),
531 resource_line_offset_(resource_line_offset),
532 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000533 inline Handle<Value> ResourceName() const;
534 inline Handle<Integer> ResourceLineOffset() const;
535 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000536 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000537 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 Handle<Integer> resource_line_offset_;
539 Handle<Integer> resource_column_offset_;
540};
541
542
543/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000544 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000546class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 public:
548
549 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000550 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000551 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000552 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000553 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000554 * when New() returns
555 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
556 * using pre_data speeds compilation if it's done multiple times.
557 * Owned by caller, no references are kept when New() returns.
558 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000559 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000560 * available to compile event handlers.
561 * \return Compiled script object (context independent; when run it
562 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000564 static Local<Script> New(Handle<String> source,
565 ScriptOrigin* origin = NULL,
566 ScriptData* pre_data = NULL,
567 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568
mads.s.agercbaa0602008-08-14 13:41:48 +0000569 /**
570 * Compiles the specified script using the specified file name
571 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000572 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000573 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000574 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000575 * as the script's origin.
576 * \return Compiled script object (context independent; when run it
577 * will use the currently entered context).
578 */
579 static Local<Script> New(Handle<String> source,
580 Handle<Value> file_name);
581
582 /**
583 * Compiles the specified script (bound to current context).
584 *
585 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000586 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000587 * when Compile() returns
588 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
589 * using pre_data speeds compilation if it's done multiple times.
590 * Owned by caller, no references are kept when Compile() returns.
591 * \param script_data Arbitrary data associated with script. Using
592 * this has same effect as calling SetData(), but makes data available
593 * earlier (i.e. to compile event handlers).
594 * \return Compiled script object, bound to the context that was active
595 * when this function was called. When run it will always use this
596 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000597 */
598 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000599 ScriptOrigin* origin = NULL,
600 ScriptData* pre_data = NULL,
601 Handle<String> script_data = Handle<String>());
602
603 /**
604 * Compiles the specified script using the specified file name
605 * object (typically a string) as the script's origin.
606 *
607 * \param source Script source code.
608 * \param file_name File name to use as script's origin
609 * \param script_data Arbitrary data associated with script. Using
610 * this has same effect as calling SetData(), but makes data available
611 * earlier (i.e. to compile event handlers).
612 * \return Compiled script object, bound to the context that was active
613 * when this function was called. When run it will always use this
614 * context.
615 */
616 static Local<Script> Compile(Handle<String> source,
617 Handle<Value> file_name,
618 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000619
v8.team.kasperl727e9952008-09-02 14:56:44 +0000620 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000621 * Runs the script returning the resulting value. If the script is
622 * context independent (created using ::New) it will be run in the
623 * currently entered context. If it is context specific (created
624 * using ::Compile) it will be run in the context in which it was
625 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000626 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000628
629 /**
630 * Returns the script id value.
631 */
632 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000633
634 /**
635 * Associate an additional data object with the script. This is mainly used
636 * with the debugger as this data object is only available through the
637 * debugger API.
638 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000639 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640};
641
642
643/**
644 * An error message.
645 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000646class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000648 Local<String> Get() const;
649 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000651 /**
652 * Returns the resource name for the script from where the function causing
653 * the error originates.
654 */
ager@chromium.org32912102009-01-16 10:38:43 +0000655 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000657 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000658 * Returns the resource data for the script from where the function causing
659 * the error originates.
660 */
661 Handle<Value> GetScriptData() const;
662
663 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000664 * Returns the number, 1-based, of the line where the error occurred.
665 */
ager@chromium.org32912102009-01-16 10:38:43 +0000666 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000668 /**
669 * Returns the index within the script of the first character where
670 * the error occurred.
671 */
ager@chromium.org32912102009-01-16 10:38:43 +0000672 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000673
674 /**
675 * Returns the index within the script of the last character where
676 * the error occurred.
677 */
ager@chromium.org32912102009-01-16 10:38:43 +0000678 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000679
680 /**
681 * Returns the index within the line of the first character where
682 * the error occurred.
683 */
ager@chromium.org32912102009-01-16 10:38:43 +0000684 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000685
686 /**
687 * Returns the index within the line of the last character where
688 * the error occurred.
689 */
ager@chromium.org32912102009-01-16 10:38:43 +0000690 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000691
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692 // TODO(1245381): Print to a string instead of on a FILE.
693 static void PrintCurrentStackTrace(FILE* out);
694};
695
696
697// --- V a l u e ---
698
699
700/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000701 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000703class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 public:
705
706 /**
707 * Returns true if this value is the undefined value. See ECMA-262
708 * 4.3.10.
709 */
ager@chromium.org32912102009-01-16 10:38:43 +0000710 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711
712 /**
713 * Returns true if this value is the null value. See ECMA-262
714 * 4.3.11.
715 */
ager@chromium.org32912102009-01-16 10:38:43 +0000716 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717
718 /**
719 * Returns true if this value is true.
720 */
ager@chromium.org32912102009-01-16 10:38:43 +0000721 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722
723 /**
724 * Returns true if this value is false.
725 */
ager@chromium.org32912102009-01-16 10:38:43 +0000726 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727
728 /**
729 * Returns true if this value is an instance of the String type.
730 * See ECMA-262 8.4.
731 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000732 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733
734 /**
735 * Returns true if this value is a function.
736 */
ager@chromium.org32912102009-01-16 10:38:43 +0000737 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
739 /**
740 * Returns true if this value is an array.
741 */
ager@chromium.org32912102009-01-16 10:38:43 +0000742 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743
v8.team.kasperl727e9952008-09-02 14:56:44 +0000744 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 * Returns true if this value is an object.
746 */
ager@chromium.org32912102009-01-16 10:38:43 +0000747 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
v8.team.kasperl727e9952008-09-02 14:56:44 +0000749 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 * Returns true if this value is boolean.
751 */
ager@chromium.org32912102009-01-16 10:38:43 +0000752 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000753
v8.team.kasperl727e9952008-09-02 14:56:44 +0000754 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 * Returns true if this value is a number.
756 */
ager@chromium.org32912102009-01-16 10:38:43 +0000757 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758
v8.team.kasperl727e9952008-09-02 14:56:44 +0000759 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 * Returns true if this value is external.
761 */
ager@chromium.org32912102009-01-16 10:38:43 +0000762 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000763
v8.team.kasperl727e9952008-09-02 14:56:44 +0000764 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 * Returns true if this value is a 32-bit signed integer.
766 */
ager@chromium.org32912102009-01-16 10:38:43 +0000767 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000769 /**
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000770 * Returns true if this value is a 32-bit signed integer.
771 */
772 bool IsUint32() const;
773
774 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000775 * Returns true if this value is a Date.
776 */
ager@chromium.org32912102009-01-16 10:38:43 +0000777 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000778
ager@chromium.org32912102009-01-16 10:38:43 +0000779 Local<Boolean> ToBoolean() const;
780 Local<Number> ToNumber() const;
781 Local<String> ToString() const;
782 Local<String> ToDetailString() const;
783 Local<Object> ToObject() const;
784 Local<Integer> ToInteger() const;
785 Local<Uint32> ToUint32() const;
786 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787
788 /**
789 * Attempts to convert a string to an array index.
790 * Returns an empty handle if the conversion fails.
791 */
ager@chromium.org32912102009-01-16 10:38:43 +0000792 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
ager@chromium.org32912102009-01-16 10:38:43 +0000794 bool BooleanValue() const;
795 double NumberValue() const;
796 int64_t IntegerValue() const;
797 uint32_t Uint32Value() const;
798 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799
800 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000801 bool Equals(Handle<Value> that) const;
802 bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000803
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000804 private:
805 inline bool QuickIsString() const;
806 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807};
808
809
810/**
811 * The superclass of primitive values. See ECMA-262 4.3.2.
812 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000813class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814
815
816/**
817 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
818 * or false value.
819 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000820class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000822 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 static inline Handle<Boolean> New(bool value);
824};
825
826
827/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000828 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000830class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000832
833 /**
834 * Returns the number of characters in this string.
835 */
ager@chromium.org32912102009-01-16 10:38:43 +0000836 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837
v8.team.kasperl727e9952008-09-02 14:56:44 +0000838 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000839 * Returns the number of bytes in the UTF-8 encoded
840 * representation of this string.
841 */
ager@chromium.org32912102009-01-16 10:38:43 +0000842 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000843
844 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000845 * Write the contents of the string to an external buffer.
846 * If no arguments are given, expects the buffer to be large
847 * enough to hold the entire string and NULL terminator. Copies
848 * the contents of the string and the NULL terminator into the
849 * buffer.
850 *
851 * Copies up to length characters into the output buffer.
852 * Only null-terminates if there is enough space in the buffer.
853 *
854 * \param buffer The buffer into which the string will be copied.
855 * \param start The starting position within the string at which
856 * copying begins.
857 * \param length The number of bytes to copy from the string.
ager@chromium.org357bf652010-04-12 11:30:10 +0000858 * \param nchars_ref The number of characters written, can be NULL.
859 * \return The number of bytes copied to the buffer
v8.team.kasperl727e9952008-09-02 14:56:44 +0000860 * excluding the NULL terminator.
861 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000862 enum WriteHints {
863 NO_HINTS = 0,
864 HINT_MANY_WRITES_EXPECTED = 1
865 };
866
867 int Write(uint16_t* buffer,
868 int start = 0,
869 int length = -1,
870 WriteHints hints = NO_HINTS) const; // UTF-16
871 int WriteAscii(char* buffer,
872 int start = 0,
873 int length = -1,
874 WriteHints hints = NO_HINTS) const; // ASCII
ager@chromium.org357bf652010-04-12 11:30:10 +0000875 int WriteUtf8(char* buffer,
876 int length = -1,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000877 int* nchars_ref = NULL,
878 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
v8.team.kasperl727e9952008-09-02 14:56:44 +0000880 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000881 * A zero length string.
882 */
883 static v8::Local<v8::String> Empty();
884
885 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000886 * Returns true if the string is external
887 */
ager@chromium.org32912102009-01-16 10:38:43 +0000888 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
v8.team.kasperl727e9952008-09-02 14:56:44 +0000890 /**
891 * Returns true if the string is both external and ascii
892 */
ager@chromium.org32912102009-01-16 10:38:43 +0000893 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000894
895 class V8EXPORT ExternalStringResourceBase {
896 public:
897 virtual ~ExternalStringResourceBase() {}
898 protected:
899 ExternalStringResourceBase() {}
900 private:
901 // Disallow copying and assigning.
902 ExternalStringResourceBase(const ExternalStringResourceBase&);
903 void operator=(const ExternalStringResourceBase&);
904 };
905
v8.team.kasperl727e9952008-09-02 14:56:44 +0000906 /**
907 * An ExternalStringResource is a wrapper around a two-byte string
908 * buffer that resides outside V8's heap. Implement an
909 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000910 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000911 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000912 class V8EXPORT ExternalStringResource
913 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 public:
915 /**
916 * Override the destructor to manage the life cycle of the underlying
917 * buffer.
918 */
919 virtual ~ExternalStringResource() {}
920 /** The string data from the underlying buffer.*/
921 virtual const uint16_t* data() const = 0;
922 /** The length of the string. That is, the number of two-byte characters.*/
923 virtual size_t length() const = 0;
924 protected:
925 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926 };
927
928 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000929 * An ExternalAsciiStringResource is a wrapper around an ascii
930 * string buffer that resides outside V8's heap. Implement an
931 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000932 * underlying buffer. Note that the string data must be immutable
933 * and that the data must be strict 7-bit ASCII, not Latin1 or
934 * UTF-8, which would require special treatment internally in the
935 * engine and, in the case of UTF-8, do not allow efficient indexing.
936 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000937 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000939 class V8EXPORT ExternalAsciiStringResource
940 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 public:
942 /**
943 * Override the destructor to manage the life cycle of the underlying
944 * buffer.
945 */
946 virtual ~ExternalAsciiStringResource() {}
947 /** The string data from the underlying buffer.*/
948 virtual const char* data() const = 0;
949 /** The number of ascii characters in the string.*/
950 virtual size_t length() const = 0;
951 protected:
952 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 };
954
955 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000956 * Get the ExternalStringResource for an external string. Returns
957 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000959 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960
961 /**
962 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000963 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964 */
ager@chromium.org32912102009-01-16 10:38:43 +0000965 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000967 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968
969 /**
970 * Allocates a new string from either utf-8 encoded or ascii data.
971 * The second parameter 'length' gives the buffer length.
972 * If the data is utf-8 encoded, the caller must
973 * be careful to supply the length parameter.
974 * If it is not given, the function calls
975 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000976 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 */
978 static Local<String> New(const char* data, int length = -1);
979
980 /** Allocates a new string from utf16 data.*/
981 static Local<String> New(const uint16_t* data, int length = -1);
982
983 /** Creates a symbol. Returns one if it exists already.*/
984 static Local<String> NewSymbol(const char* data, int length = -1);
985
v8.team.kasperl727e9952008-09-02 14:56:44 +0000986 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000987 * Creates a new string by concatenating the left and the right strings
988 * passed in as parameters.
989 */
990 static Local<String> Concat(Handle<String> left, Handle<String>right);
991
992 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000993 * Creates a new external string using the data defined in the given
994 * resource. The resource is deleted when the external string is no
995 * longer live on V8's heap. The caller of this function should not
996 * delete or modify the resource. Neither should the underlying buffer be
997 * deallocated or modified except through the destructor of the
998 * external string resource.
999 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001000 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001001
ager@chromium.org6f10e412009-02-13 10:11:16 +00001002 /**
1003 * Associate an external string resource with this string by transforming it
1004 * in place so that existing references to this string in the JavaScript heap
1005 * will use the external string resource. The external string resource's
1006 * character contents needs to be equivalent to this string.
1007 * Returns true if the string has been changed to be an external string.
1008 * The string is not modified if the operation fails.
1009 */
1010 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011
v8.team.kasperl727e9952008-09-02 14:56:44 +00001012 /**
1013 * Creates a new external string using the ascii data defined in the given
1014 * resource. The resource is deleted when the external string is no
1015 * longer live on V8's heap. The caller of this function should not
1016 * delete or modify the resource. Neither should the underlying buffer be
1017 * deallocated or modified except through the destructor of the
1018 * external string resource.
1019 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001021
ager@chromium.org6f10e412009-02-13 10:11:16 +00001022 /**
1023 * Associate an external string resource with this string by transforming it
1024 * in place so that existing references to this string in the JavaScript heap
1025 * will use the external string resource. The external string resource's
1026 * character contents needs to be equivalent to this string.
1027 * Returns true if the string has been changed to be an external string.
1028 * The string is not modified if the operation fails.
1029 */
1030 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001032 /**
1033 * Returns true if this string can be made external.
1034 */
1035 bool CanMakeExternal();
1036
kasper.lund7276f142008-07-30 08:49:36 +00001037 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 static Local<String> NewUndetectable(const char* data, int length = -1);
1039
kasper.lund7276f142008-07-30 08:49:36 +00001040 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
1042
1043 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001044 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001045 * you want to print the object. If conversion to a string fails
1046 * (eg. due to an exception in the toString() method of the object)
1047 * then the length() method returns 0 and the * operator returns
1048 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001049 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001050 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001051 public:
1052 explicit Utf8Value(Handle<v8::Value> obj);
1053 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001054 char* operator*() { return str_; }
1055 const char* operator*() const { return str_; }
1056 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001057 private:
1058 char* str_;
1059 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001060
1061 // Disallow copying and assigning.
1062 Utf8Value(const Utf8Value&);
1063 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001064 };
1065
1066 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 * Converts an object to an ascii string.
1068 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001069 * If conversion to a string fails (eg. due to an exception in the toString()
1070 * method of the object) then the length() method returns 0 and the * operator
1071 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001073 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 public:
1075 explicit AsciiValue(Handle<v8::Value> obj);
1076 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001077 char* operator*() { return str_; }
1078 const char* operator*() const { return str_; }
1079 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 private:
1081 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001082 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001083
1084 // Disallow copying and assigning.
1085 AsciiValue(const AsciiValue&);
1086 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 };
1088
1089 /**
1090 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001091 * If conversion to a string fails (eg. due to an exception in the toString()
1092 * method of the object) then the length() method returns 0 and the * operator
1093 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001095 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 public:
1097 explicit Value(Handle<v8::Value> obj);
1098 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001099 uint16_t* operator*() { return str_; }
1100 const uint16_t* operator*() const { return str_; }
1101 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 private:
1103 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001104 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001105
1106 // Disallow copying and assigning.
1107 Value(const Value&);
1108 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001110
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001111 private:
1112 void VerifyExternalStringResource(ExternalStringResource* val) const;
1113 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114};
1115
1116
1117/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001118 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001120class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001122 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001124 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 private:
1126 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001127 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128};
1129
1130
1131/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001132 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001134class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 public:
1136 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001137 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001138 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001139 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 private:
1141 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001142 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143};
1144
1145
1146/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001147 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001149class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001151 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152 private:
1153 Int32();
1154};
1155
1156
1157/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001158 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001160class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001162 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 private:
1164 Uint32();
1165};
1166
1167
1168/**
1169 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1170 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001171class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 public:
1173 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001174
1175 /**
1176 * A specialization of Value::NumberValue that is more efficient
1177 * because we know the structure of this object.
1178 */
ager@chromium.org32912102009-01-16 10:38:43 +00001179 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001180
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001181 static inline Date* Cast(v8::Value* obj);
1182 private:
1183 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184};
1185
1186
1187enum PropertyAttribute {
1188 None = 0,
1189 ReadOnly = 1 << 0,
1190 DontEnum = 1 << 1,
1191 DontDelete = 1 << 2
1192};
1193
ager@chromium.org3811b432009-10-28 14:53:37 +00001194enum ExternalArrayType {
1195 kExternalByteArray = 1,
1196 kExternalUnsignedByteArray,
1197 kExternalShortArray,
1198 kExternalUnsignedShortArray,
1199 kExternalIntArray,
1200 kExternalUnsignedIntArray,
1201 kExternalFloatArray
1202};
1203
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001205 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001207class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 public:
1209 bool Set(Handle<Value> key,
1210 Handle<Value> value,
1211 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001212
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001213 bool Set(uint32_t index,
1214 Handle<Value> value);
1215
ager@chromium.orge2902be2009-06-08 12:21:35 +00001216 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001217 // overriding accessors or read-only properties.
1218 //
1219 // Note that if the object has an interceptor the property will be set
1220 // locally, but since the interceptor takes precedence the local property
1221 // will only be returned if the interceptor doesn't return a value.
1222 //
1223 // Note also that this only works for named properties.
1224 bool ForceSet(Handle<Value> key,
1225 Handle<Value> value,
1226 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001227
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 Local<Value> Get(Handle<Value> key);
1229
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001230 Local<Value> Get(uint32_t index);
1231
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 // TODO(1245389): Replace the type-specific versions of these
1233 // functions with generic ones that accept a Handle<Value> key.
1234 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001237
1238 // Delete a property on this object bypassing interceptors and
1239 // ignoring dont-delete attributes.
1240 bool ForceDelete(Handle<Value> key);
1241
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 bool Delete(uint32_t index);
1245
1246 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001247 * Returns an array containing the names of the enumerable properties
1248 * of this object, including properties from prototype objects. The
1249 * array returned by this method contains the same values as would
1250 * be enumerated by a for-in statement over this object.
1251 */
1252 Local<Array> GetPropertyNames();
1253
1254 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 * Get the prototype object. This does not skip objects marked to
1256 * be skipped by __proto__ and it does not consult the security
1257 * handler.
1258 */
1259 Local<Value> GetPrototype();
1260
1261 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001262 * Set the prototype object. This does not skip objects marked to
1263 * be skipped by __proto__ and it does not consult the security
1264 * handler.
1265 */
1266 bool SetPrototype(Handle<Value> prototype);
1267
1268 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001269 * Finds an instance of the given function template in the prototype
1270 * chain.
1271 */
1272 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1273
1274 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 * Call builtin Object.prototype.toString on this object.
1276 * This is different from Value::ToString() that may call
1277 * user-defined toString function. This one does not.
1278 */
1279 Local<String> ObjectProtoToString();
1280
kasper.lund212ac232008-07-16 07:07:30 +00001281 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001283 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001284 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001285 /** Sets the value in an internal field. */
1286 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001288 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001289 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001290
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001291 /** Sets a native pointer in an internal field. */
1292 void SetPointerInInternalField(int index, void* value);
1293
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 // Testers for local properties.
1295 bool HasRealNamedProperty(Handle<String> key);
1296 bool HasRealIndexedProperty(uint32_t index);
1297 bool HasRealNamedCallbackProperty(Handle<String> key);
1298
1299 /**
1300 * If result.IsEmpty() no real property was located in the prototype chain.
1301 * This means interceptors in the prototype chain are not called.
1302 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001303 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1304
1305 /**
1306 * If result.IsEmpty() no real property was located on the object or
1307 * in the prototype chain.
1308 * This means interceptors in the prototype chain are not called.
1309 */
1310 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311
1312 /** Tests for a named lookup interceptor.*/
1313 bool HasNamedLookupInterceptor();
1314
kasper.lund212ac232008-07-16 07:07:30 +00001315 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 bool HasIndexedLookupInterceptor();
1317
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001318 /**
1319 * Turns on access check on the object if the object is an instance of
1320 * a template that has access check callbacks. If an object has no
1321 * access check info, the object cannot be accessed by anyone.
1322 */
1323 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001324
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001325 /**
1326 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001327 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001328 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001329 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001330 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001331 */
1332 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001333
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001334 /**
1335 * Access hidden properties on JavaScript objects. These properties are
1336 * hidden from the executing JavaScript and only accessible through the V8
1337 * C++ API. Hidden properties introduced by V8 internally (for example the
1338 * identity hash) are prefixed with "v8::".
1339 */
1340 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1341 Local<Value> GetHiddenValue(Handle<String> key);
1342 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001343
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001344 /**
1345 * Returns true if this is an instance of an api function (one
1346 * created from a function created from a function template) and has
1347 * been modified since it was created. Note that this method is
1348 * conservative and may return true for objects that haven't actually
1349 * been modified.
1350 */
1351 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001352
1353 /**
1354 * Clone this object with a fast but shallow copy. Values will point
1355 * to the same values as the original object.
1356 */
1357 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001359 /**
1360 * Set the backing store of the indexed properties to be managed by the
1361 * embedding layer. Access to the indexed properties will follow the rules
1362 * spelled out in CanvasPixelArray.
1363 * Note: The embedding program still owns the data and needs to ensure that
1364 * the backing store is preserved while V8 has a reference.
1365 */
1366 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1367
ager@chromium.org3811b432009-10-28 14:53:37 +00001368 /**
1369 * Set the backing store of the indexed properties to be managed by the
1370 * embedding layer. Access to the indexed properties will follow the rules
1371 * spelled out for the CanvasArray subtypes in the WebGL specification.
1372 * Note: The embedding program still owns the data and needs to ensure that
1373 * the backing store is preserved while V8 has a reference.
1374 */
1375 void SetIndexedPropertiesToExternalArrayData(void* data,
1376 ExternalArrayType array_type,
1377 int number_of_elements);
1378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001380 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 private:
1382 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001383 static void CheckCast(Value* obj);
1384 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001385 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001386
1387 /**
1388 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001389 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001390 */
1391 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392};
1393
1394
1395/**
1396 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1397 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001398class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001400 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401
ager@chromium.org3e875802009-06-29 08:26:34 +00001402 /**
1403 * Clones an element at index |index|. Returns an empty
1404 * handle if cloning fails (for any reason).
1405 */
1406 Local<Object> CloneElementAt(uint32_t index);
1407
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001409 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410 private:
1411 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001412 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413};
1414
1415
1416/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001417 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001419class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001421 Local<Object> NewInstance() const;
1422 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1424 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001425 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001426
1427 /**
1428 * Returns zero based line number of function body and
1429 * kLineOffsetNotFound if no information available.
1430 */
1431 int GetScriptLineNumber() const;
1432 ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001433 static inline Function* Cast(Value* obj);
ager@chromium.org5c838252010-02-19 08:53:10 +00001434 static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435 private:
1436 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001437 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438};
1439
1440
1441/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001442 * A JavaScript value that wraps a C++ void*. This type of value is
1443 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001445 *
1446 * The Wrap function V8 will return the most optimal Value object wrapping the
1447 * C++ void*. The type of the value is not guaranteed to be an External object
1448 * and no assumptions about its type should be made. To access the wrapped
1449 * value Unwrap should be used, all other operations on that object will lead
1450 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001452class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001454 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001455 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001456
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001458 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001459 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460 private:
1461 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001462 static void CheckCast(v8::Value* obj);
1463 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1464 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465};
1466
1467
1468// --- T e m p l a t e s ---
1469
1470
1471/**
1472 * The superclass of object and function templates.
1473 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001474class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 public:
1476 /** Adds a property to each instance created by this template.*/
1477 void Set(Handle<String> name, Handle<Data> value,
1478 PropertyAttribute attributes = None);
1479 inline void Set(const char* name, Handle<Data> value);
1480 private:
1481 Template();
1482
1483 friend class ObjectTemplate;
1484 friend class FunctionTemplate;
1485};
1486
1487
1488/**
1489 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001490 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 * including the receiver, the number and values of arguments, and
1492 * the holder of the function.
1493 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001494class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 public:
1496 inline int Length() const;
1497 inline Local<Value> operator[](int i) const;
1498 inline Local<Function> Callee() const;
1499 inline Local<Object> This() const;
1500 inline Local<Object> Holder() const;
1501 inline bool IsConstructCall() const;
1502 inline Local<Value> Data() const;
1503 private:
1504 Arguments();
1505 friend class ImplementationUtilities;
1506 inline Arguments(Local<Value> data,
1507 Local<Object> holder,
1508 Local<Function> callee,
1509 bool is_construct_call,
1510 void** values, int length);
1511 Local<Value> data_;
1512 Local<Object> holder_;
1513 Local<Function> callee_;
1514 bool is_construct_call_;
1515 void** values_;
1516 int length_;
1517};
1518
1519
1520/**
1521 * The information passed to an accessor callback about the context
1522 * of the property access.
1523 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001524class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001526 inline AccessorInfo(internal::Object** args)
1527 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 inline Local<Value> Data() const;
1529 inline Local<Object> This() const;
1530 inline Local<Object> Holder() const;
1531 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001532 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533};
1534
1535
1536typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1537
1538typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1539
1540/**
1541 * Accessor[Getter|Setter] are used as callback functions when
1542 * setting|getting a particular property. See objectTemplate::SetAccessor.
1543 */
1544typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1545 const AccessorInfo& info);
1546
1547
1548typedef void (*AccessorSetter)(Local<String> property,
1549 Local<Value> value,
1550 const AccessorInfo& info);
1551
1552
1553/**
1554 * NamedProperty[Getter|Setter] are used as interceptors on object.
1555 * See ObjectTemplate::SetNamedPropertyHandler.
1556 */
1557typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1558 const AccessorInfo& info);
1559
1560
1561/**
1562 * Returns the value if the setter intercepts the request.
1563 * Otherwise, returns an empty handle.
1564 */
1565typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1566 Local<Value> value,
1567 const AccessorInfo& info);
1568
1569
1570/**
1571 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001572 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 */
1574typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1575 const AccessorInfo& info);
1576
1577
1578/**
1579 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001580 * The return value is true if the property could be deleted and false
1581 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 */
1583typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1584 const AccessorInfo& info);
1585
1586/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001587 * Returns an array containing the names of the properties the named
1588 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 */
1590typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1591
v8.team.kasperl727e9952008-09-02 14:56:44 +00001592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001594 * Returns the value of the property if the getter intercepts the
1595 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 */
1597typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1598 const AccessorInfo& info);
1599
1600
1601/**
1602 * Returns the value if the setter intercepts the request.
1603 * Otherwise, returns an empty handle.
1604 */
1605typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1606 Local<Value> value,
1607 const AccessorInfo& info);
1608
1609
1610/**
1611 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001612 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 */
1614typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1615 const AccessorInfo& info);
1616
1617/**
1618 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001619 * The return value is true if the property could be deleted and false
1620 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 */
1622typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1623 const AccessorInfo& info);
1624
v8.team.kasperl727e9952008-09-02 14:56:44 +00001625/**
1626 * Returns an array containing the indices of the properties the
1627 * indexed property getter intercepts.
1628 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1630
1631
1632/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001633 * Access control specifications.
1634 *
1635 * Some accessors should be accessible across contexts. These
1636 * accessors have an explicit access control parameter which specifies
1637 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001638 *
1639 * Additionally, for security, accessors can prohibit overwriting by
1640 * accessors defined in JavaScript. For objects that have such
1641 * accessors either locally or in their prototype chain it is not
1642 * possible to overwrite the accessor by using __defineGetter__ or
1643 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 */
1645enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001646 DEFAULT = 0,
1647 ALL_CAN_READ = 1,
1648 ALL_CAN_WRITE = 1 << 1,
1649 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650};
1651
1652
1653/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001654 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 */
1656enum AccessType {
1657 ACCESS_GET,
1658 ACCESS_SET,
1659 ACCESS_HAS,
1660 ACCESS_DELETE,
1661 ACCESS_KEYS
1662};
1663
v8.team.kasperl727e9952008-09-02 14:56:44 +00001664
1665/**
1666 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001667 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001668 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001669typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 Local<Value> key,
1671 AccessType type,
1672 Local<Value> data);
1673
v8.team.kasperl727e9952008-09-02 14:56:44 +00001674
1675/**
1676 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001677 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001678 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001679typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 uint32_t index,
1681 AccessType type,
1682 Local<Value> data);
1683
1684
1685/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001686 * A FunctionTemplate is used to create functions at runtime. There
1687 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001688 * context. The lifetime of the created function is equal to the
1689 * lifetime of the context. So in case the embedder needs to create
1690 * temporary functions that can be collected using Scripts is
1691 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 *
1693 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001694 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001696 * A FunctionTemplate has a corresponding instance template which is
1697 * used to create object instances when the function is used as a
1698 * constructor. Properties added to the instance template are added to
1699 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 *
1701 * A FunctionTemplate can have a prototype template. The prototype template
1702 * is used to create the prototype object of the function.
1703 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001704 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001706 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1708 * t->Set("func_property", v8::Number::New(1));
1709 *
1710 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1711 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1712 * proto_t->Set("proto_const", v8::Number::New(2));
1713 *
1714 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1715 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1716 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1717 * instance_t->Set("instance_property", Number::New(3));
1718 *
1719 * v8::Local<v8::Function> function = t->GetFunction();
1720 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001721 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001722 *
1723 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001724 * and "instance" for the instance object created above. The function
1725 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001727 * \code
1728 * func_property in function == true;
1729 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001731 * function.prototype.proto_method() invokes 'InvokeCallback'
1732 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001734 * instance instanceof function == true;
1735 * instance.instance_accessor calls 'InstanceAccessorCallback'
1736 * instance.instance_property == 3;
1737 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001739 * A FunctionTemplate can inherit from another one by calling the
1740 * FunctionTemplate::Inherit method. The following graph illustrates
1741 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001743 * \code
1744 * FunctionTemplate Parent -> Parent() . prototype -> { }
1745 * ^ ^
1746 * | Inherit(Parent) | .__proto__
1747 * | |
1748 * FunctionTemplate Child -> Child() . prototype -> { }
1749 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001751 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1752 * object of the Child() function has __proto__ pointing to the
1753 * Parent() function's prototype object. An instance of the Child
1754 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001756 * Let Parent be the FunctionTemplate initialized in the previous
1757 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001759 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 * Local<FunctionTemplate> parent = t;
1761 * Local<FunctionTemplate> child = FunctionTemplate::New();
1762 * child->Inherit(parent);
1763 *
1764 * Local<Function> child_function = child->GetFunction();
1765 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001766 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001768 * The Child function and Child instance will have the following
1769 * properties:
1770 *
1771 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001773 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001775 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001777class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 public:
1779 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001780 static Local<FunctionTemplate> New(
1781 InvocationCallback callback = 0,
1782 Handle<Value> data = Handle<Value>(),
1783 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 /** Returns the unique function instance in the current execution context.*/
1785 Local<Function> GetFunction();
1786
v8.team.kasperl727e9952008-09-02 14:56:44 +00001787 /**
1788 * Set the call-handler callback for a FunctionTemplate. This
1789 * callback is called whenever the function created from this
1790 * FunctionTemplate is called.
1791 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 void SetCallHandler(InvocationCallback callback,
1793 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794
v8.team.kasperl727e9952008-09-02 14:56:44 +00001795 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 Local<ObjectTemplate> InstanceTemplate();
1797
1798 /** Causes the function template to inherit from a parent function template.*/
1799 void Inherit(Handle<FunctionTemplate> parent);
1800
1801 /**
1802 * A PrototypeTemplate is the template used to create the prototype object
1803 * of the function created by this template.
1804 */
1805 Local<ObjectTemplate> PrototypeTemplate();
1806
v8.team.kasperl727e9952008-09-02 14:56:44 +00001807
1808 /**
1809 * Set the class name of the FunctionTemplate. This is used for
1810 * printing objects created with the function created from the
1811 * FunctionTemplate as its constructor.
1812 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 void SetClassName(Handle<String> name);
1814
1815 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001816 * Determines whether the __proto__ accessor ignores instances of
1817 * the function template. If instances of the function template are
1818 * ignored, __proto__ skips all instances and instead returns the
1819 * next object in the prototype chain.
1820 *
1821 * Call with a value of true to make the __proto__ accessor ignore
1822 * instances of the function template. Call with a value of false
1823 * to make the __proto__ accessor not ignore instances of the
1824 * function template. By default, instances of a function template
1825 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 */
1827 void SetHiddenPrototype(bool value);
1828
1829 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001830 * Returns true if the given object is an instance of this function
1831 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832 */
1833 bool HasInstance(Handle<Value> object);
1834
1835 private:
1836 FunctionTemplate();
1837 void AddInstancePropertyAccessor(Handle<String> name,
1838 AccessorGetter getter,
1839 AccessorSetter setter,
1840 Handle<Value> data,
1841 AccessControl settings,
1842 PropertyAttribute attributes);
1843 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1844 NamedPropertySetter setter,
1845 NamedPropertyQuery query,
1846 NamedPropertyDeleter remover,
1847 NamedPropertyEnumerator enumerator,
1848 Handle<Value> data);
1849 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1850 IndexedPropertySetter setter,
1851 IndexedPropertyQuery query,
1852 IndexedPropertyDeleter remover,
1853 IndexedPropertyEnumerator enumerator,
1854 Handle<Value> data);
1855 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1856 Handle<Value> data);
1857
1858 friend class Context;
1859 friend class ObjectTemplate;
1860};
1861
1862
1863/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001864 * An ObjectTemplate is used to create objects at runtime.
1865 *
1866 * Properties added to an ObjectTemplate are added to each object
1867 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001869class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001871 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001873
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 /** Creates a new instance of this template.*/
1875 Local<Object> NewInstance();
1876
1877 /**
1878 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001879 *
1880 * Whenever the property with the given name is accessed on objects
1881 * created from this ObjectTemplate the getter and setter callbacks
1882 * are called instead of getting and setting the property directly
1883 * on the JavaScript object.
1884 *
1885 * \param name The name of the property for which an accessor is added.
1886 * \param getter The callback to invoke when getting the property.
1887 * \param setter The callback to invoke when setting the property.
1888 * \param data A piece of data that will be passed to the getter and setter
1889 * callbacks whenever they are invoked.
1890 * \param settings Access control settings for the accessor. This is a bit
1891 * field consisting of one of more of
1892 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1893 * The default is to not allow cross-context access.
1894 * ALL_CAN_READ means that all cross-context reads are allowed.
1895 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1896 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1897 * cross-context access.
1898 * \param attribute The attributes of the property for which an accessor
1899 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 */
1901 void SetAccessor(Handle<String> name,
1902 AccessorGetter getter,
1903 AccessorSetter setter = 0,
1904 Handle<Value> data = Handle<Value>(),
1905 AccessControl settings = DEFAULT,
1906 PropertyAttribute attribute = None);
1907
1908 /**
1909 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001910 *
1911 * Whenever a named property is accessed on objects created from
1912 * this object template, the provided callback is invoked instead of
1913 * accessing the property directly on the JavaScript object.
1914 *
1915 * \param getter The callback to invoke when getting a property.
1916 * \param setter The callback to invoke when setting a property.
1917 * \param query The callback to invoke to check is an object has a property.
1918 * \param deleter The callback to invoke when deleting a property.
1919 * \param enumerator The callback to invoke to enumerate all the named
1920 * properties of an object.
1921 * \param data A piece of data that will be passed to the callbacks
1922 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 */
1924 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1925 NamedPropertySetter setter = 0,
1926 NamedPropertyQuery query = 0,
1927 NamedPropertyDeleter deleter = 0,
1928 NamedPropertyEnumerator enumerator = 0,
1929 Handle<Value> data = Handle<Value>());
1930
1931 /**
1932 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001933 *
1934 * Whenever an indexed property is accessed on objects created from
1935 * this object template, the provided callback is invoked instead of
1936 * accessing the property directly on the JavaScript object.
1937 *
1938 * \param getter The callback to invoke when getting a property.
1939 * \param setter The callback to invoke when setting a property.
1940 * \param query The callback to invoke to check is an object has a property.
1941 * \param deleter The callback to invoke when deleting a property.
1942 * \param enumerator The callback to invoke to enumerate all the indexed
1943 * properties of an object.
1944 * \param data A piece of data that will be passed to the callbacks
1945 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 */
1947 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1948 IndexedPropertySetter setter = 0,
1949 IndexedPropertyQuery query = 0,
1950 IndexedPropertyDeleter deleter = 0,
1951 IndexedPropertyEnumerator enumerator = 0,
1952 Handle<Value> data = Handle<Value>());
1953 /**
1954 * Sets the callback to be used when calling instances created from
1955 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001956 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 * function.
1958 */
1959 void SetCallAsFunctionHandler(InvocationCallback callback,
1960 Handle<Value> data = Handle<Value>());
1961
v8.team.kasperl727e9952008-09-02 14:56:44 +00001962 /**
1963 * Mark object instances of the template as undetectable.
1964 *
1965 * In many ways, undetectable objects behave as though they are not
1966 * there. They behave like 'undefined' in conditionals and when
1967 * printed. However, properties can be accessed and called as on
1968 * normal objects.
1969 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 void MarkAsUndetectable();
1971
v8.team.kasperl727e9952008-09-02 14:56:44 +00001972 /**
1973 * Sets access check callbacks on the object template.
1974 *
1975 * When accessing properties on instances of this object template,
1976 * the access check callback will be called to determine whether or
1977 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001978 * The last parameter specifies whether access checks are turned
1979 * on by default on instances. If access checks are off by default,
1980 * they can be turned on on individual instances by calling
1981 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001982 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1984 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001985 Handle<Value> data = Handle<Value>(),
1986 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987
kasper.lund212ac232008-07-16 07:07:30 +00001988 /**
1989 * Gets the number of internal fields for objects generated from
1990 * this template.
1991 */
1992 int InternalFieldCount();
1993
1994 /**
1995 * Sets the number of internal fields for objects generated from
1996 * this template.
1997 */
1998 void SetInternalFieldCount(int value);
1999
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 private:
2001 ObjectTemplate();
2002 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2003 friend class FunctionTemplate;
2004};
2005
2006
2007/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002008 * A Signature specifies which receivers and arguments a function can
2009 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002011class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 public:
2013 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2014 Handle<FunctionTemplate>(),
2015 int argc = 0,
2016 Handle<FunctionTemplate> argv[] = 0);
2017 private:
2018 Signature();
2019};
2020
2021
2022/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002023 * A utility for determining the type of objects based on the template
2024 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002026class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 public:
2028 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2029 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2030 int match(Handle<Value> value);
2031 private:
2032 TypeSwitch();
2033};
2034
2035
2036// --- E x t e n s i o n s ---
2037
2038
2039/**
2040 * Ignore
2041 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002042class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 public:
2044 Extension(const char* name,
2045 const char* source = 0,
2046 int dep_count = 0,
2047 const char** deps = 0);
2048 virtual ~Extension() { }
2049 virtual v8::Handle<v8::FunctionTemplate>
2050 GetNativeFunction(v8::Handle<v8::String> name) {
2051 return v8::Handle<v8::FunctionTemplate>();
2052 }
2053
2054 const char* name() { return name_; }
2055 const char* source() { return source_; }
2056 int dependency_count() { return dep_count_; }
2057 const char** dependencies() { return deps_; }
2058 void set_auto_enable(bool value) { auto_enable_ = value; }
2059 bool auto_enable() { return auto_enable_; }
2060
2061 private:
2062 const char* name_;
2063 const char* source_;
2064 int dep_count_;
2065 const char** deps_;
2066 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002067
2068 // Disallow copying and assigning.
2069 Extension(const Extension&);
2070 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071};
2072
2073
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002074void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075
2076
2077/**
2078 * Ignore
2079 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002080class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 public:
2082 inline DeclareExtension(Extension* extension) {
2083 RegisterExtension(extension);
2084 }
2085};
2086
2087
2088// --- S t a t i c s ---
2089
2090
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002091Handle<Primitive> V8EXPORT Undefined();
2092Handle<Primitive> V8EXPORT Null();
2093Handle<Boolean> V8EXPORT True();
2094Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095
2096
2097/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002098 * A set of constraints that specifies the limits of the runtime's memory use.
2099 * You must set the heap size before initializing the VM - the size cannot be
2100 * adjusted after the VM is initialized.
2101 *
2102 * If you are using threads then you should hold the V8::Locker lock while
2103 * setting the stack limit and you must set a non-default stack limit separately
2104 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002106class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 public:
2108 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002109 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002111 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002113 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002114 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2116 private:
2117 int max_young_space_size_;
2118 int max_old_space_size_;
2119 uint32_t* stack_limit_;
2120};
2121
2122
2123bool SetResourceConstraints(ResourceConstraints* constraints);
2124
2125
2126// --- E x c e p t i o n s ---
2127
2128
2129typedef void (*FatalErrorCallback)(const char* location, const char* message);
2130
2131
2132typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2133
2134
2135/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002136 * Schedules an exception to be thrown when returning to JavaScript. When an
2137 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002139 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002141Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142
2143/**
2144 * Create new error objects by calling the corresponding error object
2145 * constructor with the message.
2146 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002147class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 public:
2149 static Local<Value> RangeError(Handle<String> message);
2150 static Local<Value> ReferenceError(Handle<String> message);
2151 static Local<Value> SyntaxError(Handle<String> message);
2152 static Local<Value> TypeError(Handle<String> message);
2153 static Local<Value> Error(Handle<String> message);
2154};
2155
2156
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002157// --- 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 +00002158
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002159typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002161typedef void* (*CreateHistogramCallback)(const char* name,
2162 int min,
2163 int max,
2164 size_t buckets);
2165
2166typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2167
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168// --- 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 ---
2169typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2170 AccessType type,
2171 Local<Value> data);
2172
2173// --- 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
2174
2175/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002176 * Applications can register callback functions which will be called
2177 * before and after a garbage collection. Allocations are not
2178 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002179 * objects (set or delete properties for example) since it is possible
2180 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002182enum GCType {
2183 kGCTypeScavenge = 1 << 0,
2184 kGCTypeMarkSweepCompact = 1 << 1,
2185 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2186};
2187
2188enum GCCallbackFlags {
2189 kNoGCCallbackFlags = 0,
2190 kGCCallbackFlagCompacted = 1 << 0
2191};
2192
2193typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2194typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196typedef void (*GCCallback)();
2197
2198
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002199// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200
2201/**
2202 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002203 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205typedef Persistent<Context> (*ContextGenerator)();
2206
2207
2208/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002209 * Profiler modules.
2210 *
2211 * In V8, profiler consists of several modules: CPU profiler, and different
2212 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002213 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2214 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002215 */
2216enum ProfilerModules {
2217 PROFILER_MODULE_NONE = 0,
2218 PROFILER_MODULE_CPU = 1,
2219 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002220 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2221 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002222};
2223
2224
2225/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002226 * Collection of V8 heap information.
2227 *
2228 * Instances of this class can be passed to v8::V8::HeapStatistics to
2229 * get heap statistics from V8.
2230 */
2231class V8EXPORT HeapStatistics {
2232 public:
2233 HeapStatistics();
2234 size_t total_heap_size() { return total_heap_size_; }
2235 size_t used_heap_size() { return used_heap_size_; }
2236
2237 private:
2238 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2239 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2240
2241 size_t total_heap_size_;
2242 size_t used_heap_size_;
2243
2244 friend class V8;
2245};
2246
2247
2248/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 * Container class for static utility functions.
2250 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002251class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002253 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 static void SetFatalErrorHandler(FatalErrorCallback that);
2255
v8.team.kasperl727e9952008-09-02 14:56:44 +00002256 /**
2257 * Ignore out-of-memory exceptions.
2258 *
2259 * V8 running out of memory is treated as a fatal error by default.
2260 * This means that the fatal error handler is called and that V8 is
2261 * terminated.
2262 *
2263 * IgnoreOutOfMemoryException can be used to not treat a
2264 * out-of-memory situation as a fatal error. This way, the contexts
2265 * that did not cause the out of memory problem might be able to
2266 * continue execution.
2267 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 static void IgnoreOutOfMemoryException();
2269
v8.team.kasperl727e9952008-09-02 14:56:44 +00002270 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002271 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002272 * fatal errors such as out-of-memory situations.
2273 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 static bool IsDead();
2275
2276 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002277 * Adds a message listener.
2278 *
2279 * The same message listener can be added more than once and it that
2280 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 */
2282 static bool AddMessageListener(MessageCallback that,
2283 Handle<Value> data = Handle<Value>());
2284
2285 /**
2286 * Remove all message listeners from the specified callback function.
2287 */
2288 static void RemoveMessageListeners(MessageCallback that);
2289
2290 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002291 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 */
2293 static void SetFlagsFromString(const char* str, int length);
2294
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002295 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002296 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002297 */
2298 static void SetFlagsFromCommandLine(int* argc,
2299 char** argv,
2300 bool remove_flags);
2301
kasper.lund7276f142008-07-30 08:49:36 +00002302 /** Get the version string. */
2303 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304
2305 /**
2306 * Enables the host application to provide a mechanism for recording
2307 * statistics counters.
2308 */
2309 static void SetCounterFunction(CounterLookupCallback);
2310
2311 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002312 * Enables the host application to provide a mechanism for recording
2313 * histograms. The CreateHistogram function returns a
2314 * histogram which will later be passed to the AddHistogramSample
2315 * function.
2316 */
2317 static void SetCreateHistogramFunction(CreateHistogramCallback);
2318 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2319
2320 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 * Enables the computation of a sliding window of states. The sliding
2322 * window information is recorded in statistics counters.
2323 */
2324 static void EnableSlidingStateWindow();
2325
2326 /** Callback function for reporting failed access checks.*/
2327 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2328
2329 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002330 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002331 * garbage collection. Allocations are not allowed in the
2332 * callback function, you therefore cannot manipulate objects (set
2333 * or delete properties for example) since it is possible such
2334 * operations will result in the allocation of objects. It is possible
2335 * to specify the GCType filter for your callback. But it is not possible to
2336 * register the same callback function two times with different
2337 * GCType filters.
2338 */
2339 static void AddGCPrologueCallback(
2340 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2341
2342 /**
2343 * This function removes callback which was installed by
2344 * AddGCPrologueCallback function.
2345 */
2346 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2347
2348 /**
2349 * The function is deprecated. Please use AddGCPrologueCallback instead.
2350 * Enables the host application to receive a notification before a
2351 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002352 * callback function, you therefore cannot manipulate objects (set
2353 * or delete properties for example) since it is possible such
2354 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355 */
2356 static void SetGlobalGCPrologueCallback(GCCallback);
2357
2358 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002359 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002360 * garbage collection. Allocations are not allowed in the
2361 * callback function, you therefore cannot manipulate objects (set
2362 * or delete properties for example) since it is possible such
2363 * operations will result in the allocation of objects. It is possible
2364 * to specify the GCType filter for your callback. But it is not possible to
2365 * register the same callback function two times with different
2366 * GCType filters.
2367 */
2368 static void AddGCEpilogueCallback(
2369 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2370
2371 /**
2372 * This function removes callback which was installed by
2373 * AddGCEpilogueCallback function.
2374 */
2375 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2376
2377 /**
2378 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2379 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002380 * major garbage collection. Allocations are not allowed in the
2381 * callback function, you therefore cannot manipulate objects (set
2382 * or delete properties for example) since it is possible such
2383 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 */
2385 static void SetGlobalGCEpilogueCallback(GCCallback);
2386
2387 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002388 * Allows the host application to group objects together. If one
2389 * object in the group is alive, all objects in the group are alive.
2390 * After each garbage collection, object groups are removed. It is
2391 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002392 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002393 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002395 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396
2397 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002398 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002399 * initialize from scratch. This function is called implicitly if
2400 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 */
2402 static bool Initialize();
2403
kasper.lund7276f142008-07-30 08:49:36 +00002404 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002405 * Adjusts the amount of registered external memory. Used to give
2406 * V8 an indication of the amount of externally allocated memory
2407 * that is kept alive by JavaScript objects. V8 uses this to decide
2408 * when to perform global garbage collections. Registering
2409 * externally allocated memory will trigger global garbage
2410 * collections more often than otherwise in an attempt to garbage
2411 * collect the JavaScript objects keeping the externally allocated
2412 * memory alive.
2413 *
2414 * \param change_in_bytes the change in externally allocated memory
2415 * that is kept alive by JavaScript objects.
2416 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002417 */
2418 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2419
iposva@chromium.org245aa852009-02-10 00:49:54 +00002420 /**
2421 * Suspends recording of tick samples in the profiler.
2422 * When the V8 profiling mode is enabled (usually via command line
2423 * switches) this function suspends recording of tick samples.
2424 * Profiling ticks are discarded until ResumeProfiler() is called.
2425 *
2426 * See also the --prof and --prof_auto command line switches to
2427 * enable V8 profiling.
2428 */
2429 static void PauseProfiler();
2430
2431 /**
2432 * Resumes recording of tick samples in the profiler.
2433 * See also PauseProfiler().
2434 */
2435 static void ResumeProfiler();
2436
ager@chromium.org41826e72009-03-30 13:30:57 +00002437 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002438 * Return whether profiler is currently paused.
2439 */
2440 static bool IsProfilerPaused();
2441
2442 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002443 * Resumes specified profiler modules. Can be called several times to
2444 * mark the opening of a profiler events block with the given tag.
2445 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002446 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2447 * See ProfilerModules enum.
2448 *
2449 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002450 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002451 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002452 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002453
2454 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002455 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2456 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2457 * same tag value. There is no need for blocks to be properly nested.
2458 * The profiler is paused when the last opened block is closed.
2459 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002460 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2461 * See ProfilerModules enum.
2462 *
2463 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002464 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002465 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002466 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002467
2468 /**
2469 * Returns active (resumed) profiler modules.
2470 * See ProfilerModules enum.
2471 *
2472 * \returns active profiler modules.
2473 */
2474 static int GetActiveProfilerModules();
2475
2476 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002477 * If logging is performed into a memory buffer (via --logfile=*), allows to
2478 * retrieve previously written messages. This can be used for retrieving
2479 * profiler log data in the application. This function is thread-safe.
2480 *
2481 * Caller provides a destination buffer that must exist during GetLogLines
2482 * call. Only whole log lines are copied into the buffer.
2483 *
2484 * \param from_pos specified a point in a buffer to read from, 0 is the
2485 * beginning of a buffer. It is assumed that caller updates its current
2486 * position using returned size value from the previous call.
2487 * \param dest_buf destination buffer for log data.
2488 * \param max_size size of the destination buffer.
2489 * \returns actual size of log data copied into buffer.
2490 */
2491 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2492
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002493 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002494 * The minimum allowed size for a log lines buffer. If the size of
2495 * the buffer given will not be enough to hold a line of the maximum
2496 * length, an attempt to find a log line end in GetLogLines will
2497 * fail, and an empty result will be returned.
2498 */
2499 static const int kMinimumSizeForLogLinesBuffer = 2048;
2500
2501 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002502 * Retrieve the V8 thread id of the calling thread.
2503 *
2504 * The thread id for a thread should only be retrieved after the V8
2505 * lock has been acquired with a Locker object with that thread.
2506 */
2507 static int GetCurrentThreadId();
2508
2509 /**
2510 * Forcefully terminate execution of a JavaScript thread. This can
2511 * be used to terminate long-running scripts.
2512 *
2513 * TerminateExecution should only be called when then V8 lock has
2514 * been acquired with a Locker object. Therefore, in order to be
2515 * able to terminate long-running threads, preemption must be
2516 * enabled to allow the user of TerminateExecution to acquire the
2517 * lock.
2518 *
2519 * The termination is achieved by throwing an exception that is
2520 * uncatchable by JavaScript exception handlers. Termination
2521 * exceptions act as if they were caught by a C++ TryCatch exception
2522 * handlers. If forceful termination is used, any C++ TryCatch
2523 * exception handler that catches an exception should check if that
2524 * exception is a termination exception and immediately return if
2525 * that is the case. Returning immediately in that case will
2526 * continue the propagation of the termination exception if needed.
2527 *
2528 * The thread id passed to TerminateExecution must have been
2529 * obtained by calling GetCurrentThreadId on the thread in question.
2530 *
2531 * \param thread_id The thread id of the thread to terminate.
2532 */
2533 static void TerminateExecution(int thread_id);
2534
2535 /**
2536 * Forcefully terminate the current thread of JavaScript execution.
2537 *
2538 * This method can be used by any thread even if that thread has not
2539 * acquired the V8 lock with a Locker object.
2540 */
2541 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002542
2543 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00002544 * Is V8 terminating JavaScript execution.
2545 *
2546 * Returns true if JavaScript execution is currently terminating
2547 * because of a call to TerminateExecution. In that case there are
2548 * still JavaScript frames on the stack and the termination
2549 * exception is still active.
2550 */
2551 static bool IsExecutionTerminating();
2552
2553 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002554 * Releases any resources used by v8 and stops any utility threads
2555 * that may be running. Note that disposing v8 is permanent, it
2556 * cannot be reinitialized.
2557 *
2558 * It should generally not be necessary to dispose v8 before exiting
2559 * a process, this should happen automatically. It is only necessary
2560 * to use if the process needs the resources taken up by v8.
2561 */
2562 static bool Dispose();
2563
ager@chromium.org3811b432009-10-28 14:53:37 +00002564 /**
2565 * Get statistics about the heap memory usage.
2566 */
2567 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002568
2569 /**
2570 * Optional notification that the embedder is idle.
2571 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002572 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002573 * Returns true if the embedder should stop calling IdleNotification
2574 * until real work has been done. This indicates that V8 has done
2575 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002576 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002577 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002578
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002579 /**
2580 * Optional notification that the system is running low on memory.
2581 * V8 uses these notifications to attempt to free memory.
2582 */
2583 static void LowMemoryNotification();
2584
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002585 /**
2586 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002587 * these notifications to guide the GC heuristic. Returns the number
2588 * of context disposals - including this one - since the last time
2589 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002590 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002591 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593 private:
2594 V8();
2595
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002596 static internal::Object** GlobalizeReference(internal::Object** handle);
2597 static void DisposeGlobal(internal::Object** global_handle);
2598 static void MakeWeak(internal::Object** global_handle,
2599 void* data,
2600 WeakReferenceCallback);
2601 static void ClearWeak(internal::Object** global_handle);
2602 static bool IsGlobalNearDeath(internal::Object** global_handle);
2603 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604
2605 template <class T> friend class Handle;
2606 template <class T> friend class Local;
2607 template <class T> friend class Persistent;
2608 friend class Context;
2609};
2610
2611
2612/**
2613 * An external exception handler.
2614 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002615class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 public:
2617
2618 /**
2619 * Creates a new try/catch block and registers it with v8.
2620 */
2621 TryCatch();
2622
2623 /**
2624 * Unregisters and deletes this try/catch block.
2625 */
2626 ~TryCatch();
2627
2628 /**
2629 * Returns true if an exception has been caught by this try/catch block.
2630 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002631 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632
2633 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002634 * For certain types of exceptions, it makes no sense to continue
2635 * execution.
2636 *
2637 * Currently, the only type of exception that can be caught by a
2638 * TryCatch handler and for which it does not make sense to continue
2639 * is termination exception. Such exceptions are thrown when the
2640 * TerminateExecution methods are called to terminate a long-running
2641 * script.
2642 *
2643 * If CanContinue returns false, the correct action is to perform
2644 * any C++ cleanup needed and then return.
2645 */
2646 bool CanContinue() const;
2647
2648 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002649 * Throws the exception caught by this TryCatch in a way that avoids
2650 * it being caught again by this same TryCatch. As with ThrowException
2651 * it is illegal to execute any JavaScript operations after calling
2652 * ReThrow; the caller must return immediately to where the exception
2653 * is caught.
2654 */
2655 Handle<Value> ReThrow();
2656
2657 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 * Returns the exception caught by this try/catch block. If no exception has
2659 * been caught an empty handle is returned.
2660 *
2661 * The returned handle is valid until this TryCatch block has been destroyed.
2662 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002663 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664
2665 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002666 * Returns the .stack property of the thrown object. If no .stack
2667 * property is present an empty handle is returned.
2668 */
2669 Local<Value> StackTrace() const;
2670
2671 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002672 * Returns the message associated with this exception. If there is
2673 * no message associated an empty handle is returned.
2674 *
2675 * The returned handle is valid until this TryCatch block has been
2676 * destroyed.
2677 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002678 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002679
2680 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 * Clears any exceptions that may have been caught by this try/catch block.
2682 * After this method has been called, HasCaught() will return false.
2683 *
2684 * It is not necessary to clear a try/catch block before using it again; if
2685 * another exception is thrown the previously caught exception will just be
2686 * overwritten. However, it is often a good idea since it makes it easier
2687 * to determine which operation threw a given exception.
2688 */
2689 void Reset();
2690
v8.team.kasperl727e9952008-09-02 14:56:44 +00002691 /**
2692 * Set verbosity of the external exception handler.
2693 *
2694 * By default, exceptions that are caught by an external exception
2695 * handler are not reported. Call SetVerbose with true on an
2696 * external exception handler to have exceptions caught by the
2697 * handler reported as if they were not caught.
2698 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 void SetVerbose(bool value);
2700
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002701 /**
2702 * Set whether or not this TryCatch should capture a Message object
2703 * which holds source information about where the exception
2704 * occurred. True by default.
2705 */
2706 void SetCaptureMessage(bool value);
2707
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002708 private:
2709 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002711 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002712 bool is_verbose_ : 1;
2713 bool can_continue_ : 1;
2714 bool capture_message_ : 1;
2715 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002716
2717 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718};
2719
2720
2721// --- C o n t e x t ---
2722
2723
2724/**
2725 * Ignore
2726 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002727class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728 public:
2729 ExtensionConfiguration(int name_count, const char* names[])
2730 : name_count_(name_count), names_(names) { }
2731 private:
2732 friend class ImplementationUtilities;
2733 int name_count_;
2734 const char** names_;
2735};
2736
2737
2738/**
2739 * A sandboxed execution context with its own set of built-in objects
2740 * and functions.
2741 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002742class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002744 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002745 Local<Object> Global();
2746
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002747 /**
2748 * Detaches the global object from its context before
2749 * the global object can be reused to create a new context.
2750 */
2751 void DetachGlobal();
2752
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002753 /**
2754 * Reattaches a global object to a context. This can be used to
2755 * restore the connection between a global object and a context
2756 * after DetachGlobal has been called.
2757 *
2758 * \param global_object The global object to reattach to the
2759 * context. For this to work, the global object must be the global
2760 * object that was associated with this context before a call to
2761 * DetachGlobal.
2762 */
2763 void ReattachGlobal(Handle<Object> global_object);
2764
v8.team.kasperl727e9952008-09-02 14:56:44 +00002765 /** Creates a new context. */
2766 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002767 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00002768 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2769 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770
kasper.lund44510672008-07-25 07:37:58 +00002771 /** Returns the last entered context. */
2772 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773
kasper.lund44510672008-07-25 07:37:58 +00002774 /** Returns the context that is on the top of the stack. */
2775 static Local<Context> GetCurrent();
2776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002778 * Returns the context of the calling JavaScript code. That is the
2779 * context of the top-most JavaScript frame. If there are no
2780 * JavaScript frames an empty handle is returned.
2781 */
2782 static Local<Context> GetCalling();
2783
2784 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785 * Sets the security token for the context. To access an object in
2786 * another context, the security tokens must match.
2787 */
2788 void SetSecurityToken(Handle<Value> token);
2789
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002790 /** Restores the security token to the default value. */
2791 void UseDefaultSecurityToken();
2792
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 /** Returns the security token of this context.*/
2794 Handle<Value> GetSecurityToken();
2795
v8.team.kasperl727e9952008-09-02 14:56:44 +00002796 /**
2797 * Enter this context. After entering a context, all code compiled
2798 * and run is compiled and run in this context. If another context
2799 * is already entered, this old context is saved so it can be
2800 * restored when the new context is exited.
2801 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002803
2804 /**
2805 * Exit this context. Exiting the current context restores the
2806 * context that was in place when entering the current context.
2807 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002808 void Exit();
2809
v8.team.kasperl727e9952008-09-02 14:56:44 +00002810 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 bool HasOutOfMemoryException();
2812
v8.team.kasperl727e9952008-09-02 14:56:44 +00002813 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002814 static bool InContext();
2815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002817 * Associate an additional data object with the context. This is mainly used
2818 * with the debugger to provide additional information on the context through
2819 * the debugger API.
2820 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00002821 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00002822 Local<Value> GetData();
2823
2824 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825 * Stack-allocated class which sets the execution context for all
2826 * operations executed within a local scope.
2827 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002828 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829 public:
2830 inline Scope(Handle<Context> context) : context_(context) {
2831 context_->Enter();
2832 }
2833 inline ~Scope() { context_->Exit(); }
2834 private:
2835 Handle<Context> context_;
2836 };
2837
2838 private:
2839 friend class Value;
2840 friend class Script;
2841 friend class Object;
2842 friend class Function;
2843};
2844
2845
2846/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002847 * Multiple threads in V8 are allowed, but only one thread at a time
2848 * is allowed to use V8. The definition of 'using V8' includes
2849 * accessing handles or holding onto object pointers obtained from V8
2850 * handles. It is up to the user of V8 to ensure (perhaps with
2851 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852 *
2853 * If you wish to start using V8 in a thread you can do this by constructing
2854 * a v8::Locker object. After the code using V8 has completed for the
2855 * current thread you can call the destructor. This can be combined
2856 * with C++ scope-based construction as follows:
2857 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002858 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002859 * ...
2860 * {
2861 * v8::Locker locker;
2862 * ...
2863 * // Code using V8 goes here.
2864 * ...
2865 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002866 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867 *
2868 * If you wish to stop using V8 in a thread A you can do this by either
2869 * by destroying the v8::Locker object as above or by constructing a
2870 * v8::Unlocker object:
2871 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002872 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873 * {
2874 * v8::Unlocker unlocker;
2875 * ...
2876 * // Code not using V8 goes here while V8 can run in another thread.
2877 * ...
2878 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002879 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 *
2881 * The Unlocker object is intended for use in a long-running callback
2882 * from V8, where you want to release the V8 lock for other threads to
2883 * use.
2884 *
2885 * The v8::Locker is a recursive lock. That is, you can lock more than
2886 * once in a given thread. This can be useful if you have code that can
2887 * be called either from code that holds the lock or from code that does
2888 * not. The Unlocker is not recursive so you can not have several
2889 * Unlockers on the stack at once, and you can not use an Unlocker in a
2890 * thread that is not inside a Locker's scope.
2891 *
2892 * An unlocker will unlock several lockers if it has to and reinstate
2893 * the correct depth of locking on its destruction. eg.:
2894 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002895 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002896 * // V8 not locked.
2897 * {
2898 * v8::Locker locker;
2899 * // V8 locked.
2900 * {
2901 * v8::Locker another_locker;
2902 * // V8 still locked (2 levels).
2903 * {
2904 * v8::Unlocker unlocker;
2905 * // V8 not locked.
2906 * }
2907 * // V8 locked again (2 levels).
2908 * }
2909 * // V8 still locked (1 level).
2910 * }
2911 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002912 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002913 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002914class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915 public:
2916 Unlocker();
2917 ~Unlocker();
2918};
2919
2920
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002921class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922 public:
2923 Locker();
2924 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002925
2926 /**
2927 * Start preemption.
2928 *
2929 * When preemption is started, a timer is fired every n milli seconds
2930 * that will switch between multiple threads that are in contention
2931 * for the V8 lock.
2932 */
2933 static void StartPreemption(int every_n_ms);
2934
2935 /**
2936 * Stop preemption.
2937 */
2938 static void StopPreemption();
2939
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002940 /**
2941 * Returns whether or not the locker is locked by the current thread.
2942 */
2943 static bool IsLocked();
2944
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002945 /**
2946 * Returns whether v8::Locker is being used by this V8 instance.
2947 */
2948 static bool IsActive() { return active_; }
2949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950 private:
2951 bool has_lock_;
2952 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002953
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002954 static bool active_;
2955
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002956 // Disallow copying and assigning.
2957 Locker(const Locker&);
2958 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959};
2960
2961
2962
2963// --- I m p l e m e n t a t i o n ---
2964
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002965
2966namespace internal {
2967
2968
2969// Tag information for HeapObject.
2970const int kHeapObjectTag = 1;
2971const int kHeapObjectTagSize = 2;
2972const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2973
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002974// Tag information for Smi.
2975const int kSmiTag = 0;
2976const int kSmiTagSize = 1;
2977const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2978
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002979template <size_t ptr_size> struct SmiConstants;
2980
2981// Smi constants for 32-bit systems.
2982template <> struct SmiConstants<4> {
2983 static const int kSmiShiftSize = 0;
2984 static const int kSmiValueSize = 31;
2985 static inline int SmiToInt(internal::Object* value) {
2986 int shift_bits = kSmiTagSize + kSmiShiftSize;
2987 // Throw away top 32 bits and shift down (requires >> to be sign extending).
2988 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
2989 }
2990};
2991
2992// Smi constants for 64-bit systems.
2993template <> struct SmiConstants<8> {
2994 static const int kSmiShiftSize = 31;
2995 static const int kSmiValueSize = 32;
2996 static inline int SmiToInt(internal::Object* value) {
2997 int shift_bits = kSmiTagSize + kSmiShiftSize;
2998 // Shift down and throw away top 32 bits.
2999 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3000 }
3001};
3002
3003const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
3004const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003005
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003006template <size_t ptr_size> struct InternalConstants;
3007
3008// Internal constants for 32-bit systems.
3009template <> struct InternalConstants<4> {
3010 static const int kStringResourceOffset = 3 * sizeof(void*);
3011};
3012
3013// Internal constants for 64-bit systems.
3014template <> struct InternalConstants<8> {
3015 static const int kStringResourceOffset = 2 * sizeof(void*);
3016};
3017
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003018/**
3019 * This class exports constants and functionality from within v8 that
3020 * is necessary to implement inline functions in the v8 api. Don't
3021 * depend on functions and constants defined here.
3022 */
3023class Internals {
3024 public:
3025
3026 // These values match non-compiler-dependent values defined within
3027 // the implementation of v8.
3028 static const int kHeapObjectMapOffset = 0;
3029 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003030 static const int kStringResourceOffset =
3031 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3032
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003033 static const int kProxyProxyOffset = sizeof(void*);
3034 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3035 static const int kFullStringRepresentationMask = 0x07;
3036 static const int kExternalTwoByteRepresentationTag = 0x03;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003037
3038 // These constants are compiler dependent so their values must be
3039 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00003040 V8EXPORT static int kJSObjectType;
3041 V8EXPORT static int kFirstNonstringType;
3042 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003043
3044 static inline bool HasHeapObjectTag(internal::Object* value) {
3045 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3046 kHeapObjectTag);
3047 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003048
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003049 static inline bool HasSmiTag(internal::Object* value) {
3050 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3051 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003052
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003053 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003054 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3055 }
3056
3057 static inline int GetInstanceType(internal::Object* obj) {
3058 typedef internal::Object O;
3059 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3060 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3061 }
3062
3063 static inline void* GetExternalPointer(internal::Object* obj) {
3064 if (HasSmiTag(obj)) {
3065 return obj;
3066 } else if (GetInstanceType(obj) == kProxyType) {
3067 return ReadField<void*>(obj, kProxyProxyOffset);
3068 } else {
3069 return NULL;
3070 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003071 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003072
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003073 static inline bool IsExternalTwoByteString(int instance_type) {
3074 int representation = (instance_type & kFullStringRepresentationMask);
3075 return representation == kExternalTwoByteRepresentationTag;
3076 }
3077
3078 template <typename T>
3079 static inline T ReadField(Object* ptr, int offset) {
3080 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3081 return *reinterpret_cast<T*>(addr);
3082 }
3083
3084};
3085
3086}
3087
3088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089template <class T>
3090Handle<T>::Handle() : val_(0) { }
3091
3092
3093template <class T>
3094Local<T>::Local() : Handle<T>() { }
3095
3096
3097template <class T>
3098Local<T> Local<T>::New(Handle<T> that) {
3099 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003100 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3102}
3103
3104
3105template <class T>
3106Persistent<T> Persistent<T>::New(Handle<T> that) {
3107 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003108 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3110}
3111
3112
3113template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003114bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003116 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117}
3118
3119
3120template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003121bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003122 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003123 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124}
3125
3126
3127template <class T>
3128void Persistent<T>::Dispose() {
3129 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003130 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003131}
3132
3133
3134template <class T>
3135Persistent<T>::Persistent() : Handle<T>() { }
3136
3137template <class T>
3138void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003139 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3140 parameters,
3141 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142}
3143
3144template <class T>
3145void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003146 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003147}
3148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149Local<Value> Arguments::operator[](int i) const {
3150 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3151 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3152}
3153
3154
3155Local<Function> Arguments::Callee() const {
3156 return callee_;
3157}
3158
3159
3160Local<Object> Arguments::This() const {
3161 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3162}
3163
3164
3165Local<Object> Arguments::Holder() const {
3166 return holder_;
3167}
3168
3169
3170Local<Value> Arguments::Data() const {
3171 return data_;
3172}
3173
3174
3175bool Arguments::IsConstructCall() const {
3176 return is_construct_call_;
3177}
3178
3179
3180int Arguments::Length() const {
3181 return length_;
3182}
3183
3184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003185template <class T>
3186Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003187 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3188 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003189 return Local<T>(reinterpret_cast<T*>(after));
3190}
3191
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003192Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003193 return resource_name_;
3194}
3195
3196
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003197Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 return resource_line_offset_;
3199}
3200
3201
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003202Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003203 return resource_column_offset_;
3204}
3205
3206
3207Handle<Boolean> Boolean::New(bool value) {
3208 return value ? True() : False();
3209}
3210
3211
3212void Template::Set(const char* name, v8::Handle<Data> value) {
3213 Set(v8::String::New(name), value);
3214}
3215
3216
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003217Local<Value> Object::GetInternalField(int index) {
3218#ifndef V8_ENABLE_CHECKS
3219 Local<Value> quick_result = UncheckedGetInternalField(index);
3220 if (!quick_result.IsEmpty()) return quick_result;
3221#endif
3222 return CheckedGetInternalField(index);
3223}
3224
3225
3226Local<Value> Object::UncheckedGetInternalField(int index) {
3227 typedef internal::Object O;
3228 typedef internal::Internals I;
3229 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003230 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003231 // If the object is a plain JSObject, which is the common case,
3232 // we know where to find the internal fields and can return the
3233 // value directly.
3234 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3235 O* value = I::ReadField<O*>(obj, offset);
3236 O** result = HandleScope::CreateHandle(value);
3237 return Local<Value>(reinterpret_cast<Value*>(result));
3238 } else {
3239 return Local<Value>();
3240 }
3241}
3242
3243
3244void* External::Unwrap(Handle<v8::Value> obj) {
3245#ifdef V8_ENABLE_CHECKS
3246 return FullUnwrap(obj);
3247#else
3248 return QuickUnwrap(obj);
3249#endif
3250}
3251
3252
3253void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3254 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003255 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003256 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003257}
3258
3259
3260void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003261 typedef internal::Object O;
3262 typedef internal::Internals I;
3263
3264 O* obj = *reinterpret_cast<O**>(this);
3265
3266 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3267 // If the object is a plain JSObject, which is the common case,
3268 // we know where to find the internal fields and can return the
3269 // value directly.
3270 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3271 O* value = I::ReadField<O*>(obj, offset);
3272 return I::GetExternalPointer(value);
3273 }
3274
3275 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003276}
3277
3278
3279String* String::Cast(v8::Value* value) {
3280#ifdef V8_ENABLE_CHECKS
3281 CheckCast(value);
3282#endif
3283 return static_cast<String*>(value);
3284}
3285
3286
3287String::ExternalStringResource* String::GetExternalStringResource() const {
3288 typedef internal::Object O;
3289 typedef internal::Internals I;
3290 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003291 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003292 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003293 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3294 result = reinterpret_cast<String::ExternalStringResource*>(value);
3295 } else {
3296 result = NULL;
3297 }
3298#ifdef V8_ENABLE_CHECKS
3299 VerifyExternalStringResource(result);
3300#endif
3301 return result;
3302}
3303
3304
3305bool Value::IsString() const {
3306#ifdef V8_ENABLE_CHECKS
3307 return FullIsString();
3308#else
3309 return QuickIsString();
3310#endif
3311}
3312
3313bool Value::QuickIsString() const {
3314 typedef internal::Object O;
3315 typedef internal::Internals I;
3316 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3317 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003318 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003319}
3320
3321
3322Number* Number::Cast(v8::Value* value) {
3323#ifdef V8_ENABLE_CHECKS
3324 CheckCast(value);
3325#endif
3326 return static_cast<Number*>(value);
3327}
3328
3329
3330Integer* Integer::Cast(v8::Value* value) {
3331#ifdef V8_ENABLE_CHECKS
3332 CheckCast(value);
3333#endif
3334 return static_cast<Integer*>(value);
3335}
3336
3337
3338Date* Date::Cast(v8::Value* value) {
3339#ifdef V8_ENABLE_CHECKS
3340 CheckCast(value);
3341#endif
3342 return static_cast<Date*>(value);
3343}
3344
3345
3346Object* Object::Cast(v8::Value* value) {
3347#ifdef V8_ENABLE_CHECKS
3348 CheckCast(value);
3349#endif
3350 return static_cast<Object*>(value);
3351}
3352
3353
3354Array* Array::Cast(v8::Value* value) {
3355#ifdef V8_ENABLE_CHECKS
3356 CheckCast(value);
3357#endif
3358 return static_cast<Array*>(value);
3359}
3360
3361
3362Function* Function::Cast(v8::Value* value) {
3363#ifdef V8_ENABLE_CHECKS
3364 CheckCast(value);
3365#endif
3366 return static_cast<Function*>(value);
3367}
3368
3369
3370External* External::Cast(v8::Value* value) {
3371#ifdef V8_ENABLE_CHECKS
3372 CheckCast(value);
3373#endif
3374 return static_cast<External*>(value);
3375}
3376
3377
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003378Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003379 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003380}
3381
3382
3383Local<Object> AccessorInfo::This() const {
3384 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3385}
3386
3387
3388Local<Object> AccessorInfo::Holder() const {
3389 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3390}
3391
3392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003393/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003394 * \example shell.cc
3395 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003396 * command-line and executes them.
3397 */
3398
3399
3400/**
3401 * \example process.cc
3402 */
3403
3404
3405} // namespace v8
3406
3407
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003408#undef V8EXPORT
3409#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410#undef TYPE_CHECK
3411
3412
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003413#endif // V8_H_