blob: 346050d5d3916b4fd80d7f5402252abdd0b8b9fc [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2007-2008 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
132class Object;
133
134}
135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
137// --- W e a k H a n d l e s
138
139
140/**
141 * A weak reference callback function.
142 *
143 * \param object the weak global object to be reclaimed by the garbage collector
144 * \param parameter the value passed in when making the weak global object
145 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000146typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 void* parameter);
148
149
150// --- H a n d l e s ---
151
152#define TYPE_CHECK(T, S) \
153 while (false) { \
154 *(static_cast<T**>(0)) = static_cast<S*>(0); \
155 }
156
157/**
158 * An object reference managed by the v8 garbage collector.
159 *
160 * All objects returned from v8 have to be tracked by the garbage
161 * collector so that it knows that the objects are still alive. Also,
162 * because the garbage collector may move objects, it is unsafe to
163 * point directly to an object. Instead, all objects are stored in
164 * handles which are known by the garbage collector and updated
165 * whenever an object moves. Handles should always be passed by value
166 * (except in cases like out-parameters) and they should never be
167 * allocated on the heap.
168 *
169 * There are two types of handles: local and persistent handles.
170 * Local handles are light-weight and transient and typically used in
171 * local operations. They are managed by HandleScopes. Persistent
172 * handles can be used when storing objects across several independent
173 * operations and have to be explicitly deallocated when they're no
174 * longer used.
175 *
176 * It is safe to extract the object stored in the handle by
177 * dereferencing the handle (for instance, to extract the Object* from
178 * an Handle<Object>); the value will still be governed by a handle
179 * behind the scenes and the same rules apply to these values as to
180 * their handles.
181 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000182template <class T> class V8EXPORT_INLINE Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 public:
184
185 /**
186 * Creates an empty handle.
187 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000188 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189
190 /**
191 * Creates a new handle for the specified value.
192 */
193 explicit Handle(T* val) : val_(val) { }
194
195 /**
196 * Creates a handle for the contents of the specified handle. This
197 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000198 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 * incompatible handles, for instance from a Handle<String> to a
200 * Handle<Number> it will cause a compiletime error. Assigning
201 * between compatible handles, for instance assigning a
202 * Handle<String> to a variable declared as Handle<Value>, is legal
203 * because String is a subclass of Value.
204 */
205 template <class S> inline Handle(Handle<S> that)
206 : val_(reinterpret_cast<T*>(*that)) {
207 /**
208 * This check fails when trying to convert between incompatible
209 * handles. For example, converting from a Handle<String> to a
210 * Handle<Number>.
211 */
212 TYPE_CHECK(T, S);
213 }
214
215 /**
216 * Returns true if the handle is empty.
217 */
ager@chromium.org32912102009-01-16 10:38:43 +0000218 bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000220 T* operator->() const { return val_; }
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
224 /**
225 * Sets the handle to be empty. IsEmpty() will then return true.
226 */
227 void Clear() { this->val_ = 0; }
228
229 /**
230 * Checks whether two handles are the same.
231 * Returns true if both are empty, or if the objects
232 * to which they refer are identical.
233 * The handles' references are not checked.
234 */
ager@chromium.org32912102009-01-16 10:38:43 +0000235 template <class S> bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000236 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
237 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 if (a == 0) return b == 0;
239 if (b == 0) return false;
240 return *a == *b;
241 }
242
243 /**
244 * Checks whether two handles are different.
245 * Returns true if only one of the handles is empty, or if
246 * the objects to which they refer are different.
247 * The handles' references are not checked.
248 */
ager@chromium.org32912102009-01-16 10:38:43 +0000249 template <class S> bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 return !operator==(that);
251 }
252
253 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000254#ifdef V8_ENABLE_CHECKS
255 // If we're going to perform the type check then we have to check
256 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000258#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 return Handle<T>(T::Cast(*that));
260 }
261
262 private:
263 T* val_;
264};
265
266
267/**
268 * A light-weight stack-allocated object handle. All operations
269 * that return objects from within v8 return them in local handles. They
270 * are created within HandleScopes, and all local handles allocated within a
271 * handle scope are destroyed when the handle scope is destroyed. Hence it
272 * is not necessary to explicitly deallocate local handles.
273 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000274template <class T> class V8EXPORT_INLINE Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000276 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 template <class S> inline Local(Local<S> that)
278 : Handle<T>(reinterpret_cast<T*>(*that)) {
279 /**
280 * This check fails when trying to convert between incompatible
281 * handles. For example, converting from a Handle<String> to a
282 * Handle<Number>.
283 */
284 TYPE_CHECK(T, S);
285 }
286 template <class S> inline Local(S* that) : Handle<T>(that) { }
287 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000288#ifdef V8_ENABLE_CHECKS
289 // If we're going to perform the type check then we have to check
290 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000292#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 return Local<T>(T::Cast(*that));
294 }
295
296 /** Create a local handle for the content of another handle.
297 * The referee is kept alive by the local handle even when
298 * the original handle is destroyed/disposed.
299 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000300 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301};
302
303
304/**
305 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000306 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 * allocated, a Persistent handle remains valid until it is explicitly
308 * disposed.
309 *
310 * A persistent handle contains a reference to a storage cell within
311 * the v8 engine which holds an object value and which is updated by
312 * the garbage collector whenever the object is moved. A new storage
313 * cell can be created using Persistent::New and existing handles can
314 * be disposed using Persistent::Dispose. Since persistent handles
315 * are passed by value you may have many persistent handle objects
316 * that point to the same storage cell. For instance, if you pass a
317 * persistent handle as an argument to a function you will not get two
318 * different storage cells but rather two references to the same
319 * storage cell.
320 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000321template <class T> class V8EXPORT_INLINE Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 public:
323
324 /**
325 * Creates an empty persistent handle that doesn't point to any
326 * storage cell.
327 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000328 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329
330 /**
331 * Creates a persistent handle for the same storage cell as the
332 * specified handle. This constructor allows you to pass persistent
333 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000334 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000336 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 * between compatible persistent handles, for instance assigning a
338 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000339 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340 */
341 template <class S> inline Persistent(Persistent<S> that)
342 : Handle<T>(reinterpret_cast<T*>(*that)) {
343 /**
344 * This check fails when trying to convert between incompatible
345 * handles. For example, converting from a Handle<String> to a
346 * Handle<Number>.
347 */
348 TYPE_CHECK(T, S);
349 }
350
351 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
352
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000353 /**
354 * "Casts" a plain handle which is known to be a persistent handle
355 * to a persistent handle.
356 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357 template <class S> explicit inline Persistent(Handle<S> that)
358 : Handle<T>(*that) { }
359
360 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000361#ifdef V8_ENABLE_CHECKS
362 // If we're going to perform the type check then we have to check
363 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000365#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 return Persistent<T>(T::Cast(*that));
367 }
368
369 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000370 * Creates a new persistent handle for an existing local or
371 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000373 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374
375 /**
376 * Releases the storage cell referenced by this persistent handle.
377 * Does not remove the reference to the cell from any handles.
378 * This handle's reference, and any any other references to the storage
379 * cell remain and IsEmpty will still return false.
380 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000381 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382
383 /**
384 * Make the reference to this object weak. When only weak handles
385 * refer to the object, the garbage collector will perform a
386 * callback to the given V8::WeakReferenceCallback function, passing
387 * it the object reference and the given parameters.
388 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000389 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390
391 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000392 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
394 /**
395 *Checks if the handle holds the only reference to an object.
396 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000397 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398
399 /**
400 * Returns true if the handle's reference is weak.
401 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000402 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403
404 private:
405 friend class ImplementationUtilities;
406 friend class ObjectTemplate;
407};
408
409
v8.team.kasperl727e9952008-09-02 14:56:44 +0000410 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411 * A stack-allocated class that governs a number of local handles.
412 * After a handle scope has been created, all local handles will be
413 * allocated within that handle scope until either the handle scope is
414 * deleted or another handle scope is created. If there is already a
415 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000416 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000417 * new handles will again be allocated in the original handle scope.
418 *
419 * After the handle scope of a local handle has been deleted the
420 * garbage collector will no longer track the object stored in the
421 * handle and may deallocate it. The behavior of accessing a handle
422 * for which the handle scope has been deleted is undefined.
423 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000424class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000426 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000428 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
430 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 * Closes the handle scope and returns the value as a handle in the
432 * previous scope, which is the new current scope after the call.
433 */
434 template <class T> Local<T> Close(Handle<T> value);
435
436 /**
437 * Counts the number of allocated handles.
438 */
439 static int NumberOfHandles();
440
441 /**
442 * Creates a new handle with the given value.
443 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000444 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
446 private:
447 // Make it impossible to create heap-allocated or illegal handle
448 // scopes by disallowing certain operations.
449 HandleScope(const HandleScope&);
450 void operator=(const HandleScope&);
451 void* operator new(size_t size);
452 void operator delete(void*, size_t);
453
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000454 // This Data class is accessible internally through a typedef in the
455 // ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000456 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 public:
458 int extensions;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000459 internal::Object** next;
460 internal::Object** limit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 inline void Initialize() {
462 extensions = -1;
463 next = limit = NULL;
464 }
465 };
466
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000467 Data previous_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000469 // Allow for the active closing of HandleScopes which allows to pass a handle
470 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000472 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 friend class ImplementationUtilities;
475};
476
477
478// --- S p e c i a l o b j e c t s ---
479
480
481/**
482 * The superclass of values and API object templates.
483 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000484class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 private:
486 Data();
487};
488
489
490/**
491 * Pre-compilation data that can be associated with a script. This
492 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000493 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000494 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000496class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 public:
498 virtual ~ScriptData() { }
499 static ScriptData* PreCompile(const char* input, int length);
500 static ScriptData* New(unsigned* data, int length);
501
502 virtual int Length() = 0;
503 virtual unsigned* Data() = 0;
504};
505
506
507/**
508 * The origin, within a file, of a script.
509 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000510class V8EXPORT ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 public:
mads.s.agercbaa0602008-08-14 13:41:48 +0000512 ScriptOrigin(Handle<Value> resource_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 Handle<Integer> resource_line_offset = Handle<Integer>(),
514 Handle<Integer> resource_column_offset = Handle<Integer>())
515 : resource_name_(resource_name),
516 resource_line_offset_(resource_line_offset),
517 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000518 inline Handle<Value> ResourceName() const;
519 inline Handle<Integer> ResourceLineOffset() const;
520 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000522 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 Handle<Integer> resource_line_offset_;
524 Handle<Integer> resource_column_offset_;
525};
526
527
528/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000529 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000531class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 public:
533
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000534 /**
535 * Compiles the specified script. The ScriptOrigin* and ScriptData*
536 * parameters are owned by the caller of Script::Compile. No
537 * references to these objects are kept after compilation finishes.
538 *
539 * The script object returned is context independent; when run it
540 * will use the currently entered context.
541 */
542 static Local<Script> New(Handle<String> source,
543 ScriptOrigin* origin = NULL,
544 ScriptData* pre_data = NULL);
545
546 /**
547 * Compiles the specified script using the specified file name
548 * object (typically a string) as the script's origin.
549 *
550 * The script object returned is context independent; when run it
551 * will use the currently entered context.
552 */
553 static Local<Script> New(Handle<String> source,
554 Handle<Value> file_name);
555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 /**
557 * Compiles the specified script. The ScriptOrigin* and ScriptData*
558 * parameters are owned by the caller of Script::Compile. No
559 * references to these objects are kept after compilation finishes.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000560 *
561 * The script object returned is bound to the context that was active
562 * when this function was called. When run it will always use this
563 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 */
565 static Local<Script> Compile(Handle<String> source,
566 ScriptOrigin* origin = NULL,
567 ScriptData* pre_data = NULL);
568
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 *
573 * The script object returned is bound to the context that was active
574 * when this function was called. When run it will always use this
575 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000576 */
577 static Local<Script> Compile(Handle<String> source,
578 Handle<Value> file_name);
579
v8.team.kasperl727e9952008-09-02 14:56:44 +0000580 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000581 * Runs the script returning the resulting value. If the script is
582 * context independent (created using ::New) it will be run in the
583 * currently entered context. If it is context specific (created
584 * using ::Compile) it will be run in the context in which it was
585 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000586 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000588
589 /**
590 * Returns the script id value.
591 */
592 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000593
594 /**
595 * Associate an additional data object with the script. This is mainly used
596 * with the debugger as this data object is only available through the
597 * debugger API.
598 */
599 void SetData(Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600};
601
602
603/**
604 * An error message.
605 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000606class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000608 Local<String> Get() const;
609 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000611 /**
612 * Returns the resource name for the script from where the function causing
613 * the error originates.
614 */
ager@chromium.org32912102009-01-16 10:38:43 +0000615 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000617 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000618 * Returns the resource data for the script from where the function causing
619 * the error originates.
620 */
621 Handle<Value> GetScriptData() const;
622
623 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000624 * Returns the number, 1-based, of the line where the error occurred.
625 */
ager@chromium.org32912102009-01-16 10:38:43 +0000626 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000628 /**
629 * Returns the index within the script of the first character where
630 * the error occurred.
631 */
ager@chromium.org32912102009-01-16 10:38:43 +0000632 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000633
634 /**
635 * Returns the index within the script of the last character where
636 * the error occurred.
637 */
ager@chromium.org32912102009-01-16 10:38:43 +0000638 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000639
640 /**
641 * Returns the index within the line of the first character where
642 * the error occurred.
643 */
ager@chromium.org32912102009-01-16 10:38:43 +0000644 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000645
646 /**
647 * Returns the index within the line of the last character where
648 * the error occurred.
649 */
ager@chromium.org32912102009-01-16 10:38:43 +0000650 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 // TODO(1245381): Print to a string instead of on a FILE.
653 static void PrintCurrentStackTrace(FILE* out);
654};
655
656
657// --- V a l u e ---
658
659
660/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000663class V8EXPORT Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 public:
665
666 /**
667 * Returns true if this value is the undefined value. See ECMA-262
668 * 4.3.10.
669 */
ager@chromium.org32912102009-01-16 10:38:43 +0000670 bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671
672 /**
673 * Returns true if this value is the null value. See ECMA-262
674 * 4.3.11.
675 */
ager@chromium.org32912102009-01-16 10:38:43 +0000676 bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677
678 /**
679 * Returns true if this value is true.
680 */
ager@chromium.org32912102009-01-16 10:38:43 +0000681 bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682
683 /**
684 * Returns true if this value is false.
685 */
ager@chromium.org32912102009-01-16 10:38:43 +0000686 bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687
688 /**
689 * Returns true if this value is an instance of the String type.
690 * See ECMA-262 8.4.
691 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000692 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693
694 /**
695 * Returns true if this value is a function.
696 */
ager@chromium.org32912102009-01-16 10:38:43 +0000697 bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
699 /**
700 * Returns true if this value is an array.
701 */
ager@chromium.org32912102009-01-16 10:38:43 +0000702 bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703
v8.team.kasperl727e9952008-09-02 14:56:44 +0000704 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 * Returns true if this value is an object.
706 */
ager@chromium.org32912102009-01-16 10:38:43 +0000707 bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708
v8.team.kasperl727e9952008-09-02 14:56:44 +0000709 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710 * Returns true if this value is boolean.
711 */
ager@chromium.org32912102009-01-16 10:38:43 +0000712 bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000713
v8.team.kasperl727e9952008-09-02 14:56:44 +0000714 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715 * Returns true if this value is a number.
716 */
ager@chromium.org32912102009-01-16 10:38:43 +0000717 bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718
v8.team.kasperl727e9952008-09-02 14:56:44 +0000719 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 * Returns true if this value is external.
721 */
ager@chromium.org32912102009-01-16 10:38:43 +0000722 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723
v8.team.kasperl727e9952008-09-02 14:56:44 +0000724 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 * Returns true if this value is a 32-bit signed integer.
726 */
ager@chromium.org32912102009-01-16 10:38:43 +0000727 bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000729 /**
730 * Returns true if this value is a Date.
731 */
ager@chromium.org32912102009-01-16 10:38:43 +0000732 bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000733
ager@chromium.org32912102009-01-16 10:38:43 +0000734 Local<Boolean> ToBoolean() const;
735 Local<Number> ToNumber() const;
736 Local<String> ToString() const;
737 Local<String> ToDetailString() const;
738 Local<Object> ToObject() const;
739 Local<Integer> ToInteger() const;
740 Local<Uint32> ToUint32() const;
741 Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742
743 /**
744 * Attempts to convert a string to an array index.
745 * Returns an empty handle if the conversion fails.
746 */
ager@chromium.org32912102009-01-16 10:38:43 +0000747 Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748
ager@chromium.org32912102009-01-16 10:38:43 +0000749 bool BooleanValue() const;
750 double NumberValue() const;
751 int64_t IntegerValue() const;
752 uint32_t Uint32Value() const;
753 int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754
755 /** JS == */
ager@chromium.org32912102009-01-16 10:38:43 +0000756 bool Equals(Handle<Value> that) const;
757 bool StrictEquals(Handle<Value> that) const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000758
759 private:
760 inline bool QuickIsString() const;
761 bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762};
763
764
765/**
766 * The superclass of primitive values. See ECMA-262 4.3.2.
767 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000768class V8EXPORT Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769
770
771/**
772 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
773 * or false value.
774 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000775class V8EXPORT Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000776 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000777 bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778 static inline Handle<Boolean> New(bool value);
779};
780
781
782/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000783 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000785class V8EXPORT String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000787
788 /**
789 * Returns the number of characters in this string.
790 */
ager@chromium.org32912102009-01-16 10:38:43 +0000791 int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792
v8.team.kasperl727e9952008-09-02 14:56:44 +0000793 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000794 * Returns the number of bytes in the UTF-8 encoded
795 * representation of this string.
796 */
ager@chromium.org32912102009-01-16 10:38:43 +0000797 int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000798
799 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000800 * Write the contents of the string to an external buffer.
801 * If no arguments are given, expects the buffer to be large
802 * enough to hold the entire string and NULL terminator. Copies
803 * the contents of the string and the NULL terminator into the
804 * buffer.
805 *
806 * Copies up to length characters into the output buffer.
807 * Only null-terminates if there is enough space in the buffer.
808 *
809 * \param buffer The buffer into which the string will be copied.
810 * \param start The starting position within the string at which
811 * copying begins.
812 * \param length The number of bytes to copy from the string.
813 * \return The number of characters copied to the buffer
814 * excluding the NULL terminator.
815 */
ager@chromium.org32912102009-01-16 10:38:43 +0000816 int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16
817 int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
818 int WriteUtf8(char* buffer, int length = -1) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819
v8.team.kasperl727e9952008-09-02 14:56:44 +0000820 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +0000821 * A zero length string.
822 */
823 static v8::Local<v8::String> Empty();
824
825 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000826 * Returns true if the string is external
827 */
ager@chromium.org32912102009-01-16 10:38:43 +0000828 bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829
v8.team.kasperl727e9952008-09-02 14:56:44 +0000830 /**
831 * Returns true if the string is both external and ascii
832 */
ager@chromium.org32912102009-01-16 10:38:43 +0000833 bool IsExternalAscii() const;
v8.team.kasperl727e9952008-09-02 14:56:44 +0000834 /**
835 * An ExternalStringResource is a wrapper around a two-byte string
836 * buffer that resides outside V8's heap. Implement an
837 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000838 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000839 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000840 class V8EXPORT ExternalStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841 public:
842 /**
843 * Override the destructor to manage the life cycle of the underlying
844 * buffer.
845 */
846 virtual ~ExternalStringResource() {}
847 /** The string data from the underlying buffer.*/
848 virtual const uint16_t* data() const = 0;
849 /** The length of the string. That is, the number of two-byte characters.*/
850 virtual size_t length() const = 0;
851 protected:
852 ExternalStringResource() {}
853 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000854 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 ExternalStringResource(const ExternalStringResource&);
856 void operator=(const ExternalStringResource&);
857 };
858
859 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000860 * An ExternalAsciiStringResource is a wrapper around an ascii
861 * string buffer that resides outside V8's heap. Implement an
862 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000863 * underlying buffer. Note that the string data must be immutable
864 * and that the data must be strict 7-bit ASCII, not Latin1 or
865 * UTF-8, which would require special treatment internally in the
866 * engine and, in the case of UTF-8, do not allow efficient indexing.
867 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000868 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000870 class V8EXPORT ExternalAsciiStringResource { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 public:
872 /**
873 * Override the destructor to manage the life cycle of the underlying
874 * buffer.
875 */
876 virtual ~ExternalAsciiStringResource() {}
877 /** The string data from the underlying buffer.*/
878 virtual const char* data() const = 0;
879 /** The number of ascii characters in the string.*/
880 virtual size_t length() const = 0;
881 protected:
882 ExternalAsciiStringResource() {}
883 private:
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000884 // Disallow copying and assigning.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 ExternalAsciiStringResource(const ExternalAsciiStringResource&);
886 void operator=(const ExternalAsciiStringResource&);
887 };
888
889 /**
ager@chromium.org9085a012009-05-11 19:22:57 +0000890 * Get the ExternalStringResource for an external string. Returns
891 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000893 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894
895 /**
896 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +0000897 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 */
ager@chromium.org32912102009-01-16 10:38:43 +0000899 ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000901 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
903 /**
904 * Allocates a new string from either utf-8 encoded or ascii data.
905 * The second parameter 'length' gives the buffer length.
906 * If the data is utf-8 encoded, the caller must
907 * be careful to supply the length parameter.
908 * If it is not given, the function calls
909 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +0000910 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 */
912 static Local<String> New(const char* data, int length = -1);
913
914 /** Allocates a new string from utf16 data.*/
915 static Local<String> New(const uint16_t* data, int length = -1);
916
917 /** Creates a symbol. Returns one if it exists already.*/
918 static Local<String> NewSymbol(const char* data, int length = -1);
919
v8.team.kasperl727e9952008-09-02 14:56:44 +0000920 /**
921 * Creates a new external string using the data defined in the given
922 * resource. The resource is deleted when the external string is no
923 * longer live on V8's heap. The caller of this function should not
924 * delete or modify the resource. Neither should the underlying buffer be
925 * deallocated or modified except through the destructor of the
926 * external string resource.
927 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928 static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000929
ager@chromium.org6f10e412009-02-13 10:11:16 +0000930 /**
931 * Associate an external string resource with this string by transforming it
932 * in place so that existing references to this string in the JavaScript heap
933 * will use the external string resource. The external string resource's
934 * character contents needs to be equivalent to this string.
935 * Returns true if the string has been changed to be an external string.
936 * The string is not modified if the operation fails.
937 */
938 bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939
v8.team.kasperl727e9952008-09-02 14:56:44 +0000940 /**
941 * Creates a new external string using the ascii data defined in the given
942 * resource. The resource is deleted when the external string is no
943 * longer live on V8's heap. The caller of this function should not
944 * delete or modify the resource. Neither should the underlying buffer be
945 * deallocated or modified except through the destructor of the
946 * external string resource.
947 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948 static Local<String> NewExternal(ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000949
ager@chromium.org6f10e412009-02-13 10:11:16 +0000950 /**
951 * Associate an external string resource with this string by transforming it
952 * in place so that existing references to this string in the JavaScript heap
953 * will use the external string resource. The external string resource's
954 * character contents needs to be equivalent to this string.
955 * Returns true if the string has been changed to be an external string.
956 * The string is not modified if the operation fails.
957 */
958 bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000960 /**
961 * Returns true if this string can be made external.
962 */
963 bool CanMakeExternal();
964
kasper.lund7276f142008-07-30 08:49:36 +0000965 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 static Local<String> NewUndetectable(const char* data, int length = -1);
967
kasper.lund7276f142008-07-30 08:49:36 +0000968 /** Creates an undetectable string from the supplied utf-16 data.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 static Local<String> NewUndetectable(const uint16_t* data, int length = -1);
970
971 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000972 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +0000973 * you want to print the object. If conversion to a string fails
974 * (eg. due to an exception in the toString() method of the object)
975 * then the length() method returns 0 and the * operator returns
976 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000977 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000978 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000979 public:
980 explicit Utf8Value(Handle<v8::Value> obj);
981 ~Utf8Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +0000982 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000983 int length() { return length_; }
984 private:
985 char* str_;
986 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000987
988 // Disallow copying and assigning.
989 Utf8Value(const Utf8Value&);
990 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000991 };
992
993 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 * Converts an object to an ascii string.
995 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +0000996 * If conversion to a string fails (eg. due to an exception in the toString()
997 * method of the object) then the length() method returns 0 and the * operator
998 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001000 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 public:
1002 explicit AsciiValue(Handle<v8::Value> obj);
1003 ~AsciiValue();
iposva@chromium.org245aa852009-02-10 00:49:54 +00001004 char* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001005 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001006 private:
1007 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001008 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001009
1010 // Disallow copying and assigning.
1011 AsciiValue(const AsciiValue&);
1012 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013 };
1014
1015 /**
1016 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001017 * If conversion to a string fails (eg. due to an exception in the toString()
1018 * method of the object) then the length() method returns 0 and the * operator
1019 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001021 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 public:
1023 explicit Value(Handle<v8::Value> obj);
1024 ~Value();
iposva@chromium.org245aa852009-02-10 00:49:54 +00001025 uint16_t* operator*() const { return str_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001026 int length() { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027 private:
1028 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001029 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001030
1031 // Disallow copying and assigning.
1032 Value(const Value&);
1033 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 };
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001035
1036 private:
1037 void VerifyExternalStringResource(ExternalStringResource* val) const;
1038 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039};
1040
1041
1042/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001043 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001044 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001045class V8EXPORT Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001047 double Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001049 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050 private:
1051 Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001052 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053};
1054
1055
1056/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001057 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001059class V8EXPORT Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060 public:
1061 static Local<Integer> New(int32_t value);
ager@chromium.org32912102009-01-16 10:38:43 +00001062 int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001063 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001064 private:
1065 Integer();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001066 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067};
1068
1069
1070/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001071 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001073class V8EXPORT Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001075 int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 private:
1077 Int32();
1078};
1079
1080
1081/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001082 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001084class V8EXPORT Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001086 uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 private:
1088 Uint32();
1089};
1090
1091
1092/**
1093 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1094 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001095class V8EXPORT Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 public:
1097 static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001098
1099 /**
1100 * A specialization of Value::NumberValue that is more efficient
1101 * because we know the structure of this object.
1102 */
ager@chromium.org32912102009-01-16 10:38:43 +00001103 double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001104
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001105 static inline Date* Cast(v8::Value* obj);
1106 private:
1107 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108};
1109
1110
1111enum PropertyAttribute {
1112 None = 0,
1113 ReadOnly = 1 << 0,
1114 DontEnum = 1 << 1,
1115 DontDelete = 1 << 2
1116};
1117
1118/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001119 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001121class V8EXPORT Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 public:
1123 bool Set(Handle<Value> key,
1124 Handle<Value> value,
1125 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001126
ager@chromium.orge2902be2009-06-08 12:21:35 +00001127 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001128 // overriding accessors or read-only properties.
1129 //
1130 // Note that if the object has an interceptor the property will be set
1131 // locally, but since the interceptor takes precedence the local property
1132 // will only be returned if the interceptor doesn't return a value.
1133 //
1134 // Note also that this only works for named properties.
1135 bool ForceSet(Handle<Value> key,
1136 Handle<Value> value,
1137 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 Local<Value> Get(Handle<Value> key);
1140
1141 // TODO(1245389): Replace the type-specific versions of these
1142 // functions with generic ones that accept a Handle<Value> key.
1143 bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001144
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001146
1147 // Delete a property on this object bypassing interceptors and
1148 // ignoring dont-delete attributes.
1149 bool ForceDelete(Handle<Value> key);
1150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001152
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 bool Delete(uint32_t index);
1154
1155 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001156 * Returns an array containing the names of the enumerable properties
1157 * of this object, including properties from prototype objects. The
1158 * array returned by this method contains the same values as would
1159 * be enumerated by a for-in statement over this object.
1160 */
1161 Local<Array> GetPropertyNames();
1162
1163 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 * Get the prototype object. This does not skip objects marked to
1165 * be skipped by __proto__ and it does not consult the security
1166 * handler.
1167 */
1168 Local<Value> GetPrototype();
1169
1170 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001171 * Finds an instance of the given function template in the prototype
1172 * chain.
1173 */
1174 Local<Object> FindInstanceInPrototypeChain(Handle<FunctionTemplate> tmpl);
1175
1176 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 * Call builtin Object.prototype.toString on this object.
1178 * This is different from Value::ToString() that may call
1179 * user-defined toString function. This one does not.
1180 */
1181 Local<String> ObjectProtoToString();
1182
kasper.lund212ac232008-07-16 07:07:30 +00001183 /** Gets the number of internal fields for this Object. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001185 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001186 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001187 /** Sets the value in an internal field. */
1188 void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001190 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001191 inline void* GetPointerFromInternalField(int index);
1192
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001193 /** Sets a native pointer in an internal field. */
1194 void SetPointerInInternalField(int index, void* value);
1195
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196 // Testers for local properties.
1197 bool HasRealNamedProperty(Handle<String> key);
1198 bool HasRealIndexedProperty(uint32_t index);
1199 bool HasRealNamedCallbackProperty(Handle<String> key);
1200
1201 /**
1202 * If result.IsEmpty() no real property was located in the prototype chain.
1203 * This means interceptors in the prototype chain are not called.
1204 */
1205 Handle<Value> GetRealNamedPropertyInPrototypeChain(Handle<String> key);
1206
1207 /** Tests for a named lookup interceptor.*/
1208 bool HasNamedLookupInterceptor();
1209
kasper.lund212ac232008-07-16 07:07:30 +00001210 /** Tests for an index lookup interceptor.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211 bool HasIndexedLookupInterceptor();
1212
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001213 /**
1214 * Turns on access check on the object if the object is an instance of
1215 * a template that has access check callbacks. If an object has no
1216 * access check info, the object cannot be accessed by anyone.
1217 */
1218 void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001219
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001220 /**
1221 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001222 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001223 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001224 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001225 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001226 */
1227 int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001228
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001229 /**
1230 * Access hidden properties on JavaScript objects. These properties are
1231 * hidden from the executing JavaScript and only accessible through the V8
1232 * C++ API. Hidden properties introduced by V8 internally (for example the
1233 * identity hash) are prefixed with "v8::".
1234 */
1235 bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1236 Local<Value> GetHiddenValue(Handle<String> key);
1237 bool DeleteHiddenValue(Handle<String> key);
1238
1239 /**
1240 * Clone this object with a fast but shallow copy. Values will point
1241 * to the same values as the original object.
1242 */
1243 Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001245 /**
1246 * Set the backing store of the indexed properties to be managed by the
1247 * embedding layer. Access to the indexed properties will follow the rules
1248 * spelled out in CanvasPixelArray.
1249 * Note: The embedding program still owns the data and needs to ensure that
1250 * the backing store is preserved while V8 has a reference.
1251 */
1252 void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
1253
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254 static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001255 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256 private:
1257 Object();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001258 static void CheckCast(Value* obj);
1259 Local<Value> CheckedGetInternalField(int index);
1260
1261 /**
1262 * If quick access to the internal field is possible this method
1263 * returns the value. Otherwise an empty handle is returned.
1264 */
1265 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266};
1267
1268
1269/**
1270 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1271 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001272class V8EXPORT Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001274 uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275
ager@chromium.org3e875802009-06-29 08:26:34 +00001276 /**
1277 * Clones an element at index |index|. Returns an empty
1278 * handle if cloning fails (for any reason).
1279 */
1280 Local<Object> CloneElementAt(uint32_t index);
1281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001283 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 private:
1285 Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001286 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287};
1288
1289
1290/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001291 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001293class V8EXPORT Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 public:
ager@chromium.org32912102009-01-16 10:38:43 +00001295 Local<Object> NewInstance() const;
1296 Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 Local<Value> Call(Handle<Object> recv, int argc, Handle<Value> argv[]);
1298 void SetName(Handle<String> name);
ager@chromium.org32912102009-01-16 10:38:43 +00001299 Handle<Value> GetName() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001300 static inline Function* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 private:
1302 Function();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001303 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304};
1305
1306
1307/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001308 * A JavaScript value that wraps a C++ void*. This type of value is
1309 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001311 *
1312 * The Wrap function V8 will return the most optimal Value object wrapping the
1313 * C++ void*. The type of the value is not guaranteed to be an External object
1314 * and no assumptions about its type should be made. To access the wrapped
1315 * value Unwrap should be used, all other operations on that object will lead
1316 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001318class V8EXPORT External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 public:
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001320 static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001321 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001324 static inline External* Cast(Value* obj);
ager@chromium.org32912102009-01-16 10:38:43 +00001325 void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 private:
1327 External();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001328 static void CheckCast(v8::Value* obj);
1329 static inline void* QuickUnwrap(Handle<v8::Value> obj);
1330 static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331};
1332
1333
1334// --- T e m p l a t e s ---
1335
1336
1337/**
1338 * The superclass of object and function templates.
1339 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001340class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 public:
1342 /** Adds a property to each instance created by this template.*/
1343 void Set(Handle<String> name, Handle<Data> value,
1344 PropertyAttribute attributes = None);
1345 inline void Set(const char* name, Handle<Data> value);
1346 private:
1347 Template();
1348
1349 friend class ObjectTemplate;
1350 friend class FunctionTemplate;
1351};
1352
1353
1354/**
1355 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001356 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 * including the receiver, the number and values of arguments, and
1358 * the holder of the function.
1359 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001360class V8EXPORT Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 public:
1362 inline int Length() const;
1363 inline Local<Value> operator[](int i) const;
1364 inline Local<Function> Callee() const;
1365 inline Local<Object> This() const;
1366 inline Local<Object> Holder() const;
1367 inline bool IsConstructCall() const;
1368 inline Local<Value> Data() const;
1369 private:
1370 Arguments();
1371 friend class ImplementationUtilities;
1372 inline Arguments(Local<Value> data,
1373 Local<Object> holder,
1374 Local<Function> callee,
1375 bool is_construct_call,
1376 void** values, int length);
1377 Local<Value> data_;
1378 Local<Object> holder_;
1379 Local<Function> callee_;
1380 bool is_construct_call_;
1381 void** values_;
1382 int length_;
1383};
1384
1385
1386/**
1387 * The information passed to an accessor callback about the context
1388 * of the property access.
1389 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001390class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 public:
1392 inline AccessorInfo(Local<Object> self,
1393 Local<Value> data,
1394 Local<Object> holder)
1395 : self_(self), data_(data), holder_(holder) { }
1396 inline Local<Value> Data() const;
1397 inline Local<Object> This() const;
1398 inline Local<Object> Holder() const;
1399 private:
1400 Local<Object> self_;
1401 Local<Value> data_;
1402 Local<Object> holder_;
1403};
1404
1405
1406typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1407
1408typedef int (*LookupCallback)(Local<Object> self, Local<String> name);
1409
1410/**
1411 * Accessor[Getter|Setter] are used as callback functions when
1412 * setting|getting a particular property. See objectTemplate::SetAccessor.
1413 */
1414typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1415 const AccessorInfo& info);
1416
1417
1418typedef void (*AccessorSetter)(Local<String> property,
1419 Local<Value> value,
1420 const AccessorInfo& info);
1421
1422
1423/**
1424 * NamedProperty[Getter|Setter] are used as interceptors on object.
1425 * See ObjectTemplate::SetNamedPropertyHandler.
1426 */
1427typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1428 const AccessorInfo& info);
1429
1430
1431/**
1432 * Returns the value if the setter intercepts the request.
1433 * Otherwise, returns an empty handle.
1434 */
1435typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1436 Local<Value> value,
1437 const AccessorInfo& info);
1438
1439
1440/**
1441 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001442 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 */
1444typedef Handle<Boolean> (*NamedPropertyQuery)(Local<String> property,
1445 const AccessorInfo& info);
1446
1447
1448/**
1449 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001450 * The return value is true if the property could be deleted and false
1451 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 */
1453typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1454 const AccessorInfo& info);
1455
1456/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001457 * Returns an array containing the names of the properties the named
1458 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 */
1460typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1461
v8.team.kasperl727e9952008-09-02 14:56:44 +00001462
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001464 * Returns the value of the property if the getter intercepts the
1465 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 */
1467typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1468 const AccessorInfo& info);
1469
1470
1471/**
1472 * Returns the value if the setter intercepts the request.
1473 * Otherwise, returns an empty handle.
1474 */
1475typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1476 Local<Value> value,
1477 const AccessorInfo& info);
1478
1479
1480/**
1481 * Returns a non-empty handle if the interceptor intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001482 * The result is true if the property exists and false otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 */
1484typedef Handle<Boolean> (*IndexedPropertyQuery)(uint32_t index,
1485 const AccessorInfo& info);
1486
1487/**
1488 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001489 * The return value is true if the property could be deleted and false
1490 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 */
1492typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1493 const AccessorInfo& info);
1494
v8.team.kasperl727e9952008-09-02 14:56:44 +00001495/**
1496 * Returns an array containing the indices of the properties the
1497 * indexed property getter intercepts.
1498 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1500
1501
1502/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001503 * Access control specifications.
1504 *
1505 * Some accessors should be accessible across contexts. These
1506 * accessors have an explicit access control parameter which specifies
1507 * the kind of cross-context access that should be allowed.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001508 *
1509 * Additionally, for security, accessors can prohibit overwriting by
1510 * accessors defined in JavaScript. For objects that have such
1511 * accessors either locally or in their prototype chain it is not
1512 * possible to overwrite the accessor by using __defineGetter__ or
1513 * __defineSetter__ from JavaScript code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001514 */
1515enum AccessControl {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001516 DEFAULT = 0,
1517 ALL_CAN_READ = 1,
1518 ALL_CAN_WRITE = 1 << 1,
1519 PROHIBITS_OVERWRITING = 1 << 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520};
1521
1522
1523/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001524 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 */
1526enum AccessType {
1527 ACCESS_GET,
1528 ACCESS_SET,
1529 ACCESS_HAS,
1530 ACCESS_DELETE,
1531 ACCESS_KEYS
1532};
1533
v8.team.kasperl727e9952008-09-02 14:56:44 +00001534
1535/**
1536 * Returns true if cross-context access should be allowed to the named
1537 * property with the given key on the global object.
1538 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539typedef bool (*NamedSecurityCallback)(Local<Object> global,
1540 Local<Value> key,
1541 AccessType type,
1542 Local<Value> data);
1543
v8.team.kasperl727e9952008-09-02 14:56:44 +00001544
1545/**
1546 * Returns true if cross-context access should be allowed to the indexed
1547 * property with the given index on the global object.
1548 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549typedef bool (*IndexedSecurityCallback)(Local<Object> global,
1550 uint32_t index,
1551 AccessType type,
1552 Local<Value> data);
1553
1554
1555/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001556 * A FunctionTemplate is used to create functions at runtime. There
1557 * can only be one function created from a FunctionTemplate in a
1558 * context.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 *
1560 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001561 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001563 * A FunctionTemplate has a corresponding instance template which is
1564 * used to create object instances when the function is used as a
1565 * constructor. Properties added to the instance template are added to
1566 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 *
1568 * A FunctionTemplate can have a prototype template. The prototype template
1569 * is used to create the prototype object of the function.
1570 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001571 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001573 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1575 * t->Set("func_property", v8::Number::New(1));
1576 *
1577 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1578 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1579 * proto_t->Set("proto_const", v8::Number::New(2));
1580 *
1581 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1582 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1583 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1584 * instance_t->Set("instance_property", Number::New(3));
1585 *
1586 * v8::Local<v8::Function> function = t->GetFunction();
1587 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001588 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 *
1590 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001591 * and "instance" for the instance object created above. The function
1592 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001594 * \code
1595 * func_property in function == true;
1596 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001598 * function.prototype.proto_method() invokes 'InvokeCallback'
1599 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001601 * instance instanceof function == true;
1602 * instance.instance_accessor calls 'InstanceAccessorCallback'
1603 * instance.instance_property == 3;
1604 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001606 * A FunctionTemplate can inherit from another one by calling the
1607 * FunctionTemplate::Inherit method. The following graph illustrates
1608 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001610 * \code
1611 * FunctionTemplate Parent -> Parent() . prototype -> { }
1612 * ^ ^
1613 * | Inherit(Parent) | .__proto__
1614 * | |
1615 * FunctionTemplate Child -> Child() . prototype -> { }
1616 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001618 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
1619 * object of the Child() function has __proto__ pointing to the
1620 * Parent() function's prototype object. An instance of the Child
1621 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001623 * Let Parent be the FunctionTemplate initialized in the previous
1624 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001626 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 * Local<FunctionTemplate> parent = t;
1628 * Local<FunctionTemplate> child = FunctionTemplate::New();
1629 * child->Inherit(parent);
1630 *
1631 * Local<Function> child_function = child->GetFunction();
1632 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001633 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001635 * The Child function and Child instance will have the following
1636 * properties:
1637 *
1638 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001640 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00001642 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001644class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 public:
1646 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00001647 static Local<FunctionTemplate> New(
1648 InvocationCallback callback = 0,
1649 Handle<Value> data = Handle<Value>(),
1650 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 /** Returns the unique function instance in the current execution context.*/
1652 Local<Function> GetFunction();
1653
v8.team.kasperl727e9952008-09-02 14:56:44 +00001654 /**
1655 * Set the call-handler callback for a FunctionTemplate. This
1656 * callback is called whenever the function created from this
1657 * FunctionTemplate is called.
1658 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 void SetCallHandler(InvocationCallback callback,
1660 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661
v8.team.kasperl727e9952008-09-02 14:56:44 +00001662 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 Local<ObjectTemplate> InstanceTemplate();
1664
1665 /** Causes the function template to inherit from a parent function template.*/
1666 void Inherit(Handle<FunctionTemplate> parent);
1667
1668 /**
1669 * A PrototypeTemplate is the template used to create the prototype object
1670 * of the function created by this template.
1671 */
1672 Local<ObjectTemplate> PrototypeTemplate();
1673
v8.team.kasperl727e9952008-09-02 14:56:44 +00001674
1675 /**
1676 * Set the class name of the FunctionTemplate. This is used for
1677 * printing objects created with the function created from the
1678 * FunctionTemplate as its constructor.
1679 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 void SetClassName(Handle<String> name);
1681
1682 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001683 * Determines whether the __proto__ accessor ignores instances of
1684 * the function template. If instances of the function template are
1685 * ignored, __proto__ skips all instances and instead returns the
1686 * next object in the prototype chain.
1687 *
1688 * Call with a value of true to make the __proto__ accessor ignore
1689 * instances of the function template. Call with a value of false
1690 * to make the __proto__ accessor not ignore instances of the
1691 * function template. By default, instances of a function template
1692 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693 */
1694 void SetHiddenPrototype(bool value);
1695
1696 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001697 * Returns true if the given object is an instance of this function
1698 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 */
1700 bool HasInstance(Handle<Value> object);
1701
1702 private:
1703 FunctionTemplate();
1704 void AddInstancePropertyAccessor(Handle<String> name,
1705 AccessorGetter getter,
1706 AccessorSetter setter,
1707 Handle<Value> data,
1708 AccessControl settings,
1709 PropertyAttribute attributes);
1710 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
1711 NamedPropertySetter setter,
1712 NamedPropertyQuery query,
1713 NamedPropertyDeleter remover,
1714 NamedPropertyEnumerator enumerator,
1715 Handle<Value> data);
1716 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
1717 IndexedPropertySetter setter,
1718 IndexedPropertyQuery query,
1719 IndexedPropertyDeleter remover,
1720 IndexedPropertyEnumerator enumerator,
1721 Handle<Value> data);
1722 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
1723 Handle<Value> data);
1724
1725 friend class Context;
1726 friend class ObjectTemplate;
1727};
1728
1729
1730/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001731 * An ObjectTemplate is used to create objects at runtime.
1732 *
1733 * Properties added to an ObjectTemplate are added to each object
1734 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001736class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00001738 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 /** Creates a new instance of this template.*/
1742 Local<Object> NewInstance();
1743
1744 /**
1745 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001746 *
1747 * Whenever the property with the given name is accessed on objects
1748 * created from this ObjectTemplate the getter and setter callbacks
1749 * are called instead of getting and setting the property directly
1750 * on the JavaScript object.
1751 *
1752 * \param name The name of the property for which an accessor is added.
1753 * \param getter The callback to invoke when getting the property.
1754 * \param setter The callback to invoke when setting the property.
1755 * \param data A piece of data that will be passed to the getter and setter
1756 * callbacks whenever they are invoked.
1757 * \param settings Access control settings for the accessor. This is a bit
1758 * field consisting of one of more of
1759 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
1760 * The default is to not allow cross-context access.
1761 * ALL_CAN_READ means that all cross-context reads are allowed.
1762 * ALL_CAN_WRITE means that all cross-context writes are allowed.
1763 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
1764 * cross-context access.
1765 * \param attribute The attributes of the property for which an accessor
1766 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 */
1768 void SetAccessor(Handle<String> name,
1769 AccessorGetter getter,
1770 AccessorSetter setter = 0,
1771 Handle<Value> data = Handle<Value>(),
1772 AccessControl settings = DEFAULT,
1773 PropertyAttribute attribute = None);
1774
1775 /**
1776 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001777 *
1778 * Whenever a named property is accessed on objects created from
1779 * this object template, the provided callback is invoked instead of
1780 * accessing the property directly on the JavaScript object.
1781 *
1782 * \param getter The callback to invoke when getting a property.
1783 * \param setter The callback to invoke when setting a property.
1784 * \param query The callback to invoke to check is an object has a property.
1785 * \param deleter The callback to invoke when deleting a property.
1786 * \param enumerator The callback to invoke to enumerate all the named
1787 * properties of an object.
1788 * \param data A piece of data that will be passed to the callbacks
1789 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 */
1791 void SetNamedPropertyHandler(NamedPropertyGetter getter,
1792 NamedPropertySetter setter = 0,
1793 NamedPropertyQuery query = 0,
1794 NamedPropertyDeleter deleter = 0,
1795 NamedPropertyEnumerator enumerator = 0,
1796 Handle<Value> data = Handle<Value>());
1797
1798 /**
1799 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001800 *
1801 * Whenever an indexed property is accessed on objects created from
1802 * this object template, the provided callback is invoked instead of
1803 * accessing the property directly on the JavaScript object.
1804 *
1805 * \param getter The callback to invoke when getting a property.
1806 * \param setter The callback to invoke when setting a property.
1807 * \param query The callback to invoke to check is an object has a property.
1808 * \param deleter The callback to invoke when deleting a property.
1809 * \param enumerator The callback to invoke to enumerate all the indexed
1810 * properties of an object.
1811 * \param data A piece of data that will be passed to the callbacks
1812 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 */
1814 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
1815 IndexedPropertySetter setter = 0,
1816 IndexedPropertyQuery query = 0,
1817 IndexedPropertyDeleter deleter = 0,
1818 IndexedPropertyEnumerator enumerator = 0,
1819 Handle<Value> data = Handle<Value>());
1820 /**
1821 * Sets the callback to be used when calling instances created from
1822 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00001823 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 * function.
1825 */
1826 void SetCallAsFunctionHandler(InvocationCallback callback,
1827 Handle<Value> data = Handle<Value>());
1828
v8.team.kasperl727e9952008-09-02 14:56:44 +00001829 /**
1830 * Mark object instances of the template as undetectable.
1831 *
1832 * In many ways, undetectable objects behave as though they are not
1833 * there. They behave like 'undefined' in conditionals and when
1834 * printed. However, properties can be accessed and called as on
1835 * normal objects.
1836 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 void MarkAsUndetectable();
1838
v8.team.kasperl727e9952008-09-02 14:56:44 +00001839 /**
1840 * Sets access check callbacks on the object template.
1841 *
1842 * When accessing properties on instances of this object template,
1843 * the access check callback will be called to determine whether or
1844 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001845 * The last parameter specifies whether access checks are turned
1846 * on by default on instances. If access checks are off by default,
1847 * they can be turned on on individual instances by calling
1848 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00001849 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
1851 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001852 Handle<Value> data = Handle<Value>(),
1853 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
kasper.lund212ac232008-07-16 07:07:30 +00001855 /**
1856 * Gets the number of internal fields for objects generated from
1857 * this template.
1858 */
1859 int InternalFieldCount();
1860
1861 /**
1862 * Sets the number of internal fields for objects generated from
1863 * this template.
1864 */
1865 void SetInternalFieldCount(int value);
1866
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 private:
1868 ObjectTemplate();
1869 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
1870 friend class FunctionTemplate;
1871};
1872
1873
1874/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001875 * A Signature specifies which receivers and arguments a function can
1876 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001878class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 public:
1880 static Local<Signature> New(Handle<FunctionTemplate> receiver =
1881 Handle<FunctionTemplate>(),
1882 int argc = 0,
1883 Handle<FunctionTemplate> argv[] = 0);
1884 private:
1885 Signature();
1886};
1887
1888
1889/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001890 * A utility for determining the type of objects based on the template
1891 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001893class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894 public:
1895 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
1896 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
1897 int match(Handle<Value> value);
1898 private:
1899 TypeSwitch();
1900};
1901
1902
1903// --- E x t e n s i o n s ---
1904
1905
1906/**
1907 * Ignore
1908 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001909class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910 public:
1911 Extension(const char* name,
1912 const char* source = 0,
1913 int dep_count = 0,
1914 const char** deps = 0);
1915 virtual ~Extension() { }
1916 virtual v8::Handle<v8::FunctionTemplate>
1917 GetNativeFunction(v8::Handle<v8::String> name) {
1918 return v8::Handle<v8::FunctionTemplate>();
1919 }
1920
1921 const char* name() { return name_; }
1922 const char* source() { return source_; }
1923 int dependency_count() { return dep_count_; }
1924 const char** dependencies() { return deps_; }
1925 void set_auto_enable(bool value) { auto_enable_ = value; }
1926 bool auto_enable() { return auto_enable_; }
1927
1928 private:
1929 const char* name_;
1930 const char* source_;
1931 int dep_count_;
1932 const char** deps_;
1933 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001934
1935 // Disallow copying and assigning.
1936 Extension(const Extension&);
1937 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938};
1939
1940
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001941void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942
1943
1944/**
1945 * Ignore
1946 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001947class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 public:
1949 inline DeclareExtension(Extension* extension) {
1950 RegisterExtension(extension);
1951 }
1952};
1953
1954
1955// --- S t a t i c s ---
1956
1957
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001958Handle<Primitive> V8EXPORT Undefined();
1959Handle<Primitive> V8EXPORT Null();
1960Handle<Boolean> V8EXPORT True();
1961Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962
1963
1964/**
1965 * A set of constraints that specifies the limits of the runtime's
1966 * memory use.
1967 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001968class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 public:
1970 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001971 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001973 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001975 uint32_t* stack_limit() const { return stack_limit_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
1977 private:
1978 int max_young_space_size_;
1979 int max_old_space_size_;
1980 uint32_t* stack_limit_;
1981};
1982
1983
1984bool SetResourceConstraints(ResourceConstraints* constraints);
1985
1986
1987// --- E x c e p t i o n s ---
1988
1989
1990typedef void (*FatalErrorCallback)(const char* location, const char* message);
1991
1992
1993typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
1994
1995
1996/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001997 * Schedules an exception to be thrown when returning to JavaScript. When an
1998 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002000 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002002Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003
2004/**
2005 * Create new error objects by calling the corresponding error object
2006 * constructor with the message.
2007 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002008class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 public:
2010 static Local<Value> RangeError(Handle<String> message);
2011 static Local<Value> ReferenceError(Handle<String> message);
2012 static Local<Value> SyntaxError(Handle<String> message);
2013 static Local<Value> TypeError(Handle<String> message);
2014 static Local<Value> Error(Handle<String> message);
2015};
2016
2017
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002018// --- 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 +00002019
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002020typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002022typedef void* (*CreateHistogramCallback)(const char* name,
2023 int min,
2024 int max,
2025 size_t buckets);
2026
2027typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2028
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029// --- 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 ---
2030typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2031 AccessType type,
2032 Local<Value> data);
2033
2034// --- 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
2035
2036/**
2037 * Applications can register a callback function which is called
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 * before and after a major garbage collection. Allocations are not
2039 * allowed in the callback function, you therefore cannot manipulate
2040 * objects (set or delete properties for example) since it is possible
2041 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 */
2043typedef void (*GCCallback)();
2044
2045
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002046// --- C o n t e x t G e n e r a t o r ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
2048/**
2049 * Applications must provide a callback function which is called to generate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002050 * a context if a context was not deserialized from the snapshot.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052typedef Persistent<Context> (*ContextGenerator)();
2053
2054
2055/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002056 * Profiler modules.
2057 *
2058 * In V8, profiler consists of several modules: CPU profiler, and different
2059 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002060 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2061 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002062 */
2063enum ProfilerModules {
2064 PROFILER_MODULE_NONE = 0,
2065 PROFILER_MODULE_CPU = 1,
2066 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002067 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2068 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002069};
2070
2071
2072/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 * Container class for static utility functions.
2074 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002075class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002077 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 static void SetFatalErrorHandler(FatalErrorCallback that);
2079
v8.team.kasperl727e9952008-09-02 14:56:44 +00002080 /**
2081 * Ignore out-of-memory exceptions.
2082 *
2083 * V8 running out of memory is treated as a fatal error by default.
2084 * This means that the fatal error handler is called and that V8 is
2085 * terminated.
2086 *
2087 * IgnoreOutOfMemoryException can be used to not treat a
2088 * out-of-memory situation as a fatal error. This way, the contexts
2089 * that did not cause the out of memory problem might be able to
2090 * continue execution.
2091 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 static void IgnoreOutOfMemoryException();
2093
v8.team.kasperl727e9952008-09-02 14:56:44 +00002094 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002095 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002096 * fatal errors such as out-of-memory situations.
2097 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 static bool IsDead();
2099
2100 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002101 * Adds a message listener.
2102 *
2103 * The same message listener can be added more than once and it that
2104 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 */
2106 static bool AddMessageListener(MessageCallback that,
2107 Handle<Value> data = Handle<Value>());
2108
2109 /**
2110 * Remove all message listeners from the specified callback function.
2111 */
2112 static void RemoveMessageListeners(MessageCallback that);
2113
2114 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002115 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 */
2117 static void SetFlagsFromString(const char* str, int length);
2118
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002119 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002120 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002121 */
2122 static void SetFlagsFromCommandLine(int* argc,
2123 char** argv,
2124 bool remove_flags);
2125
kasper.lund7276f142008-07-30 08:49:36 +00002126 /** Get the version string. */
2127 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128
2129 /**
2130 * Enables the host application to provide a mechanism for recording
2131 * statistics counters.
2132 */
2133 static void SetCounterFunction(CounterLookupCallback);
2134
2135 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002136 * Enables the host application to provide a mechanism for recording
2137 * histograms. The CreateHistogram function returns a
2138 * histogram which will later be passed to the AddHistogramSample
2139 * function.
2140 */
2141 static void SetCreateHistogramFunction(CreateHistogramCallback);
2142 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2143
2144 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 * Enables the computation of a sliding window of states. The sliding
2146 * window information is recorded in statistics counters.
2147 */
2148 static void EnableSlidingStateWindow();
2149
2150 /** Callback function for reporting failed access checks.*/
2151 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2152
2153 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002154 * Enables the host application to receive a notification before a
2155 * major garbage colletion. Allocations are not allowed in the
2156 * callback function, you therefore cannot manipulate objects (set
2157 * or delete properties for example) since it is possible such
2158 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159 */
2160 static void SetGlobalGCPrologueCallback(GCCallback);
2161
2162 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002163 * Enables the host application to receive a notification after a
2164 * major garbage collection. Allocations are not allowed in the
2165 * callback function, you therefore cannot manipulate objects (set
2166 * or delete properties for example) since it is possible such
2167 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 */
2169 static void SetGlobalGCEpilogueCallback(GCCallback);
2170
2171 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * Allows the host application to group objects together. If one
2173 * object in the group is alive, all objects in the group are alive.
2174 * After each garbage collection, object groups are removed. It is
2175 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002176 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002177 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002179 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180
2181 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002182 * Initializes from snapshot if possible. Otherwise, attempts to
2183 * initialize from scratch.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 */
2185 static bool Initialize();
2186
kasper.lund7276f142008-07-30 08:49:36 +00002187 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 * Adjusts the amount of registered external memory. Used to give
2189 * V8 an indication of the amount of externally allocated memory
2190 * that is kept alive by JavaScript objects. V8 uses this to decide
2191 * when to perform global garbage collections. Registering
2192 * externally allocated memory will trigger global garbage
2193 * collections more often than otherwise in an attempt to garbage
2194 * collect the JavaScript objects keeping the externally allocated
2195 * memory alive.
2196 *
2197 * \param change_in_bytes the change in externally allocated memory
2198 * that is kept alive by JavaScript objects.
2199 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002200 */
2201 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2202
iposva@chromium.org245aa852009-02-10 00:49:54 +00002203 /**
2204 * Suspends recording of tick samples in the profiler.
2205 * When the V8 profiling mode is enabled (usually via command line
2206 * switches) this function suspends recording of tick samples.
2207 * Profiling ticks are discarded until ResumeProfiler() is called.
2208 *
2209 * See also the --prof and --prof_auto command line switches to
2210 * enable V8 profiling.
2211 */
2212 static void PauseProfiler();
2213
2214 /**
2215 * Resumes recording of tick samples in the profiler.
2216 * See also PauseProfiler().
2217 */
2218 static void ResumeProfiler();
2219
ager@chromium.org41826e72009-03-30 13:30:57 +00002220 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002221 * Return whether profiler is currently paused.
2222 */
2223 static bool IsProfilerPaused();
2224
2225 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002226 * Resumes specified profiler modules.
2227 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2228 * See ProfilerModules enum.
2229 *
2230 * \param flags Flags specifying profiler modules.
2231 */
2232 static void ResumeProfilerEx(int flags);
2233
2234 /**
2235 * Pauses specified profiler modules.
2236 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2237 * See ProfilerModules enum.
2238 *
2239 * \param flags Flags specifying profiler modules.
2240 */
2241 static void PauseProfilerEx(int flags);
2242
2243 /**
2244 * Returns active (resumed) profiler modules.
2245 * See ProfilerModules enum.
2246 *
2247 * \returns active profiler modules.
2248 */
2249 static int GetActiveProfilerModules();
2250
2251 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002252 * If logging is performed into a memory buffer (via --logfile=*), allows to
2253 * retrieve previously written messages. This can be used for retrieving
2254 * profiler log data in the application. This function is thread-safe.
2255 *
2256 * Caller provides a destination buffer that must exist during GetLogLines
2257 * call. Only whole log lines are copied into the buffer.
2258 *
2259 * \param from_pos specified a point in a buffer to read from, 0 is the
2260 * beginning of a buffer. It is assumed that caller updates its current
2261 * position using returned size value from the previous call.
2262 * \param dest_buf destination buffer for log data.
2263 * \param max_size size of the destination buffer.
2264 * \returns actual size of log data copied into buffer.
2265 */
2266 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2267
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002268 /**
2269 * Retrieve the V8 thread id of the calling thread.
2270 *
2271 * The thread id for a thread should only be retrieved after the V8
2272 * lock has been acquired with a Locker object with that thread.
2273 */
2274 static int GetCurrentThreadId();
2275
2276 /**
2277 * Forcefully terminate execution of a JavaScript thread. This can
2278 * be used to terminate long-running scripts.
2279 *
2280 * TerminateExecution should only be called when then V8 lock has
2281 * been acquired with a Locker object. Therefore, in order to be
2282 * able to terminate long-running threads, preemption must be
2283 * enabled to allow the user of TerminateExecution to acquire the
2284 * lock.
2285 *
2286 * The termination is achieved by throwing an exception that is
2287 * uncatchable by JavaScript exception handlers. Termination
2288 * exceptions act as if they were caught by a C++ TryCatch exception
2289 * handlers. If forceful termination is used, any C++ TryCatch
2290 * exception handler that catches an exception should check if that
2291 * exception is a termination exception and immediately return if
2292 * that is the case. Returning immediately in that case will
2293 * continue the propagation of the termination exception if needed.
2294 *
2295 * The thread id passed to TerminateExecution must have been
2296 * obtained by calling GetCurrentThreadId on the thread in question.
2297 *
2298 * \param thread_id The thread id of the thread to terminate.
2299 */
2300 static void TerminateExecution(int thread_id);
2301
2302 /**
2303 * Forcefully terminate the current thread of JavaScript execution.
2304 *
2305 * This method can be used by any thread even if that thread has not
2306 * acquired the V8 lock with a Locker object.
2307 */
2308 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002309
2310 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002311 * Releases any resources used by v8 and stops any utility threads
2312 * that may be running. Note that disposing v8 is permanent, it
2313 * cannot be reinitialized.
2314 *
2315 * It should generally not be necessary to dispose v8 before exiting
2316 * a process, this should happen automatically. It is only necessary
2317 * to use if the process needs the resources taken up by v8.
2318 */
2319 static bool Dispose();
2320
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002321
2322 /**
2323 * Optional notification that the embedder is idle.
2324 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002325 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002326 * \param is_high_priority tells whether the embedder is high priority.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002327 * Returns true if the embedder should stop calling IdleNotification
2328 * until real work has been done. This indicates that V8 has done
2329 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002330 */
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002331 static bool IdleNotification(bool is_high_priority);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002332
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002333 /**
2334 * Optional notification that the system is running low on memory.
2335 * V8 uses these notifications to attempt to free memory.
2336 */
2337 static void LowMemoryNotification();
2338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 private:
2340 V8();
2341
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002342 static internal::Object** GlobalizeReference(internal::Object** handle);
2343 static void DisposeGlobal(internal::Object** global_handle);
2344 static void MakeWeak(internal::Object** global_handle,
2345 void* data,
2346 WeakReferenceCallback);
2347 static void ClearWeak(internal::Object** global_handle);
2348 static bool IsGlobalNearDeath(internal::Object** global_handle);
2349 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350
2351 template <class T> friend class Handle;
2352 template <class T> friend class Local;
2353 template <class T> friend class Persistent;
2354 friend class Context;
2355};
2356
2357
2358/**
2359 * An external exception handler.
2360 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002361class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 public:
2363
2364 /**
2365 * Creates a new try/catch block and registers it with v8.
2366 */
2367 TryCatch();
2368
2369 /**
2370 * Unregisters and deletes this try/catch block.
2371 */
2372 ~TryCatch();
2373
2374 /**
2375 * Returns true if an exception has been caught by this try/catch block.
2376 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002377 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378
2379 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002380 * For certain types of exceptions, it makes no sense to continue
2381 * execution.
2382 *
2383 * Currently, the only type of exception that can be caught by a
2384 * TryCatch handler and for which it does not make sense to continue
2385 * is termination exception. Such exceptions are thrown when the
2386 * TerminateExecution methods are called to terminate a long-running
2387 * script.
2388 *
2389 * If CanContinue returns false, the correct action is to perform
2390 * any C++ cleanup needed and then return.
2391 */
2392 bool CanContinue() const;
2393
2394 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 * Returns the exception caught by this try/catch block. If no exception has
2396 * been caught an empty handle is returned.
2397 *
2398 * The returned handle is valid until this TryCatch block has been destroyed.
2399 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002400 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401
2402 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002403 * Returns the .stack property of the thrown object. If no .stack
2404 * property is present an empty handle is returned.
2405 */
2406 Local<Value> StackTrace() const;
2407
2408 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002409 * Returns the message associated with this exception. If there is
2410 * no message associated an empty handle is returned.
2411 *
2412 * The returned handle is valid until this TryCatch block has been
2413 * destroyed.
2414 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002415 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002416
2417 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418 * Clears any exceptions that may have been caught by this try/catch block.
2419 * After this method has been called, HasCaught() will return false.
2420 *
2421 * It is not necessary to clear a try/catch block before using it again; if
2422 * another exception is thrown the previously caught exception will just be
2423 * overwritten. However, it is often a good idea since it makes it easier
2424 * to determine which operation threw a given exception.
2425 */
2426 void Reset();
2427
v8.team.kasperl727e9952008-09-02 14:56:44 +00002428 /**
2429 * Set verbosity of the external exception handler.
2430 *
2431 * By default, exceptions that are caught by an external exception
2432 * handler are not reported. Call SetVerbose with true on an
2433 * external exception handler to have exceptions caught by the
2434 * handler reported as if they were not caught.
2435 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 void SetVerbose(bool value);
2437
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002438 /**
2439 * Set whether or not this TryCatch should capture a Message object
2440 * which holds source information about where the exception
2441 * occurred. True by default.
2442 */
2443 void SetCaptureMessage(bool value);
2444
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445 public:
2446 TryCatch* next_;
2447 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002448 void* message_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002449 bool is_verbose_;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002450 bool can_continue_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002451 bool capture_message_;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002452 void* js_handler_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453};
2454
2455
2456// --- C o n t e x t ---
2457
2458
2459/**
2460 * Ignore
2461 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002462class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463 public:
2464 ExtensionConfiguration(int name_count, const char* names[])
2465 : name_count_(name_count), names_(names) { }
2466 private:
2467 friend class ImplementationUtilities;
2468 int name_count_;
2469 const char** names_;
2470};
2471
2472
2473/**
2474 * A sandboxed execution context with its own set of built-in objects
2475 * and functions.
2476 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002477class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002479 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 Local<Object> Global();
2481
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002482 /**
2483 * Detaches the global object from its context before
2484 * the global object can be reused to create a new context.
2485 */
2486 void DetachGlobal();
2487
v8.team.kasperl727e9952008-09-02 14:56:44 +00002488 /** Creates a new context. */
2489 static Persistent<Context> New(
2490 ExtensionConfiguration* extensions = 0,
2491 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
2492 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002493
kasper.lund44510672008-07-25 07:37:58 +00002494 /** Returns the last entered context. */
2495 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496
kasper.lund44510672008-07-25 07:37:58 +00002497 /** Returns the context that is on the top of the stack. */
2498 static Local<Context> GetCurrent();
2499
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00002501 * Returns the context of the calling JavaScript code. That is the
2502 * context of the top-most JavaScript frame. If there are no
2503 * JavaScript frames an empty handle is returned.
2504 */
2505 static Local<Context> GetCalling();
2506
2507 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 * Sets the security token for the context. To access an object in
2509 * another context, the security tokens must match.
2510 */
2511 void SetSecurityToken(Handle<Value> token);
2512
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002513 /** Restores the security token to the default value. */
2514 void UseDefaultSecurityToken();
2515
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002516 /** Returns the security token of this context.*/
2517 Handle<Value> GetSecurityToken();
2518
v8.team.kasperl727e9952008-09-02 14:56:44 +00002519 /**
2520 * Enter this context. After entering a context, all code compiled
2521 * and run is compiled and run in this context. If another context
2522 * is already entered, this old context is saved so it can be
2523 * restored when the new context is exited.
2524 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002526
2527 /**
2528 * Exit this context. Exiting the current context restores the
2529 * context that was in place when entering the current context.
2530 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 void Exit();
2532
v8.team.kasperl727e9952008-09-02 14:56:44 +00002533 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 bool HasOutOfMemoryException();
2535
v8.team.kasperl727e9952008-09-02 14:56:44 +00002536 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537 static bool InContext();
2538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002539 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002540 * Associate an additional data object with the context. This is mainly used
2541 * with the debugger to provide additional information on the context through
2542 * the debugger API.
2543 */
2544 void SetData(Handle<Value> data);
2545 Local<Value> GetData();
2546
2547 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 * Stack-allocated class which sets the execution context for all
2549 * operations executed within a local scope.
2550 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002551 class V8EXPORT Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552 public:
2553 inline Scope(Handle<Context> context) : context_(context) {
2554 context_->Enter();
2555 }
2556 inline ~Scope() { context_->Exit(); }
2557 private:
2558 Handle<Context> context_;
2559 };
2560
2561 private:
2562 friend class Value;
2563 friend class Script;
2564 friend class Object;
2565 friend class Function;
2566};
2567
2568
2569/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002570 * Multiple threads in V8 are allowed, but only one thread at a time
2571 * is allowed to use V8. The definition of 'using V8' includes
2572 * accessing handles or holding onto object pointers obtained from V8
2573 * handles. It is up to the user of V8 to ensure (perhaps with
2574 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575 *
2576 * If you wish to start using V8 in a thread you can do this by constructing
2577 * a v8::Locker object. After the code using V8 has completed for the
2578 * current thread you can call the destructor. This can be combined
2579 * with C++ scope-based construction as follows:
2580 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002581 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 * ...
2583 * {
2584 * v8::Locker locker;
2585 * ...
2586 * // Code using V8 goes here.
2587 * ...
2588 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00002589 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 *
2591 * If you wish to stop using V8 in a thread A you can do this by either
2592 * by destroying the v8::Locker object as above or by constructing a
2593 * v8::Unlocker object:
2594 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002595 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 * {
2597 * v8::Unlocker unlocker;
2598 * ...
2599 * // Code not using V8 goes here while V8 can run in another thread.
2600 * ...
2601 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002602 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 *
2604 * The Unlocker object is intended for use in a long-running callback
2605 * from V8, where you want to release the V8 lock for other threads to
2606 * use.
2607 *
2608 * The v8::Locker is a recursive lock. That is, you can lock more than
2609 * once in a given thread. This can be useful if you have code that can
2610 * be called either from code that holds the lock or from code that does
2611 * not. The Unlocker is not recursive so you can not have several
2612 * Unlockers on the stack at once, and you can not use an Unlocker in a
2613 * thread that is not inside a Locker's scope.
2614 *
2615 * An unlocker will unlock several lockers if it has to and reinstate
2616 * the correct depth of locking on its destruction. eg.:
2617 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002618 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 * // V8 not locked.
2620 * {
2621 * v8::Locker locker;
2622 * // V8 locked.
2623 * {
2624 * v8::Locker another_locker;
2625 * // V8 still locked (2 levels).
2626 * {
2627 * v8::Unlocker unlocker;
2628 * // V8 not locked.
2629 * }
2630 * // V8 locked again (2 levels).
2631 * }
2632 * // V8 still locked (1 level).
2633 * }
2634 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002635 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002637class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 public:
2639 Unlocker();
2640 ~Unlocker();
2641};
2642
2643
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002644class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 public:
2646 Locker();
2647 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002648
2649 /**
2650 * Start preemption.
2651 *
2652 * When preemption is started, a timer is fired every n milli seconds
2653 * that will switch between multiple threads that are in contention
2654 * for the V8 lock.
2655 */
2656 static void StartPreemption(int every_n_ms);
2657
2658 /**
2659 * Stop preemption.
2660 */
2661 static void StopPreemption();
2662
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002663 /**
2664 * Returns whether or not the locker is locked by the current thread.
2665 */
2666 static bool IsLocked();
2667
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002668 /**
2669 * Returns whether v8::Locker is being used by this V8 instance.
2670 */
2671 static bool IsActive() { return active_; }
2672
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002673 private:
2674 bool has_lock_;
2675 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002676
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002677 static bool active_;
2678
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002679 // Disallow copying and assigning.
2680 Locker(const Locker&);
2681 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682};
2683
2684
2685
2686// --- I m p l e m e n t a t i o n ---
2687
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002688
2689namespace internal {
2690
2691
2692// Tag information for HeapObject.
2693const int kHeapObjectTag = 1;
2694const int kHeapObjectTagSize = 2;
2695const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
2696
2697
2698// Tag information for Smi.
2699const int kSmiTag = 0;
2700const int kSmiTagSize = 1;
2701const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
2702
2703
2704/**
2705 * This class exports constants and functionality from within v8 that
2706 * is necessary to implement inline functions in the v8 api. Don't
2707 * depend on functions and constants defined here.
2708 */
2709class Internals {
2710 public:
2711
2712 // These values match non-compiler-dependent values defined within
2713 // the implementation of v8.
2714 static const int kHeapObjectMapOffset = 0;
2715 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
2716 static const int kStringResourceOffset = 2 * sizeof(void*);
2717 static const int kProxyProxyOffset = sizeof(void*);
2718 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
2719 static const int kFullStringRepresentationMask = 0x07;
2720 static const int kExternalTwoByteRepresentationTag = 0x03;
2721 static const int kAlignedPointerShift = 2;
2722
2723 // These constants are compiler dependent so their values must be
2724 // defined within the implementation.
2725 static int kJSObjectType;
2726 static int kFirstNonstringType;
2727 static int kProxyType;
2728
2729 static inline bool HasHeapObjectTag(internal::Object* value) {
2730 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
2731 kHeapObjectTag);
2732 }
2733
2734 static inline bool HasSmiTag(internal::Object* value) {
2735 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
2736 }
2737
2738 static inline int SmiValue(internal::Object* value) {
2739 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> kSmiTagSize;
2740 }
2741
2742 static inline bool IsExternalTwoByteString(int instance_type) {
2743 int representation = (instance_type & kFullStringRepresentationMask);
2744 return representation == kExternalTwoByteRepresentationTag;
2745 }
2746
2747 template <typename T>
2748 static inline T ReadField(Object* ptr, int offset) {
2749 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
2750 return *reinterpret_cast<T*>(addr);
2751 }
2752
2753};
2754
2755}
2756
2757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758template <class T>
2759Handle<T>::Handle() : val_(0) { }
2760
2761
2762template <class T>
2763Local<T>::Local() : Handle<T>() { }
2764
2765
2766template <class T>
2767Local<T> Local<T>::New(Handle<T> that) {
2768 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002769 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
2771}
2772
2773
2774template <class T>
2775Persistent<T> Persistent<T>::New(Handle<T> that) {
2776 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002777 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
2779}
2780
2781
2782template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002783bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002785 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786}
2787
2788
2789template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00002790bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002791 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002792 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793}
2794
2795
2796template <class T>
2797void Persistent<T>::Dispose() {
2798 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002799 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800}
2801
2802
2803template <class T>
2804Persistent<T>::Persistent() : Handle<T>() { }
2805
2806template <class T>
2807void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002808 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
2809 parameters,
2810 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002811}
2812
2813template <class T>
2814void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002815 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816}
2817
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818Local<Value> Arguments::operator[](int i) const {
2819 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
2820 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
2821}
2822
2823
2824Local<Function> Arguments::Callee() const {
2825 return callee_;
2826}
2827
2828
2829Local<Object> Arguments::This() const {
2830 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
2831}
2832
2833
2834Local<Object> Arguments::Holder() const {
2835 return holder_;
2836}
2837
2838
2839Local<Value> Arguments::Data() const {
2840 return data_;
2841}
2842
2843
2844bool Arguments::IsConstructCall() const {
2845 return is_construct_call_;
2846}
2847
2848
2849int Arguments::Length() const {
2850 return length_;
2851}
2852
2853
2854Local<Value> AccessorInfo::Data() const {
2855 return data_;
2856}
2857
2858
2859Local<Object> AccessorInfo::This() const {
2860 return self_;
2861}
2862
2863
2864Local<Object> AccessorInfo::Holder() const {
2865 return holder_;
2866}
2867
2868
2869template <class T>
2870Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002871 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
2872 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873 return Local<T>(reinterpret_cast<T*>(after));
2874}
2875
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002876Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877 return resource_name_;
2878}
2879
2880
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002881Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882 return resource_line_offset_;
2883}
2884
2885
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002886Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002887 return resource_column_offset_;
2888}
2889
2890
2891Handle<Boolean> Boolean::New(bool value) {
2892 return value ? True() : False();
2893}
2894
2895
2896void Template::Set(const char* name, v8::Handle<Data> value) {
2897 Set(v8::String::New(name), value);
2898}
2899
2900
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002901Local<Value> Object::GetInternalField(int index) {
2902#ifndef V8_ENABLE_CHECKS
2903 Local<Value> quick_result = UncheckedGetInternalField(index);
2904 if (!quick_result.IsEmpty()) return quick_result;
2905#endif
2906 return CheckedGetInternalField(index);
2907}
2908
2909
2910Local<Value> Object::UncheckedGetInternalField(int index) {
2911 typedef internal::Object O;
2912 typedef internal::Internals I;
2913 O* obj = *reinterpret_cast<O**>(this);
2914 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2915 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2916 if (instance_type == I::kJSObjectType) {
2917 // If the object is a plain JSObject, which is the common case,
2918 // we know where to find the internal fields and can return the
2919 // value directly.
2920 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
2921 O* value = I::ReadField<O*>(obj, offset);
2922 O** result = HandleScope::CreateHandle(value);
2923 return Local<Value>(reinterpret_cast<Value*>(result));
2924 } else {
2925 return Local<Value>();
2926 }
2927}
2928
2929
2930void* External::Unwrap(Handle<v8::Value> obj) {
2931#ifdef V8_ENABLE_CHECKS
2932 return FullUnwrap(obj);
2933#else
2934 return QuickUnwrap(obj);
2935#endif
2936}
2937
2938
2939void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
2940 typedef internal::Object O;
2941 typedef internal::Internals I;
2942 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
2943 if (I::HasSmiTag(obj)) {
2944 int value = I::SmiValue(obj) << I::kAlignedPointerShift;
2945 return reinterpret_cast<void*>(value);
2946 } else {
2947 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2948 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2949 if (instance_type == I::kProxyType) {
2950 return I::ReadField<void*>(obj, I::kProxyProxyOffset);
2951 } else {
2952 return NULL;
2953 }
2954 }
2955}
2956
2957
2958void* Object::GetPointerFromInternalField(int index) {
2959 return External::Unwrap(GetInternalField(index));
2960}
2961
2962
2963String* String::Cast(v8::Value* value) {
2964#ifdef V8_ENABLE_CHECKS
2965 CheckCast(value);
2966#endif
2967 return static_cast<String*>(value);
2968}
2969
2970
2971String::ExternalStringResource* String::GetExternalStringResource() const {
2972 typedef internal::Object O;
2973 typedef internal::Internals I;
2974 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
2975 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
2976 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
2977 String::ExternalStringResource* result;
2978 if (I::IsExternalTwoByteString(instance_type)) {
2979 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
2980 result = reinterpret_cast<String::ExternalStringResource*>(value);
2981 } else {
2982 result = NULL;
2983 }
2984#ifdef V8_ENABLE_CHECKS
2985 VerifyExternalStringResource(result);
2986#endif
2987 return result;
2988}
2989
2990
2991bool Value::IsString() const {
2992#ifdef V8_ENABLE_CHECKS
2993 return FullIsString();
2994#else
2995 return QuickIsString();
2996#endif
2997}
2998
2999bool Value::QuickIsString() const {
3000 typedef internal::Object O;
3001 typedef internal::Internals I;
3002 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3003 if (!I::HasHeapObjectTag(obj)) return false;
3004 O* map = I::ReadField<O*>(obj, I::kHeapObjectMapOffset);
3005 int instance_type = I::ReadField<uint8_t>(map, I::kMapInstanceTypeOffset);
3006 return (instance_type < I::kFirstNonstringType);
3007}
3008
3009
3010Number* Number::Cast(v8::Value* value) {
3011#ifdef V8_ENABLE_CHECKS
3012 CheckCast(value);
3013#endif
3014 return static_cast<Number*>(value);
3015}
3016
3017
3018Integer* Integer::Cast(v8::Value* value) {
3019#ifdef V8_ENABLE_CHECKS
3020 CheckCast(value);
3021#endif
3022 return static_cast<Integer*>(value);
3023}
3024
3025
3026Date* Date::Cast(v8::Value* value) {
3027#ifdef V8_ENABLE_CHECKS
3028 CheckCast(value);
3029#endif
3030 return static_cast<Date*>(value);
3031}
3032
3033
3034Object* Object::Cast(v8::Value* value) {
3035#ifdef V8_ENABLE_CHECKS
3036 CheckCast(value);
3037#endif
3038 return static_cast<Object*>(value);
3039}
3040
3041
3042Array* Array::Cast(v8::Value* value) {
3043#ifdef V8_ENABLE_CHECKS
3044 CheckCast(value);
3045#endif
3046 return static_cast<Array*>(value);
3047}
3048
3049
3050Function* Function::Cast(v8::Value* value) {
3051#ifdef V8_ENABLE_CHECKS
3052 CheckCast(value);
3053#endif
3054 return static_cast<Function*>(value);
3055}
3056
3057
3058External* External::Cast(v8::Value* value) {
3059#ifdef V8_ENABLE_CHECKS
3060 CheckCast(value);
3061#endif
3062 return static_cast<External*>(value);
3063}
3064
3065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003067 * \example shell.cc
3068 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069 * command-line and executes them.
3070 */
3071
3072
3073/**
3074 * \example process.cc
3075 */
3076
3077
3078} // namespace v8
3079
3080
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003081#undef V8EXPORT
3082#undef V8EXPORT_INLINE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003083#undef TYPE_CHECK
3084
3085
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003086#endif // V8_H_