blob: c7e4552b4d449092effb0b711742f8c69757d522 [file] [log] [blame]
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001// Copyright 2007-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
v8.team.kasperl727e9952008-09-02 14:56:44 +000028/** \mainpage V8 API Reference Guide
ager@chromium.org9258b6b2008-09-11 09:11:10 +000029 *
30 * V8 is Google's open source JavaScript engine.
v8.team.kasperl727e9952008-09-02 14:56:44 +000031 *
32 * This set of documents provides reference material generated from the
33 * V8 header file, include/v8.h.
34 *
35 * For other documentation see http://code.google.com/apis/v8/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036 */
v8.team.kasperl727e9952008-09-02 14:56:44 +000037
ager@chromium.org9258b6b2008-09-11 09:11:10 +000038#ifndef V8_H_
39#define V8_H_
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41#include <stdio.h>
42
43#ifdef _WIN32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044// When compiling on MinGW stdint.h is available.
45#ifdef __MINGW32__
46#include <stdint.h>
47#else // __MINGW32__
ager@chromium.org5ec48922009-05-05 07:25:34 +000048typedef signed char int8_t;
49typedef unsigned char uint8_t;
50typedef short int16_t; // NOLINT
51typedef unsigned short uint16_t; // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052typedef int int32_t;
53typedef unsigned int uint32_t;
ager@chromium.org5ec48922009-05-05 07:25:34 +000054typedef __int64 int64_t;
55typedef unsigned __int64 uint64_t;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000056// intptr_t and friends are defined in crtdefs.h through stdio.h.
57#endif // __MINGW32__
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000058
59// Setup for Windows DLL export/import. When building the V8 DLL the
60// BUILDING_V8_SHARED needs to be defined. When building a program which uses
61// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
62// static library or building a program which uses the V8 static library neither
63// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000064#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
65#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
66 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067#endif
68
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000069#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000070#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000071#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000072#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000075#endif // BUILDING_V8_SHARED
76
77#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000078
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079#include <stdint.h>
80
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000081// Setup for Linux shared library export. There is no need to distinguish
82// between building or using the V8 shared library, but we should not
83// export symbols when we are building a static library.
84#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000085#define V8EXPORT __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000087#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000088#endif // defined(__GNUC__) && (__GNUC__ >= 4)
89
90#endif // _WIN32
91
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000093 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 */
95namespace v8 {
96
97class Context;
98class String;
99class Value;
100class Utils;
101class Number;
102class Object;
103class Array;
104class Int32;
105class Uint32;
106class External;
107class Primitive;
108class Boolean;
109class Integer;
110class Function;
111class Date;
112class ImplementationUtilities;
113class Signature;
114template <class T> class Handle;
115template <class T> class Local;
116template <class T> class Persistent;
117class FunctionTemplate;
118class ObjectTemplate;
119class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000120class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000121class StackTrace;
122class StackFrame;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000124namespace internal {
125
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000126class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000127class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000128class Heap;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000129class Top;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000130
131}
132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133
134// --- W e a k H a n d l e s
135
136
137/**
138 * A weak reference callback function.
139 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000140 * This callback should either explicitly invoke Dispose on |object| if
141 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
142 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 * \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
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000152#define TYPE_CHECK(T, S) \
153 while (false) { \
154 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 }
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 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000182template <class T> class 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 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000193 inline explicit Handle(T* val) : val_(val) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194
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 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000218 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000220 inline T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000222 inline 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 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000227 inline void Clear() { this->val_ = 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228
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 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000235 template <class S> inline 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 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000249 template <class S> inline 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
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000262 template <class S> inline Handle<S> As() {
263 return Handle<S>::Cast(*this);
264 }
265
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 private:
267 T* val_;
268};
269
270
271/**
272 * A light-weight stack-allocated object handle. All operations
273 * that return objects from within v8 return them in local handles. They
274 * are created within HandleScopes, and all local handles allocated within a
275 * handle scope are destroyed when the handle scope is destroyed. Hence it
276 * is not necessary to explicitly deallocate local handles.
277 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000278template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000280 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 template <class S> inline Local(Local<S> that)
282 : Handle<T>(reinterpret_cast<T*>(*that)) {
283 /**
284 * This check fails when trying to convert between incompatible
285 * handles. For example, converting from a Handle<String> to a
286 * Handle<Number>.
287 */
288 TYPE_CHECK(T, S);
289 }
290 template <class S> inline Local(S* that) : Handle<T>(that) { }
291 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000292#ifdef V8_ENABLE_CHECKS
293 // If we're going to perform the type check then we have to check
294 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000296#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 return Local<T>(T::Cast(*that));
298 }
299
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000300 template <class S> inline Local<S> As() {
301 return Local<S>::Cast(*this);
302 }
303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 /** Create a local handle for the content of another handle.
305 * The referee is kept alive by the local handle even when
306 * the original handle is destroyed/disposed.
307 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000308 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309};
310
311
312/**
313 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000314 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000315 * allocated, a Persistent handle remains valid until it is explicitly
316 * disposed.
317 *
318 * A persistent handle contains a reference to a storage cell within
319 * the v8 engine which holds an object value and which is updated by
320 * the garbage collector whenever the object is moved. A new storage
321 * cell can be created using Persistent::New and existing handles can
322 * be disposed using Persistent::Dispose. Since persistent handles
323 * are passed by value you may have many persistent handle objects
324 * that point to the same storage cell. For instance, if you pass a
325 * persistent handle as an argument to a function you will not get two
326 * different storage cells but rather two references to the same
327 * storage cell.
328 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000329template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 public:
331
332 /**
333 * Creates an empty persistent handle that doesn't point to any
334 * storage cell.
335 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000336 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337
338 /**
339 * Creates a persistent handle for the same storage cell as the
340 * specified handle. This constructor allows you to pass persistent
341 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000342 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 * persistent handles, for instance from a Persistent<String> to a
v8.team.kasperl727e9952008-09-02 14:56:44 +0000344 * Persistent<Number> will cause a compiletime error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345 * between compatible persistent handles, for instance assigning a
346 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000347 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000348 */
349 template <class S> inline Persistent(Persistent<S> that)
350 : Handle<T>(reinterpret_cast<T*>(*that)) {
351 /**
352 * This check fails when trying to convert between incompatible
353 * handles. For example, converting from a Handle<String> to a
354 * Handle<Number>.
355 */
356 TYPE_CHECK(T, S);
357 }
358
359 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
360
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000361 /**
362 * "Casts" a plain handle which is known to be a persistent handle
363 * to a persistent handle.
364 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 template <class S> explicit inline Persistent(Handle<S> that)
366 : Handle<T>(*that) { }
367
368 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000369#ifdef V8_ENABLE_CHECKS
370 // If we're going to perform the type check then we have to check
371 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000373#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374 return Persistent<T>(T::Cast(*that));
375 }
376
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000377 template <class S> inline Persistent<S> As() {
378 return Persistent<S>::Cast(*this);
379 }
380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000382 * Creates a new persistent handle for an existing local or
383 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000385 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386
387 /**
388 * Releases the storage cell referenced by this persistent handle.
389 * Does not remove the reference to the cell from any handles.
390 * This handle's reference, and any any other references to the storage
391 * cell remain and IsEmpty will still return false.
392 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000393 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395 /**
396 * Make the reference to this object weak. When only weak handles
397 * refer to the object, the garbage collector will perform a
398 * callback to the given V8::WeakReferenceCallback function, passing
399 * it the object reference and the given parameters.
400 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000401 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402
403 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000404 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000405
406 /**
407 *Checks if the handle holds the only reference to an object.
408 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000409 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410
411 /**
412 * Returns true if the handle's reference is weak.
413 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000414 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415
416 private:
417 friend class ImplementationUtilities;
418 friend class ObjectTemplate;
419};
420
421
v8.team.kasperl727e9952008-09-02 14:56:44 +0000422 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 * A stack-allocated class that governs a number of local handles.
424 * After a handle scope has been created, all local handles will be
425 * allocated within that handle scope until either the handle scope is
426 * deleted or another handle scope is created. If there is already a
427 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000428 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 * new handles will again be allocated in the original handle scope.
430 *
431 * After the handle scope of a local handle has been deleted the
432 * garbage collector will no longer track the object stored in the
433 * handle and may deallocate it. The behavior of accessing a handle
434 * for which the handle scope has been deleted is undefined.
435 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000436class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000438 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000439
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000440 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441
442 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 * Closes the handle scope and returns the value as a handle in the
444 * previous scope, which is the new current scope after the call.
445 */
446 template <class T> Local<T> Close(Handle<T> value);
447
448 /**
449 * Counts the number of allocated handles.
450 */
451 static int NumberOfHandles();
452
453 /**
454 * Creates a new handle with the given value.
455 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000456 static internal::Object** CreateHandle(internal::Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457
458 private:
459 // Make it impossible to create heap-allocated or illegal handle
460 // scopes by disallowing certain operations.
461 HandleScope(const HandleScope&);
462 void operator=(const HandleScope&);
463 void* operator new(size_t size);
464 void operator delete(void*, size_t);
465
ager@chromium.org3811b432009-10-28 14:53:37 +0000466 // This Data class is accessible internally as HandleScopeData through a
467 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000468 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000470 internal::Object** next;
471 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000472 int level;
473
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000476 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 }
478 };
lrn@chromium.org303ada72010-10-27 09:33:13 +0000479
480 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
lrn@chromium.org303ada72010-10-27 09:33:13 +0000482
483 internal::Object** prev_next_;
484 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000486 // Allow for the active closing of HandleScopes which allows to pass a handle
487 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000489 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 friend class ImplementationUtilities;
492};
493
494
495// --- S p e c i a l o b j e c t s ---
496
497
498/**
499 * The superclass of values and API object templates.
500 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000501class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 private:
503 Data();
504};
505
506
507/**
508 * Pre-compilation data that can be associated with a script. This
509 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000510 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000511 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000513class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 public:
515 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000516
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000517 /**
518 * Pre-compiles the specified script (context-independent).
519 *
520 * \param input Pointer to UTF-8 script source code.
521 * \param length Length of UTF-8 script source code.
522 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000525 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000526 * Pre-compiles the specified script (context-independent).
527 *
528 * NOTE: Pre-compilation using this method cannot happen on another thread
529 * without using Lockers.
530 *
531 * \param source Script source code.
532 */
533 static ScriptData* PreCompile(Handle<String> source);
534
535 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000536 * Load previous pre-compilation data.
537 *
538 * \param data Pointer to data returned by a call to Data() of a previous
539 * ScriptData. Ownership is not transferred.
540 * \param length Length of data.
541 */
542 static ScriptData* New(const char* data, int length);
543
544 /**
545 * Returns the length of Data().
546 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000548
549 /**
550 * Returns a serialized representation of this ScriptData that can later be
551 * passed to New(). NOTE: Serialized data is platform-dependent.
552 */
553 virtual const char* Data() = 0;
554
555 /**
556 * Returns true if the source code could not be parsed.
557 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000558 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559};
560
561
562/**
563 * The origin, within a file, of a script.
564 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000565class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000567 inline ScriptOrigin(
568 Handle<Value> resource_name,
569 Handle<Integer> resource_line_offset = Handle<Integer>(),
570 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 : resource_name_(resource_name),
572 resource_line_offset_(resource_line_offset),
573 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000574 inline Handle<Value> ResourceName() const;
575 inline Handle<Integer> ResourceLineOffset() const;
576 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000578 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 Handle<Integer> resource_line_offset_;
580 Handle<Integer> resource_column_offset_;
581};
582
583
584/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000585 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000587class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 public:
589
590 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000591 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000592 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000593 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000594 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000595 * when New() returns
596 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
597 * using pre_data speeds compilation if it's done multiple times.
598 * Owned by caller, no references are kept when New() returns.
599 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000600 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 * available to compile event handlers.
602 * \return Compiled script object (context independent; when run it
603 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 static Local<Script> New(Handle<String> source,
606 ScriptOrigin* origin = NULL,
607 ScriptData* pre_data = NULL,
608 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
mads.s.agercbaa0602008-08-14 13:41:48 +0000610 /**
611 * Compiles the specified script using the specified file name
612 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000613 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000614 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000615 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000616 * as the script's origin.
617 * \return Compiled script object (context independent; when run it
618 * will use the currently entered context).
619 */
620 static Local<Script> New(Handle<String> source,
621 Handle<Value> file_name);
622
623 /**
624 * Compiles the specified script (bound to current context).
625 *
626 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000627 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 * when Compile() returns
629 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
630 * using pre_data speeds compilation if it's done multiple times.
631 * Owned by caller, no references are kept when Compile() returns.
632 * \param script_data Arbitrary data associated with script. Using
633 * this has same effect as calling SetData(), but makes data available
634 * earlier (i.e. to compile event handlers).
635 * \return Compiled script object, bound to the context that was active
636 * when this function was called. When run it will always use this
637 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000638 */
639 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 ScriptOrigin* origin = NULL,
641 ScriptData* pre_data = NULL,
642 Handle<String> script_data = Handle<String>());
643
644 /**
645 * Compiles the specified script using the specified file name
646 * object (typically a string) as the script's origin.
647 *
648 * \param source Script source code.
649 * \param file_name File name to use as script's origin
650 * \param script_data Arbitrary data associated with script. Using
651 * this has same effect as calling SetData(), but makes data available
652 * earlier (i.e. to compile event handlers).
653 * \return Compiled script object, bound to the context that was active
654 * when this function was called. When run it will always use this
655 * context.
656 */
657 static Local<Script> Compile(Handle<String> source,
658 Handle<Value> file_name,
659 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000660
v8.team.kasperl727e9952008-09-02 14:56:44 +0000661 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000662 * Runs the script returning the resulting value. If the script is
663 * context independent (created using ::New) it will be run in the
664 * currently entered context. If it is context specific (created
665 * using ::Compile) it will be run in the context in which it was
666 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000667 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000669
670 /**
671 * Returns the script id value.
672 */
673 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000674
675 /**
676 * Associate an additional data object with the script. This is mainly used
677 * with the debugger as this data object is only available through the
678 * debugger API.
679 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000680 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681};
682
683
684/**
685 * An error message.
686 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000687class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000689 Local<String> Get() const;
690 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000691
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000692 /**
693 * Returns the resource name for the script from where the function causing
694 * the error originates.
695 */
ager@chromium.org32912102009-01-16 10:38:43 +0000696 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000698 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000699 * Returns the resource data for the script from where the function causing
700 * the error originates.
701 */
702 Handle<Value> GetScriptData() const;
703
704 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000705 * Exception stack trace. By default stack traces are not captured for
706 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
707 * to change this option.
708 */
709 Handle<StackTrace> GetStackTrace() const;
710
711 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000712 * Returns the number, 1-based, of the line where the error occurred.
713 */
ager@chromium.org32912102009-01-16 10:38:43 +0000714 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000716 /**
717 * Returns the index within the script of the first character where
718 * the error occurred.
719 */
ager@chromium.org32912102009-01-16 10:38:43 +0000720 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000721
722 /**
723 * Returns the index within the script of the last character where
724 * the error occurred.
725 */
ager@chromium.org32912102009-01-16 10:38:43 +0000726 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000727
728 /**
729 * Returns the index within the line of the first character where
730 * the error occurred.
731 */
ager@chromium.org32912102009-01-16 10:38:43 +0000732 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000733
734 /**
735 * Returns the index within the line of the last character where
736 * the error occurred.
737 */
ager@chromium.org32912102009-01-16 10:38:43 +0000738 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000739
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740 // TODO(1245381): Print to a string instead of on a FILE.
741 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000742
743 static const int kNoLineNumberInfo = 0;
744 static const int kNoColumnInfo = 0;
745};
746
747
748/**
749 * Representation of a JavaScript stack trace. The information collected is a
750 * snapshot of the execution stack and the information remains valid after
751 * execution continues.
752 */
753class V8EXPORT StackTrace {
754 public:
755 /**
756 * Flags that determine what information is placed captured for each
757 * StackFrame when grabbing the current stack trace.
758 */
759 enum StackTraceOptions {
760 kLineNumber = 1,
761 kColumnOffset = 1 << 1 | kLineNumber,
762 kScriptName = 1 << 2,
763 kFunctionName = 1 << 3,
764 kIsEval = 1 << 4,
765 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000766 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000767 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000768 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000769 };
770
771 /**
772 * Returns a StackFrame at a particular index.
773 */
774 Local<StackFrame> GetFrame(uint32_t index) const;
775
776 /**
777 * Returns the number of StackFrames.
778 */
779 int GetFrameCount() const;
780
781 /**
782 * Returns StackTrace as a v8::Array that contains StackFrame objects.
783 */
784 Local<Array> AsArray();
785
786 /**
787 * Grab a snapshot of the the current JavaScript execution stack.
788 *
789 * \param frame_limit The maximum number of stack frames we want to capture.
790 * \param options Enumerates the set of things we will capture for each
791 * StackFrame.
792 */
793 static Local<StackTrace> CurrentStackTrace(
794 int frame_limit,
795 StackTraceOptions options = kOverview);
796};
797
798
799/**
800 * A single JavaScript stack frame.
801 */
802class V8EXPORT StackFrame {
803 public:
804 /**
805 * Returns the number, 1-based, of the line for the associate function call.
806 * This method will return Message::kNoLineNumberInfo if it is unable to
807 * retrieve the line number, or if kLineNumber was not passed as an option
808 * when capturing the StackTrace.
809 */
810 int GetLineNumber() const;
811
812 /**
813 * Returns the 1-based column offset on the line for the associated function
814 * call.
815 * This method will return Message::kNoColumnInfo if it is unable to retrieve
816 * the column number, or if kColumnOffset was not passed as an option when
817 * capturing the StackTrace.
818 */
819 int GetColumn() const;
820
821 /**
822 * Returns the name of the resource that contains the script for the
823 * function for this StackFrame.
824 */
825 Local<String> GetScriptName() const;
826
827 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000828 * Returns the name of the resource that contains the script for the
829 * function for this StackFrame or sourceURL value if the script name
830 * is undefined and its source ends with //@ sourceURL=... string.
831 */
832 Local<String> GetScriptNameOrSourceURL() const;
833
834 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000835 * Returns the name of the function associated with this stack frame.
836 */
837 Local<String> GetFunctionName() const;
838
839 /**
840 * Returns whether or not the associated function is compiled via a call to
841 * eval().
842 */
843 bool IsEval() const;
844
845 /**
846 * Returns whther or not the associated function is called as a
847 * constructor via "new".
848 */
849 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850};
851
852
853// --- V a l u e ---
854
855
856/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000857 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000859class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860 public:
861
862 /**
863 * Returns true if this value is the undefined value. See ECMA-262
864 * 4.3.10.
865 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000866 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
868 /**
869 * Returns true if this value is the null value. See ECMA-262
870 * 4.3.11.
871 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000872 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
874 /**
875 * Returns true if this value is true.
876 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000877 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878
879 /**
880 * Returns true if this value is false.
881 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000882 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883
884 /**
885 * Returns true if this value is an instance of the String type.
886 * See ECMA-262 8.4.
887 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000888 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
890 /**
891 * Returns true if this value is a function.
892 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000893 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894
895 /**
896 * Returns true if this value is an array.
897 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000898 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
v8.team.kasperl727e9952008-09-02 14:56:44 +0000900 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 * Returns true if this value is an object.
902 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000903 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
v8.team.kasperl727e9952008-09-02 14:56:44 +0000905 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 * Returns true if this value is boolean.
907 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000908 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000909
v8.team.kasperl727e9952008-09-02 14:56:44 +0000910 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 * Returns true if this value is a number.
912 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000913 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914
v8.team.kasperl727e9952008-09-02 14:56:44 +0000915 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 * Returns true if this value is external.
917 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000918 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919
v8.team.kasperl727e9952008-09-02 14:56:44 +0000920 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 * Returns true if this value is a 32-bit signed integer.
922 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000923 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000925 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000926 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000927 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000928 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000929
930 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000931 * Returns true if this value is a Date.
932 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000933 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000934
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000935 /**
936 * Returns true if this value is a RegExp.
937 */
938 V8EXPORT bool IsRegExp() const;
939
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000940 V8EXPORT Local<Boolean> ToBoolean() const;
941 V8EXPORT Local<Number> ToNumber() const;
942 V8EXPORT Local<String> ToString() const;
943 V8EXPORT Local<String> ToDetailString() const;
944 V8EXPORT Local<Object> ToObject() const;
945 V8EXPORT Local<Integer> ToInteger() const;
946 V8EXPORT Local<Uint32> ToUint32() const;
947 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948
949 /**
950 * Attempts to convert a string to an array index.
951 * Returns an empty handle if the conversion fails.
952 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000953 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000955 V8EXPORT bool BooleanValue() const;
956 V8EXPORT double NumberValue() const;
957 V8EXPORT int64_t IntegerValue() const;
958 V8EXPORT uint32_t Uint32Value() const;
959 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000960
961 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000962 V8EXPORT bool Equals(Handle<Value> that) const;
963 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000964
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000965 private:
966 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000967 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968};
969
970
971/**
972 * The superclass of primitive values. See ECMA-262 4.3.2.
973 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000974class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
976
977/**
978 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
979 * or false value.
980 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000981class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000983 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 static inline Handle<Boolean> New(bool value);
985};
986
987
988/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000989 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000991class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000993
994 /**
995 * Returns the number of characters in this string.
996 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000997 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998
v8.team.kasperl727e9952008-09-02 14:56:44 +0000999 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001000 * Returns the number of bytes in the UTF-8 encoded
1001 * representation of this string.
1002 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001003 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001004
1005 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001006 * Write the contents of the string to an external buffer.
1007 * If no arguments are given, expects the buffer to be large
1008 * enough to hold the entire string and NULL terminator. Copies
1009 * the contents of the string and the NULL terminator into the
1010 * buffer.
1011 *
1012 * Copies up to length characters into the output buffer.
1013 * Only null-terminates if there is enough space in the buffer.
1014 *
1015 * \param buffer The buffer into which the string will be copied.
1016 * \param start The starting position within the string at which
1017 * copying begins.
1018 * \param length The number of bytes to copy from the string.
ager@chromium.org357bf652010-04-12 11:30:10 +00001019 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001020 * \param hints Various hints that might affect performance of this or
1021 * subsequent operations.
ager@chromium.org357bf652010-04-12 11:30:10 +00001022 * \return The number of bytes copied to the buffer
v8.team.kasperl727e9952008-09-02 14:56:44 +00001023 * excluding the NULL terminator.
1024 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001025 enum WriteHints {
1026 NO_HINTS = 0,
1027 HINT_MANY_WRITES_EXPECTED = 1
1028 };
1029
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001030 V8EXPORT int Write(uint16_t* buffer,
1031 int start = 0,
1032 int length = -1,
1033 WriteHints hints = NO_HINTS) const; // UTF-16
1034 V8EXPORT int WriteAscii(char* buffer,
1035 int start = 0,
1036 int length = -1,
1037 WriteHints hints = NO_HINTS) const; // ASCII
1038 V8EXPORT int WriteUtf8(char* buffer,
1039 int length = -1,
1040 int* nchars_ref = NULL,
1041 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042
v8.team.kasperl727e9952008-09-02 14:56:44 +00001043 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001044 * A zero length string.
1045 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001046 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001047
1048 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001049 * Returns true if the string is external
1050 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001051 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
v8.team.kasperl727e9952008-09-02 14:56:44 +00001053 /**
1054 * Returns true if the string is both external and ascii
1055 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001056 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001057
1058 class V8EXPORT ExternalStringResourceBase {
1059 public:
1060 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001061
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001062 protected:
1063 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001064
1065 /**
1066 * Internally V8 will call this Dispose method when the external string
1067 * resource is no longer needed. The default implementation will use the
1068 * delete operator. This method can be overridden in subclasses to
1069 * control how allocated external string resources are disposed.
1070 */
1071 virtual void Dispose() { delete this; }
1072
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001073 private:
1074 // Disallow copying and assigning.
1075 ExternalStringResourceBase(const ExternalStringResourceBase&);
1076 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001077
1078 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001079 };
1080
v8.team.kasperl727e9952008-09-02 14:56:44 +00001081 /**
1082 * An ExternalStringResource is a wrapper around a two-byte string
1083 * buffer that resides outside V8's heap. Implement an
1084 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001085 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001087 class V8EXPORT ExternalStringResource
1088 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
1090 /**
1091 * Override the destructor to manage the life cycle of the underlying
1092 * buffer.
1093 */
1094 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001095
1096 /**
1097 * The string data from the underlying buffer.
1098 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001100
1101 /**
1102 * The length of the string. That is, the number of two-byte characters.
1103 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 protected:
1107 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 };
1109
1110 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001111 * An ExternalAsciiStringResource is a wrapper around an ascii
1112 * string buffer that resides outside V8's heap. Implement an
1113 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001114 * underlying buffer. Note that the string data must be immutable
1115 * and that the data must be strict 7-bit ASCII, not Latin1 or
1116 * UTF-8, which would require special treatment internally in the
1117 * engine and, in the case of UTF-8, do not allow efficient indexing.
1118 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001119 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001121 class V8EXPORT ExternalAsciiStringResource
1122 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 public:
1124 /**
1125 * Override the destructor to manage the life cycle of the underlying
1126 * buffer.
1127 */
1128 virtual ~ExternalAsciiStringResource() {}
1129 /** The string data from the underlying buffer.*/
1130 virtual const char* data() const = 0;
1131 /** The number of ascii characters in the string.*/
1132 virtual size_t length() const = 0;
1133 protected:
1134 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 };
1136
1137 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001138 * Get the ExternalStringResource for an external string. Returns
1139 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001141 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142
1143 /**
1144 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001145 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001147 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001149 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150
1151 /**
1152 * Allocates a new string from either utf-8 encoded or ascii data.
1153 * The second parameter 'length' gives the buffer length.
1154 * If the data is utf-8 encoded, the caller must
1155 * be careful to supply the length parameter.
1156 * If it is not given, the function calls
1157 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001158 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001160 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161
1162 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001163 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164
1165 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001166 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
v8.team.kasperl727e9952008-09-02 14:56:44 +00001168 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001169 * Creates a new string by concatenating the left and the right strings
1170 * passed in as parameters.
1171 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001172 V8EXPORT static Local<String> Concat(Handle<String> left,
1173 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001174
1175 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001176 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001177 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001178 * resource will be disposed by calling its Dispose method. The caller of
1179 * this function should not otherwise delete or modify the resource. Neither
1180 * should the underlying buffer be deallocated or modified except through the
1181 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001182 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001183 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001184
ager@chromium.org6f10e412009-02-13 10:11:16 +00001185 /**
1186 * Associate an external string resource with this string by transforming it
1187 * in place so that existing references to this string in the JavaScript heap
1188 * will use the external string resource. The external string resource's
1189 * character contents needs to be equivalent to this string.
1190 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001191 * The string is not modified if the operation fails. See NewExternal for
1192 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001193 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001194 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196 /**
1197 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001198 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001199 * resource will be disposed by calling its Dispose method. The caller of
1200 * this function should not otherwise delete or modify the resource. Neither
1201 * should the underlying buffer be deallocated or modified except through the
1202 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001203 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001204 V8EXPORT static Local<String> NewExternal(
1205 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001206
ager@chromium.org6f10e412009-02-13 10:11:16 +00001207 /**
1208 * Associate an external string resource with this string by transforming it
1209 * in place so that existing references to this string in the JavaScript heap
1210 * will use the external string resource. The external string resource's
1211 * character contents needs to be equivalent to this string.
1212 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001213 * The string is not modified if the operation fails. See NewExternal for
1214 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001215 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001216 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001218 /**
1219 * Returns true if this string can be made external.
1220 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001221 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001222
kasper.lund7276f142008-07-30 08:49:36 +00001223 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001224 V8EXPORT static Local<String> NewUndetectable(const char* data,
1225 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226
kasper.lund7276f142008-07-30 08:49:36 +00001227 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001228 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1229 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230
1231 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001232 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001233 * you want to print the object. If conversion to a string fails
1234 * (eg. due to an exception in the toString() method of the object)
1235 * then the length() method returns 0 and the * operator returns
1236 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001237 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001238 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001239 public:
1240 explicit Utf8Value(Handle<v8::Value> obj);
1241 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001242 char* operator*() { return str_; }
1243 const char* operator*() const { return str_; }
1244 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001245 private:
1246 char* str_;
1247 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001248
1249 // Disallow copying and assigning.
1250 Utf8Value(const Utf8Value&);
1251 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001252 };
1253
1254 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 * Converts an object to an ascii string.
1256 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001257 * If conversion to a string fails (eg. due to an exception in the toString()
1258 * method of the object) then the length() method returns 0 and the * operator
1259 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001261 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 public:
1263 explicit AsciiValue(Handle<v8::Value> obj);
1264 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001265 char* operator*() { return str_; }
1266 const char* operator*() const { return str_; }
1267 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 private:
1269 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001270 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001271
1272 // Disallow copying and assigning.
1273 AsciiValue(const AsciiValue&);
1274 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 };
1276
1277 /**
1278 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001279 * If conversion to a string fails (eg. due to an exception in the toString()
1280 * method of the object) then the length() method returns 0 and the * operator
1281 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001283 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 public:
1285 explicit Value(Handle<v8::Value> obj);
1286 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001287 uint16_t* operator*() { return str_; }
1288 const uint16_t* operator*() const { return str_; }
1289 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 private:
1291 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001292 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001293
1294 // Disallow copying and assigning.
1295 Value(const Value&);
1296 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001298
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001299 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001300 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1301 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302};
1303
1304
1305/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001306 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001308class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001310 V8EXPORT double Value() const;
1311 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001312 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001314 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001315 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316};
1317
1318
1319/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001320 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001322class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001324 V8EXPORT static Local<Integer> New(int32_t value);
1325 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1326 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001327 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001329 V8EXPORT Integer();
1330 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331};
1332
1333
1334/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001335 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001337class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001339 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001341 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342};
1343
1344
1345/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001346 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001348class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001350 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001352 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353};
1354
1355
1356/**
1357 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1358 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001359class Date : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001360 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001361 V8EXPORT static Local<Value> New(double time);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001362
1363 /**
1364 * A specialization of Value::NumberValue that is more efficient
1365 * because we know the structure of this object.
1366 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001367 V8EXPORT double NumberValue() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001368
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001369 static inline Date* Cast(v8::Value* obj);
1370 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001371 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372};
1373
1374
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001375/**
1376 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1377 */
1378class RegExp : public Value {
1379 public:
1380 /**
1381 * Regular expression flag bits. They can be or'ed to enable a set
1382 * of flags.
1383 */
1384 enum Flags {
1385 kNone = 0,
1386 kGlobal = 1,
1387 kIgnoreCase = 2,
1388 kMultiline = 4
1389 };
1390
1391 /**
1392 * Creates a regular expression from the given pattern string and
1393 * the flags bit field. May throw a JavaScript exception as
1394 * described in ECMA-262, 15.10.4.1.
1395 *
1396 * For example,
1397 * RegExp::New(v8::String::New("foo"),
1398 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1399 * is equivalent to evaluating "/foo/gm".
1400 */
1401 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1402 Flags flags);
1403
1404 /**
1405 * Returns the value of the source property: a string representing
1406 * the regular expression.
1407 */
1408 V8EXPORT Local<String> GetSource() const;
1409
1410 /**
1411 * Returns the flags bit field.
1412 */
1413 V8EXPORT Flags GetFlags() const;
1414
1415 static inline RegExp* Cast(v8::Value* obj);
1416
1417 private:
1418 V8EXPORT static void CheckCast(v8::Value* obj);
1419};
1420
1421
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422enum PropertyAttribute {
1423 None = 0,
1424 ReadOnly = 1 << 0,
1425 DontEnum = 1 << 1,
1426 DontDelete = 1 << 2
1427};
1428
ager@chromium.org3811b432009-10-28 14:53:37 +00001429enum ExternalArrayType {
1430 kExternalByteArray = 1,
1431 kExternalUnsignedByteArray,
1432 kExternalShortArray,
1433 kExternalUnsignedShortArray,
1434 kExternalIntArray,
1435 kExternalUnsignedIntArray,
1436 kExternalFloatArray
1437};
1438
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001440 * Accessor[Getter|Setter] are used as callback functions when
1441 * setting|getting a particular property. See Object and ObjectTemplate's
1442 * method SetAccessor.
1443 */
1444typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1445 const AccessorInfo& info);
1446
1447
1448typedef void (*AccessorSetter)(Local<String> property,
1449 Local<Value> value,
1450 const AccessorInfo& info);
1451
1452
1453/**
1454 * Access control specifications.
1455 *
1456 * Some accessors should be accessible across contexts. These
1457 * accessors have an explicit access control parameter which specifies
1458 * the kind of cross-context access that should be allowed.
1459 *
1460 * Additionally, for security, accessors can prohibit overwriting by
1461 * accessors defined in JavaScript. For objects that have such
1462 * accessors either locally or in their prototype chain it is not
1463 * possible to overwrite the accessor by using __defineGetter__ or
1464 * __defineSetter__ from JavaScript code.
1465 */
1466enum AccessControl {
1467 DEFAULT = 0,
1468 ALL_CAN_READ = 1,
1469 ALL_CAN_WRITE = 1 << 1,
1470 PROHIBITS_OVERWRITING = 1 << 2
1471};
1472
1473
1474/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001475 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001477class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001479 V8EXPORT bool Set(Handle<Value> key,
1480 Handle<Value> value,
1481 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001482
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001483 V8EXPORT bool Set(uint32_t index,
1484 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001485
ager@chromium.orge2902be2009-06-08 12:21:35 +00001486 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001487 // overriding accessors or read-only properties.
1488 //
1489 // Note that if the object has an interceptor the property will be set
1490 // locally, but since the interceptor takes precedence the local property
1491 // will only be returned if the interceptor doesn't return a value.
1492 //
1493 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001494 V8EXPORT bool ForceSet(Handle<Value> key,
1495 Handle<Value> value,
1496 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001497
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001498 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001500 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001501
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502 // TODO(1245389): Replace the type-specific versions of these
1503 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001504 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001505
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001506 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001507
1508 // Delete a property on this object bypassing interceptors and
1509 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001510 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001511
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001512 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001513
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001514 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001516 V8EXPORT bool SetAccessor(Handle<String> name,
1517 AccessorGetter getter,
1518 AccessorSetter setter = 0,
1519 Handle<Value> data = Handle<Value>(),
1520 AccessControl settings = DEFAULT,
1521 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001524 * Returns an array containing the names of the enumerable properties
1525 * of this object, including properties from prototype objects. The
1526 * array returned by this method contains the same values as would
1527 * be enumerated by a for-in statement over this object.
1528 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001529 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001530
1531 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532 * Get the prototype object. This does not skip objects marked to
1533 * be skipped by __proto__ and it does not consult the security
1534 * handler.
1535 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001536 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537
1538 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001539 * Set the prototype object. This does not skip objects marked to
1540 * be skipped by __proto__ and it does not consult the security
1541 * handler.
1542 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001543 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001544
1545 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001546 * Finds an instance of the given function template in the prototype
1547 * chain.
1548 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001549 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1550 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001551
1552 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553 * Call builtin Object.prototype.toString on this object.
1554 * This is different from Value::ToString() that may call
1555 * user-defined toString function. This one does not.
1556 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001557 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558
kasper.lund212ac232008-07-16 07:07:30 +00001559 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001560 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001561 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001562 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001563 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001564 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001566 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001567 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001568
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001569 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001570 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001571
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 // Testers for local properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001573 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1574 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1575 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
1577 /**
1578 * If result.IsEmpty() no real property was located in the prototype chain.
1579 * This means interceptors in the prototype chain are not called.
1580 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001581 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1582 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001583
1584 /**
1585 * If result.IsEmpty() no real property was located on the object or
1586 * in the prototype chain.
1587 * This means interceptors in the prototype chain are not called.
1588 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590
1591 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001592 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593
kasper.lund212ac232008-07-16 07:07:30 +00001594 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001595 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001597 /**
1598 * Turns on access check on the object if the object is an instance of
1599 * a template that has access check callbacks. If an object has no
1600 * access check info, the object cannot be accessed by anyone.
1601 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001602 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001603
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001604 /**
1605 * Returns the identity hash for this object. The current implemenation uses
ager@chromium.org9085a012009-05-11 19:22:57 +00001606 * a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001607 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001608 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001609 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001610 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001611 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001612
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001613 /**
1614 * Access hidden properties on JavaScript objects. These properties are
1615 * hidden from the executing JavaScript and only accessible through the V8
1616 * C++ API. Hidden properties introduced by V8 internally (for example the
1617 * identity hash) are prefixed with "v8::".
1618 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001619 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1620 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1621 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001622
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001623 /**
1624 * Returns true if this is an instance of an api function (one
1625 * created from a function created from a function template) and has
1626 * been modified since it was created. Note that this method is
1627 * conservative and may return true for objects that haven't actually
1628 * been modified.
1629 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001630 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001631
1632 /**
1633 * Clone this object with a fast but shallow copy. Values will point
1634 * to the same values as the original object.
1635 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001636 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001638 /**
1639 * Set the backing store of the indexed properties to be managed by the
1640 * embedding layer. Access to the indexed properties will follow the rules
1641 * spelled out in CanvasPixelArray.
1642 * Note: The embedding program still owns the data and needs to ensure that
1643 * the backing store is preserved while V8 has a reference.
1644 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001645 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001646 bool HasIndexedPropertiesInPixelData();
1647 uint8_t* GetIndexedPropertiesPixelData();
1648 int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001649
ager@chromium.org3811b432009-10-28 14:53:37 +00001650 /**
1651 * Set the backing store of the indexed properties to be managed by the
1652 * embedding layer. Access to the indexed properties will follow the rules
1653 * spelled out for the CanvasArray subtypes in the WebGL specification.
1654 * Note: The embedding program still owns the data and needs to ensure that
1655 * the backing store is preserved while V8 has a reference.
1656 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001657 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1658 void* data,
1659 ExternalArrayType array_type,
1660 int number_of_elements);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001661 bool HasIndexedPropertiesInExternalArrayData();
1662 void* GetIndexedPropertiesExternalArrayData();
1663 ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1664 int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001665
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001666 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001667 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001669 V8EXPORT Object();
1670 V8EXPORT static void CheckCast(Value* obj);
1671 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1672 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001673
1674 /**
1675 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001676 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001677 */
1678 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679};
1680
1681
1682/**
1683 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1684 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001685class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001687 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688
ager@chromium.org3e875802009-06-29 08:26:34 +00001689 /**
1690 * Clones an element at index |index|. Returns an empty
1691 * handle if cloning fails (for any reason).
1692 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001693 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001694
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001695 V8EXPORT static Local<Array> New(int length = 0);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001696 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001698 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001699 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700};
1701
1702
1703/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001704 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001708 V8EXPORT Local<Object> NewInstance() const;
1709 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1710 V8EXPORT Local<Value> Call(Handle<Object> recv,
1711 int argc,
1712 Handle<Value> argv[]);
1713 V8EXPORT void SetName(Handle<String> name);
1714 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001715
1716 /**
1717 * Returns zero based line number of function body and
1718 * kLineOffsetNotFound if no information available.
1719 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001720 V8EXPORT int GetScriptLineNumber() const;
1721 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001722 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001723 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001725 V8EXPORT Function();
1726 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727};
1728
1729
1730/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001731 * A JavaScript value that wraps a C++ void*. This type of value is
1732 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001734 *
1735 * The Wrap function V8 will return the most optimal Value object wrapping the
1736 * C++ void*. The type of the value is not guaranteed to be an External object
1737 * and no assumptions about its type should be made. To access the wrapped
1738 * value Unwrap should be used, all other operations on that object will lead
1739 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001741class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001743 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001744 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001745
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001746 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001747 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001748 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001750 V8EXPORT External();
1751 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001752 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001753 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754};
1755
1756
1757// --- T e m p l a t e s ---
1758
1759
1760/**
1761 * The superclass of object and function templates.
1762 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001763class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 public:
1765 /** Adds a property to each instance created by this template.*/
1766 void Set(Handle<String> name, Handle<Data> value,
1767 PropertyAttribute attributes = None);
1768 inline void Set(const char* name, Handle<Data> value);
1769 private:
1770 Template();
1771
1772 friend class ObjectTemplate;
1773 friend class FunctionTemplate;
1774};
1775
1776
1777/**
1778 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001779 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 * including the receiver, the number and values of arguments, and
1781 * the holder of the function.
1782 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001783class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 public:
1785 inline int Length() const;
1786 inline Local<Value> operator[](int i) const;
1787 inline Local<Function> Callee() const;
1788 inline Local<Object> This() const;
1789 inline Local<Object> Holder() const;
1790 inline bool IsConstructCall() const;
1791 inline Local<Value> Data() const;
1792 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001793 static const int kDataIndex = 0;
1794 static const int kCalleeIndex = -1;
1795 static const int kHolderIndex = -2;
1796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001798 inline Arguments(internal::Object** implicit_args,
1799 internal::Object** values,
1800 int length,
1801 bool is_construct_call);
1802 internal::Object** implicit_args_;
1803 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001805 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806};
1807
1808
1809/**
1810 * The information passed to an accessor callback about the context
1811 * of the property access.
1812 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001813class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001815 inline AccessorInfo(internal::Object** args)
1816 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 inline Local<Value> Data() const;
1818 inline Local<Object> This() const;
1819 inline Local<Object> Holder() const;
1820 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001821 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822};
1823
1824
1825typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 * NamedProperty[Getter|Setter] are used as interceptors on object.
1829 * See ObjectTemplate::SetNamedPropertyHandler.
1830 */
1831typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1832 const AccessorInfo& info);
1833
1834
1835/**
1836 * Returns the value if the setter intercepts the request.
1837 * Otherwise, returns an empty handle.
1838 */
1839typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1840 Local<Value> value,
1841 const AccessorInfo& info);
1842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843/**
1844 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001845 * The result is an integer encoding property attributes (like v8::None,
1846 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001848typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1849 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850
1851
1852/**
1853 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001854 * The return value is true if the property could be deleted and false
1855 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 */
1857typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1858 const AccessorInfo& info);
1859
1860/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001861 * Returns an array containing the names of the properties the named
1862 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 */
1864typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1865
v8.team.kasperl727e9952008-09-02 14:56:44 +00001866
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001868 * Returns the value of the property if the getter intercepts the
1869 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 */
1871typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1872 const AccessorInfo& info);
1873
1874
1875/**
1876 * Returns the value if the setter intercepts the request.
1877 * Otherwise, returns an empty handle.
1878 */
1879typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1880 Local<Value> value,
1881 const AccessorInfo& info);
1882
1883
1884/**
1885 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001886 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001888typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1889 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890
1891/**
1892 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001893 * The return value is true if the property could be deleted and false
1894 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 */
1896typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1897 const AccessorInfo& info);
1898
v8.team.kasperl727e9952008-09-02 14:56:44 +00001899/**
1900 * Returns an array containing the indices of the properties the
1901 * indexed property getter intercepts.
1902 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1904
1905
1906/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001907 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 */
1909enum AccessType {
1910 ACCESS_GET,
1911 ACCESS_SET,
1912 ACCESS_HAS,
1913 ACCESS_DELETE,
1914 ACCESS_KEYS
1915};
1916
v8.team.kasperl727e9952008-09-02 14:56:44 +00001917
1918/**
1919 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001920 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001921 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001922typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 Local<Value> key,
1924 AccessType type,
1925 Local<Value> data);
1926
v8.team.kasperl727e9952008-09-02 14:56:44 +00001927
1928/**
1929 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001930 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001931 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001932typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 uint32_t index,
1934 AccessType type,
1935 Local<Value> data);
1936
1937
1938/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001939 * A FunctionTemplate is used to create functions at runtime. There
1940 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001941 * context. The lifetime of the created function is equal to the
1942 * lifetime of the context. So in case the embedder needs to create
1943 * temporary functions that can be collected using Scripts is
1944 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 *
1946 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00001947 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001949 * A FunctionTemplate has a corresponding instance template which is
1950 * used to create object instances when the function is used as a
1951 * constructor. Properties added to the instance template are added to
1952 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 *
1954 * A FunctionTemplate can have a prototype template. The prototype template
1955 * is used to create the prototype object of the function.
1956 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001959 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
1961 * t->Set("func_property", v8::Number::New(1));
1962 *
1963 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
1964 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
1965 * proto_t->Set("proto_const", v8::Number::New(2));
1966 *
1967 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
1968 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
1969 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
1970 * instance_t->Set("instance_property", Number::New(3));
1971 *
1972 * v8::Local<v8::Function> function = t->GetFunction();
1973 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00001974 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 *
1976 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00001977 * and "instance" for the instance object created above. The function
1978 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001980 * \code
1981 * func_property in function == true;
1982 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001984 * function.prototype.proto_method() invokes 'InvokeCallback'
1985 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001987 * instance instanceof function == true;
1988 * instance.instance_accessor calls 'InstanceAccessorCallback'
1989 * instance.instance_property == 3;
1990 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001992 * A FunctionTemplate can inherit from another one by calling the
1993 * FunctionTemplate::Inherit method. The following graph illustrates
1994 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001996 * \code
1997 * FunctionTemplate Parent -> Parent() . prototype -> { }
1998 * ^ ^
1999 * | Inherit(Parent) | .__proto__
2000 * | |
2001 * FunctionTemplate Child -> Child() . prototype -> { }
2002 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002004 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2005 * object of the Child() function has __proto__ pointing to the
2006 * Parent() function's prototype object. An instance of the Child
2007 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002009 * Let Parent be the FunctionTemplate initialized in the previous
2010 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002012 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013 * Local<FunctionTemplate> parent = t;
2014 * Local<FunctionTemplate> child = FunctionTemplate::New();
2015 * child->Inherit(parent);
2016 *
2017 * Local<Function> child_function = child->GetFunction();
2018 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002019 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002021 * The Child function and Child instance will have the following
2022 * properties:
2023 *
2024 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002026 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002028 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002030class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 public:
2032 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002033 static Local<FunctionTemplate> New(
2034 InvocationCallback callback = 0,
2035 Handle<Value> data = Handle<Value>(),
2036 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 /** Returns the unique function instance in the current execution context.*/
2038 Local<Function> GetFunction();
2039
v8.team.kasperl727e9952008-09-02 14:56:44 +00002040 /**
2041 * Set the call-handler callback for a FunctionTemplate. This
2042 * callback is called whenever the function created from this
2043 * FunctionTemplate is called.
2044 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 void SetCallHandler(InvocationCallback callback,
2046 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 Local<ObjectTemplate> InstanceTemplate();
2050
2051 /** Causes the function template to inherit from a parent function template.*/
2052 void Inherit(Handle<FunctionTemplate> parent);
2053
2054 /**
2055 * A PrototypeTemplate is the template used to create the prototype object
2056 * of the function created by this template.
2057 */
2058 Local<ObjectTemplate> PrototypeTemplate();
2059
v8.team.kasperl727e9952008-09-02 14:56:44 +00002060
2061 /**
2062 * Set the class name of the FunctionTemplate. This is used for
2063 * printing objects created with the function created from the
2064 * FunctionTemplate as its constructor.
2065 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 void SetClassName(Handle<String> name);
2067
2068 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002069 * Determines whether the __proto__ accessor ignores instances of
2070 * the function template. If instances of the function template are
2071 * ignored, __proto__ skips all instances and instead returns the
2072 * next object in the prototype chain.
2073 *
2074 * Call with a value of true to make the __proto__ accessor ignore
2075 * instances of the function template. Call with a value of false
2076 * to make the __proto__ accessor not ignore instances of the
2077 * function template. By default, instances of a function template
2078 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 */
2080 void SetHiddenPrototype(bool value);
2081
2082 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083 * Returns true if the given object is an instance of this function
2084 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 */
2086 bool HasInstance(Handle<Value> object);
2087
2088 private:
2089 FunctionTemplate();
2090 void AddInstancePropertyAccessor(Handle<String> name,
2091 AccessorGetter getter,
2092 AccessorSetter setter,
2093 Handle<Value> data,
2094 AccessControl settings,
2095 PropertyAttribute attributes);
2096 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2097 NamedPropertySetter setter,
2098 NamedPropertyQuery query,
2099 NamedPropertyDeleter remover,
2100 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002101 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2103 IndexedPropertySetter setter,
2104 IndexedPropertyQuery query,
2105 IndexedPropertyDeleter remover,
2106 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002107 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2109 Handle<Value> data);
2110
2111 friend class Context;
2112 friend class ObjectTemplate;
2113};
2114
2115
2116/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002117 * An ObjectTemplate is used to create objects at runtime.
2118 *
2119 * Properties added to an ObjectTemplate are added to each object
2120 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002122class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002124 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 /** Creates a new instance of this template.*/
2128 Local<Object> NewInstance();
2129
2130 /**
2131 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002132 *
2133 * Whenever the property with the given name is accessed on objects
2134 * created from this ObjectTemplate the getter and setter callbacks
2135 * are called instead of getting and setting the property directly
2136 * on the JavaScript object.
2137 *
2138 * \param name The name of the property for which an accessor is added.
2139 * \param getter The callback to invoke when getting the property.
2140 * \param setter The callback to invoke when setting the property.
2141 * \param data A piece of data that will be passed to the getter and setter
2142 * callbacks whenever they are invoked.
2143 * \param settings Access control settings for the accessor. This is a bit
2144 * field consisting of one of more of
2145 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2146 * The default is to not allow cross-context access.
2147 * ALL_CAN_READ means that all cross-context reads are allowed.
2148 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2149 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2150 * cross-context access.
2151 * \param attribute The attributes of the property for which an accessor
2152 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 */
2154 void SetAccessor(Handle<String> name,
2155 AccessorGetter getter,
2156 AccessorSetter setter = 0,
2157 Handle<Value> data = Handle<Value>(),
2158 AccessControl settings = DEFAULT,
2159 PropertyAttribute attribute = None);
2160
2161 /**
2162 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002163 *
2164 * Whenever a named property is accessed on objects created from
2165 * this object template, the provided callback is invoked instead of
2166 * accessing the property directly on the JavaScript object.
2167 *
2168 * \param getter The callback to invoke when getting a property.
2169 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002170 * \param query The callback to invoke to check if a property is present,
2171 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002172 * \param deleter The callback to invoke when deleting a property.
2173 * \param enumerator The callback to invoke to enumerate all the named
2174 * properties of an object.
2175 * \param data A piece of data that will be passed to the callbacks
2176 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 */
2178 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2179 NamedPropertySetter setter = 0,
2180 NamedPropertyQuery query = 0,
2181 NamedPropertyDeleter deleter = 0,
2182 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002183 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184
2185 /**
2186 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002187 *
2188 * Whenever an indexed property is accessed on objects created from
2189 * this object template, the provided callback is invoked instead of
2190 * accessing the property directly on the JavaScript object.
2191 *
2192 * \param getter The callback to invoke when getting a property.
2193 * \param setter The callback to invoke when setting a property.
2194 * \param query The callback to invoke to check is an object has a property.
2195 * \param deleter The callback to invoke when deleting a property.
2196 * \param enumerator The callback to invoke to enumerate all the indexed
2197 * properties of an object.
2198 * \param data A piece of data that will be passed to the callbacks
2199 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 */
2201 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2202 IndexedPropertySetter setter = 0,
2203 IndexedPropertyQuery query = 0,
2204 IndexedPropertyDeleter deleter = 0,
2205 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002206 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002207
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 /**
2209 * Sets the callback to be used when calling instances created from
2210 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002211 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 * function.
2213 */
2214 void SetCallAsFunctionHandler(InvocationCallback callback,
2215 Handle<Value> data = Handle<Value>());
2216
v8.team.kasperl727e9952008-09-02 14:56:44 +00002217 /**
2218 * Mark object instances of the template as undetectable.
2219 *
2220 * In many ways, undetectable objects behave as though they are not
2221 * there. They behave like 'undefined' in conditionals and when
2222 * printed. However, properties can be accessed and called as on
2223 * normal objects.
2224 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 void MarkAsUndetectable();
2226
v8.team.kasperl727e9952008-09-02 14:56:44 +00002227 /**
2228 * Sets access check callbacks on the object template.
2229 *
2230 * When accessing properties on instances of this object template,
2231 * the access check callback will be called to determine whether or
2232 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002233 * The last parameter specifies whether access checks are turned
2234 * on by default on instances. If access checks are off by default,
2235 * they can be turned on on individual instances by calling
2236 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002237 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2239 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002240 Handle<Value> data = Handle<Value>(),
2241 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242
kasper.lund212ac232008-07-16 07:07:30 +00002243 /**
2244 * Gets the number of internal fields for objects generated from
2245 * this template.
2246 */
2247 int InternalFieldCount();
2248
2249 /**
2250 * Sets the number of internal fields for objects generated from
2251 * this template.
2252 */
2253 void SetInternalFieldCount(int value);
2254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 private:
2256 ObjectTemplate();
2257 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2258 friend class FunctionTemplate;
2259};
2260
2261
2262/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 * A Signature specifies which receivers and arguments a function can
2264 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002266class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 public:
2268 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2269 Handle<FunctionTemplate>(),
2270 int argc = 0,
2271 Handle<FunctionTemplate> argv[] = 0);
2272 private:
2273 Signature();
2274};
2275
2276
2277/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002278 * A utility for determining the type of objects based on the template
2279 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002281class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 public:
2283 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2284 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2285 int match(Handle<Value> value);
2286 private:
2287 TypeSwitch();
2288};
2289
2290
2291// --- E x t e n s i o n s ---
2292
2293
2294/**
2295 * Ignore
2296 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002297class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 public:
2299 Extension(const char* name,
2300 const char* source = 0,
2301 int dep_count = 0,
2302 const char** deps = 0);
2303 virtual ~Extension() { }
2304 virtual v8::Handle<v8::FunctionTemplate>
2305 GetNativeFunction(v8::Handle<v8::String> name) {
2306 return v8::Handle<v8::FunctionTemplate>();
2307 }
2308
2309 const char* name() { return name_; }
2310 const char* source() { return source_; }
2311 int dependency_count() { return dep_count_; }
2312 const char** dependencies() { return deps_; }
2313 void set_auto_enable(bool value) { auto_enable_ = value; }
2314 bool auto_enable() { return auto_enable_; }
2315
2316 private:
2317 const char* name_;
2318 const char* source_;
2319 int dep_count_;
2320 const char** deps_;
2321 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002322
2323 // Disallow copying and assigning.
2324 Extension(const Extension&);
2325 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326};
2327
2328
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002329void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330
2331
2332/**
2333 * Ignore
2334 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002335class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 public:
2337 inline DeclareExtension(Extension* extension) {
2338 RegisterExtension(extension);
2339 }
2340};
2341
2342
2343// --- S t a t i c s ---
2344
2345
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002346Handle<Primitive> V8EXPORT Undefined();
2347Handle<Primitive> V8EXPORT Null();
2348Handle<Boolean> V8EXPORT True();
2349Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350
2351
2352/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002353 * A set of constraints that specifies the limits of the runtime's memory use.
2354 * You must set the heap size before initializing the VM - the size cannot be
2355 * adjusted after the VM is initialized.
2356 *
2357 * If you are using threads then you should hold the V8::Locker lock while
2358 * setting the stack limit and you must set a non-default stack limit separately
2359 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002361class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 public:
2363 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002364 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002366 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002368 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002369 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2371 private:
2372 int max_young_space_size_;
2373 int max_old_space_size_;
2374 uint32_t* stack_limit_;
2375};
2376
2377
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002378bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002379
2380
2381// --- E x c e p t i o n s ---
2382
2383
2384typedef void (*FatalErrorCallback)(const char* location, const char* message);
2385
2386
2387typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2388
2389
2390/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002391 * Schedules an exception to be thrown when returning to JavaScript. When an
2392 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002394 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002396Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397
2398/**
2399 * Create new error objects by calling the corresponding error object
2400 * constructor with the message.
2401 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002402class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 public:
2404 static Local<Value> RangeError(Handle<String> message);
2405 static Local<Value> ReferenceError(Handle<String> message);
2406 static Local<Value> SyntaxError(Handle<String> message);
2407 static Local<Value> TypeError(Handle<String> message);
2408 static Local<Value> Error(Handle<String> message);
2409};
2410
2411
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002412// --- 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 +00002413
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002414typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002416typedef void* (*CreateHistogramCallback)(const char* name,
2417 int min,
2418 int max,
2419 size_t buckets);
2420
2421typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2422
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002423// --- M e m o r y A l l o c a t i o n C a l l b a c k ---
2424 enum ObjectSpace {
2425 kObjectSpaceNewSpace = 1 << 0,
2426 kObjectSpaceOldPointerSpace = 1 << 1,
2427 kObjectSpaceOldDataSpace = 1 << 2,
2428 kObjectSpaceCodeSpace = 1 << 3,
2429 kObjectSpaceMapSpace = 1 << 4,
2430 kObjectSpaceLoSpace = 1 << 5,
2431
2432 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2433 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2434 kObjectSpaceLoSpace
2435 };
2436
2437 enum AllocationAction {
2438 kAllocationActionAllocate = 1 << 0,
2439 kAllocationActionFree = 1 << 1,
2440 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2441 };
2442
2443typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2444 AllocationAction action,
2445 int size);
2446
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447// --- 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 ---
2448typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2449 AccessType type,
2450 Local<Value> data);
2451
2452// --- 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
2453
2454/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002455 * Applications can register callback functions which will be called
2456 * before and after a garbage collection. Allocations are not
2457 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002458 * objects (set or delete properties for example) since it is possible
2459 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002461enum GCType {
2462 kGCTypeScavenge = 1 << 0,
2463 kGCTypeMarkSweepCompact = 1 << 1,
2464 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2465};
2466
2467enum GCCallbackFlags {
2468 kNoGCCallbackFlags = 0,
2469 kGCCallbackFlagCompacted = 1 << 0
2470};
2471
2472typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2473typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2474
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002475typedef void (*GCCallback)();
2476
2477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002479 * Profiler modules.
2480 *
2481 * In V8, profiler consists of several modules: CPU profiler, and different
2482 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002483 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2484 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002485 */
2486enum ProfilerModules {
2487 PROFILER_MODULE_NONE = 0,
2488 PROFILER_MODULE_CPU = 1,
2489 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002490 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2491 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002492};
2493
2494
2495/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002496 * Collection of V8 heap information.
2497 *
2498 * Instances of this class can be passed to v8::V8::HeapStatistics to
2499 * get heap statistics from V8.
2500 */
2501class V8EXPORT HeapStatistics {
2502 public:
2503 HeapStatistics();
2504 size_t total_heap_size() { return total_heap_size_; }
2505 size_t used_heap_size() { return used_heap_size_; }
2506
2507 private:
2508 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
2509 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
2510
2511 size_t total_heap_size_;
2512 size_t used_heap_size_;
2513
2514 friend class V8;
2515};
2516
2517
2518/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 * Container class for static utility functions.
2520 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002521class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002523 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524 static void SetFatalErrorHandler(FatalErrorCallback that);
2525
v8.team.kasperl727e9952008-09-02 14:56:44 +00002526 /**
2527 * Ignore out-of-memory exceptions.
2528 *
2529 * V8 running out of memory is treated as a fatal error by default.
2530 * This means that the fatal error handler is called and that V8 is
2531 * terminated.
2532 *
2533 * IgnoreOutOfMemoryException can be used to not treat a
2534 * out-of-memory situation as a fatal error. This way, the contexts
2535 * that did not cause the out of memory problem might be able to
2536 * continue execution.
2537 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 static void IgnoreOutOfMemoryException();
2539
v8.team.kasperl727e9952008-09-02 14:56:44 +00002540 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002541 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002542 * fatal errors such as out-of-memory situations.
2543 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002544 static bool IsDead();
2545
2546 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002547 * Adds a message listener.
2548 *
2549 * The same message listener can be added more than once and it that
2550 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002551 */
2552 static bool AddMessageListener(MessageCallback that,
2553 Handle<Value> data = Handle<Value>());
2554
2555 /**
2556 * Remove all message listeners from the specified callback function.
2557 */
2558 static void RemoveMessageListeners(MessageCallback that);
2559
2560 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002561 * Tells V8 to capture current stack trace when uncaught exception occurs
2562 * and report it to the message listeners. The option is off by default.
2563 */
2564 static void SetCaptureStackTraceForUncaughtExceptions(
2565 bool capture,
2566 int frame_limit = 10,
2567 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2568
2569 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002570 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 */
2572 static void SetFlagsFromString(const char* str, int length);
2573
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002574 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002575 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002576 */
2577 static void SetFlagsFromCommandLine(int* argc,
2578 char** argv,
2579 bool remove_flags);
2580
kasper.lund7276f142008-07-30 08:49:36 +00002581 /** Get the version string. */
2582 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583
2584 /**
2585 * Enables the host application to provide a mechanism for recording
2586 * statistics counters.
2587 */
2588 static void SetCounterFunction(CounterLookupCallback);
2589
2590 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002591 * Enables the host application to provide a mechanism for recording
2592 * histograms. The CreateHistogram function returns a
2593 * histogram which will later be passed to the AddHistogramSample
2594 * function.
2595 */
2596 static void SetCreateHistogramFunction(CreateHistogramCallback);
2597 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2598
2599 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600 * Enables the computation of a sliding window of states. The sliding
2601 * window information is recorded in statistics counters.
2602 */
2603 static void EnableSlidingStateWindow();
2604
2605 /** Callback function for reporting failed access checks.*/
2606 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2607
2608 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002609 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002610 * garbage collection. Allocations are not allowed in the
2611 * callback function, you therefore cannot manipulate objects (set
2612 * or delete properties for example) since it is possible such
2613 * operations will result in the allocation of objects. It is possible
2614 * to specify the GCType filter for your callback. But it is not possible to
2615 * register the same callback function two times with different
2616 * GCType filters.
2617 */
2618 static void AddGCPrologueCallback(
2619 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2620
2621 /**
2622 * This function removes callback which was installed by
2623 * AddGCPrologueCallback function.
2624 */
2625 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2626
2627 /**
2628 * The function is deprecated. Please use AddGCPrologueCallback instead.
2629 * Enables the host application to receive a notification before a
2630 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002631 * callback function, you therefore cannot manipulate objects (set
2632 * or delete properties for example) since it is possible such
2633 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 */
2635 static void SetGlobalGCPrologueCallback(GCCallback);
2636
2637 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002638 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002639 * garbage collection. Allocations are not allowed in the
2640 * callback function, you therefore cannot manipulate objects (set
2641 * or delete properties for example) since it is possible such
2642 * operations will result in the allocation of objects. It is possible
2643 * to specify the GCType filter for your callback. But it is not possible to
2644 * register the same callback function two times with different
2645 * GCType filters.
2646 */
2647 static void AddGCEpilogueCallback(
2648 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2649
2650 /**
2651 * This function removes callback which was installed by
2652 * AddGCEpilogueCallback function.
2653 */
2654 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2655
2656 /**
2657 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2658 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002659 * major garbage collection. Allocations are not allowed in the
2660 * callback function, you therefore cannot manipulate objects (set
2661 * or delete properties for example) since it is possible such
2662 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 */
2664 static void SetGlobalGCEpilogueCallback(GCCallback);
2665
2666 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002667 * Enables the host application to provide a mechanism to be notified
2668 * and perform custom logging when V8 Allocates Executable Memory.
2669 */
2670 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2671 ObjectSpace space,
2672 AllocationAction action);
2673
2674 /**
2675 * This function removes callback which was installed by
2676 * AddMemoryAllocationCallback function.
2677 */
2678 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2679
2680 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002681 * Allows the host application to group objects together. If one
2682 * object in the group is alive, all objects in the group are alive.
2683 * After each garbage collection, object groups are removed. It is
2684 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002685 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002686 * wrapper objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 */
ager@chromium.org8bb60582008-12-11 12:02:20 +00002688 static void AddObjectGroup(Persistent<Value>* objects, size_t length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689
2690 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002691 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002692 * initialize from scratch. This function is called implicitly if
2693 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694 */
2695 static bool Initialize();
2696
kasper.lund7276f142008-07-30 08:49:36 +00002697 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002698 * Adjusts the amount of registered external memory. Used to give
2699 * V8 an indication of the amount of externally allocated memory
2700 * that is kept alive by JavaScript objects. V8 uses this to decide
2701 * when to perform global garbage collections. Registering
2702 * externally allocated memory will trigger global garbage
2703 * collections more often than otherwise in an attempt to garbage
2704 * collect the JavaScript objects keeping the externally allocated
2705 * memory alive.
2706 *
2707 * \param change_in_bytes the change in externally allocated memory
2708 * that is kept alive by JavaScript objects.
2709 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002710 */
2711 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2712
iposva@chromium.org245aa852009-02-10 00:49:54 +00002713 /**
2714 * Suspends recording of tick samples in the profiler.
2715 * When the V8 profiling mode is enabled (usually via command line
2716 * switches) this function suspends recording of tick samples.
2717 * Profiling ticks are discarded until ResumeProfiler() is called.
2718 *
2719 * See also the --prof and --prof_auto command line switches to
2720 * enable V8 profiling.
2721 */
2722 static void PauseProfiler();
2723
2724 /**
2725 * Resumes recording of tick samples in the profiler.
2726 * See also PauseProfiler().
2727 */
2728 static void ResumeProfiler();
2729
ager@chromium.org41826e72009-03-30 13:30:57 +00002730 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002731 * Return whether profiler is currently paused.
2732 */
2733 static bool IsProfilerPaused();
2734
2735 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002736 * Resumes specified profiler modules. Can be called several times to
2737 * mark the opening of a profiler events block with the given tag.
2738 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002739 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2740 * See ProfilerModules enum.
2741 *
2742 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002743 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002744 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002745 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002746
2747 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002748 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2749 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2750 * same tag value. There is no need for blocks to be properly nested.
2751 * The profiler is paused when the last opened block is closed.
2752 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002753 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2754 * See ProfilerModules enum.
2755 *
2756 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002757 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002758 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002759 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002760
2761 /**
2762 * Returns active (resumed) profiler modules.
2763 * See ProfilerModules enum.
2764 *
2765 * \returns active profiler modules.
2766 */
2767 static int GetActiveProfilerModules();
2768
2769 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00002770 * If logging is performed into a memory buffer (via --logfile=*), allows to
2771 * retrieve previously written messages. This can be used for retrieving
2772 * profiler log data in the application. This function is thread-safe.
2773 *
2774 * Caller provides a destination buffer that must exist during GetLogLines
2775 * call. Only whole log lines are copied into the buffer.
2776 *
2777 * \param from_pos specified a point in a buffer to read from, 0 is the
2778 * beginning of a buffer. It is assumed that caller updates its current
2779 * position using returned size value from the previous call.
2780 * \param dest_buf destination buffer for log data.
2781 * \param max_size size of the destination buffer.
2782 * \returns actual size of log data copied into buffer.
2783 */
2784 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
2785
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002786 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002787 * The minimum allowed size for a log lines buffer. If the size of
2788 * the buffer given will not be enough to hold a line of the maximum
2789 * length, an attempt to find a log line end in GetLogLines will
2790 * fail, and an empty result will be returned.
2791 */
2792 static const int kMinimumSizeForLogLinesBuffer = 2048;
2793
2794 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002795 * Retrieve the V8 thread id of the calling thread.
2796 *
2797 * The thread id for a thread should only be retrieved after the V8
2798 * lock has been acquired with a Locker object with that thread.
2799 */
2800 static int GetCurrentThreadId();
2801
2802 /**
2803 * Forcefully terminate execution of a JavaScript thread. This can
2804 * be used to terminate long-running scripts.
2805 *
2806 * TerminateExecution should only be called when then V8 lock has
2807 * been acquired with a Locker object. Therefore, in order to be
2808 * able to terminate long-running threads, preemption must be
2809 * enabled to allow the user of TerminateExecution to acquire the
2810 * lock.
2811 *
2812 * The termination is achieved by throwing an exception that is
2813 * uncatchable by JavaScript exception handlers. Termination
2814 * exceptions act as if they were caught by a C++ TryCatch exception
2815 * handlers. If forceful termination is used, any C++ TryCatch
2816 * exception handler that catches an exception should check if that
2817 * exception is a termination exception and immediately return if
2818 * that is the case. Returning immediately in that case will
2819 * continue the propagation of the termination exception if needed.
2820 *
2821 * The thread id passed to TerminateExecution must have been
2822 * obtained by calling GetCurrentThreadId on the thread in question.
2823 *
2824 * \param thread_id The thread id of the thread to terminate.
2825 */
2826 static void TerminateExecution(int thread_id);
2827
2828 /**
2829 * Forcefully terminate the current thread of JavaScript execution.
2830 *
2831 * This method can be used by any thread even if that thread has not
2832 * acquired the V8 lock with a Locker object.
2833 */
2834 static void TerminateExecution();
ager@chromium.org9085a012009-05-11 19:22:57 +00002835
2836 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00002837 * Is V8 terminating JavaScript execution.
2838 *
2839 * Returns true if JavaScript execution is currently terminating
2840 * because of a call to TerminateExecution. In that case there are
2841 * still JavaScript frames on the stack and the termination
2842 * exception is still active.
2843 */
2844 static bool IsExecutionTerminating();
2845
2846 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00002847 * Releases any resources used by v8 and stops any utility threads
2848 * that may be running. Note that disposing v8 is permanent, it
2849 * cannot be reinitialized.
2850 *
2851 * It should generally not be necessary to dispose v8 before exiting
2852 * a process, this should happen automatically. It is only necessary
2853 * to use if the process needs the resources taken up by v8.
2854 */
2855 static bool Dispose();
2856
ager@chromium.org3811b432009-10-28 14:53:37 +00002857 /**
2858 * Get statistics about the heap memory usage.
2859 */
2860 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002861
2862 /**
2863 * Optional notification that the embedder is idle.
2864 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002865 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002866 * Returns true if the embedder should stop calling IdleNotification
2867 * until real work has been done. This indicates that V8 has done
2868 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002869 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002870 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00002871
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002872 /**
2873 * Optional notification that the system is running low on memory.
2874 * V8 uses these notifications to attempt to free memory.
2875 */
2876 static void LowMemoryNotification();
2877
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002878 /**
2879 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002880 * these notifications to guide the GC heuristic. Returns the number
2881 * of context disposals - including this one - since the last time
2882 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002883 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002884 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00002885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886 private:
2887 V8();
2888
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002889 static internal::Object** GlobalizeReference(internal::Object** handle);
2890 static void DisposeGlobal(internal::Object** global_handle);
2891 static void MakeWeak(internal::Object** global_handle,
2892 void* data,
2893 WeakReferenceCallback);
2894 static void ClearWeak(internal::Object** global_handle);
2895 static bool IsGlobalNearDeath(internal::Object** global_handle);
2896 static bool IsGlobalWeak(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897
2898 template <class T> friend class Handle;
2899 template <class T> friend class Local;
2900 template <class T> friend class Persistent;
2901 friend class Context;
2902};
2903
2904
2905/**
2906 * An external exception handler.
2907 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002908class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909 public:
2910
2911 /**
2912 * Creates a new try/catch block and registers it with v8.
2913 */
2914 TryCatch();
2915
2916 /**
2917 * Unregisters and deletes this try/catch block.
2918 */
2919 ~TryCatch();
2920
2921 /**
2922 * Returns true if an exception has been caught by this try/catch block.
2923 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002924 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002925
2926 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002927 * For certain types of exceptions, it makes no sense to continue
2928 * execution.
2929 *
2930 * Currently, the only type of exception that can be caught by a
2931 * TryCatch handler and for which it does not make sense to continue
2932 * is termination exception. Such exceptions are thrown when the
2933 * TerminateExecution methods are called to terminate a long-running
2934 * script.
2935 *
2936 * If CanContinue returns false, the correct action is to perform
2937 * any C++ cleanup needed and then return.
2938 */
2939 bool CanContinue() const;
2940
2941 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00002942 * Throws the exception caught by this TryCatch in a way that avoids
2943 * it being caught again by this same TryCatch. As with ThrowException
2944 * it is illegal to execute any JavaScript operations after calling
2945 * ReThrow; the caller must return immediately to where the exception
2946 * is caught.
2947 */
2948 Handle<Value> ReThrow();
2949
2950 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951 * Returns the exception caught by this try/catch block. If no exception has
2952 * been caught an empty handle is returned.
2953 *
2954 * The returned handle is valid until this TryCatch block has been destroyed.
2955 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002956 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957
2958 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002959 * Returns the .stack property of the thrown object. If no .stack
2960 * property is present an empty handle is returned.
2961 */
2962 Local<Value> StackTrace() const;
2963
2964 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002965 * Returns the message associated with this exception. If there is
2966 * no message associated an empty handle is returned.
2967 *
2968 * The returned handle is valid until this TryCatch block has been
2969 * destroyed.
2970 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002971 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002972
2973 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002974 * Clears any exceptions that may have been caught by this try/catch block.
2975 * After this method has been called, HasCaught() will return false.
2976 *
2977 * It is not necessary to clear a try/catch block before using it again; if
2978 * another exception is thrown the previously caught exception will just be
2979 * overwritten. However, it is often a good idea since it makes it easier
2980 * to determine which operation threw a given exception.
2981 */
2982 void Reset();
2983
v8.team.kasperl727e9952008-09-02 14:56:44 +00002984 /**
2985 * Set verbosity of the external exception handler.
2986 *
2987 * By default, exceptions that are caught by an external exception
2988 * handler are not reported. Call SetVerbose with true on an
2989 * external exception handler to have exceptions caught by the
2990 * handler reported as if they were not caught.
2991 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 void SetVerbose(bool value);
2993
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002994 /**
2995 * Set whether or not this TryCatch should capture a Message object
2996 * which holds source information about where the exception
2997 * occurred. True by default.
2998 */
2999 void SetCaptureMessage(bool value);
3000
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003001 private:
3002 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003004 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003005 bool is_verbose_ : 1;
3006 bool can_continue_ : 1;
3007 bool capture_message_ : 1;
3008 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003009
3010 friend class v8::internal::Top;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011};
3012
3013
3014// --- C o n t e x t ---
3015
3016
3017/**
3018 * Ignore
3019 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003020class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 public:
3022 ExtensionConfiguration(int name_count, const char* names[])
3023 : name_count_(name_count), names_(names) { }
3024 private:
3025 friend class ImplementationUtilities;
3026 int name_count_;
3027 const char** names_;
3028};
3029
3030
3031/**
3032 * A sandboxed execution context with its own set of built-in objects
3033 * and functions.
3034 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003035class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036 public:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003037 /** Returns the global object of the context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003038 Local<Object> Global();
3039
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003040 /**
3041 * Detaches the global object from its context before
3042 * the global object can be reused to create a new context.
3043 */
3044 void DetachGlobal();
3045
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003046 /**
3047 * Reattaches a global object to a context. This can be used to
3048 * restore the connection between a global object and a context
3049 * after DetachGlobal has been called.
3050 *
3051 * \param global_object The global object to reattach to the
3052 * context. For this to work, the global object must be the global
3053 * object that was associated with this context before a call to
3054 * DetachGlobal.
3055 */
3056 void ReattachGlobal(Handle<Object> global_object);
3057
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003058 /** Creates a new context.
3059 *
3060 * Returns a persistent handle to the newly allocated context. This
3061 * persistent handle has to be disposed when the context is no
3062 * longer used so the context can be garbage collected.
3063 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003064 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003065 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003066 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3067 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003068
kasper.lund44510672008-07-25 07:37:58 +00003069 /** Returns the last entered context. */
3070 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071
kasper.lund44510672008-07-25 07:37:58 +00003072 /** Returns the context that is on the top of the stack. */
3073 static Local<Context> GetCurrent();
3074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003075 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003076 * Returns the context of the calling JavaScript code. That is the
3077 * context of the top-most JavaScript frame. If there are no
3078 * JavaScript frames an empty handle is returned.
3079 */
3080 static Local<Context> GetCalling();
3081
3082 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003083 * Sets the security token for the context. To access an object in
3084 * another context, the security tokens must match.
3085 */
3086 void SetSecurityToken(Handle<Value> token);
3087
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003088 /** Restores the security token to the default value. */
3089 void UseDefaultSecurityToken();
3090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003091 /** Returns the security token of this context.*/
3092 Handle<Value> GetSecurityToken();
3093
v8.team.kasperl727e9952008-09-02 14:56:44 +00003094 /**
3095 * Enter this context. After entering a context, all code compiled
3096 * and run is compiled and run in this context. If another context
3097 * is already entered, this old context is saved so it can be
3098 * restored when the new context is exited.
3099 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003101
3102 /**
3103 * Exit this context. Exiting the current context restores the
3104 * context that was in place when entering the current context.
3105 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106 void Exit();
3107
v8.team.kasperl727e9952008-09-02 14:56:44 +00003108 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109 bool HasOutOfMemoryException();
3110
v8.team.kasperl727e9952008-09-02 14:56:44 +00003111 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112 static bool InContext();
3113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003115 * Associate an additional data object with the context. This is mainly used
3116 * with the debugger to provide additional information on the context through
3117 * the debugger API.
3118 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003119 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003120 Local<Value> GetData();
3121
3122 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 * Stack-allocated class which sets the execution context for all
3124 * operations executed within a local scope.
3125 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003126 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003127 public:
3128 inline Scope(Handle<Context> context) : context_(context) {
3129 context_->Enter();
3130 }
3131 inline ~Scope() { context_->Exit(); }
3132 private:
3133 Handle<Context> context_;
3134 };
3135
3136 private:
3137 friend class Value;
3138 friend class Script;
3139 friend class Object;
3140 friend class Function;
3141};
3142
3143
3144/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003145 * Multiple threads in V8 are allowed, but only one thread at a time
3146 * is allowed to use V8. The definition of 'using V8' includes
3147 * accessing handles or holding onto object pointers obtained from V8
3148 * handles. It is up to the user of V8 to ensure (perhaps with
3149 * locking) that this constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150 *
3151 * If you wish to start using V8 in a thread you can do this by constructing
3152 * a v8::Locker object. After the code using V8 has completed for the
3153 * current thread you can call the destructor. This can be combined
3154 * with C++ scope-based construction as follows:
3155 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003156 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157 * ...
3158 * {
3159 * v8::Locker locker;
3160 * ...
3161 * // Code using V8 goes here.
3162 * ...
3163 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003164 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165 *
3166 * If you wish to stop using V8 in a thread A you can do this by either
3167 * by destroying the v8::Locker object as above or by constructing a
3168 * v8::Unlocker object:
3169 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003170 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003171 * {
3172 * v8::Unlocker unlocker;
3173 * ...
3174 * // Code not using V8 goes here while V8 can run in another thread.
3175 * ...
3176 * } // Destructor called here.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003177 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 *
3179 * The Unlocker object is intended for use in a long-running callback
3180 * from V8, where you want to release the V8 lock for other threads to
3181 * use.
3182 *
3183 * The v8::Locker is a recursive lock. That is, you can lock more than
3184 * once in a given thread. This can be useful if you have code that can
3185 * be called either from code that holds the lock or from code that does
3186 * not. The Unlocker is not recursive so you can not have several
3187 * Unlockers on the stack at once, and you can not use an Unlocker in a
3188 * thread that is not inside a Locker's scope.
3189 *
3190 * An unlocker will unlock several lockers if it has to and reinstate
3191 * the correct depth of locking on its destruction. eg.:
3192 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003193 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194 * // V8 not locked.
3195 * {
3196 * v8::Locker locker;
3197 * // V8 locked.
3198 * {
3199 * v8::Locker another_locker;
3200 * // V8 still locked (2 levels).
3201 * {
3202 * v8::Unlocker unlocker;
3203 * // V8 not locked.
3204 * }
3205 * // V8 locked again (2 levels).
3206 * }
3207 * // V8 still locked (1 level).
3208 * }
3209 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003210 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003211 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003212class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003213 public:
3214 Unlocker();
3215 ~Unlocker();
3216};
3217
3218
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003219class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220 public:
3221 Locker();
3222 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003223
3224 /**
3225 * Start preemption.
3226 *
3227 * When preemption is started, a timer is fired every n milli seconds
3228 * that will switch between multiple threads that are in contention
3229 * for the V8 lock.
3230 */
3231 static void StartPreemption(int every_n_ms);
3232
3233 /**
3234 * Stop preemption.
3235 */
3236 static void StopPreemption();
3237
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003238 /**
3239 * Returns whether or not the locker is locked by the current thread.
3240 */
3241 static bool IsLocked();
3242
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003243 /**
3244 * Returns whether v8::Locker is being used by this V8 instance.
3245 */
3246 static bool IsActive() { return active_; }
3247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248 private:
3249 bool has_lock_;
3250 bool top_level_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003251
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003252 static bool active_;
3253
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003254 // Disallow copying and assigning.
3255 Locker(const Locker&);
3256 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257};
3258
3259
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003260/**
3261 * An interface for exporting data from V8, using "push" model.
3262 */
3263class V8EXPORT OutputStream {
3264public:
3265 enum OutputEncoding {
3266 kAscii = 0 // 7-bit ASCII.
3267 };
3268 enum WriteResult {
3269 kContinue = 0,
3270 kAbort = 1
3271 };
3272 virtual ~OutputStream() {}
3273 /** Notify about the end of stream. */
3274 virtual void EndOfStream() = 0;
3275 /** Get preferred output chunk size. Called only once. */
3276 virtual int GetChunkSize() { return 1024; }
3277 /** Get preferred output encoding. Called only once. */
3278 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3279 /**
3280 * Writes the next chunk of snapshot data into the stream. Writing
3281 * can be stopped by returning kAbort as function result. EndOfStream
3282 * will not be called in case writing was aborted.
3283 */
3284 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3285};
3286
3287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288
3289// --- I m p l e m e n t a t i o n ---
3290
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003291
3292namespace internal {
3293
3294
3295// Tag information for HeapObject.
3296const int kHeapObjectTag = 1;
3297const int kHeapObjectTagSize = 2;
3298const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3299
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003300// Tag information for Smi.
3301const int kSmiTag = 0;
3302const int kSmiTagSize = 1;
3303const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3304
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003305template <size_t ptr_size> struct SmiConstants;
3306
3307// Smi constants for 32-bit systems.
3308template <> struct SmiConstants<4> {
3309 static const int kSmiShiftSize = 0;
3310 static const int kSmiValueSize = 31;
3311 static inline int SmiToInt(internal::Object* value) {
3312 int shift_bits = kSmiTagSize + kSmiShiftSize;
3313 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3314 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3315 }
3316};
3317
3318// Smi constants for 64-bit systems.
3319template <> struct SmiConstants<8> {
3320 static const int kSmiShiftSize = 31;
3321 static const int kSmiValueSize = 32;
3322 static inline int SmiToInt(internal::Object* value) {
3323 int shift_bits = kSmiTagSize + kSmiShiftSize;
3324 // Shift down and throw away top 32 bits.
3325 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3326 }
3327};
3328
3329const int kSmiShiftSize = SmiConstants<sizeof(void*)>::kSmiShiftSize;
3330const int kSmiValueSize = SmiConstants<sizeof(void*)>::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003331
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003332template <size_t ptr_size> struct InternalConstants;
3333
3334// Internal constants for 32-bit systems.
3335template <> struct InternalConstants<4> {
3336 static const int kStringResourceOffset = 3 * sizeof(void*);
3337};
3338
3339// Internal constants for 64-bit systems.
3340template <> struct InternalConstants<8> {
ager@chromium.orgac091b72010-05-05 07:34:42 +00003341 static const int kStringResourceOffset = 3 * sizeof(void*);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003342};
3343
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003344/**
3345 * This class exports constants and functionality from within v8 that
3346 * is necessary to implement inline functions in the v8 api. Don't
3347 * depend on functions and constants defined here.
3348 */
3349class Internals {
3350 public:
3351
3352 // These values match non-compiler-dependent values defined within
3353 // the implementation of v8.
3354 static const int kHeapObjectMapOffset = 0;
3355 static const int kMapInstanceTypeOffset = sizeof(void*) + sizeof(int);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003356 static const int kStringResourceOffset =
3357 InternalConstants<sizeof(void*)>::kStringResourceOffset;
3358
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003359 static const int kProxyProxyOffset = sizeof(void*);
3360 static const int kJSObjectHeaderSize = 3 * sizeof(void*);
3361 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003362 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003363
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003364 static const int kJSObjectType = 0x9f;
3365 static const int kFirstNonstringType = 0x80;
3366 static const int kProxyType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003367
3368 static inline bool HasHeapObjectTag(internal::Object* value) {
3369 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3370 kHeapObjectTag);
3371 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003372
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003373 static inline bool HasSmiTag(internal::Object* value) {
3374 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3375 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003376
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003377 static inline int SmiValue(internal::Object* value) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003378 return SmiConstants<sizeof(void*)>::SmiToInt(value);
3379 }
3380
3381 static inline int GetInstanceType(internal::Object* obj) {
3382 typedef internal::Object O;
3383 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3384 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3385 }
3386
3387 static inline void* GetExternalPointer(internal::Object* obj) {
3388 if (HasSmiTag(obj)) {
3389 return obj;
3390 } else if (GetInstanceType(obj) == kProxyType) {
3391 return ReadField<void*>(obj, kProxyProxyOffset);
3392 } else {
3393 return NULL;
3394 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003395 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003396
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003397 static inline bool IsExternalTwoByteString(int instance_type) {
3398 int representation = (instance_type & kFullStringRepresentationMask);
3399 return representation == kExternalTwoByteRepresentationTag;
3400 }
3401
3402 template <typename T>
3403 static inline T ReadField(Object* ptr, int offset) {
3404 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3405 return *reinterpret_cast<T*>(addr);
3406 }
3407
3408};
3409
3410}
3411
3412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003413template <class T>
3414Handle<T>::Handle() : val_(0) { }
3415
3416
3417template <class T>
3418Local<T>::Local() : Handle<T>() { }
3419
3420
3421template <class T>
3422Local<T> Local<T>::New(Handle<T> that) {
3423 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003424 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3426}
3427
3428
3429template <class T>
3430Persistent<T> Persistent<T>::New(Handle<T> that) {
3431 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003432 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003433 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3434}
3435
3436
3437template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003438bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003439 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003440 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441}
3442
3443
3444template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003445bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003446 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003447 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003448}
3449
3450
3451template <class T>
3452void Persistent<T>::Dispose() {
3453 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003454 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003455}
3456
3457
3458template <class T>
3459Persistent<T>::Persistent() : Handle<T>() { }
3460
3461template <class T>
3462void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003463 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3464 parameters,
3465 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003466}
3467
3468template <class T>
3469void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003470 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003471}
3472
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003473
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003474Arguments::Arguments(internal::Object** implicit_args,
3475 internal::Object** values, int length,
3476 bool is_construct_call)
3477 : implicit_args_(implicit_args),
3478 values_(values),
3479 length_(length),
3480 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003481
3482
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483Local<Value> Arguments::operator[](int i) const {
3484 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3485 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3486}
3487
3488
3489Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003490 return Local<Function>(reinterpret_cast<Function*>(
3491 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492}
3493
3494
3495Local<Object> Arguments::This() const {
3496 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3497}
3498
3499
3500Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003501 return Local<Object>(reinterpret_cast<Object*>(
3502 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003503}
3504
3505
3506Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003507 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508}
3509
3510
3511bool Arguments::IsConstructCall() const {
3512 return is_construct_call_;
3513}
3514
3515
3516int Arguments::Length() const {
3517 return length_;
3518}
3519
3520
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521template <class T>
3522Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003523 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3524 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003525 return Local<T>(reinterpret_cast<T*>(after));
3526}
3527
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003528Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529 return resource_name_;
3530}
3531
3532
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003533Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003534 return resource_line_offset_;
3535}
3536
3537
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003538Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539 return resource_column_offset_;
3540}
3541
3542
3543Handle<Boolean> Boolean::New(bool value) {
3544 return value ? True() : False();
3545}
3546
3547
3548void Template::Set(const char* name, v8::Handle<Data> value) {
3549 Set(v8::String::New(name), value);
3550}
3551
3552
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003553Local<Value> Object::GetInternalField(int index) {
3554#ifndef V8_ENABLE_CHECKS
3555 Local<Value> quick_result = UncheckedGetInternalField(index);
3556 if (!quick_result.IsEmpty()) return quick_result;
3557#endif
3558 return CheckedGetInternalField(index);
3559}
3560
3561
3562Local<Value> Object::UncheckedGetInternalField(int index) {
3563 typedef internal::Object O;
3564 typedef internal::Internals I;
3565 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003566 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003567 // If the object is a plain JSObject, which is the common case,
3568 // we know where to find the internal fields and can return the
3569 // value directly.
3570 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3571 O* value = I::ReadField<O*>(obj, offset);
3572 O** result = HandleScope::CreateHandle(value);
3573 return Local<Value>(reinterpret_cast<Value*>(result));
3574 } else {
3575 return Local<Value>();
3576 }
3577}
3578
3579
3580void* External::Unwrap(Handle<v8::Value> obj) {
3581#ifdef V8_ENABLE_CHECKS
3582 return FullUnwrap(obj);
3583#else
3584 return QuickUnwrap(obj);
3585#endif
3586}
3587
3588
3589void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3590 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003591 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003592 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003593}
3594
3595
3596void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003597 typedef internal::Object O;
3598 typedef internal::Internals I;
3599
3600 O* obj = *reinterpret_cast<O**>(this);
3601
3602 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3603 // If the object is a plain JSObject, which is the common case,
3604 // we know where to find the internal fields and can return the
3605 // value directly.
3606 int offset = I::kJSObjectHeaderSize + (sizeof(void*) * index);
3607 O* value = I::ReadField<O*>(obj, offset);
3608 return I::GetExternalPointer(value);
3609 }
3610
3611 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003612}
3613
3614
3615String* String::Cast(v8::Value* value) {
3616#ifdef V8_ENABLE_CHECKS
3617 CheckCast(value);
3618#endif
3619 return static_cast<String*>(value);
3620}
3621
3622
3623String::ExternalStringResource* String::GetExternalStringResource() const {
3624 typedef internal::Object O;
3625 typedef internal::Internals I;
3626 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003627 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003628 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003629 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3630 result = reinterpret_cast<String::ExternalStringResource*>(value);
3631 } else {
3632 result = NULL;
3633 }
3634#ifdef V8_ENABLE_CHECKS
3635 VerifyExternalStringResource(result);
3636#endif
3637 return result;
3638}
3639
3640
3641bool Value::IsString() const {
3642#ifdef V8_ENABLE_CHECKS
3643 return FullIsString();
3644#else
3645 return QuickIsString();
3646#endif
3647}
3648
3649bool Value::QuickIsString() const {
3650 typedef internal::Object O;
3651 typedef internal::Internals I;
3652 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
3653 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003654 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003655}
3656
3657
3658Number* Number::Cast(v8::Value* value) {
3659#ifdef V8_ENABLE_CHECKS
3660 CheckCast(value);
3661#endif
3662 return static_cast<Number*>(value);
3663}
3664
3665
3666Integer* Integer::Cast(v8::Value* value) {
3667#ifdef V8_ENABLE_CHECKS
3668 CheckCast(value);
3669#endif
3670 return static_cast<Integer*>(value);
3671}
3672
3673
3674Date* Date::Cast(v8::Value* value) {
3675#ifdef V8_ENABLE_CHECKS
3676 CheckCast(value);
3677#endif
3678 return static_cast<Date*>(value);
3679}
3680
3681
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00003682RegExp* RegExp::Cast(v8::Value* value) {
3683#ifdef V8_ENABLE_CHECKS
3684 CheckCast(value);
3685#endif
3686 return static_cast<RegExp*>(value);
3687}
3688
3689
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003690Object* Object::Cast(v8::Value* value) {
3691#ifdef V8_ENABLE_CHECKS
3692 CheckCast(value);
3693#endif
3694 return static_cast<Object*>(value);
3695}
3696
3697
3698Array* Array::Cast(v8::Value* value) {
3699#ifdef V8_ENABLE_CHECKS
3700 CheckCast(value);
3701#endif
3702 return static_cast<Array*>(value);
3703}
3704
3705
3706Function* Function::Cast(v8::Value* value) {
3707#ifdef V8_ENABLE_CHECKS
3708 CheckCast(value);
3709#endif
3710 return static_cast<Function*>(value);
3711}
3712
3713
3714External* External::Cast(v8::Value* value) {
3715#ifdef V8_ENABLE_CHECKS
3716 CheckCast(value);
3717#endif
3718 return static_cast<External*>(value);
3719}
3720
3721
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003722Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00003723 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003724}
3725
3726
3727Local<Object> AccessorInfo::This() const {
3728 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
3729}
3730
3731
3732Local<Object> AccessorInfo::Holder() const {
3733 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
3734}
3735
3736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003737/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003738 * \example shell.cc
3739 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003740 * command-line and executes them.
3741 */
3742
3743
3744/**
3745 * \example process.cc
3746 */
3747
3748
3749} // namespace v8
3750
3751
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003752#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003753#undef TYPE_CHECK
3754
3755
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003756#endif // V8_H_