blob: db739bb7b68feb10e4280bd9ad246262bf365933 [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 */
ager@chromium.org32912102009-01-16 10:38:43 +0000862 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
863 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
ager@chromium.org357bf652010-04-12 11:30:10 +0000864 int WriteUtf8(char* buffer,
865 int length = -1,
866 int* nchars_ref = NULL) const; // UTF-8
867
868 /**
869 * Flatten internal memory. Operations on the string tend to run faster
870 * after flattening especially if the string is a concatenation of many
871 * others.
872 */
873 void Flatten();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
v8.team.kasperl727e9952008-09-02 14:56:44 +0000875 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000876 * A zero length string.
877 */
878 static v8::Local<v8::String> Empty();
879
880 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000881 * Returns true if the string is external
882 */
ager@chromium.org32912102009-01-16 10:38:43 +0000883 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
v8.team.kasperl727e9952008-09-02 14:56:44 +0000885 /**
886 * Returns true if the string is both external and ascii
887 */
ager@chromium.org32912102009-01-16 10:38:43 +0000888 bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000889
890 class V8EXPORT ExternalStringResourceBase {
891 public:
892 virtual ~ExternalStringResourceBase() {}
893 protected:
894 ExternalStringResourceBase() {}
895 private:
896 // Disallow copying and assigning.
897 ExternalStringResourceBase(const ExternalStringResourceBase&);
898 void operator=(const ExternalStringResourceBase&);
899 };
900
v8.team.kasperl727e9952008-09-02 14:56:44 +0000901 /**
902 * An ExternalStringResource is a wrapper around a two-byte string
903 * buffer that resides outside V8's heap. Implement an
904 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000905 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000906 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000907 class V8EXPORT ExternalStringResource
908 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909 public:
910 /**
911 * Override the destructor to manage the life cycle of the underlying
912 * buffer.
913 */
914 virtual ~ExternalStringResource() {}
915 /** The string data from the underlying buffer.*/
916 virtual const uint16_t* data() const = 0;
917 /** The length of the string. That is, the number of two-byte characters.*/
918 virtual size_t length() const = 0;
919 protected:
920 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 };
922
923 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000924 * An ExternalAsciiStringResource is a wrapper around an ascii
925 * string buffer that resides outside V8's heap. Implement an
926 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000927 * underlying buffer. Note that the string data must be immutable
928 * and that the data must be strict 7-bit ASCII, not Latin1 or
929 * UTF-8, which would require special treatment internally in the
930 * engine and, in the case of UTF-8, do not allow efficient indexing.
931 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000932 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000934 class V8EXPORT ExternalAsciiStringResource
935 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936 public:
937 /**
938 * Override the destructor to manage the life cycle of the underlying
939 * buffer.
940 */
941 virtual ~ExternalAsciiStringResource() {}
942 /** The string data from the underlying buffer.*/
943 virtual const char* data() const = 0;
944 /** The number of ascii characters in the string.*/
945 virtual size_t length() const = 0;
946 protected:
947 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 };
949
950 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000951 * Get the ExternalStringResource for an external string. Returns
952 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000954 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955
956 /**
957 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000958 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959 */
ager@chromium.org32912102009-01-16 10:38:43 +0000960 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000962 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000963
964 /**
965 * Allocates a new string from either utf-8 encoded or ascii data.
966 * The second parameter 'length' gives the buffer length.
967 * If the data is utf-8 encoded, the caller must
968 * be careful to supply the length parameter.
969 * If it is not given, the function calls
970 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000971 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 */
973 static Local<String> New(const char* data, int length = -1);
974
975 /** Allocates a new string from utf16 data.*/
976 static Local<String> New(const uint16_t* data, int length = -1);
977
978 /** Creates a symbol. Returns one if it exists already.*/
979 static Local<String> NewSymbol(const char* data, int length = -1);
980
v8.team.kasperl727e9952008-09-02 14:56:44 +0000981 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000982 * Creates a new string by concatenating the left and the right strings
983 * passed in as parameters.
984 */
985 static Local<String> Concat(Handle<String> left, Handle<String>right);
986
987 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000988 * Creates a new external string using the data defined in the given
989 * resource. The resource is deleted when the external string is no
990 * longer live on V8's heap. The caller of this function should not
991 * delete or modify the resource. Neither should the underlying buffer be
992 * deallocated or modified except through the destructor of the
993 * external string resource.
994 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000996
ager@chromium.org6f10e412009-02-13 10:11:16 +0000997 /**
998 * Associate an external string resource with this string by transforming it
999 * in place so that existing references to this string in the JavaScript heap
1000 * will use the external string resource. The external string resource's
1001 * character contents needs to be equivalent to this string.
1002 * Returns true if the string has been changed to be an external string.
1003 * The string is not modified if the operation fails.
1004 */
1005 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006
v8.team.kasperl727e9952008-09-02 14:56:44 +00001007 /**
1008 * Creates a new external string using the ascii data defined in the given
1009 * resource. The resource is deleted when the external string is no
1010 * longer live on V8's heap. The caller of this function should not
1011 * delete or modify the resource. Neither should the underlying buffer be
1012 * deallocated or modified except through the destructor of the
1013 * external string resource.
1014 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001016
ager@chromium.org6f10e412009-02-13 10:11:16 +00001017 /**
1018 * Associate an external string resource with this string by transforming it
1019 * in place so that existing references to this string in the JavaScript heap
1020 * will use the external string resource. The external string resource's
1021 * character contents needs to be equivalent to this string.
1022 * Returns true if the string has been changed to be an external string.
1023 * The string is not modified if the operation fails.
1024 */
1025 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001027 /**
1028 * Returns true if this string can be made external.
1029 */
1030 bool CanMakeExternal();
1031
kasper.lund7276f142008-07-30 08:49:36 +00001032 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033 static Local<String> NewUndetectable(const char* data, int length = -1);
1034
kasper.lund7276f142008-07-30 08:49:36 +00001035 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
1037
1038 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001039 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001040 * you want to print the object. If conversion to a string fails
1041 * (eg. due to an exception in the toString() method of the object)
1042 * then the length() method returns 0 and the * operator returns
1043 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001044 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001045 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001046 public:
1047 explicit Utf8Value(Handle<v8::Value> obj);
1048 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001049 char* operator*() { return str_; }
1050 const char* operator*() const { return str_; }
1051 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001052 private:
1053 char* str_;
1054 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001055
1056 // Disallow copying and assigning.
1057 Utf8Value(const Utf8Value&);
1058 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001059 };
1060
1061 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 * Converts an object to an ascii string.
1063 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001064 * If conversion to a string fails (eg. due to an exception in the toString()
1065 * method of the object) then the length() method returns 0 and the * operator
1066 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001068 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001069 public:
1070 explicit AsciiValue(Handle<v8::Value> obj);
1071 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001072 char* operator*() { return str_; }
1073 const char* operator*() const { return str_; }
1074 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075 private:
1076 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001077 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001078
1079 // Disallow copying and assigning.
1080 AsciiValue(const AsciiValue&);
1081 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 };
1083
1084 /**
1085 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001086 * If conversion to a string fails (eg. due to an exception in the toString()
1087 * method of the object) then the length() method returns 0 and the * operator
1088 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001090 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091 public:
1092 explicit Value(Handle<v8::Value> obj);
1093 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001094 uint16_t* operator*() { return str_; }
1095 const uint16_t* operator*() const { return str_; }
1096 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 private:
1098 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001099 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001100
1101 // Disallow copying and assigning.
1102 Value(const Value&);
1103 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001105
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001106 private:
1107 void VerifyExternalStringResource(ExternalStringResource* val) const;
1108 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109};
1110
1111
1112/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001113 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001115class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001117 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001119 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 private:
1121 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001122 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123};
1124
1125
1126/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001127 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001129class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130 public:
1131 static Local<Integer> New(int32_t value);
ager@chromium.org3811b432009-10-28 14:53:37 +00001132 static Local<Integer> NewFromUnsigned(uint32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001133 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001134 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 private:
1136 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001137 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138};
1139
1140
1141/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001142 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001144class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001146 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147 private:
1148 Int32();
1149};
1150
1151
1152/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001153 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001155class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001157 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158 private:
1159 Uint32();
1160};
1161
1162
1163/**
1164 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1165 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001166class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 public:
1168 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001169
1170 /**
1171 * A specialization of Value::NumberValue that is more efficient
1172 * because we know the structure of this object.
1173 */
ager@chromium.org32912102009-01-16 10:38:43 +00001174 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001175
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001176 static inline Date* Cast(v8::Value* obj);
1177 private:
1178 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179};
1180
1181
1182enum PropertyAttribute {
1183 None = 0,
1184 ReadOnly = 1 << 0,
1185 DontEnum = 1 << 1,
1186 DontDelete = 1 << 2
1187};
1188
ager@chromium.org3811b432009-10-28 14:53:37 +00001189enum ExternalArrayType {
1190 kExternalByteArray = 1,
1191 kExternalUnsignedByteArray,
1192 kExternalShortArray,
1193 kExternalUnsignedShortArray,
1194 kExternalIntArray,
1195 kExternalUnsignedIntArray,
1196 kExternalFloatArray
1197};
1198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001200 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001202class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 public:
1204 bool Set(Handle<Value> key,
1205 Handle<Value> value,
1206 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001207
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001208 bool Set(uint32_t index,
1209 Handle<Value> value);
1210
ager@chromium.orge2902be2009-06-08 12:21:35 +00001211 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001212 // overriding accessors or read-only properties.
1213 //
1214 // Note that if the object has an interceptor the property will be set
1215 // locally, but since the interceptor takes precedence the local property
1216 // will only be returned if the interceptor doesn't return a value.
1217 //
1218 // Note also that this only works for named properties.
1219 bool ForceSet(Handle<Value> key,
1220 Handle<Value> value,
1221 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 Local<Value> Get(Handle<Value> key);
1224
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001225 Local<Value> Get(uint32_t index);
1226
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 // TODO(1245389): Replace the type-specific versions of these
1228 // functions with generic ones that accept a Handle<Value> key.
1229 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001232
1233 // Delete a property on this object bypassing interceptors and
1234 // ignoring dont-delete attributes.
1235 bool ForceDelete(Handle<Value> key);
1236
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 bool Delete(uint32_t index);
1240
1241 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001242 * Returns an array containing the names of the enumerable properties
1243 * of this object, including properties from prototype objects. The
1244 * array returned by this method contains the same values as would
1245 * be enumerated by a for-in statement over this object.
1246 */
1247 Local<Array> GetPropertyNames();
1248
1249 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 * Get the prototype object. This does not skip objects marked to
1251 * be skipped by __proto__ and it does not consult the security
1252 * handler.
1253 */
1254 Local<Value> GetPrototype();
1255
1256 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001257 * Set the prototype object. This does not skip objects marked to
1258 * be skipped by __proto__ and it does not consult the security
1259 * handler.
1260 */
1261 bool SetPrototype(Handle<Value> prototype);
1262
1263 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001264 * Finds an instance of the given function template in the prototype
1265 * chain.
1266 */
1267 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1268
1269 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 * Call builtin Object.prototype.toString on this object.
1271 * This is different from Value::ToString() that may call
1272 * user-defined toString function. This one does not.
1273 */
1274 Local<String> ObjectProtoToString();
1275
kasper.lund212ac232008-07-16 07:07:30 +00001276 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001278 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001279 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001280 /** Sets the value in an internal field. */
1281 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001283 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001284 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001285
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001286 /** Sets a native pointer in an internal field. */
1287 void SetPointerInInternalField(int index, void* value);
1288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 // Testers for local properties.
1290 bool HasRealNamedProperty(Handle<String> key);
1291 bool HasRealIndexedProperty(uint32_t index);
1292 bool HasRealNamedCallbackProperty(Handle<String> key);
1293
1294 /**
1295 * If result.IsEmpty() no real property was located in the prototype chain.
1296 * This means interceptors in the prototype chain are not called.
1297 */
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001298 Local<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1299
1300 /**
1301 * If result.IsEmpty() no real property was located on the object or
1302 * in the prototype chain.
1303 * This means interceptors in the prototype chain are not called.
1304 */
1305 Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306
1307 /** Tests for a named lookup interceptor.*/
1308 bool HasNamedLookupInterceptor();
1309
kasper.lund212ac232008-07-16 07:07:30 +00001310 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 bool HasIndexedLookupInterceptor();
1312
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001313 /**
1314 * Turns on access check on the object if the object is an instance of
1315 * a template that has access check callbacks. If an object has no
1316 * access check info, the object cannot be accessed by anyone.
1317 */
1318 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001319
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001320 /**
1321 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001322 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001323 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001324 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001325 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001326 */
1327 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001328
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001329 /**
1330 * Access hidden properties on JavaScript objects. These properties are
1331 * hidden from the executing JavaScript and only accessible through the V8
1332 * C++ API. Hidden properties introduced by V8 internally (for example the
1333 * identity hash) are prefixed with "v8::".
1334 */
1335 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1336 Local<Value> GetHiddenValue(Handle<String> key);
1337 bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001338
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001339 /**
1340 * Returns true if this is an instance of an api function (one
1341 * created from a function created from a function template) and has
1342 * been modified since it was created. Note that this method is
1343 * conservative and may return true for objects that haven't actually
1344 * been modified.
1345 */
1346 bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001347
1348 /**
1349 * Clone this object with a fast but shallow copy. Values will point
1350 * to the same values as the original object.
1351 */
1352 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001354 /**
1355 * Set the backing store of the indexed properties to be managed by the
1356 * embedding layer. Access to the indexed properties will follow the rules
1357 * spelled out in CanvasPixelArray.
1358 * Note: The embedding program still owns the data and needs to ensure that
1359 * the backing store is preserved while V8 has a reference.
1360 */
1361 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1362
ager@chromium.org3811b432009-10-28 14:53:37 +00001363 /**
1364 * Set the backing store of the indexed properties to be managed by the
1365 * embedding layer. Access to the indexed properties will follow the rules
1366 * spelled out for the CanvasArray subtypes in the WebGL specification.
1367 * Note: The embedding program still owns the data and needs to ensure that
1368 * the backing store is preserved while V8 has a reference.
1369 */
1370 void SetIndexedPropertiesToExternalArrayData(void* data,
1371 ExternalArrayType array_type,
1372 int number_of_elements);
1373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001375 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 private:
1377 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001378 static void CheckCast(Value* obj);
1379 Local<Value> CheckedGetInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001380 void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001381
1382 /**
1383 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001384 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001385 */
1386 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387};
1388
1389
1390/**
1391 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1392 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001393class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001395 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396
ager@chromium.org3e875802009-06-29 08:26:34 +00001397 /**
1398 * Clones an element at index |index|. Returns an empty
1399 * handle if cloning fails (for any reason).
1400 */
1401 Local<Object> CloneElementAt(uint32_t index);
1402
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001404 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 private:
1406 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001407 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408};
1409
1410
1411/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001412 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001414class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001416 Local<Object> NewInstance() const;
1417 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1419 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001420 Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001421
1422 /**
1423 * Returns zero based line number of function body and
1424 * kLineOffsetNotFound if no information available.
1425 */
1426 int GetScriptLineNumber() const;
1427 ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001428 static inline Function* Cast(Value* obj);
ager@chromium.org5c838252010-02-19 08:53:10 +00001429 static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 private:
1431 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001432 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433};
1434
1435
1436/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001437 * A JavaScript value that wraps a C++ void*. This type of value is
1438 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001440 *
1441 * The Wrap function V8 will return the most optimal Value object wrapping the
1442 * C++ void*. The type of the value is not guaranteed to be an External object
1443 * and no assumptions about its type should be made. To access the wrapped
1444 * value Unwrap should be used, all other operations on that object will lead
1445 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001447class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001449 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001450 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001451
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001453 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001454 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 private:
1456 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001457 static void CheckCast(v8::Value* obj);
1458 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1459 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001460};
1461
1462
1463// --- T e m p l a t e s ---
1464
1465
1466/**
1467 * The superclass of object and function templates.
1468 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001469class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470 public:
1471 /** Adds a property to each instance created by this template.*/
1472 void Set(Handle<String> name, Handle<Data> value,
1473 PropertyAttribute attributes = None);
1474 inline void Set(const char* name, Handle<Data> value);
1475 private:
1476 Template();
1477
1478 friend class ObjectTemplate;
1479 friend class FunctionTemplate;
1480};
1481
1482
1483/**
1484 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001485 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 * including the receiver, the number and values of arguments, and
1487 * the holder of the function.
1488 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001489class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 public:
1491 inline int Length() const;
1492 inline Local<Value> operator[](int i) const;
1493 inline Local<Function> Callee() const;
1494 inline Local<Object> This() const;
1495 inline Local<Object> Holder() const;
1496 inline bool IsConstructCall() const;
1497 inline Local<Value> Data() const;
1498 private:
1499 Arguments();
1500 friend class ImplementationUtilities;
1501 inline Arguments(Local<Value> data,
1502 Local<Object> holder,
1503 Local<Function> callee,
1504 bool is_construct_call,
1505 void** values, int length);
1506 Local<Value> data_;
1507 Local<Object> holder_;
1508 Local<Function> callee_;
1509 bool is_construct_call_;
1510 void** values_;
1511 int length_;
1512};
1513
1514
1515/**
1516 * The information passed to an accessor callback about the context
1517 * of the property access.
1518 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001519class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001521 inline AccessorInfo(internal::Object** args)
1522 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 inline Local<Value> Data() const;
1524 inline Local<Object> This() const;
1525 inline Local<Object> Holder() const;
1526 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001527 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528};
1529
1530
1531typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1532
1533typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1534
1535/**
1536 * Accessor[Getter|Setter] are used as callback functions when
1537 * setting|getting a particular property. See objectTemplate::SetAccessor.
1538 */
1539typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1540 const AccessorInfo& info);
1541
1542
1543typedef void (*AccessorSetter)(Local<String> property,
1544 Local<Value> value,
1545 const AccessorInfo& info);
1546
1547
1548/**
1549 * NamedProperty[Getter|Setter] are used as interceptors on object.
1550 * See ObjectTemplate::SetNamedPropertyHandler.
1551 */
1552typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1553 const AccessorInfo& info);
1554
1555
1556/**
1557 * Returns the value if the setter intercepts the request.
1558 * Otherwise, returns an empty handle.
1559 */
1560typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1561 Local<Value> value,
1562 const AccessorInfo& info);
1563
1564
1565/**
1566 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001567 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 */
1569typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1570 const AccessorInfo& info);
1571
1572
1573/**
1574 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001575 * The return value is true if the property could be deleted and false
1576 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 */
1578typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1579 const AccessorInfo& info);
1580
1581/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001582 * Returns an array containing the names of the properties the named
1583 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 */
1585typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1586
v8.team.kasperl727e9952008-09-02 14:56:44 +00001587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001589 * Returns the value of the property if the getter intercepts the
1590 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591 */
1592typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1593 const AccessorInfo& info);
1594
1595
1596/**
1597 * Returns the value if the setter intercepts the request.
1598 * Otherwise, returns an empty handle.
1599 */
1600typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1601 Local<Value> value,
1602 const AccessorInfo& info);
1603
1604
1605/**
1606 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001607 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 */
1609typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1610 const AccessorInfo& info);
1611
1612/**
1613 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001614 * The return value is true if the property could be deleted and false
1615 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 */
1617typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1618 const AccessorInfo& info);
1619
v8.team.kasperl727e9952008-09-02 14:56:44 +00001620/**
1621 * Returns an array containing the indices of the properties the
1622 * indexed property getter intercepts.
1623 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1625
1626
1627/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001628 * Access control specifications.
1629 *
1630 * Some accessors should be accessible across contexts. These
1631 * accessors have an explicit access control parameter which specifies
1632 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001633 *
1634 * Additionally, for security, accessors can prohibit overwriting by
1635 * accessors defined in JavaScript. For objects that have such
1636 * accessors either locally or in their prototype chain it is not
1637 * possible to overwrite the accessor by using __defineGetter__ or
1638 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 */
1640enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001641 DEFAULT = 0,
1642 ALL_CAN_READ = 1,
1643 ALL_CAN_WRITE = 1 << 1,
1644 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645};
1646
1647
1648/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001649 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 */
1651enum AccessType {
1652 ACCESS_GET,
1653 ACCESS_SET,
1654 ACCESS_HAS,
1655 ACCESS_DELETE,
1656 ACCESS_KEYS
1657};
1658
v8.team.kasperl727e9952008-09-02 14:56:44 +00001659
1660/**
1661 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001662 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001663 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001664typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 Local<Value> key,
1666 AccessType type,
1667 Local<Value> data);
1668
v8.team.kasperl727e9952008-09-02 14:56:44 +00001669
1670/**
1671 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001672 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001673 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001674typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 uint32_t index,
1676 AccessType type,
1677 Local<Value> data);
1678
1679
1680/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001681 * A FunctionTemplate is used to create functions at runtime. There
1682 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001683 * context. The lifetime of the created function is equal to the
1684 * lifetime of the context. So in case the embedder needs to create
1685 * temporary functions that can be collected using Scripts is
1686 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 *
1688 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001689 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001691 * A FunctionTemplate has a corresponding instance template which is
1692 * used to create object instances when the function is used as a
1693 * constructor. Properties added to the instance template are added to
1694 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695 *
1696 * A FunctionTemplate can have a prototype template. The prototype template
1697 * is used to create the prototype object of the function.
1698 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001699 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001701 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1703 * t->Set("func_property", v8::Number::New(1));
1704 *
1705 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1706 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1707 * proto_t->Set("proto_const", v8::Number::New(2));
1708 *
1709 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1710 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1711 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1712 * instance_t->Set("instance_property", Number::New(3));
1713 *
1714 * v8::Local<v8::Function> function = t->GetFunction();
1715 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001716 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 *
1718 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001719 * and "instance" for the instance object created above. The function
1720 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001722 * \code
1723 * func_property in function == true;
1724 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001726 * function.prototype.proto_method() invokes 'InvokeCallback'
1727 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001729 * instance instanceof function == true;
1730 * instance.instance_accessor calls 'InstanceAccessorCallback'
1731 * instance.instance_property == 3;
1732 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001734 * A FunctionTemplate can inherit from another one by calling the
1735 * FunctionTemplate::Inherit method. The following graph illustrates
1736 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001738 * \code
1739 * FunctionTemplate Parent -> Parent() . prototype -> { }
1740 * ^ ^
1741 * | Inherit(Parent) | .__proto__
1742 * | |
1743 * FunctionTemplate Child -> Child() . prototype -> { }
1744 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001746 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1747 * object of the Child() function has __proto__ pointing to the
1748 * Parent() function's prototype object. An instance of the Child
1749 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001751 * Let Parent be the FunctionTemplate initialized in the previous
1752 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001754 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 * Local<FunctionTemplate> parent = t;
1756 * Local<FunctionTemplate> child = FunctionTemplate::New();
1757 * child->Inherit(parent);
1758 *
1759 * Local<Function> child_function = child->GetFunction();
1760 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001761 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001763 * The Child function and Child instance will have the following
1764 * properties:
1765 *
1766 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001768 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001770 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001772class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 public:
1774 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001775 static Local<FunctionTemplate> New(
1776 InvocationCallback callback = 0,
1777 Handle<Value> data = Handle<Value>(),
1778 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 /** Returns the unique function instance in the current execution context.*/
1780 Local<Function> GetFunction();
1781
v8.team.kasperl727e9952008-09-02 14:56:44 +00001782 /**
1783 * Set the call-handler callback for a FunctionTemplate. This
1784 * callback is called whenever the function created from this
1785 * FunctionTemplate is called.
1786 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 void SetCallHandler(InvocationCallback callback,
1788 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789
v8.team.kasperl727e9952008-09-02 14:56:44 +00001790 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 Local<ObjectTemplate> InstanceTemplate();
1792
1793 /** Causes the function template to inherit from a parent function template.*/
1794 void Inherit(Handle<FunctionTemplate> parent);
1795
1796 /**
1797 * A PrototypeTemplate is the template used to create the prototype object
1798 * of the function created by this template.
1799 */
1800 Local<ObjectTemplate> PrototypeTemplate();
1801
v8.team.kasperl727e9952008-09-02 14:56:44 +00001802
1803 /**
1804 * Set the class name of the FunctionTemplate. This is used for
1805 * printing objects created with the function created from the
1806 * FunctionTemplate as its constructor.
1807 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 void SetClassName(Handle<String> name);
1809
1810 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001811 * Determines whether the __proto__ accessor ignores instances of
1812 * the function template. If instances of the function template are
1813 * ignored, __proto__ skips all instances and instead returns the
1814 * next object in the prototype chain.
1815 *
1816 * Call with a value of true to make the __proto__ accessor ignore
1817 * instances of the function template. Call with a value of false
1818 * to make the __proto__ accessor not ignore instances of the
1819 * function template. By default, instances of a function template
1820 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 */
1822 void SetHiddenPrototype(bool value);
1823
1824 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001825 * Returns true if the given object is an instance of this function
1826 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 */
1828 bool HasInstance(Handle<Value> object);
1829
1830 private:
1831 FunctionTemplate();
1832 void AddInstancePropertyAccessor(Handle<String> name,
1833 AccessorGetter getter,
1834 AccessorSetter setter,
1835 Handle<Value> data,
1836 AccessControl settings,
1837 PropertyAttribute attributes);
1838 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1839 NamedPropertySetter setter,
1840 NamedPropertyQuery query,
1841 NamedPropertyDeleter remover,
1842 NamedPropertyEnumerator enumerator,
1843 Handle<Value> data);
1844 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1845 IndexedPropertySetter setter,
1846 IndexedPropertyQuery query,
1847 IndexedPropertyDeleter remover,
1848 IndexedPropertyEnumerator enumerator,
1849 Handle<Value> data);
1850 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1851 Handle<Value> data);
1852
1853 friend class Context;
1854 friend class ObjectTemplate;
1855};
1856
1857
1858/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001859 * An ObjectTemplate is used to create objects at runtime.
1860 *
1861 * Properties added to an ObjectTemplate are added to each object
1862 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001864class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001866 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001868
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 /** Creates a new instance of this template.*/
1870 Local<Object> NewInstance();
1871
1872 /**
1873 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001874 *
1875 * Whenever the property with the given name is accessed on objects
1876 * created from this ObjectTemplate the getter and setter callbacks
1877 * are called instead of getting and setting the property directly
1878 * on the JavaScript object.
1879 *
1880 * \param name The name of the property for which an accessor is added.
1881 * \param getter The callback to invoke when getting the property.
1882 * \param setter The callback to invoke when setting the property.
1883 * \param data A piece of data that will be passed to the getter and setter
1884 * callbacks whenever they are invoked.
1885 * \param settings Access control settings for the accessor. This is a bit
1886 * field consisting of one of more of
1887 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1888 * The default is to not allow cross-context access.
1889 * ALL_CAN_READ means that all cross-context reads are allowed.
1890 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1891 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1892 * cross-context access.
1893 * \param attribute The attributes of the property for which an accessor
1894 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 */
1896 void SetAccessor(Handle<String> name,
1897 AccessorGetter getter,
1898 AccessorSetter setter = 0,
1899 Handle<Value> data = Handle<Value>(),
1900 AccessControl settings = DEFAULT,
1901 PropertyAttribute attribute = None);
1902
1903 /**
1904 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001905 *
1906 * Whenever a named property is accessed on objects created from
1907 * this object template, the provided callback is invoked instead of
1908 * accessing the property directly on the JavaScript object.
1909 *
1910 * \param getter The callback to invoke when getting a property.
1911 * \param setter The callback to invoke when setting a property.
1912 * \param query The callback to invoke to check is an object has a property.
1913 * \param deleter The callback to invoke when deleting a property.
1914 * \param enumerator The callback to invoke to enumerate all the named
1915 * properties of an object.
1916 * \param data A piece of data that will be passed to the callbacks
1917 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 */
1919 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1920 NamedPropertySetter setter = 0,
1921 NamedPropertyQuery query = 0,
1922 NamedPropertyDeleter deleter = 0,
1923 NamedPropertyEnumerator enumerator = 0,
1924 Handle<Value> data = Handle<Value>());
1925
1926 /**
1927 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001928 *
1929 * Whenever an indexed property is accessed on objects created from
1930 * this object template, the provided callback is invoked instead of
1931 * accessing the property directly on the JavaScript object.
1932 *
1933 * \param getter The callback to invoke when getting a property.
1934 * \param setter The callback to invoke when setting a property.
1935 * \param query The callback to invoke to check is an object has a property.
1936 * \param deleter The callback to invoke when deleting a property.
1937 * \param enumerator The callback to invoke to enumerate all the indexed
1938 * properties of an object.
1939 * \param data A piece of data that will be passed to the callbacks
1940 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 */
1942 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1943 IndexedPropertySetter setter = 0,
1944 IndexedPropertyQuery query = 0,
1945 IndexedPropertyDeleter deleter = 0,
1946 IndexedPropertyEnumerator enumerator = 0,
1947 Handle<Value> data = Handle<Value>());
1948 /**
1949 * Sets the callback to be used when calling instances created from
1950 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001951 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 * function.
1953 */
1954 void SetCallAsFunctionHandler(InvocationCallback callback,
1955 Handle<Value> data = Handle<Value>());
1956
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 /**
1958 * Mark object instances of the template as undetectable.
1959 *
1960 * In many ways, undetectable objects behave as though they are not
1961 * there. They behave like 'undefined' in conditionals and when
1962 * printed. However, properties can be accessed and called as on
1963 * normal objects.
1964 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 void MarkAsUndetectable();
1966
v8.team.kasperl727e9952008-09-02 14:56:44 +00001967 /**
1968 * Sets access check callbacks on the object template.
1969 *
1970 * When accessing properties on instances of this object template,
1971 * the access check callback will be called to determine whether or
1972 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001973 * The last parameter specifies whether access checks are turned
1974 * on by default on instances. If access checks are off by default,
1975 * they can be turned on on individual instances by calling
1976 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001977 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1979 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001980 Handle<Value> data = Handle<Value>(),
1981 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001982
kasper.lund212ac232008-07-16 07:07:30 +00001983 /**
1984 * Gets the number of internal fields for objects generated from
1985 * this template.
1986 */
1987 int InternalFieldCount();
1988
1989 /**
1990 * Sets the number of internal fields for objects generated from
1991 * this template.
1992 */
1993 void SetInternalFieldCount(int value);
1994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 private:
1996 ObjectTemplate();
1997 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1998 friend class FunctionTemplate;
1999};
2000
2001
2002/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002003 * A Signature specifies which receivers and arguments a function can
2004 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002006class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 public:
2008 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2009 Handle<FunctionTemplate>(),
2010 int argc = 0,
2011 Handle<FunctionTemplate> argv[] = 0);
2012 private:
2013 Signature();
2014};
2015
2016
2017/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002018 * A utility for determining the type of objects based on the template
2019 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002021class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 public:
2023 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2024 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2025 int match(Handle<Value> value);
2026 private:
2027 TypeSwitch();
2028};
2029
2030
2031// --- E x t e n s i o n s ---
2032
2033
2034/**
2035 * Ignore
2036 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002037class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 public:
2039 Extension(const char* name,
2040 const char* source = 0,
2041 int dep_count = 0,
2042 const char** deps = 0);
2043 virtual ~Extension() { }
2044 virtual v8::Handle<v8::FunctionTemplate>
2045 GetNativeFunction(v8::Handle<v8::String> name) {
2046 return v8::Handle<v8::FunctionTemplate>();
2047 }
2048
2049 const char* name() { return name_; }
2050 const char* source() { return source_; }
2051 int dependency_count() { return dep_count_; }
2052 const char** dependencies() { return deps_; }
2053 void set_auto_enable(bool value) { auto_enable_ = value; }
2054 bool auto_enable() { return auto_enable_; }
2055
2056 private:
2057 const char* name_;
2058 const char* source_;
2059 int dep_count_;
2060 const char** deps_;
2061 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002062
2063 // Disallow copying and assigning.
2064 Extension(const Extension&);
2065 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066};
2067
2068
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002069void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070
2071
2072/**
2073 * Ignore
2074 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002075class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 public:
2077 inline DeclareExtension(Extension* extension) {
2078 RegisterExtension(extension);
2079 }
2080};
2081
2082
2083// --- S t a t i c s ---
2084
2085
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002086Handle<Primitive> V8EXPORT Undefined();
2087Handle<Primitive> V8EXPORT Null();
2088Handle<Boolean> V8EXPORT True();
2089Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090
2091
2092/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002093 * A set of constraints that specifies the limits of the runtime's memory use.
2094 * You must set the heap size before initializing the VM - the size cannot be
2095 * adjusted after the VM is initialized.
2096 *
2097 * If you are using threads then you should hold the V8::Locker lock while
2098 * setting the stack limit and you must set a non-default stack limit separately
2099 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002101class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 public:
2103 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002104 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002106 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002108 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002109 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2111 private:
2112 int max_young_space_size_;
2113 int max_old_space_size_;
2114 uint32_t* stack_limit_;
2115};
2116
2117
2118bool SetResourceConstraints(ResourceConstraints* constraints);
2119
2120
2121// --- E x c e p t i o n s ---
2122
2123
2124typedef void (*FatalErrorCallback)(const char* location, const char* message);
2125
2126
2127typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2128
2129
2130/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002131 * Schedules an exception to be thrown when returning to JavaScript. When an
2132 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002134 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002136Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137
2138/**
2139 * Create new error objects by calling the corresponding error object
2140 * constructor with the message.
2141 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002142class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 public:
2144 static Local<Value> RangeError(Handle<String> message);
2145 static Local<Value> ReferenceError(Handle<String> message);
2146 static Local<Value> SyntaxError(Handle<String> message);
2147 static Local<Value> TypeError(Handle<String> message);
2148 static Local<Value> Error(Handle<String> message);
2149};
2150
2151
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002152// --- 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 +00002153
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002154typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002156typedef void* (*CreateHistogramCallback)(const char* name,
2157 int min,
2158 int max,
2159 size_t buckets);
2160
2161typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163// --- 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 ---
2164typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2165 AccessType type,
2166 Local<Value> data);
2167
2168// --- 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
2169
2170/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002171 * Applications can register callback functions which will be called
2172 * before and after a garbage collection. Allocations are not
2173 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002174 * objects (set or delete properties for example) since it is possible
2175 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002177enum GCType {
2178 kGCTypeScavenge = 1 << 0,
2179 kGCTypeMarkSweepCompact = 1 << 1,
2180 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2181};
2182
2183enum GCCallbackFlags {
2184 kNoGCCallbackFlags = 0,
2185 kGCCallbackFlagCompacted = 1 << 0
2186};
2187
2188typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2189typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191typedef void (*GCCallback)();
2192
2193
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002194// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195
2196/**
2197 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200typedef Persistent<Context> (*ContextGenerator)();
2201
2202
2203/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002204 * Profiler modules.
2205 *
2206 * In V8, profiler consists of several modules: CPU profiler, and different
2207 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002208 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2209 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002210 */
2211enum ProfilerModules {
2212 PROFILER_MODULE_NONE = 0,
2213 PROFILER_MODULE_CPU = 1,
2214 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002215 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2216 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002217};
2218
2219
2220/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002221 * Collection of V8 heap information.
2222 *
2223 * Instances of this class can be passed to v8::V8::HeapStatistics to
2224 * get heap statistics from V8.
2225 */
2226class V8EXPORT HeapStatistics {
2227 public:
2228 HeapStatistics();
2229 size_t total_heap_size() { return total_heap_size_; }
2230 size_t used_heap_size() { return used_heap_size_; }
2231
2232 private:
2233 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2234 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2235
2236 size_t total_heap_size_;
2237 size_t used_heap_size_;
2238
2239 friend class V8;
2240};
2241
2242
2243/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 * Container class for static utility functions.
2245 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002246class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 static void SetFatalErrorHandler(FatalErrorCallback that);
2250
v8.team.kasperl727e9952008-09-02 14:56:44 +00002251 /**
2252 * Ignore out-of-memory exceptions.
2253 *
2254 * V8 running out of memory is treated as a fatal error by default.
2255 * This means that the fatal error handler is called and that V8 is
2256 * terminated.
2257 *
2258 * IgnoreOutOfMemoryException can be used to not treat a
2259 * out-of-memory situation as a fatal error. This way, the contexts
2260 * that did not cause the out of memory problem might be able to
2261 * continue execution.
2262 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 static void IgnoreOutOfMemoryException();
2264
v8.team.kasperl727e9952008-09-02 14:56:44 +00002265 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002266 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002267 * fatal errors such as out-of-memory situations.
2268 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 static bool IsDead();
2270
2271 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002272 * Adds a message listener.
2273 *
2274 * The same message listener can be added more than once and it that
2275 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 */
2277 static bool AddMessageListener(MessageCallback that,
2278 Handle<Value> data = Handle<Value>());
2279
2280 /**
2281 * Remove all message listeners from the specified callback function.
2282 */
2283 static void RemoveMessageListeners(MessageCallback that);
2284
2285 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002286 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287 */
2288 static void SetFlagsFromString(const char* str, int length);
2289
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002290 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002291 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002292 */
2293 static void SetFlagsFromCommandLine(int* argc,
2294 char** argv,
2295 bool remove_flags);
2296
kasper.lund7276f142008-07-30 08:49:36 +00002297 /** Get the version string. */
2298 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299
2300 /**
2301 * Enables the host application to provide a mechanism for recording
2302 * statistics counters.
2303 */
2304 static void SetCounterFunction(CounterLookupCallback);
2305
2306 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002307 * Enables the host application to provide a mechanism for recording
2308 * histograms. The CreateHistogram function returns a
2309 * histogram which will later be passed to the AddHistogramSample
2310 * function.
2311 */
2312 static void SetCreateHistogramFunction(CreateHistogramCallback);
2313 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2314
2315 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 * Enables the computation of a sliding window of states. The sliding
2317 * window information is recorded in statistics counters.
2318 */
2319 static void EnableSlidingStateWindow();
2320
2321 /** Callback function for reporting failed access checks.*/
2322 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2323
2324 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002325 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002326 * garbage collection. Allocations are not allowed in the
2327 * callback function, you therefore cannot manipulate objects (set
2328 * or delete properties for example) since it is possible such
2329 * operations will result in the allocation of objects. It is possible
2330 * to specify the GCType filter for your callback. But it is not possible to
2331 * register the same callback function two times with different
2332 * GCType filters.
2333 */
2334 static void AddGCPrologueCallback(
2335 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2336
2337 /**
2338 * This function removes callback which was installed by
2339 * AddGCPrologueCallback function.
2340 */
2341 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2342
2343 /**
2344 * The function is deprecated. Please use AddGCPrologueCallback instead.
2345 * Enables the host application to receive a notification before a
2346 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002347 * callback function, you therefore cannot manipulate objects (set
2348 * or delete properties for example) since it is possible such
2349 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 */
2351 static void SetGlobalGCPrologueCallback(GCCallback);
2352
2353 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002354 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002355 * garbage collection. Allocations are not allowed in the
2356 * callback function, you therefore cannot manipulate objects (set
2357 * or delete properties for example) since it is possible such
2358 * operations will result in the allocation of objects. It is possible
2359 * to specify the GCType filter for your callback. But it is not possible to
2360 * register the same callback function two times with different
2361 * GCType filters.
2362 */
2363 static void AddGCEpilogueCallback(
2364 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2365
2366 /**
2367 * This function removes callback which was installed by
2368 * AddGCEpilogueCallback function.
2369 */
2370 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2371
2372 /**
2373 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2374 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002375 * major garbage collection. Allocations are not allowed in the
2376 * callback function, you therefore cannot manipulate objects (set
2377 * or delete properties for example) since it is possible such
2378 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379 */
2380 static void SetGlobalGCEpilogueCallback(GCCallback);
2381
2382 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002383 * Allows the host application to group objects together. If one
2384 * object in the group is alive, all objects in the group are alive.
2385 * After each garbage collection, object groups are removed. It is
2386 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002387 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002388 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002390 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
2392 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002393 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002394 * initialize from scratch. This function is called implicitly if
2395 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396 */
2397 static bool Initialize();
2398
kasper.lund7276f142008-07-30 08:49:36 +00002399 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002400 * Adjusts the amount of registered external memory. Used to give
2401 * V8 an indication of the amount of externally allocated memory
2402 * that is kept alive by JavaScript objects. V8 uses this to decide
2403 * when to perform global garbage collections. Registering
2404 * externally allocated memory will trigger global garbage
2405 * collections more often than otherwise in an attempt to garbage
2406 * collect the JavaScript objects keeping the externally allocated
2407 * memory alive.
2408 *
2409 * \param change_in_bytes the change in externally allocated memory
2410 * that is kept alive by JavaScript objects.
2411 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002412 */
2413 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2414
iposva@chromium.org245aa852009-02-10 00:49:54 +00002415 /**
2416 * Suspends recording of tick samples in the profiler.
2417 * When the V8 profiling mode is enabled (usually via command line
2418 * switches) this function suspends recording of tick samples.
2419 * Profiling ticks are discarded until ResumeProfiler() is called.
2420 *
2421 * See also the --prof and --prof_auto command line switches to
2422 * enable V8 profiling.
2423 */
2424 static void PauseProfiler();
2425
2426 /**
2427 * Resumes recording of tick samples in the profiler.
2428 * See also PauseProfiler().
2429 */
2430 static void ResumeProfiler();
2431
ager@chromium.org41826e72009-03-30 13:30:57 +00002432 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002433 * Return whether profiler is currently paused.
2434 */
2435 static bool IsProfilerPaused();
2436
2437 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002438 * Resumes specified profiler modules. Can be called several times to
2439 * mark the opening of a profiler events block with the given tag.
2440 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002441 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2442 * See ProfilerModules enum.
2443 *
2444 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002445 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002446 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002447 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002448
2449 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002450 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2451 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2452 * same tag value. There is no need for blocks to be properly nested.
2453 * The profiler is paused when the last opened block is closed.
2454 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002455 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2456 * See ProfilerModules enum.
2457 *
2458 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002459 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002460 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002461 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002462
2463 /**
2464 * Returns active (resumed) profiler modules.
2465 * See ProfilerModules enum.
2466 *
2467 * \returns active profiler modules.
2468 */
2469 static int GetActiveProfilerModules();
2470
2471 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002472 * If logging is performed into a memory buffer (via --logfile=*), allows to
2473 * retrieve previously written messages. This can be used for retrieving
2474 * profiler log data in the application. This function is thread-safe.
2475 *
2476 * Caller provides a destination buffer that must exist during GetLogLines
2477 * call. Only whole log lines are copied into the buffer.
2478 *
2479 * \param from_pos specified a point in a buffer to read from, 0 is the
2480 * beginning of a buffer. It is assumed that caller updates its current
2481 * position using returned size value from the previous call.
2482 * \param dest_buf destination buffer for log data.
2483 * \param max_size size of the destination buffer.
2484 * \returns actual size of log data copied into buffer.
2485 */
2486 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2487
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002488 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002489 * The minimum allowed size for a log lines buffer. If the size of
2490 * the buffer given will not be enough to hold a line of the maximum
2491 * length, an attempt to find a log line end in GetLogLines will
2492 * fail, and an empty result will be returned.
2493 */
2494 static const int kMinimumSizeForLogLinesBuffer = 2048;
2495
2496 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002497 * Retrieve the V8 thread id of the calling thread.
2498 *
2499 * The thread id for a thread should only be retrieved after the V8
2500 * lock has been acquired with a Locker object with that thread.
2501 */
2502 static int GetCurrentThreadId();
2503
2504 /**
2505 * Forcefully terminate execution of a JavaScript thread. This can
2506 * be used to terminate long-running scripts.
2507 *
2508 * TerminateExecution should only be called when then V8 lock has
2509 * been acquired with a Locker object. Therefore, in order to be
2510 * able to terminate long-running threads, preemption must be
2511 * enabled to allow the user of TerminateExecution to acquire the
2512 * lock.
2513 *
2514 * The termination is achieved by throwing an exception that is
2515 * uncatchable by JavaScript exception handlers. Termination
2516 * exceptions act as if they were caught by a C++ TryCatch exception
2517 * handlers. If forceful termination is used, any C++ TryCatch
2518 * exception handler that catches an exception should check if that
2519 * exception is a termination exception and immediately return if
2520 * that is the case. Returning immediately in that case will
2521 * continue the propagation of the termination exception if needed.
2522 *
2523 * The thread id passed to TerminateExecution must have been
2524 * obtained by calling GetCurrentThreadId on the thread in question.
2525 *
2526 * \param thread_id The thread id of the thread to terminate.
2527 */
2528 static void TerminateExecution(int thread_id);
2529
2530 /**
2531 * Forcefully terminate the current thread of JavaScript execution.
2532 *
2533 * This method can be used by any thread even if that thread has not
2534 * acquired the V8 lock with a Locker object.
2535 */
2536 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002537
2538 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00002539 * Is V8 terminating JavaScript execution.
2540 *
2541 * Returns true if JavaScript execution is currently terminating
2542 * because of a call to TerminateExecution. In that case there are
2543 * still JavaScript frames on the stack and the termination
2544 * exception is still active.
2545 */
2546 static bool IsExecutionTerminating();
2547
2548 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002549 * Releases any resources used by v8 and stops any utility threads
2550 * that may be running. Note that disposing v8 is permanent, it
2551 * cannot be reinitialized.
2552 *
2553 * It should generally not be necessary to dispose v8 before exiting
2554 * a process, this should happen automatically. It is only necessary
2555 * to use if the process needs the resources taken up by v8.
2556 */
2557 static bool Dispose();
2558
ager@chromium.org3811b432009-10-28 14:53:37 +00002559 /**
2560 * Get statistics about the heap memory usage.
2561 */
2562 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002563
2564 /**
2565 * Optional notification that the embedder is idle.
2566 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002567 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002568 * Returns true if the embedder should stop calling IdleNotification
2569 * until real work has been done. This indicates that V8 has done
2570 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002571 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002572 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002573
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002574 /**
2575 * Optional notification that the system is running low on memory.
2576 * V8 uses these notifications to attempt to free memory.
2577 */
2578 static void LowMemoryNotification();
2579
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002580 /**
2581 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002582 * these notifications to guide the GC heuristic. Returns the number
2583 * of context disposals - including this one - since the last time
2584 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002585 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002586 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002588 private:
2589 V8();
2590
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002591 static internal::Object** GlobalizeReference(internal::Object** handle);
2592 static void DisposeGlobal(internal::Object** global_handle);
2593 static void MakeWeak(internal::Object** global_handle,
2594 void* data,
2595 WeakReferenceCallback);
2596 static void ClearWeak(internal::Object** global_handle);
2597 static bool IsGlobalNearDeath(internal::Object** global_handle);
2598 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599
2600 template <class T> friend class Handle;
2601 template <class T> friend class Local;
2602 template <class T> friend class Persistent;
2603 friend class Context;
2604};
2605
2606
2607/**
2608 * An external exception handler.
2609 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002610class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611 public:
2612
2613 /**
2614 * Creates a new try/catch block and registers it with v8.
2615 */
2616 TryCatch();
2617
2618 /**
2619 * Unregisters and deletes this try/catch block.
2620 */
2621 ~TryCatch();
2622
2623 /**
2624 * Returns true if an exception has been caught by this try/catch block.
2625 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002626 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627
2628 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002629 * For certain types of exceptions, it makes no sense to continue
2630 * execution.
2631 *
2632 * Currently, the only type of exception that can be caught by a
2633 * TryCatch handler and for which it does not make sense to continue
2634 * is termination exception. Such exceptions are thrown when the
2635 * TerminateExecution methods are called to terminate a long-running
2636 * script.
2637 *
2638 * If CanContinue returns false, the correct action is to perform
2639 * any C++ cleanup needed and then return.
2640 */
2641 bool CanContinue() const;
2642
2643 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002644 * Throws the exception caught by this TryCatch in a way that avoids
2645 * it being caught again by this same TryCatch. As with ThrowException
2646 * it is illegal to execute any JavaScript operations after calling
2647 * ReThrow; the caller must return immediately to where the exception
2648 * is caught.
2649 */
2650 Handle<Value> ReThrow();
2651
2652 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 * Returns the exception caught by this try/catch block. If no exception has
2654 * been caught an empty handle is returned.
2655 *
2656 * The returned handle is valid until this TryCatch block has been destroyed.
2657 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002658 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659
2660 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002661 * Returns the .stack property of the thrown object. If no .stack
2662 * property is present an empty handle is returned.
2663 */
2664 Local<Value> StackTrace() const;
2665
2666 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002667 * Returns the message associated with this exception. If there is
2668 * no message associated an empty handle is returned.
2669 *
2670 * The returned handle is valid until this TryCatch block has been
2671 * destroyed.
2672 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002673 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002674
2675 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676 * Clears any exceptions that may have been caught by this try/catch block.
2677 * After this method has been called, HasCaught() will return false.
2678 *
2679 * It is not necessary to clear a try/catch block before using it again; if
2680 * another exception is thrown the previously caught exception will just be
2681 * overwritten. However, it is often a good idea since it makes it easier
2682 * to determine which operation threw a given exception.
2683 */
2684 void Reset();
2685
v8.team.kasperl727e9952008-09-02 14:56:44 +00002686 /**
2687 * Set verbosity of the external exception handler.
2688 *
2689 * By default, exceptions that are caught by an external exception
2690 * handler are not reported. Call SetVerbose with true on an
2691 * external exception handler to have exceptions caught by the
2692 * handler reported as if they were not caught.
2693 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 void SetVerbose(bool value);
2695
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002696 /**
2697 * Set whether or not this TryCatch should capture a Message object
2698 * which holds source information about where the exception
2699 * occurred. True by default.
2700 */
2701 void SetCaptureMessage(bool value);
2702
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002703 private:
2704 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002706 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002707 bool is_verbose_ : 1;
2708 bool can_continue_ : 1;
2709 bool capture_message_ : 1;
2710 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002711
2712 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713};
2714
2715
2716// --- C o n t e x t ---
2717
2718
2719/**
2720 * Ignore
2721 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002722class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 public:
2724 ExtensionConfiguration(int name_count, const char* names[])
2725 : name_count_(name_count), names_(names) { }
2726 private:
2727 friend class ImplementationUtilities;
2728 int name_count_;
2729 const char** names_;
2730};
2731
2732
2733/**
2734 * A sandboxed execution context with its own set of built-in objects
2735 * and functions.
2736 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002737class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002739 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 Local<Object> Global();
2741
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002742 /**
2743 * Detaches the global object from its context before
2744 * the global object can be reused to create a new context.
2745 */
2746 void DetachGlobal();
2747
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00002748 /**
2749 * Reattaches a global object to a context. This can be used to
2750 * restore the connection between a global object and a context
2751 * after DetachGlobal has been called.
2752 *
2753 * \param global_object The global object to reattach to the
2754 * context. For this to work, the global object must be the global
2755 * object that was associated with this context before a call to
2756 * DetachGlobal.
2757 */
2758 void ReattachGlobal(Handle<Object> global_object);
2759
v8.team.kasperl727e9952008-09-02 14:56:44 +00002760 /** Creates a new context. */
2761 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002762 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00002763 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2764 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765
kasper.lund44510672008-07-25 07:37:58 +00002766 /** Returns the last entered context. */
2767 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768
kasper.lund44510672008-07-25 07:37:58 +00002769 /** Returns the context that is on the top of the stack. */
2770 static Local<Context> GetCurrent();
2771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002773 * Returns the context of the calling JavaScript code. That is the
2774 * context of the top-most JavaScript frame. If there are no
2775 * JavaScript frames an empty handle is returned.
2776 */
2777 static Local<Context> GetCalling();
2778
2779 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 * Sets the security token for the context. To access an object in
2781 * another context, the security tokens must match.
2782 */
2783 void SetSecurityToken(Handle<Value> token);
2784
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002785 /** Restores the security token to the default value. */
2786 void UseDefaultSecurityToken();
2787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788 /** Returns the security token of this context.*/
2789 Handle<Value> GetSecurityToken();
2790
v8.team.kasperl727e9952008-09-02 14:56:44 +00002791 /**
2792 * Enter this context. After entering a context, all code compiled
2793 * and run is compiled and run in this context. If another context
2794 * is already entered, this old context is saved so it can be
2795 * restored when the new context is exited.
2796 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002797 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002798
2799 /**
2800 * Exit this context. Exiting the current context restores the
2801 * context that was in place when entering the current context.
2802 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002803 void Exit();
2804
v8.team.kasperl727e9952008-09-02 14:56:44 +00002805 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 bool HasOutOfMemoryException();
2807
v8.team.kasperl727e9952008-09-02 14:56:44 +00002808 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 static bool InContext();
2810
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002812 * Associate an additional data object with the context. This is mainly used
2813 * with the debugger to provide additional information on the context through
2814 * the debugger API.
2815 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00002816 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00002817 Local<Value> GetData();
2818
2819 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820 * Stack-allocated class which sets the execution context for all
2821 * operations executed within a local scope.
2822 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002823 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824 public:
2825 inline Scope(Handle<Context> context) : context_(context) {
2826 context_->Enter();
2827 }
2828 inline ~Scope() { context_->Exit(); }
2829 private:
2830 Handle<Context> context_;
2831 };
2832
2833 private:
2834 friend class Value;
2835 friend class Script;
2836 friend class Object;
2837 friend class Function;
2838};
2839
2840
2841/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002842 * Multiple threads in V8 are allowed, but only one thread at a time
2843 * is allowed to use V8. The definition of 'using V8' includes
2844 * accessing handles or holding onto object pointers obtained from V8
2845 * handles. It is up to the user of V8 to ensure (perhaps with
2846 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002847 *
2848 * If you wish to start using V8 in a thread you can do this by constructing
2849 * a v8::Locker object. After the code using V8 has completed for the
2850 * current thread you can call the destructor. This can be combined
2851 * with C++ scope-based construction as follows:
2852 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002853 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002854 * ...
2855 * {
2856 * v8::Locker locker;
2857 * ...
2858 * // Code using V8 goes here.
2859 * ...
2860 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002861 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002862 *
2863 * If you wish to stop using V8 in a thread A you can do this by either
2864 * by destroying the v8::Locker object as above or by constructing a
2865 * v8::Unlocker object:
2866 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002867 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868 * {
2869 * v8::Unlocker unlocker;
2870 * ...
2871 * // Code not using V8 goes here while V8 can run in another thread.
2872 * ...
2873 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002874 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 *
2876 * The Unlocker object is intended for use in a long-running callback
2877 * from V8, where you want to release the V8 lock for other threads to
2878 * use.
2879 *
2880 * The v8::Locker is a recursive lock. That is, you can lock more than
2881 * once in a given thread. This can be useful if you have code that can
2882 * be called either from code that holds the lock or from code that does
2883 * not. The Unlocker is not recursive so you can not have several
2884 * Unlockers on the stack at once, and you can not use an Unlocker in a
2885 * thread that is not inside a Locker's scope.
2886 *
2887 * An unlocker will unlock several lockers if it has to and reinstate
2888 * the correct depth of locking on its destruction. eg.:
2889 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002890 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891 * // V8 not locked.
2892 * {
2893 * v8::Locker locker;
2894 * // V8 locked.
2895 * {
2896 * v8::Locker another_locker;
2897 * // V8 still locked (2 levels).
2898 * {
2899 * v8::Unlocker unlocker;
2900 * // V8 not locked.
2901 * }
2902 * // V8 locked again (2 levels).
2903 * }
2904 * // V8 still locked (1 level).
2905 * }
2906 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002907 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002909class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002910 public:
2911 Unlocker();
2912 ~Unlocker();
2913};
2914
2915
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002916class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002917 public:
2918 Locker();
2919 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002920
2921 /**
2922 * Start preemption.
2923 *
2924 * When preemption is started, a timer is fired every n milli seconds
2925 * that will switch between multiple threads that are in contention
2926 * for the V8 lock.
2927 */
2928 static void StartPreemption(int every_n_ms);
2929
2930 /**
2931 * Stop preemption.
2932 */
2933 static void StopPreemption();
2934
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002935 /**
2936 * Returns whether or not the locker is locked by the current thread.
2937 */
2938 static bool IsLocked();
2939
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002940 /**
2941 * Returns whether v8::Locker is being used by this V8 instance.
2942 */
2943 static bool IsActive() { return active_; }
2944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002945 private:
2946 bool has_lock_;
2947 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002948
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002949 static bool active_;
2950
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002951 // Disallow copying and assigning.
2952 Locker(const Locker&);
2953 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002954};
2955
2956
2957
2958// --- I m p l e m e n t a t i o n ---
2959
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002960
2961namespace internal {
2962
2963
2964// Tag information for HeapObject.
2965const int kHeapObjectTag = 1;
2966const int kHeapObjectTagSize = 2;
2967const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2968
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002969// Tag information for Smi.
2970const int kSmiTag = 0;
2971const int kSmiTagSize = 1;
2972const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2973
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002974template <size_t ptr_size> struct SmiConstants;
2975
2976// Smi constants for 32-bit systems.
2977template <> struct SmiConstants<4> {
2978 static const int kSmiShiftSize = 0;
2979 static const int kSmiValueSize = 31;
2980 static inline int SmiToInt(internal::Object* value) {
2981 int shift_bits = kSmiTagSize + kSmiShiftSize;
2982 // Throw away top 32 bits and shift down (requires >> to be sign extending).
2983 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
2984 }
2985};
2986
2987// Smi constants for 64-bit systems.
2988template <> struct SmiConstants<8> {
2989 static const int kSmiShiftSize = 31;
2990 static const int kSmiValueSize = 32;
2991 static inline int SmiToInt(internal::Object* value) {
2992 int shift_bits = kSmiTagSize + kSmiShiftSize;
2993 // Shift down and throw away top 32 bits.
2994 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
2995 }
2996};
2997
2998const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
2999const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003000
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003001template <size_t ptr_size> struct InternalConstants;
3002
3003// Internal constants for 32-bit systems.
3004template <> struct InternalConstants<4> {
3005 static const int kStringResourceOffset = 3 * sizeof(void*);
3006};
3007
3008// Internal constants for 64-bit systems.
3009template <> struct InternalConstants<8> {
3010 static const int kStringResourceOffset = 2 * sizeof(void*);
3011};
3012
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003013/**
3014 * This class exports constants and functionality from within v8 that
3015 * is necessary to implement inline functions in the v8 api. Don't
3016 * depend on functions and constants defined here.
3017 */
3018class Internals {
3019 public:
3020
3021 // These values match non-compiler-dependent values defined within
3022 // the implementation of v8.
3023 static const int kHeapObjectMapOffset = 0;
3024 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003025 static const int kStringResourceOffset =
3026 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3027
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003028 static const int kProxyProxyOffset = sizeof(void*);
3029 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3030 static const int kFullStringRepresentationMask = 0x07;
3031 static const int kExternalTwoByteRepresentationTag = 0x03;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003032
3033 // These constants are compiler dependent so their values must be
3034 // defined within the implementation.
ager@chromium.org4af710e2009-09-15 12:20:11 +00003035 V8EXPORT static int kJSObjectType;
3036 V8EXPORT static int kFirstNonstringType;
3037 V8EXPORT static int kProxyType;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003038
3039 static inline bool HasHeapObjectTag(internal::Object* value) {
3040 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3041 kHeapObjectTag);
3042 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003043
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003044 static inline bool HasSmiTag(internal::Object* value) {
3045 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3046 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003047
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003048 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003049 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3050 }
3051
3052 static inline int GetInstanceType(internal::Object* obj) {
3053 typedef internal::Object O;
3054 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3055 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3056 }
3057
3058 static inline void* GetExternalPointer(internal::Object* obj) {
3059 if (HasSmiTag(obj)) {
3060 return obj;
3061 } else if (GetInstanceType(obj) == kProxyType) {
3062 return ReadField<void*>(obj, kProxyProxyOffset);
3063 } else {
3064 return NULL;
3065 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003066 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003067
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003068 static inline bool IsExternalTwoByteString(int instance_type) {
3069 int representation = (instance_type & kFullStringRepresentationMask);
3070 return representation == kExternalTwoByteRepresentationTag;
3071 }
3072
3073 template <typename T>
3074 static inline T ReadField(Object* ptr, int offset) {
3075 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3076 return *reinterpret_cast<T*>(addr);
3077 }
3078
3079};
3080
3081}
3082
3083
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084template <class T>
3085Handle<T>::Handle() : val_(0) { }
3086
3087
3088template <class T>
3089Local<T>::Local() : Handle<T>() { }
3090
3091
3092template <class T>
3093Local<T> Local<T>::New(Handle<T> that) {
3094 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003095 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003096 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3097}
3098
3099
3100template <class T>
3101Persistent<T> Persistent<T>::New(Handle<T> that) {
3102 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003103 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003104 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3105}
3106
3107
3108template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003109bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003111 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112}
3113
3114
3115template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003116bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003118 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003119}
3120
3121
3122template <class T>
3123void Persistent<T>::Dispose() {
3124 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003125 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126}
3127
3128
3129template <class T>
3130Persistent<T>::Persistent() : Handle<T>() { }
3131
3132template <class T>
3133void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003134 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3135 parameters,
3136 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003137}
3138
3139template <class T>
3140void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003141 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142}
3143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144Local<Value> Arguments::operator[](int i) const {
3145 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3146 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3147}
3148
3149
3150Local<Function> Arguments::Callee() const {
3151 return callee_;
3152}
3153
3154
3155Local<Object> Arguments::This() const {
3156 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3157}
3158
3159
3160Local<Object> Arguments::Holder() const {
3161 return holder_;
3162}
3163
3164
3165Local<Value> Arguments::Data() const {
3166 return data_;
3167}
3168
3169
3170bool Arguments::IsConstructCall() const {
3171 return is_construct_call_;
3172}
3173
3174
3175int Arguments::Length() const {
3176 return length_;
3177}
3178
3179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180template <class T>
3181Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003182 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3183 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184 return Local<T>(reinterpret_cast<T*>(after));
3185}
3186
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003187Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003188 return resource_name_;
3189}
3190
3191
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003192Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003193 return resource_line_offset_;
3194}
3195
3196
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003197Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 return resource_column_offset_;
3199}
3200
3201
3202Handle<Boolean> Boolean::New(bool value) {
3203 return value ? True() : False();
3204}
3205
3206
3207void Template::Set(const char* name, v8::Handle<Data> value) {
3208 Set(v8::String::New(name), value);
3209}
3210
3211
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003212Local<Value> Object::GetInternalField(int index) {
3213#ifndef V8_ENABLE_CHECKS
3214 Local<Value> quick_result = UncheckedGetInternalField(index);
3215 if (!quick_result.IsEmpty()) return quick_result;
3216#endif
3217 return CheckedGetInternalField(index);
3218}
3219
3220
3221Local<Value> Object::UncheckedGetInternalField(int index) {
3222 typedef internal::Object O;
3223 typedef internal::Internals I;
3224 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003225 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003226 // If the object is a plain JSObject, which is the common case,
3227 // we know where to find the internal fields and can return the
3228 // value directly.
3229 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3230 O* value = I::ReadField<O*>(obj, offset);
3231 O** result = HandleScope::CreateHandle(value);
3232 return Local<Value>(reinterpret_cast<Value*>(result));
3233 } else {
3234 return Local<Value>();
3235 }
3236}
3237
3238
3239void* External::Unwrap(Handle<v8::Value> obj) {
3240#ifdef V8_ENABLE_CHECKS
3241 return FullUnwrap(obj);
3242#else
3243 return QuickUnwrap(obj);
3244#endif
3245}
3246
3247
3248void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3249 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003250 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003251 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003252}
3253
3254
3255void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003256 typedef internal::Object O;
3257 typedef internal::Internals I;
3258
3259 O* obj = *reinterpret_cast<O**>(this);
3260
3261 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3262 // If the object is a plain JSObject, which is the common case,
3263 // we know where to find the internal fields and can return the
3264 // value directly.
3265 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3266 O* value = I::ReadField<O*>(obj, offset);
3267 return I::GetExternalPointer(value);
3268 }
3269
3270 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003271}
3272
3273
3274String* String::Cast(v8::Value* value) {
3275#ifdef V8_ENABLE_CHECKS
3276 CheckCast(value);
3277#endif
3278 return static_cast<String*>(value);
3279}
3280
3281
3282String::ExternalStringResource* String::GetExternalStringResource() const {
3283 typedef internal::Object O;
3284 typedef internal::Internals I;
3285 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003286 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003287 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003288 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3289 result = reinterpret_cast<String::ExternalStringResource*>(value);
3290 } else {
3291 result = NULL;
3292 }
3293#ifdef V8_ENABLE_CHECKS
3294 VerifyExternalStringResource(result);
3295#endif
3296 return result;
3297}
3298
3299
3300bool Value::IsString() const {
3301#ifdef V8_ENABLE_CHECKS
3302 return FullIsString();
3303#else
3304 return QuickIsString();
3305#endif
3306}
3307
3308bool Value::QuickIsString() const {
3309 typedef internal::Object O;
3310 typedef internal::Internals I;
3311 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3312 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003313 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003314}
3315
3316
3317Number* Number::Cast(v8::Value* value) {
3318#ifdef V8_ENABLE_CHECKS
3319 CheckCast(value);
3320#endif
3321 return static_cast<Number*>(value);
3322}
3323
3324
3325Integer* Integer::Cast(v8::Value* value) {
3326#ifdef V8_ENABLE_CHECKS
3327 CheckCast(value);
3328#endif
3329 return static_cast<Integer*>(value);
3330}
3331
3332
3333Date* Date::Cast(v8::Value* value) {
3334#ifdef V8_ENABLE_CHECKS
3335 CheckCast(value);
3336#endif
3337 return static_cast<Date*>(value);
3338}
3339
3340
3341Object* Object::Cast(v8::Value* value) {
3342#ifdef V8_ENABLE_CHECKS
3343 CheckCast(value);
3344#endif
3345 return static_cast<Object*>(value);
3346}
3347
3348
3349Array* Array::Cast(v8::Value* value) {
3350#ifdef V8_ENABLE_CHECKS
3351 CheckCast(value);
3352#endif
3353 return static_cast<Array*>(value);
3354}
3355
3356
3357Function* Function::Cast(v8::Value* value) {
3358#ifdef V8_ENABLE_CHECKS
3359 CheckCast(value);
3360#endif
3361 return static_cast<Function*>(value);
3362}
3363
3364
3365External* External::Cast(v8::Value* value) {
3366#ifdef V8_ENABLE_CHECKS
3367 CheckCast(value);
3368#endif
3369 return static_cast<External*>(value);
3370}
3371
3372
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003373Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003374 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003375}
3376
3377
3378Local<Object> AccessorInfo::This() const {
3379 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3380}
3381
3382
3383Local<Object> AccessorInfo::Holder() const {
3384 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3385}
3386
3387
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003388/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003389 * \example shell.cc
3390 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003391 * command-line and executes them.
3392 */
3393
3394
3395/**
3396 * \example process.cc
3397 */
3398
3399
3400} // namespace v8
3401
3402
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003403#undef V8EXPORT
3404#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405#undef TYPE_CHECK
3406
3407
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003408#endif // V8_H_