blob: b4598c67dba7934b251b712126a46bbfc0aca2e5 [file] [log] [blame]
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001// Copyright 2011 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
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000041#include "v8stdint.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
43#ifdef _WIN32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
45// Setup for Windows DLL export/import. When building the V8 DLL the
46// BUILDING_V8_SHARED needs to be defined. When building a program which uses
47// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
48// static library or building a program which uses the V8 static library neither
49// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
51#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
52 build configuration to ensure that at most one of these is set
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053#endif
54
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000056#define V8EXPORT __declspec(dllexport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057#elif USING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000058#define V8EXPORT __declspec(dllimport)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000060#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061#endif // BUILDING_V8_SHARED
62
63#else // _WIN32
ager@chromium.org9085a012009-05-11 19:22:57 +000064
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +000065// Setup for Linux shared library export. There is no need to distinguish
66// between building or using the V8 shared library, but we should not
67// export symbols when we are building a static library.
68#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000070#else // defined(__GNUC__) && (__GNUC__ >= 4)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000071#define V8EXPORT
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000072#endif // defined(__GNUC__) && (__GNUC__ >= 4)
73
74#endif // _WIN32
75
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000077 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 */
79namespace v8 {
80
81class Context;
82class String;
83class Value;
84class Utils;
85class Number;
86class Object;
87class Array;
88class Int32;
89class Uint32;
90class External;
91class Primitive;
92class Boolean;
93class Integer;
94class Function;
95class Date;
96class ImplementationUtilities;
97class Signature;
98template <class T> class Handle;
99template <class T> class Local;
100template <class T> class Persistent;
101class FunctionTemplate;
102class ObjectTemplate;
103class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000104class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000105class StackTrace;
106class StackFrame;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000108namespace internal {
109
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000110class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000111class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000112class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113class HeapObject;
114class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000115}
116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000118// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
120
121/**
122 * A weak reference callback function.
123 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000124 * This callback should either explicitly invoke Dispose on |object| if
125 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
126 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127 * \param object the weak global object to be reclaimed by the garbage collector
128 * \param parameter the value passed in when making the weak global object
129 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000130typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 void* parameter);
132
133
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000134// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000136#define TYPE_CHECK(T, S) \
137 while (false) { \
138 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 }
140
141/**
142 * An object reference managed by the v8 garbage collector.
143 *
144 * All objects returned from v8 have to be tracked by the garbage
145 * collector so that it knows that the objects are still alive. Also,
146 * because the garbage collector may move objects, it is unsafe to
147 * point directly to an object. Instead, all objects are stored in
148 * handles which are known by the garbage collector and updated
149 * whenever an object moves. Handles should always be passed by value
150 * (except in cases like out-parameters) and they should never be
151 * allocated on the heap.
152 *
153 * There are two types of handles: local and persistent handles.
154 * Local handles are light-weight and transient and typically used in
155 * local operations. They are managed by HandleScopes. Persistent
156 * handles can be used when storing objects across several independent
157 * operations and have to be explicitly deallocated when they're no
158 * longer used.
159 *
160 * It is safe to extract the object stored in the handle by
161 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000162 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 * behind the scenes and the same rules apply to these values as to
164 * their handles.
165 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000166template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 public:
168
169 /**
170 * Creates an empty handle.
171 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000172 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173
174 /**
175 * Creates a new handle for the specified value.
176 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000177 inline explicit Handle(T* val) : val_(val) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
179 /**
180 * Creates a handle for the contents of the specified handle. This
181 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000182 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000184 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 * between compatible handles, for instance assigning a
186 * Handle<String> to a variable declared as Handle<Value>, is legal
187 * because String is a subclass of Value.
188 */
189 template <class S> inline Handle(Handle<S> that)
190 : val_(reinterpret_cast<T*>(*that)) {
191 /**
192 * This check fails when trying to convert between incompatible
193 * handles. For example, converting from a Handle<String> to a
194 * Handle<Number>.
195 */
196 TYPE_CHECK(T, S);
197 }
198
199 /**
200 * Returns true if the handle is empty.
201 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000202 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000204 inline T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000206 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
208 /**
209 * Sets the handle to be empty. IsEmpty() will then return true.
210 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000211 inline void Clear() { this->val_ = 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212
213 /**
214 * Checks whether two handles are the same.
215 * Returns true if both are empty, or if the objects
216 * to which they refer are identical.
217 * The handles' references are not checked.
218 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000219 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000220 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
221 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 if (a == 0) return b == 0;
223 if (b == 0) return false;
224 return *a == *b;
225 }
226
227 /**
228 * Checks whether two handles are different.
229 * Returns true if only one of the handles is empty, or if
230 * the objects to which they refer are different.
231 * The handles' references are not checked.
232 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000233 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 return !operator==(that);
235 }
236
237 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238#ifdef V8_ENABLE_CHECKS
239 // If we're going to perform the type check then we have to check
240 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000242#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 return Handle<T>(T::Cast(*that));
244 }
245
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000246 template <class S> inline Handle<S> As() {
247 return Handle<S>::Cast(*this);
248 }
249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 private:
251 T* val_;
252};
253
254
255/**
256 * A light-weight stack-allocated object handle. All operations
257 * that return objects from within v8 return them in local handles. They
258 * are created within HandleScopes, and all local handles allocated within a
259 * handle scope are destroyed when the handle scope is destroyed. Hence it
260 * is not necessary to explicitly deallocate local handles.
261 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000262template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000264 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 template <class S> inline Local(Local<S> that)
266 : Handle<T>(reinterpret_cast<T*>(*that)) {
267 /**
268 * This check fails when trying to convert between incompatible
269 * handles. For example, converting from a Handle<String> to a
270 * Handle<Number>.
271 */
272 TYPE_CHECK(T, S);
273 }
274 template <class S> inline Local(S* that) : Handle<T>(that) { }
275 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000276#ifdef V8_ENABLE_CHECKS
277 // If we're going to perform the type check then we have to check
278 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000280#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 return Local<T>(T::Cast(*that));
282 }
283
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000284 template <class S> inline Local<S> As() {
285 return Local<S>::Cast(*this);
286 }
287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 /** Create a local handle for the content of another handle.
289 * The referee is kept alive by the local handle even when
290 * the original handle is destroyed/disposed.
291 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000292 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293};
294
295
296/**
297 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000298 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 * allocated, a Persistent handle remains valid until it is explicitly
300 * disposed.
301 *
302 * A persistent handle contains a reference to a storage cell within
303 * the v8 engine which holds an object value and which is updated by
304 * the garbage collector whenever the object is moved. A new storage
305 * cell can be created using Persistent::New and existing handles can
306 * be disposed using Persistent::Dispose. Since persistent handles
307 * are passed by value you may have many persistent handle objects
308 * that point to the same storage cell. For instance, if you pass a
309 * persistent handle as an argument to a function you will not get two
310 * different storage cells but rather two references to the same
311 * storage cell.
312 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000313template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 public:
315
316 /**
317 * Creates an empty persistent handle that doesn't point to any
318 * storage cell.
319 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000320 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321
322 /**
323 * Creates a persistent handle for the same storage cell as the
324 * specified handle. This constructor allows you to pass persistent
325 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000326 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000328 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 * between compatible persistent handles, for instance assigning a
330 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000331 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 */
333 template <class S> inline Persistent(Persistent<S> that)
334 : Handle<T>(reinterpret_cast<T*>(*that)) {
335 /**
336 * This check fails when trying to convert between incompatible
337 * handles. For example, converting from a Handle<String> to a
338 * Handle<Number>.
339 */
340 TYPE_CHECK(T, S);
341 }
342
343 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
344
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000345 /**
346 * "Casts" a plain handle which is known to be a persistent handle
347 * to a persistent handle.
348 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 template <class S> explicit inline Persistent(Handle<S> that)
350 : Handle<T>(*that) { }
351
352 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000353#ifdef V8_ENABLE_CHECKS
354 // If we're going to perform the type check then we have to check
355 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000357#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000358 return Persistent<T>(T::Cast(*that));
359 }
360
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000361 template <class S> inline Persistent<S> As() {
362 return Persistent<S>::Cast(*this);
363 }
364
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000366 * Creates a new persistent handle for an existing local or
367 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000369 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370
371 /**
372 * Releases the storage cell referenced by this persistent handle.
373 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000374 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 * cell remain and IsEmpty will still return false.
376 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000377 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378
379 /**
380 * Make the reference to this object weak. When only weak handles
381 * refer to the object, the garbage collector will perform a
382 * callback to the given V8::WeakReferenceCallback function, passing
383 * it the object reference and the given parameters.
384 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000385 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386
387 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000388 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389
390 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000391 * Marks the reference to this object independent. Garbage collector
392 * is free to ignore any object groups containing this object.
393 * Weak callback for an independent handle should not
394 * assume that it will be preceded by a global GC prologue callback
395 * or followed by a global GC epilogue callback.
396 */
397 inline void MarkIndependent();
398
399 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 *Checks if the handle holds the only reference to an object.
401 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000402 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403
404 /**
405 * Returns true if the handle's reference is weak.
406 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000407 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000409 /**
410 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
411 * interface description in v8-profiler.h for details.
412 */
413 inline void SetWrapperClassId(uint16_t class_id);
414
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415 private:
416 friend class ImplementationUtilities;
417 friend class ObjectTemplate;
418};
419
420
v8.team.kasperl727e9952008-09-02 14:56:44 +0000421 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000422 * A stack-allocated class that governs a number of local handles.
423 * After a handle scope has been created, all local handles will be
424 * allocated within that handle scope until either the handle scope is
425 * deleted or another handle scope is created. If there is already a
426 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000427 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000428 * new handles will again be allocated in the original handle scope.
429 *
430 * After the handle scope of a local handle has been deleted the
431 * garbage collector will no longer track the object stored in the
432 * handle and may deallocate it. The behavior of accessing a handle
433 * for which the handle scope has been deleted is undefined.
434 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000435class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000437 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000439 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
441 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442 * Closes the handle scope and returns the value as a handle in the
443 * previous scope, which is the new current scope after the call.
444 */
445 template <class T> Local<T> Close(Handle<T> value);
446
447 /**
448 * Counts the number of allocated handles.
449 */
450 static int NumberOfHandles();
451
452 /**
453 * Creates a new handle with the given value.
454 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000455 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 // Faster version, uses HeapObject to obtain the current Isolate.
457 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458
459 private:
460 // Make it impossible to create heap-allocated or illegal handle
461 // scopes by disallowing certain operations.
462 HandleScope(const HandleScope&);
463 void operator=(const HandleScope&);
464 void* operator new(size_t size);
465 void operator delete(void*, size_t);
466
ager@chromium.org3811b432009-10-28 14:53:37 +0000467 // This Data class is accessible internally as HandleScopeData through a
468 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000469 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000471 internal::Object** next;
472 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000473 int level;
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 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000479
lrn@chromium.org303ada72010-10-27 09:33:13 +0000480 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000482 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000483 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
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000495// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496
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 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000787 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000788 *
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 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000846 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000847 * constructor via "new".
848 */
849 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850};
851
852
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000853// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000854
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 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001012 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1013 * before the end of the buffer.
1014 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001015 * Copies up to length characters into the output buffer.
1016 * Only null-terminates if there is enough space in the buffer.
1017 *
1018 * \param buffer The buffer into which the string will be copied.
1019 * \param start The starting position within the string at which
1020 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001021 * \param length The number of characters to copy from the string. For
1022 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001023 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001024 * \param hints Various hints that might affect performance of this or
1025 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001026 * \return The number of characters copied to the buffer excluding the null
1027 * terminator. For WriteUtf8: The number of bytes copied to the buffer
1028 * including the null terminator.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001029 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001030 enum WriteHints {
1031 NO_HINTS = 0,
1032 HINT_MANY_WRITES_EXPECTED = 1
1033 };
1034
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001035 V8EXPORT int Write(uint16_t* buffer,
1036 int start = 0,
1037 int length = -1,
1038 WriteHints hints = NO_HINTS) const; // UTF-16
1039 V8EXPORT int WriteAscii(char* buffer,
1040 int start = 0,
1041 int length = -1,
1042 WriteHints hints = NO_HINTS) const; // ASCII
1043 V8EXPORT int WriteUtf8(char* buffer,
1044 int length = -1,
1045 int* nchars_ref = NULL,
1046 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047
v8.team.kasperl727e9952008-09-02 14:56:44 +00001048 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001049 * A zero length string.
1050 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001051 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001052
1053 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001054 * Returns true if the string is external
1055 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001056 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
v8.team.kasperl727e9952008-09-02 14:56:44 +00001058 /**
1059 * Returns true if the string is both external and ascii
1060 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001061 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001062
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001063 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001064 public:
1065 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001066
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001067 protected:
1068 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001069
1070 /**
1071 * Internally V8 will call this Dispose method when the external string
1072 * resource is no longer needed. The default implementation will use the
1073 * delete operator. This method can be overridden in subclasses to
1074 * control how allocated external string resources are disposed.
1075 */
1076 virtual void Dispose() { delete this; }
1077
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001078 private:
1079 // Disallow copying and assigning.
1080 ExternalStringResourceBase(const ExternalStringResourceBase&);
1081 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001082
1083 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001084 };
1085
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 /**
1087 * An ExternalStringResource is a wrapper around a two-byte string
1088 * buffer that resides outside V8's heap. Implement an
1089 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001090 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001091 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001092 class V8EXPORT ExternalStringResource
1093 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 public:
1095 /**
1096 * Override the destructor to manage the life cycle of the underlying
1097 * buffer.
1098 */
1099 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001100
1101 /**
1102 * The string data from the underlying buffer.
1103 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105
1106 /**
1107 * The length of the string. That is, the number of two-byte characters.
1108 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001110
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 protected:
1112 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 };
1114
1115 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001116 * An ExternalAsciiStringResource is a wrapper around an ascii
1117 * string buffer that resides outside V8's heap. Implement an
1118 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001119 * underlying buffer. Note that the string data must be immutable
1120 * and that the data must be strict 7-bit ASCII, not Latin1 or
1121 * UTF-8, which would require special treatment internally in the
1122 * engine and, in the case of UTF-8, do not allow efficient indexing.
1123 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001124 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001126 class V8EXPORT ExternalAsciiStringResource
1127 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 public:
1129 /**
1130 * Override the destructor to manage the life cycle of the underlying
1131 * buffer.
1132 */
1133 virtual ~ExternalAsciiStringResource() {}
1134 /** The string data from the underlying buffer.*/
1135 virtual const char* data() const = 0;
1136 /** The number of ascii characters in the string.*/
1137 virtual size_t length() const = 0;
1138 protected:
1139 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 };
1141
1142 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001143 * Get the ExternalStringResource for an external string. Returns
1144 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001146 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147
1148 /**
1149 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001150 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001152 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001154 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155
1156 /**
1157 * Allocates a new string from either utf-8 encoded or ascii data.
1158 * The second parameter 'length' gives the buffer length.
1159 * If the data is utf-8 encoded, the caller must
1160 * be careful to supply the length parameter.
1161 * If it is not given, the function calls
1162 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001163 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001165 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166
1167 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001168 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169
1170 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001171 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172
v8.team.kasperl727e9952008-09-02 14:56:44 +00001173 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001174 * Creates a new string by concatenating the left and the right strings
1175 * passed in as parameters.
1176 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001177 V8EXPORT static Local<String> Concat(Handle<String> left,
1178 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001179
1180 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001181 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001182 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001183 * resource will be disposed by calling its Dispose method. The caller of
1184 * this function should not otherwise delete or modify the resource. Neither
1185 * should the underlying buffer be deallocated or modified except through the
1186 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001187 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001188 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001189
ager@chromium.org6f10e412009-02-13 10:11:16 +00001190 /**
1191 * Associate an external string resource with this string by transforming it
1192 * in place so that existing references to this string in the JavaScript heap
1193 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001194 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001195 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001196 * The string is not modified if the operation fails. See NewExternal for
1197 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001198 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001199 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200
v8.team.kasperl727e9952008-09-02 14:56:44 +00001201 /**
1202 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001203 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001204 * resource will be disposed by calling its Dispose method. The caller of
1205 * this function should not otherwise delete or modify the resource. Neither
1206 * should the underlying buffer be deallocated or modified except through the
1207 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001208 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001209 V8EXPORT static Local<String> NewExternal(
1210 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001211
ager@chromium.org6f10e412009-02-13 10:11:16 +00001212 /**
1213 * Associate an external string resource with this string by transforming it
1214 * in place so that existing references to this string in the JavaScript heap
1215 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001216 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001217 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001218 * The string is not modified if the operation fails. See NewExternal for
1219 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001220 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001221 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001223 /**
1224 * Returns true if this string can be made external.
1225 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001226 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001227
kasper.lund7276f142008-07-30 08:49:36 +00001228 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001229 V8EXPORT static Local<String> NewUndetectable(const char* data,
1230 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231
kasper.lund7276f142008-07-30 08:49:36 +00001232 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001233 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1234 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235
1236 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001237 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001238 * you want to print the object. If conversion to a string fails
1239 * (eg. due to an exception in the toString() method of the object)
1240 * then the length() method returns 0 and the * operator returns
1241 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001242 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001243 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001244 public:
1245 explicit Utf8Value(Handle<v8::Value> obj);
1246 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001247 char* operator*() { return str_; }
1248 const char* operator*() const { return str_; }
1249 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001250 private:
1251 char* str_;
1252 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001253
1254 // Disallow copying and assigning.
1255 Utf8Value(const Utf8Value&);
1256 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001257 };
1258
1259 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 * Converts an object to an ascii string.
1261 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001262 * If conversion to a string fails (eg. due to an exception in the toString()
1263 * method of the object) then the length() method returns 0 and the * operator
1264 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001266 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 public:
1268 explicit AsciiValue(Handle<v8::Value> obj);
1269 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001270 char* operator*() { return str_; }
1271 const char* operator*() const { return str_; }
1272 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 private:
1274 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001275 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001276
1277 // Disallow copying and assigning.
1278 AsciiValue(const AsciiValue&);
1279 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 };
1281
1282 /**
1283 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001284 * If conversion to a string fails (eg. due to an exception in the toString()
1285 * method of the object) then the length() method returns 0 and the * operator
1286 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001288 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 public:
1290 explicit Value(Handle<v8::Value> obj);
1291 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001292 uint16_t* operator*() { return str_; }
1293 const uint16_t* operator*() const { return str_; }
1294 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 private:
1296 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001297 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001298
1299 // Disallow copying and assigning.
1300 Value(const Value&);
1301 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001303
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001304 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001305 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1306 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307};
1308
1309
1310/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001311 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001313class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001315 V8EXPORT double Value() const;
1316 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001317 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001319 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001320 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321};
1322
1323
1324/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001325 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001327class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001329 V8EXPORT static Local<Integer> New(int32_t value);
1330 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1331 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001332 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001334 V8EXPORT Integer();
1335 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336};
1337
1338
1339/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001340 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001342class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001344 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001346 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347};
1348
1349
1350/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001351 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001353class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001355 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001357 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358};
1359
1360
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361enum PropertyAttribute {
1362 None = 0,
1363 ReadOnly = 1 << 0,
1364 DontEnum = 1 << 1,
1365 DontDelete = 1 << 2
1366};
1367
ager@chromium.org3811b432009-10-28 14:53:37 +00001368enum ExternalArrayType {
1369 kExternalByteArray = 1,
1370 kExternalUnsignedByteArray,
1371 kExternalShortArray,
1372 kExternalUnsignedShortArray,
1373 kExternalIntArray,
1374 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001375 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001376 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001377 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001378};
1379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001381 * Accessor[Getter|Setter] are used as callback functions when
1382 * setting|getting a particular property. See Object and ObjectTemplate's
1383 * method SetAccessor.
1384 */
1385typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1386 const AccessorInfo& info);
1387
1388
1389typedef void (*AccessorSetter)(Local<String> property,
1390 Local<Value> value,
1391 const AccessorInfo& info);
1392
1393
1394/**
1395 * Access control specifications.
1396 *
1397 * Some accessors should be accessible across contexts. These
1398 * accessors have an explicit access control parameter which specifies
1399 * the kind of cross-context access that should be allowed.
1400 *
1401 * Additionally, for security, accessors can prohibit overwriting by
1402 * accessors defined in JavaScript. For objects that have such
1403 * accessors either locally or in their prototype chain it is not
1404 * possible to overwrite the accessor by using __defineGetter__ or
1405 * __defineSetter__ from JavaScript code.
1406 */
1407enum AccessControl {
1408 DEFAULT = 0,
1409 ALL_CAN_READ = 1,
1410 ALL_CAN_WRITE = 1 << 1,
1411 PROHIBITS_OVERWRITING = 1 << 2
1412};
1413
1414
1415/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001416 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001418class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001420 V8EXPORT bool Set(Handle<Value> key,
1421 Handle<Value> value,
1422 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001423
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001424 V8EXPORT bool Set(uint32_t index,
1425 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001426
ager@chromium.orge2902be2009-06-08 12:21:35 +00001427 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001428 // overriding accessors or read-only properties.
1429 //
1430 // Note that if the object has an interceptor the property will be set
1431 // locally, but since the interceptor takes precedence the local property
1432 // will only be returned if the interceptor doesn't return a value.
1433 //
1434 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001435 V8EXPORT bool ForceSet(Handle<Value> key,
1436 Handle<Value> value,
1437 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001438
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001439 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001441 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 // TODO(1245389): Replace the type-specific versions of these
1444 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001445 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001446
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001447 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001448
1449 // Delete a property on this object bypassing interceptors and
1450 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001451 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001452
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001453 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001454
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001455 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001457 V8EXPORT bool SetAccessor(Handle<String> name,
1458 AccessorGetter getter,
1459 AccessorSetter setter = 0,
1460 Handle<Value> data = Handle<Value>(),
1461 AccessControl settings = DEFAULT,
1462 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001465 * Returns an array containing the names of the enumerable properties
1466 * of this object, including properties from prototype objects. The
1467 * array returned by this method contains the same values as would
1468 * be enumerated by a for-in statement over this object.
1469 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001470 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001471
1472 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 * Get the prototype object. This does not skip objects marked to
1474 * be skipped by __proto__ and it does not consult the security
1475 * handler.
1476 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001477 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478
1479 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001480 * Set the prototype object. This does not skip objects marked to
1481 * be skipped by __proto__ and it does not consult the security
1482 * handler.
1483 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001484 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001485
1486 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001487 * Finds an instance of the given function template in the prototype
1488 * chain.
1489 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001490 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1491 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001492
1493 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494 * Call builtin Object.prototype.toString on this object.
1495 * This is different from Value::ToString() that may call
1496 * user-defined toString function. This one does not.
1497 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001498 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001500 /**
1501 * Returns the name of the function invoked as a constructor for this object.
1502 */
1503 V8EXPORT Local<String> GetConstructorName();
1504
kasper.lund212ac232008-07-16 07:07:30 +00001505 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001506 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001507 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001508 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001509 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001510 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001512 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001513 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001514
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001515 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001516 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001519 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001520 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1521 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1522 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523
1524 /**
1525 * If result.IsEmpty() no real property was located in the prototype chain.
1526 * This means interceptors in the prototype chain are not called.
1527 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001528 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1529 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001530
1531 /**
1532 * If result.IsEmpty() no real property was located on the object or
1533 * in the prototype chain.
1534 * This means interceptors in the prototype chain are not called.
1535 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001536 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537
1538 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
kasper.lund212ac232008-07-16 07:07:30 +00001541 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001542 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001544 /**
1545 * Turns on access check on the object if the object is an instance of
1546 * a template that has access check callbacks. If an object has no
1547 * access check info, the object cannot be accessed by anyone.
1548 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001549 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001550
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001551 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001552 * Returns the identity hash for this object. The current implementation
1553 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001554 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001555 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001556 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001557 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001558 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001559
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001560 /**
1561 * Access hidden properties on JavaScript objects. These properties are
1562 * hidden from the executing JavaScript and only accessible through the V8
1563 * C++ API. Hidden properties introduced by V8 internally (for example the
1564 * identity hash) are prefixed with "v8::".
1565 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001566 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1567 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1568 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001569
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001570 /**
1571 * Returns true if this is an instance of an api function (one
1572 * created from a function created from a function template) and has
1573 * been modified since it was created. Note that this method is
1574 * conservative and may return true for objects that haven't actually
1575 * been modified.
1576 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001577 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001578
1579 /**
1580 * Clone this object with a fast but shallow copy. Values will point
1581 * to the same values as the original object.
1582 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001583 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001585 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001586 * Returns the context in which the object was created.
1587 */
1588 V8EXPORT Local<Context> CreationContext();
1589
1590 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001591 * Set the backing store of the indexed properties to be managed by the
1592 * embedding layer. Access to the indexed properties will follow the rules
1593 * spelled out in CanvasPixelArray.
1594 * Note: The embedding program still owns the data and needs to ensure that
1595 * the backing store is preserved while V8 has a reference.
1596 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001597 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001598 V8EXPORT bool HasIndexedPropertiesInPixelData();
1599 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1600 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001601
ager@chromium.org3811b432009-10-28 14:53:37 +00001602 /**
1603 * Set the backing store of the indexed properties to be managed by the
1604 * embedding layer. Access to the indexed properties will follow the rules
1605 * spelled out for the CanvasArray subtypes in the WebGL specification.
1606 * Note: The embedding program still owns the data and needs to ensure that
1607 * the backing store is preserved while V8 has a reference.
1608 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001609 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1610 void* data,
1611 ExternalArrayType array_type,
1612 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001613 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1614 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1615 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1616 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001617
lrn@chromium.org1c092762011-05-09 09:42:16 +00001618 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001619 * Checks whether a callback is set by the
1620 * ObjectTemplate::SetCallAsFunctionHandler method.
1621 * When an Object is callable this method returns true.
1622 */
1623 V8EXPORT bool IsCallable();
1624
1625 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00001626 * Call an Object as a function if a callback is set by the
1627 * ObjectTemplate::SetCallAsFunctionHandler method.
1628 */
1629 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1630 int argc,
1631 Handle<Value> argv[]);
1632
1633 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001634 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001635 * ObjectTemplate::SetCallAsFunctionHandler method.
1636 * Note: This method behaves like the Function::NewInstance method.
1637 */
1638 V8EXPORT Local<Value> CallAsConstructor(int argc,
1639 Handle<Value> argv[]);
1640
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001641 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001642 static inline Object* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001644 V8EXPORT Object();
1645 V8EXPORT static void CheckCast(Value* obj);
1646 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1647 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001648
1649 /**
1650 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001651 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001652 */
1653 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654};
1655
1656
1657/**
1658 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1659 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001660class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001662 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663
ager@chromium.org3e875802009-06-29 08:26:34 +00001664 /**
1665 * Clones an element at index |index|. Returns an empty
1666 * handle if cloning fails (for any reason).
1667 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001668 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001669
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001670 /**
1671 * Creates a JavaScript array with the given length. If the length
1672 * is negative the returned array will have length 0.
1673 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001674 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001675
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001676 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001678 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001679 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680};
1681
1682
1683/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001684 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001686class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001688 V8EXPORT Local<Object> NewInstance() const;
1689 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1690 V8EXPORT Local<Value> Call(Handle<Object> recv,
1691 int argc,
1692 Handle<Value> argv[]);
1693 V8EXPORT void SetName(Handle<String> name);
1694 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001695
1696 /**
1697 * Returns zero based line number of function body and
1698 * kLineOffsetNotFound if no information available.
1699 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001700 V8EXPORT int GetScriptLineNumber() const;
1701 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001702 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001703 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001705 V8EXPORT Function();
1706 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707};
1708
1709
1710/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001711 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1712 */
1713class Date : public Object {
1714 public:
1715 V8EXPORT static Local<Value> New(double time);
1716
1717 /**
1718 * A specialization of Value::NumberValue that is more efficient
1719 * because we know the structure of this object.
1720 */
1721 V8EXPORT double NumberValue() const;
1722
1723 static inline Date* Cast(v8::Value* obj);
1724
1725 /**
1726 * Notification that the embedder has changed the time zone,
1727 * daylight savings time, or other date / time configuration
1728 * parameters. V8 keeps a cache of various values used for
1729 * date / time computation. This notification will reset
1730 * those cached values for the current context so that date /
1731 * time configuration changes would be reflected in the Date
1732 * object.
1733 *
1734 * This API should not be called more than needed as it will
1735 * negatively impact the performance of date operations.
1736 */
1737 V8EXPORT static void DateTimeConfigurationChangeNotification();
1738
1739 private:
1740 V8EXPORT static void CheckCast(v8::Value* obj);
1741};
1742
1743
1744/**
1745 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1746 */
1747class RegExp : public Object {
1748 public:
1749 /**
1750 * Regular expression flag bits. They can be or'ed to enable a set
1751 * of flags.
1752 */
1753 enum Flags {
1754 kNone = 0,
1755 kGlobal = 1,
1756 kIgnoreCase = 2,
1757 kMultiline = 4
1758 };
1759
1760 /**
1761 * Creates a regular expression from the given pattern string and
1762 * the flags bit field. May throw a JavaScript exception as
1763 * described in ECMA-262, 15.10.4.1.
1764 *
1765 * For example,
1766 * RegExp::New(v8::String::New("foo"),
1767 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1768 * is equivalent to evaluating "/foo/gm".
1769 */
1770 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1771 Flags flags);
1772
1773 /**
1774 * Returns the value of the source property: a string representing
1775 * the regular expression.
1776 */
1777 V8EXPORT Local<String> GetSource() const;
1778
1779 /**
1780 * Returns the flags bit field.
1781 */
1782 V8EXPORT Flags GetFlags() const;
1783
1784 static inline RegExp* Cast(v8::Value* obj);
1785
1786 private:
1787 V8EXPORT static void CheckCast(v8::Value* obj);
1788};
1789
1790
1791/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001792 * A JavaScript value that wraps a C++ void*. This type of value is
1793 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001795 *
1796 * The Wrap function V8 will return the most optimal Value object wrapping the
1797 * C++ void*. The type of the value is not guaranteed to be an External object
1798 * and no assumptions about its type should be made. To access the wrapped
1799 * value Unwrap should be used, all other operations on that object will lead
1800 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001802class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001804 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001805 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001806
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001807 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001808 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001809 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001811 V8EXPORT External();
1812 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001813 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001814 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815};
1816
1817
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001818// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819
1820
1821/**
1822 * The superclass of object and function templates.
1823 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001824class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 public:
1826 /** Adds a property to each instance created by this template.*/
1827 void Set(Handle<String> name, Handle<Data> value,
1828 PropertyAttribute attributes = None);
1829 inline void Set(const char* name, Handle<Data> value);
1830 private:
1831 Template();
1832
1833 friend class ObjectTemplate;
1834 friend class FunctionTemplate;
1835};
1836
1837
1838/**
1839 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001840 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 * including the receiver, the number and values of arguments, and
1842 * the holder of the function.
1843 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001844class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 public:
1846 inline int Length() const;
1847 inline Local<Value> operator[](int i) const;
1848 inline Local<Function> Callee() const;
1849 inline Local<Object> This() const;
1850 inline Local<Object> Holder() const;
1851 inline bool IsConstructCall() const;
1852 inline Local<Value> Data() const;
1853 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001854 static const int kDataIndex = 0;
1855 static const int kCalleeIndex = -1;
1856 static const int kHolderIndex = -2;
1857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001859 inline Arguments(internal::Object** implicit_args,
1860 internal::Object** values,
1861 int length,
1862 bool is_construct_call);
1863 internal::Object** implicit_args_;
1864 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001866 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867};
1868
1869
1870/**
1871 * The information passed to an accessor callback about the context
1872 * of the property access.
1873 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001874class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001876 inline AccessorInfo(internal::Object** args)
1877 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 inline Local<Value> Data() const;
1879 inline Local<Object> This() const;
1880 inline Local<Object> Holder() const;
1881 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001882 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883};
1884
1885
1886typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 * NamedProperty[Getter|Setter] are used as interceptors on object.
1890 * See ObjectTemplate::SetNamedPropertyHandler.
1891 */
1892typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1893 const AccessorInfo& info);
1894
1895
1896/**
1897 * Returns the value if the setter intercepts the request.
1898 * Otherwise, returns an empty handle.
1899 */
1900typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1901 Local<Value> value,
1902 const AccessorInfo& info);
1903
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904/**
1905 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001906 * The result is an integer encoding property attributes (like v8::None,
1907 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001909typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1910 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911
1912
1913/**
1914 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001915 * The return value is true if the property could be deleted and false
1916 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 */
1918typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1919 const AccessorInfo& info);
1920
1921/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001922 * Returns an array containing the names of the properties the named
1923 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 */
1925typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1926
v8.team.kasperl727e9952008-09-02 14:56:44 +00001927
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001929 * Returns the value of the property if the getter intercepts the
1930 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 */
1932typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1933 const AccessorInfo& info);
1934
1935
1936/**
1937 * Returns the value if the setter intercepts the request.
1938 * Otherwise, returns an empty handle.
1939 */
1940typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1941 Local<Value> value,
1942 const AccessorInfo& info);
1943
1944
1945/**
1946 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001947 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001949typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1950 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951
1952/**
1953 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001954 * The return value is true if the property could be deleted and false
1955 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956 */
1957typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1958 const AccessorInfo& info);
1959
v8.team.kasperl727e9952008-09-02 14:56:44 +00001960/**
1961 * Returns an array containing the indices of the properties the
1962 * indexed property getter intercepts.
1963 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1965
1966
1967/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001968 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 */
1970enum AccessType {
1971 ACCESS_GET,
1972 ACCESS_SET,
1973 ACCESS_HAS,
1974 ACCESS_DELETE,
1975 ACCESS_KEYS
1976};
1977
v8.team.kasperl727e9952008-09-02 14:56:44 +00001978
1979/**
1980 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001981 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001982 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001983typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 Local<Value> key,
1985 AccessType type,
1986 Local<Value> data);
1987
v8.team.kasperl727e9952008-09-02 14:56:44 +00001988
1989/**
1990 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001991 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001992 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001993typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 uint32_t index,
1995 AccessType type,
1996 Local<Value> data);
1997
1998
1999/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002000 * A FunctionTemplate is used to create functions at runtime. There
2001 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002002 * context. The lifetime of the created function is equal to the
2003 * lifetime of the context. So in case the embedder needs to create
2004 * temporary functions that can be collected using Scripts is
2005 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 *
2007 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002008 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002010 * A FunctionTemplate has a corresponding instance template which is
2011 * used to create object instances when the function is used as a
2012 * constructor. Properties added to the instance template are added to
2013 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 *
2015 * A FunctionTemplate can have a prototype template. The prototype template
2016 * is used to create the prototype object of the function.
2017 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002018 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002020 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2022 * t->Set("func_property", v8::Number::New(1));
2023 *
2024 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2025 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2026 * proto_t->Set("proto_const", v8::Number::New(2));
2027 *
2028 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2029 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2030 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2031 * instance_t->Set("instance_property", Number::New(3));
2032 *
2033 * v8::Local<v8::Function> function = t->GetFunction();
2034 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002035 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 *
2037 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 * and "instance" for the instance object created above. The function
2039 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002041 * \code
2042 * func_property in function == true;
2043 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002045 * function.prototype.proto_method() invokes 'InvokeCallback'
2046 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 * instance instanceof function == true;
2049 * instance.instance_accessor calls 'InstanceAccessorCallback'
2050 * instance.instance_property == 3;
2051 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002053 * A FunctionTemplate can inherit from another one by calling the
2054 * FunctionTemplate::Inherit method. The following graph illustrates
2055 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002057 * \code
2058 * FunctionTemplate Parent -> Parent() . prototype -> { }
2059 * ^ ^
2060 * | Inherit(Parent) | .__proto__
2061 * | |
2062 * FunctionTemplate Child -> Child() . prototype -> { }
2063 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002065 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2066 * object of the Child() function has __proto__ pointing to the
2067 * Parent() function's prototype object. An instance of the Child
2068 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002070 * Let Parent be the FunctionTemplate initialized in the previous
2071 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 * Local<FunctionTemplate> parent = t;
2075 * Local<FunctionTemplate> child = FunctionTemplate::New();
2076 * child->Inherit(parent);
2077 *
2078 * Local<Function> child_function = child->GetFunction();
2079 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002080 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002082 * The Child function and Child instance will have the following
2083 * properties:
2084 *
2085 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002087 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002089 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002091class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 public:
2093 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002094 static Local<FunctionTemplate> New(
2095 InvocationCallback callback = 0,
2096 Handle<Value> data = Handle<Value>(),
2097 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 /** Returns the unique function instance in the current execution context.*/
2099 Local<Function> GetFunction();
2100
v8.team.kasperl727e9952008-09-02 14:56:44 +00002101 /**
2102 * Set the call-handler callback for a FunctionTemplate. This
2103 * callback is called whenever the function created from this
2104 * FunctionTemplate is called.
2105 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 void SetCallHandler(InvocationCallback callback,
2107 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108
v8.team.kasperl727e9952008-09-02 14:56:44 +00002109 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 Local<ObjectTemplate> InstanceTemplate();
2111
2112 /** Causes the function template to inherit from a parent function template.*/
2113 void Inherit(Handle<FunctionTemplate> parent);
2114
2115 /**
2116 * A PrototypeTemplate is the template used to create the prototype object
2117 * of the function created by this template.
2118 */
2119 Local<ObjectTemplate> PrototypeTemplate();
2120
v8.team.kasperl727e9952008-09-02 14:56:44 +00002121
2122 /**
2123 * Set the class name of the FunctionTemplate. This is used for
2124 * printing objects created with the function created from the
2125 * FunctionTemplate as its constructor.
2126 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 void SetClassName(Handle<String> name);
2128
2129 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002130 * Determines whether the __proto__ accessor ignores instances of
2131 * the function template. If instances of the function template are
2132 * ignored, __proto__ skips all instances and instead returns the
2133 * next object in the prototype chain.
2134 *
2135 * Call with a value of true to make the __proto__ accessor ignore
2136 * instances of the function template. Call with a value of false
2137 * to make the __proto__ accessor not ignore instances of the
2138 * function template. By default, instances of a function template
2139 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 */
2141 void SetHiddenPrototype(bool value);
2142
2143 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002144 * Returns true if the given object is an instance of this function
2145 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 */
2147 bool HasInstance(Handle<Value> object);
2148
2149 private:
2150 FunctionTemplate();
2151 void AddInstancePropertyAccessor(Handle<String> name,
2152 AccessorGetter getter,
2153 AccessorSetter setter,
2154 Handle<Value> data,
2155 AccessControl settings,
2156 PropertyAttribute attributes);
2157 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2158 NamedPropertySetter setter,
2159 NamedPropertyQuery query,
2160 NamedPropertyDeleter remover,
2161 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002162 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2164 IndexedPropertySetter setter,
2165 IndexedPropertyQuery query,
2166 IndexedPropertyDeleter remover,
2167 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002168 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2170 Handle<Value> data);
2171
2172 friend class Context;
2173 friend class ObjectTemplate;
2174};
2175
2176
2177/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002178 * An ObjectTemplate is used to create objects at runtime.
2179 *
2180 * Properties added to an ObjectTemplate are added to each object
2181 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002183class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002185 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 /** Creates a new instance of this template.*/
2189 Local<Object> NewInstance();
2190
2191 /**
2192 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002193 *
2194 * Whenever the property with the given name is accessed on objects
2195 * created from this ObjectTemplate the getter and setter callbacks
2196 * are called instead of getting and setting the property directly
2197 * on the JavaScript object.
2198 *
2199 * \param name The name of the property for which an accessor is added.
2200 * \param getter The callback to invoke when getting the property.
2201 * \param setter The callback to invoke when setting the property.
2202 * \param data A piece of data that will be passed to the getter and setter
2203 * callbacks whenever they are invoked.
2204 * \param settings Access control settings for the accessor. This is a bit
2205 * field consisting of one of more of
2206 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2207 * The default is to not allow cross-context access.
2208 * ALL_CAN_READ means that all cross-context reads are allowed.
2209 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2210 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2211 * cross-context access.
2212 * \param attribute The attributes of the property for which an accessor
2213 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 */
2215 void SetAccessor(Handle<String> name,
2216 AccessorGetter getter,
2217 AccessorSetter setter = 0,
2218 Handle<Value> data = Handle<Value>(),
2219 AccessControl settings = DEFAULT,
2220 PropertyAttribute attribute = None);
2221
2222 /**
2223 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002224 *
2225 * Whenever a named property is accessed on objects created from
2226 * this object template, the provided callback is invoked instead of
2227 * accessing the property directly on the JavaScript object.
2228 *
2229 * \param getter The callback to invoke when getting a property.
2230 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002231 * \param query The callback to invoke to check if a property is present,
2232 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002233 * \param deleter The callback to invoke when deleting a property.
2234 * \param enumerator The callback to invoke to enumerate all the named
2235 * properties of an object.
2236 * \param data A piece of data that will be passed to the callbacks
2237 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 */
2239 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2240 NamedPropertySetter setter = 0,
2241 NamedPropertyQuery query = 0,
2242 NamedPropertyDeleter deleter = 0,
2243 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002244 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245
2246 /**
2247 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 *
2249 * Whenever an indexed property is accessed on objects created from
2250 * this object template, the provided callback is invoked instead of
2251 * accessing the property directly on the JavaScript object.
2252 *
2253 * \param getter The callback to invoke when getting a property.
2254 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002255 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002256 * \param deleter The callback to invoke when deleting a property.
2257 * \param enumerator The callback to invoke to enumerate all the indexed
2258 * properties of an object.
2259 * \param data A piece of data that will be passed to the callbacks
2260 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 */
2262 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2263 IndexedPropertySetter setter = 0,
2264 IndexedPropertyQuery query = 0,
2265 IndexedPropertyDeleter deleter = 0,
2266 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002267 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 /**
2270 * Sets the callback to be used when calling instances created from
2271 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002272 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 * function.
2274 */
2275 void SetCallAsFunctionHandler(InvocationCallback callback,
2276 Handle<Value> data = Handle<Value>());
2277
v8.team.kasperl727e9952008-09-02 14:56:44 +00002278 /**
2279 * Mark object instances of the template as undetectable.
2280 *
2281 * In many ways, undetectable objects behave as though they are not
2282 * there. They behave like 'undefined' in conditionals and when
2283 * printed. However, properties can be accessed and called as on
2284 * normal objects.
2285 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 void MarkAsUndetectable();
2287
v8.team.kasperl727e9952008-09-02 14:56:44 +00002288 /**
2289 * Sets access check callbacks on the object template.
2290 *
2291 * When accessing properties on instances of this object template,
2292 * the access check callback will be called to determine whether or
2293 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002294 * The last parameter specifies whether access checks are turned
2295 * on by default on instances. If access checks are off by default,
2296 * they can be turned on on individual instances by calling
2297 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002298 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2300 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002301 Handle<Value> data = Handle<Value>(),
2302 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303
kasper.lund212ac232008-07-16 07:07:30 +00002304 /**
2305 * Gets the number of internal fields for objects generated from
2306 * this template.
2307 */
2308 int InternalFieldCount();
2309
2310 /**
2311 * Sets the number of internal fields for objects generated from
2312 * this template.
2313 */
2314 void SetInternalFieldCount(int value);
2315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 private:
2317 ObjectTemplate();
2318 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2319 friend class FunctionTemplate;
2320};
2321
2322
2323/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002324 * A Signature specifies which receivers and arguments a function can
2325 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002327class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 public:
2329 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2330 Handle<FunctionTemplate>(),
2331 int argc = 0,
2332 Handle<FunctionTemplate> argv[] = 0);
2333 private:
2334 Signature();
2335};
2336
2337
2338/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002339 * A utility for determining the type of objects based on the template
2340 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002342class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 public:
2344 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2345 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2346 int match(Handle<Value> value);
2347 private:
2348 TypeSwitch();
2349};
2350
2351
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002352// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
2354
2355/**
2356 * Ignore
2357 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002358class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 public:
2360 Extension(const char* name,
2361 const char* source = 0,
2362 int dep_count = 0,
2363 const char** deps = 0);
2364 virtual ~Extension() { }
2365 virtual v8::Handle<v8::FunctionTemplate>
2366 GetNativeFunction(v8::Handle<v8::String> name) {
2367 return v8::Handle<v8::FunctionTemplate>();
2368 }
2369
2370 const char* name() { return name_; }
2371 const char* source() { return source_; }
2372 int dependency_count() { return dep_count_; }
2373 const char** dependencies() { return deps_; }
2374 void set_auto_enable(bool value) { auto_enable_ = value; }
2375 bool auto_enable() { return auto_enable_; }
2376
2377 private:
2378 const char* name_;
2379 const char* source_;
2380 int dep_count_;
2381 const char** deps_;
2382 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002383
2384 // Disallow copying and assigning.
2385 Extension(const Extension&);
2386 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387};
2388
2389
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002390void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
2392
2393/**
2394 * Ignore
2395 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002396class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 public:
2398 inline DeclareExtension(Extension* extension) {
2399 RegisterExtension(extension);
2400 }
2401};
2402
2403
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002404// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405
2406
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002407Handle<Primitive> V8EXPORT Undefined();
2408Handle<Primitive> V8EXPORT Null();
2409Handle<Boolean> V8EXPORT True();
2410Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411
2412
2413/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002414 * A set of constraints that specifies the limits of the runtime's memory use.
2415 * You must set the heap size before initializing the VM - the size cannot be
2416 * adjusted after the VM is initialized.
2417 *
2418 * If you are using threads then you should hold the V8::Locker lock while
2419 * setting the stack limit and you must set a non-default stack limit separately
2420 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002422class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002423 public:
2424 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002425 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002427 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002429 int max_executable_size() { return max_executable_size_; }
2430 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002431 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002432 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2434 private:
2435 int max_young_space_size_;
2436 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002437 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 uint32_t* stack_limit_;
2439};
2440
2441
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002442bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443
2444
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002445// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446
2447
2448typedef void (*FatalErrorCallback)(const char* location, const char* message);
2449
2450
2451typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2452
2453
2454/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002455 * Schedules an exception to be thrown when returning to JavaScript. When an
2456 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002458 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002459 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002460Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461
2462/**
2463 * Create new error objects by calling the corresponding error object
2464 * constructor with the message.
2465 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002466class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 public:
2468 static Local<Value> RangeError(Handle<String> message);
2469 static Local<Value> ReferenceError(Handle<String> message);
2470 static Local<Value> SyntaxError(Handle<String> message);
2471 static Local<Value> TypeError(Handle<String> message);
2472 static Local<Value> Error(Handle<String> message);
2473};
2474
2475
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002476// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002478typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002480typedef void* (*CreateHistogramCallback)(const char* name,
2481 int min,
2482 int max,
2483 size_t buckets);
2484
2485typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2486
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002487// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002488 enum ObjectSpace {
2489 kObjectSpaceNewSpace = 1 << 0,
2490 kObjectSpaceOldPointerSpace = 1 << 1,
2491 kObjectSpaceOldDataSpace = 1 << 2,
2492 kObjectSpaceCodeSpace = 1 << 3,
2493 kObjectSpaceMapSpace = 1 << 4,
2494 kObjectSpaceLoSpace = 1 << 5,
2495
2496 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2497 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2498 kObjectSpaceLoSpace
2499 };
2500
2501 enum AllocationAction {
2502 kAllocationActionAllocate = 1 << 0,
2503 kAllocationActionFree = 1 << 1,
2504 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2505 };
2506
2507typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2508 AllocationAction action,
2509 int size);
2510
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002511// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2513 AccessType type,
2514 Local<Value> data);
2515
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002516// --- AllowCodeGenerationFromStrings callbacks ---
2517
2518/**
2519 * Callback to check if code generation from strings is allowed. See
2520 * Context::AllowCodeGenerationFromStrings.
2521 */
2522typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2523
2524// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525
2526/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002527 * Applications can register callback functions which will be called
2528 * before and after a garbage collection. Allocations are not
2529 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002530 * objects (set or delete properties for example) since it is possible
2531 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002533enum GCType {
2534 kGCTypeScavenge = 1 << 0,
2535 kGCTypeMarkSweepCompact = 1 << 1,
2536 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2537};
2538
2539enum GCCallbackFlags {
2540 kNoGCCallbackFlags = 0,
2541 kGCCallbackFlagCompacted = 1 << 0
2542};
2543
2544typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2545typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2546
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547typedef void (*GCCallback)();
2548
2549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002551 * Profiler modules.
2552 *
2553 * In V8, profiler consists of several modules: CPU profiler, and different
2554 * kinds of heap profiling. Each can be turned on / off independently.
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002555 * When PROFILER_MODULE_HEAP_SNAPSHOT flag is passed to ResumeProfilerEx,
2556 * modules are enabled only temporarily for making a snapshot of the heap.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002557 */
2558enum ProfilerModules {
2559 PROFILER_MODULE_NONE = 0,
2560 PROFILER_MODULE_CPU = 1,
2561 PROFILER_MODULE_HEAP_STATS = 1 << 1,
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +00002562 PROFILER_MODULE_JS_CONSTRUCTORS = 1 << 2,
2563 PROFILER_MODULE_HEAP_SNAPSHOT = 1 << 16
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002564};
2565
2566
2567/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002568 * Collection of V8 heap information.
2569 *
2570 * Instances of this class can be passed to v8::V8::HeapStatistics to
2571 * get heap statistics from V8.
2572 */
2573class V8EXPORT HeapStatistics {
2574 public:
2575 HeapStatistics();
2576 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002577 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002578 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002579 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002580
2581 private:
2582 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002583 void set_total_heap_size_executable(size_t size) {
2584 total_heap_size_executable_ = size;
2585 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002586 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002587 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002588
2589 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002590 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002591 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002592 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002593
2594 friend class V8;
2595};
2596
2597
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002598class RetainedObjectInfo;
2599
ager@chromium.org3811b432009-10-28 14:53:37 +00002600/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002601 * Isolate represents an isolated instance of the V8 engine. V8
2602 * isolates have completely separate states. Objects from one isolate
2603 * must not be used in other isolates. When V8 is initialized a
2604 * default isolate is implicitly created and entered. The embedder
2605 * can create additional isolates and use them in parallel in multiple
2606 * threads. An isolate can be entered by at most one thread at any
2607 * given time. The Locker/Unlocker API can be used to synchronize.
2608 */
2609class V8EXPORT Isolate {
2610 public:
2611 /**
2612 * Stack-allocated class which sets the isolate for all operations
2613 * executed within a local scope.
2614 */
2615 class V8EXPORT Scope {
2616 public:
2617 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2618 isolate->Enter();
2619 }
2620
2621 ~Scope() { isolate_->Exit(); }
2622
2623 private:
2624 Isolate* const isolate_;
2625
2626 // Prevent copying of Scope objects.
2627 Scope(const Scope&);
2628 Scope& operator=(const Scope&);
2629 };
2630
2631 /**
2632 * Creates a new isolate. Does not change the currently entered
2633 * isolate.
2634 *
2635 * When an isolate is no longer used its resources should be freed
2636 * by calling Dispose(). Using the delete operator is not allowed.
2637 */
2638 static Isolate* New();
2639
2640 /**
2641 * Returns the entered isolate for the current thread or NULL in
2642 * case there is no current isolate.
2643 */
2644 static Isolate* GetCurrent();
2645
2646 /**
2647 * Methods below this point require holding a lock (using Locker) in
2648 * a multi-threaded environment.
2649 */
2650
2651 /**
2652 * Sets this isolate as the entered one for the current thread.
2653 * Saves the previously entered one (if any), so that it can be
2654 * restored when exiting. Re-entering an isolate is allowed.
2655 */
2656 void Enter();
2657
2658 /**
2659 * Exits this isolate by restoring the previously entered one in the
2660 * current thread. The isolate may still stay the same, if it was
2661 * entered more than once.
2662 *
2663 * Requires: this == Isolate::GetCurrent().
2664 */
2665 void Exit();
2666
2667 /**
2668 * Disposes the isolate. The isolate must not be entered by any
2669 * thread to be disposable.
2670 */
2671 void Dispose();
2672
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002673 /**
2674 * Associate embedder-specific data with the isolate
2675 */
2676 void SetData(void* data);
2677
2678 /**
2679 * Retrive embedder-specific data from the isolate.
2680 * Returns NULL if SetData has never been called.
2681 */
2682 void* GetData();
2683
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002684 private:
2685
2686 Isolate();
2687 Isolate(const Isolate&);
2688 ~Isolate();
2689 Isolate& operator=(const Isolate&);
2690 void* operator new(size_t size);
2691 void operator delete(void*, size_t);
2692};
2693
2694
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002695class StartupData {
2696 public:
2697 enum CompressionAlgorithm {
2698 kUncompressed,
2699 kBZip2
2700 };
2701
2702 const char* data;
2703 int compressed_size;
2704 int raw_size;
2705};
2706
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002707/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 * Container class for static utility functions.
2709 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002710class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002712 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 static void SetFatalErrorHandler(FatalErrorCallback that);
2714
v8.team.kasperl727e9952008-09-02 14:56:44 +00002715 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002716 * Set the callback to invoke to check if code generation from
2717 * strings should be allowed.
2718 */
2719 static void SetAllowCodeGenerationFromStringsCallback(
2720 AllowCodeGenerationFromStringsCallback that);
2721
2722 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002723 * Ignore out-of-memory exceptions.
2724 *
2725 * V8 running out of memory is treated as a fatal error by default.
2726 * This means that the fatal error handler is called and that V8 is
2727 * terminated.
2728 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002729 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002730 * out-of-memory situation as a fatal error. This way, the contexts
2731 * that did not cause the out of memory problem might be able to
2732 * continue execution.
2733 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734 static void IgnoreOutOfMemoryException();
2735
v8.team.kasperl727e9952008-09-02 14:56:44 +00002736 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002737 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002738 * fatal errors such as out-of-memory situations.
2739 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 static bool IsDead();
2741
2742 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002743 * The following 4 functions are to be used when V8 is built with
2744 * the 'compress_startup_data' flag enabled. In this case, the
2745 * embedder must decompress startup data prior to initializing V8.
2746 *
2747 * This is how interaction with V8 should look like:
2748 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2749 * v8::StartupData* compressed_data =
2750 * new v8::StartupData[compressed_data_count];
2751 * v8::V8::GetCompressedStartupData(compressed_data);
2752 * ... decompress data (compressed_data can be updated in-place) ...
2753 * v8::V8::SetDecompressedStartupData(compressed_data);
2754 * ... now V8 can be initialized
2755 * ... make sure the decompressed data stays valid until V8 shutdown
2756 */
2757 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2758 static int GetCompressedStartupDataCount();
2759 static void GetCompressedStartupData(StartupData* compressed_data);
2760 static void SetDecompressedStartupData(StartupData* decompressed_data);
2761
2762 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002763 * Adds a message listener.
2764 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002765 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002766 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002767 */
2768 static bool AddMessageListener(MessageCallback that,
2769 Handle<Value> data = Handle<Value>());
2770
2771 /**
2772 * Remove all message listeners from the specified callback function.
2773 */
2774 static void RemoveMessageListeners(MessageCallback that);
2775
2776 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002777 * Tells V8 to capture current stack trace when uncaught exception occurs
2778 * and report it to the message listeners. The option is off by default.
2779 */
2780 static void SetCaptureStackTraceForUncaughtExceptions(
2781 bool capture,
2782 int frame_limit = 10,
2783 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2784
2785 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002786 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 */
2788 static void SetFlagsFromString(const char* str, int length);
2789
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002790 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002791 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002792 */
2793 static void SetFlagsFromCommandLine(int* argc,
2794 char** argv,
2795 bool remove_flags);
2796
kasper.lund7276f142008-07-30 08:49:36 +00002797 /** Get the version string. */
2798 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799
2800 /**
2801 * Enables the host application to provide a mechanism for recording
2802 * statistics counters.
2803 */
2804 static void SetCounterFunction(CounterLookupCallback);
2805
2806 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002807 * Enables the host application to provide a mechanism for recording
2808 * histograms. The CreateHistogram function returns a
2809 * histogram which will later be passed to the AddHistogramSample
2810 * function.
2811 */
2812 static void SetCreateHistogramFunction(CreateHistogramCallback);
2813 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2814
2815 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002816 * Enables the computation of a sliding window of states. The sliding
2817 * window information is recorded in statistics counters.
2818 */
2819 static void EnableSlidingStateWindow();
2820
2821 /** Callback function for reporting failed access checks.*/
2822 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2823
2824 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002825 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002826 * garbage collection. Allocations are not allowed in the
2827 * callback function, you therefore cannot manipulate objects (set
2828 * or delete properties for example) since it is possible such
2829 * operations will result in the allocation of objects. It is possible
2830 * to specify the GCType filter for your callback. But it is not possible to
2831 * register the same callback function two times with different
2832 * GCType filters.
2833 */
2834 static void AddGCPrologueCallback(
2835 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2836
2837 /**
2838 * This function removes callback which was installed by
2839 * AddGCPrologueCallback function.
2840 */
2841 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2842
2843 /**
2844 * The function is deprecated. Please use AddGCPrologueCallback instead.
2845 * Enables the host application to receive a notification before a
2846 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002847 * callback function, you therefore cannot manipulate objects (set
2848 * or delete properties for example) since it is possible such
2849 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002850 */
2851 static void SetGlobalGCPrologueCallback(GCCallback);
2852
2853 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002854 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002855 * garbage collection. Allocations are not allowed in the
2856 * callback function, you therefore cannot manipulate objects (set
2857 * or delete properties for example) since it is possible such
2858 * operations will result in the allocation of objects. It is possible
2859 * to specify the GCType filter for your callback. But it is not possible to
2860 * register the same callback function two times with different
2861 * GCType filters.
2862 */
2863 static void AddGCEpilogueCallback(
2864 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2865
2866 /**
2867 * This function removes callback which was installed by
2868 * AddGCEpilogueCallback function.
2869 */
2870 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2871
2872 /**
2873 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2874 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002875 * major garbage collection. Allocations are not allowed in the
2876 * callback function, you therefore cannot manipulate objects (set
2877 * or delete properties for example) since it is possible such
2878 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002879 */
2880 static void SetGlobalGCEpilogueCallback(GCCallback);
2881
2882 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002883 * Enables the host application to provide a mechanism to be notified
2884 * and perform custom logging when V8 Allocates Executable Memory.
2885 */
2886 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2887 ObjectSpace space,
2888 AllocationAction action);
2889
2890 /**
2891 * This function removes callback which was installed by
2892 * AddMemoryAllocationCallback function.
2893 */
2894 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2895
2896 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002897 * Allows the host application to group objects together. If one
2898 * object in the group is alive, all objects in the group are alive.
2899 * After each garbage collection, object groups are removed. It is
2900 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002901 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002902 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002903 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002905 static void AddObjectGroup(Persistent<Value>* objects,
2906 size_t length,
2907 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908
2909 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002910 * Allows the host application to declare implicit references between
2911 * the objects: if |parent| is alive, all |children| are alive too.
2912 * After each garbage collection, all implicit references
2913 * are removed. It is intended to be used in the before-garbage-collection
2914 * callback function.
2915 */
2916 static void AddImplicitReferences(Persistent<Object> parent,
2917 Persistent<Value>* children,
2918 size_t length);
2919
2920 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002921 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002922 * initialize from scratch. This function is called implicitly if
2923 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002924 */
2925 static bool Initialize();
2926
kasper.lund7276f142008-07-30 08:49:36 +00002927 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002928 * Adjusts the amount of registered external memory. Used to give
2929 * V8 an indication of the amount of externally allocated memory
2930 * that is kept alive by JavaScript objects. V8 uses this to decide
2931 * when to perform global garbage collections. Registering
2932 * externally allocated memory will trigger global garbage
2933 * collections more often than otherwise in an attempt to garbage
2934 * collect the JavaScript objects keeping the externally allocated
2935 * memory alive.
2936 *
2937 * \param change_in_bytes the change in externally allocated memory
2938 * that is kept alive by JavaScript objects.
2939 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002940 */
2941 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2942
iposva@chromium.org245aa852009-02-10 00:49:54 +00002943 /**
2944 * Suspends recording of tick samples in the profiler.
2945 * When the V8 profiling mode is enabled (usually via command line
2946 * switches) this function suspends recording of tick samples.
2947 * Profiling ticks are discarded until ResumeProfiler() is called.
2948 *
2949 * See also the --prof and --prof_auto command line switches to
2950 * enable V8 profiling.
2951 */
2952 static void PauseProfiler();
2953
2954 /**
2955 * Resumes recording of tick samples in the profiler.
2956 * See also PauseProfiler().
2957 */
2958 static void ResumeProfiler();
2959
ager@chromium.org41826e72009-03-30 13:30:57 +00002960 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002961 * Return whether profiler is currently paused.
2962 */
2963 static bool IsProfilerPaused();
2964
2965 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002966 * Resumes specified profiler modules. Can be called several times to
2967 * mark the opening of a profiler events block with the given tag.
2968 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002969 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
2970 * See ProfilerModules enum.
2971 *
2972 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002973 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002974 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002975 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002976
2977 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002978 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
2979 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
2980 * same tag value. There is no need for blocks to be properly nested.
2981 * The profiler is paused when the last opened block is closed.
2982 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002983 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
2984 * See ProfilerModules enum.
2985 *
2986 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00002987 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002988 */
ager@chromium.org5c838252010-02-19 08:53:10 +00002989 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002990
2991 /**
2992 * Returns active (resumed) profiler modules.
2993 * See ProfilerModules enum.
2994 *
2995 * \returns active profiler modules.
2996 */
2997 static int GetActiveProfilerModules();
2998
2999 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003000 * If logging is performed into a memory buffer (via --logfile=*), allows to
3001 * retrieve previously written messages. This can be used for retrieving
3002 * profiler log data in the application. This function is thread-safe.
3003 *
3004 * Caller provides a destination buffer that must exist during GetLogLines
3005 * call. Only whole log lines are copied into the buffer.
3006 *
3007 * \param from_pos specified a point in a buffer to read from, 0 is the
3008 * beginning of a buffer. It is assumed that caller updates its current
3009 * position using returned size value from the previous call.
3010 * \param dest_buf destination buffer for log data.
3011 * \param max_size size of the destination buffer.
3012 * \returns actual size of log data copied into buffer.
3013 */
3014 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
3015
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003016 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003017 * The minimum allowed size for a log lines buffer. If the size of
3018 * the buffer given will not be enough to hold a line of the maximum
3019 * length, an attempt to find a log line end in GetLogLines will
3020 * fail, and an empty result will be returned.
3021 */
3022 static const int kMinimumSizeForLogLinesBuffer = 2048;
3023
3024 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003025 * Retrieve the V8 thread id of the calling thread.
3026 *
3027 * The thread id for a thread should only be retrieved after the V8
3028 * lock has been acquired with a Locker object with that thread.
3029 */
3030 static int GetCurrentThreadId();
3031
3032 /**
3033 * Forcefully terminate execution of a JavaScript thread. This can
3034 * be used to terminate long-running scripts.
3035 *
3036 * TerminateExecution should only be called when then V8 lock has
3037 * been acquired with a Locker object. Therefore, in order to be
3038 * able to terminate long-running threads, preemption must be
3039 * enabled to allow the user of TerminateExecution to acquire the
3040 * lock.
3041 *
3042 * The termination is achieved by throwing an exception that is
3043 * uncatchable by JavaScript exception handlers. Termination
3044 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003045 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003046 * exception handler that catches an exception should check if that
3047 * exception is a termination exception and immediately return if
3048 * that is the case. Returning immediately in that case will
3049 * continue the propagation of the termination exception if needed.
3050 *
3051 * The thread id passed to TerminateExecution must have been
3052 * obtained by calling GetCurrentThreadId on the thread in question.
3053 *
3054 * \param thread_id The thread id of the thread to terminate.
3055 */
3056 static void TerminateExecution(int thread_id);
3057
3058 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003059 * Forcefully terminate the current thread of JavaScript execution
3060 * in the given isolate. If no isolate is provided, the default
3061 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003062 *
3063 * This method can be used by any thread even if that thread has not
3064 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003065 *
3066 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003067 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003068 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003069
3070 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003071 * Is V8 terminating JavaScript execution.
3072 *
3073 * Returns true if JavaScript execution is currently terminating
3074 * because of a call to TerminateExecution. In that case there are
3075 * still JavaScript frames on the stack and the termination
3076 * exception is still active.
3077 */
3078 static bool IsExecutionTerminating();
3079
3080 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003081 * Releases any resources used by v8 and stops any utility threads
3082 * that may be running. Note that disposing v8 is permanent, it
3083 * cannot be reinitialized.
3084 *
3085 * It should generally not be necessary to dispose v8 before exiting
3086 * a process, this should happen automatically. It is only necessary
3087 * to use if the process needs the resources taken up by v8.
3088 */
3089 static bool Dispose();
3090
ager@chromium.org3811b432009-10-28 14:53:37 +00003091 /**
3092 * Get statistics about the heap memory usage.
3093 */
3094 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003095
3096 /**
3097 * Optional notification that the embedder is idle.
3098 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003099 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003100 * Returns true if the embedder should stop calling IdleNotification
3101 * until real work has been done. This indicates that V8 has done
3102 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003103 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003104 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003105
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003106 /**
3107 * Optional notification that the system is running low on memory.
3108 * V8 uses these notifications to attempt to free memory.
3109 */
3110 static void LowMemoryNotification();
3111
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003112 /**
3113 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003114 * these notifications to guide the GC heuristic. Returns the number
3115 * of context disposals - including this one - since the last time
3116 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003117 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003118 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 private:
3121 V8();
3122
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003123 static internal::Object** GlobalizeReference(internal::Object** handle);
3124 static void DisposeGlobal(internal::Object** global_handle);
3125 static void MakeWeak(internal::Object** global_handle,
3126 void* data,
3127 WeakReferenceCallback);
3128 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003129 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003130 static bool IsGlobalNearDeath(internal::Object** global_handle);
3131 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003132 static void SetWrapperClassId(internal::Object** global_handle,
3133 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003134
3135 template <class T> friend class Handle;
3136 template <class T> friend class Local;
3137 template <class T> friend class Persistent;
3138 friend class Context;
3139};
3140
3141
3142/**
3143 * An external exception handler.
3144 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003145class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 public:
3147
3148 /**
3149 * Creates a new try/catch block and registers it with v8.
3150 */
3151 TryCatch();
3152
3153 /**
3154 * Unregisters and deletes this try/catch block.
3155 */
3156 ~TryCatch();
3157
3158 /**
3159 * Returns true if an exception has been caught by this try/catch block.
3160 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003161 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162
3163 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003164 * For certain types of exceptions, it makes no sense to continue
3165 * execution.
3166 *
3167 * Currently, the only type of exception that can be caught by a
3168 * TryCatch handler and for which it does not make sense to continue
3169 * is termination exception. Such exceptions are thrown when the
3170 * TerminateExecution methods are called to terminate a long-running
3171 * script.
3172 *
3173 * If CanContinue returns false, the correct action is to perform
3174 * any C++ cleanup needed and then return.
3175 */
3176 bool CanContinue() const;
3177
3178 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003179 * Throws the exception caught by this TryCatch in a way that avoids
3180 * it being caught again by this same TryCatch. As with ThrowException
3181 * it is illegal to execute any JavaScript operations after calling
3182 * ReThrow; the caller must return immediately to where the exception
3183 * is caught.
3184 */
3185 Handle<Value> ReThrow();
3186
3187 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003188 * Returns the exception caught by this try/catch block. If no exception has
3189 * been caught an empty handle is returned.
3190 *
3191 * The returned handle is valid until this TryCatch block has been destroyed.
3192 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003193 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194
3195 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003196 * Returns the .stack property of the thrown object. If no .stack
3197 * property is present an empty handle is returned.
3198 */
3199 Local<Value> StackTrace() const;
3200
3201 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003202 * Returns the message associated with this exception. If there is
3203 * no message associated an empty handle is returned.
3204 *
3205 * The returned handle is valid until this TryCatch block has been
3206 * destroyed.
3207 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003208 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003209
3210 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003211 * Clears any exceptions that may have been caught by this try/catch block.
3212 * After this method has been called, HasCaught() will return false.
3213 *
3214 * It is not necessary to clear a try/catch block before using it again; if
3215 * another exception is thrown the previously caught exception will just be
3216 * overwritten. However, it is often a good idea since it makes it easier
3217 * to determine which operation threw a given exception.
3218 */
3219 void Reset();
3220
v8.team.kasperl727e9952008-09-02 14:56:44 +00003221 /**
3222 * Set verbosity of the external exception handler.
3223 *
3224 * By default, exceptions that are caught by an external exception
3225 * handler are not reported. Call SetVerbose with true on an
3226 * external exception handler to have exceptions caught by the
3227 * handler reported as if they were not caught.
3228 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229 void SetVerbose(bool value);
3230
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003231 /**
3232 * Set whether or not this TryCatch should capture a Message object
3233 * which holds source information about where the exception
3234 * occurred. True by default.
3235 */
3236 void SetCaptureMessage(bool value);
3237
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003238 private:
3239 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003240 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003241 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003242 bool is_verbose_ : 1;
3243 bool can_continue_ : 1;
3244 bool capture_message_ : 1;
3245 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003246
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003247 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248};
3249
3250
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003251// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003252
3253
3254/**
3255 * Ignore
3256 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003257class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 public:
3259 ExtensionConfiguration(int name_count, const char* names[])
3260 : name_count_(name_count), names_(names) { }
3261 private:
3262 friend class ImplementationUtilities;
3263 int name_count_;
3264 const char** names_;
3265};
3266
3267
3268/**
3269 * A sandboxed execution context with its own set of built-in objects
3270 * and functions.
3271 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003272class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003274 /**
3275 * Returns the global proxy object or global object itself for
3276 * detached contexts.
3277 *
3278 * Global proxy object is a thin wrapper whose prototype points to
3279 * actual context's global object with the properties like Object, etc.
3280 * This is done that way for security reasons (for more details see
3281 * https://wiki.mozilla.org/Gecko:SplitWindow).
3282 *
3283 * Please note that changes to global proxy object prototype most probably
3284 * would break VM---v8 expects only global object as a prototype of
3285 * global proxy object.
3286 *
3287 * If DetachGlobal() has been invoked, Global() would return actual global
3288 * object until global is reattached with ReattachGlobal().
3289 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290 Local<Object> Global();
3291
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003292 /**
3293 * Detaches the global object from its context before
3294 * the global object can be reused to create a new context.
3295 */
3296 void DetachGlobal();
3297
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003298 /**
3299 * Reattaches a global object to a context. This can be used to
3300 * restore the connection between a global object and a context
3301 * after DetachGlobal has been called.
3302 *
3303 * \param global_object The global object to reattach to the
3304 * context. For this to work, the global object must be the global
3305 * object that was associated with this context before a call to
3306 * DetachGlobal.
3307 */
3308 void ReattachGlobal(Handle<Object> global_object);
3309
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003310 /** Creates a new context.
3311 *
3312 * Returns a persistent handle to the newly allocated context. This
3313 * persistent handle has to be disposed when the context is no
3314 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003315 *
3316 * \param extensions An optional extension configuration containing
3317 * the extensions to be installed in the newly created context.
3318 *
3319 * \param global_template An optional object template from which the
3320 * global object for the newly created context will be created.
3321 *
3322 * \param global_object An optional global object to be reused for
3323 * the newly created context. This global object must have been
3324 * created by a previous call to Context::New with the same global
3325 * template. The state of the global object will be completely reset
3326 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003327 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003328 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003329 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003330 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3331 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003332
kasper.lund44510672008-07-25 07:37:58 +00003333 /** Returns the last entered context. */
3334 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335
kasper.lund44510672008-07-25 07:37:58 +00003336 /** Returns the context that is on the top of the stack. */
3337 static Local<Context> GetCurrent();
3338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003340 * Returns the context of the calling JavaScript code. That is the
3341 * context of the top-most JavaScript frame. If there are no
3342 * JavaScript frames an empty handle is returned.
3343 */
3344 static Local<Context> GetCalling();
3345
3346 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003347 * Sets the security token for the context. To access an object in
3348 * another context, the security tokens must match.
3349 */
3350 void SetSecurityToken(Handle<Value> token);
3351
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003352 /** Restores the security token to the default value. */
3353 void UseDefaultSecurityToken();
3354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355 /** Returns the security token of this context.*/
3356 Handle<Value> GetSecurityToken();
3357
v8.team.kasperl727e9952008-09-02 14:56:44 +00003358 /**
3359 * Enter this context. After entering a context, all code compiled
3360 * and run is compiled and run in this context. If another context
3361 * is already entered, this old context is saved so it can be
3362 * restored when the new context is exited.
3363 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003365
3366 /**
3367 * Exit this context. Exiting the current context restores the
3368 * context that was in place when entering the current context.
3369 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370 void Exit();
3371
v8.team.kasperl727e9952008-09-02 14:56:44 +00003372 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003373 bool HasOutOfMemoryException();
3374
v8.team.kasperl727e9952008-09-02 14:56:44 +00003375 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376 static bool InContext();
3377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003378 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003379 * Associate an additional data object with the context. This is mainly used
3380 * with the debugger to provide additional information on the context through
3381 * the debugger API.
3382 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003383 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003384 Local<Value> GetData();
3385
3386 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003387 * Control whether code generation from strings is allowed. Calling
3388 * this method with false will disable 'eval' and the 'Function'
3389 * constructor for code running in this context. If 'eval' or the
3390 * 'Function' constructor are used an exception will be thrown.
3391 *
3392 * If code generation from strings is not allowed the
3393 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3394 * set before blocking the call to 'eval' or the 'Function'
3395 * constructor. If that callback returns true, the call will be
3396 * allowed, otherwise an exception will be thrown. If no callback is
3397 * set an exception will be thrown.
3398 */
3399 void AllowCodeGenerationFromStrings(bool allow);
3400
3401 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003402 * Stack-allocated class which sets the execution context for all
3403 * operations executed within a local scope.
3404 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003405 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003407 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003408 context_->Enter();
3409 }
3410 inline ~Scope() { context_->Exit(); }
3411 private:
3412 Handle<Context> context_;
3413 };
3414
3415 private:
3416 friend class Value;
3417 friend class Script;
3418 friend class Object;
3419 friend class Function;
3420};
3421
3422
3423/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003424 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003425 * is allowed to use any given V8 isolate. See Isolate class
3426 * comments. The definition of 'using V8 isolate' includes
3427 * accessing handles or holding onto object pointers obtained
3428 * from V8 handles while in the particular V8 isolate. It is up
3429 * to the user of V8 to ensure (perhaps with locking) that this
3430 * constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003432 * v8::Locker is a scoped lock object. While it's
3433 * active (i.e. between its construction and destruction) the current thread is
3434 * allowed to use the locked isolate. V8 guarantees that an isolate can be locked
3435 * by at most one thread at any time. In other words, the scope of a v8::Locker is
3436 * a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003437 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003438 * Sample usage:
3439* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003440 * ...
3441 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003442 * v8::Locker locker(isolate);
3443 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003444 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003445 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003446 * ...
3447 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003448 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003449 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003450 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003451 * by destroying the v8::Locker object as above or by constructing a
3452 * v8::Unlocker object:
3453 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003454 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003455 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003456 * isolate->Exit();
3457 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003458 * ...
3459 * // Code not using V8 goes here while V8 can run in another thread.
3460 * ...
3461 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003462 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003463 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003464 *
3465 * The Unlocker object is intended for use in a long-running callback
3466 * from V8, where you want to release the V8 lock for other threads to
3467 * use.
3468 *
3469 * The v8::Locker is a recursive lock. That is, you can lock more than
3470 * once in a given thread. This can be useful if you have code that can
3471 * be called either from code that holds the lock or from code that does
3472 * not. The Unlocker is not recursive so you can not have several
3473 * Unlockers on the stack at once, and you can not use an Unlocker in a
3474 * thread that is not inside a Locker's scope.
3475 *
3476 * An unlocker will unlock several lockers if it has to and reinstate
3477 * the correct depth of locking on its destruction. eg.:
3478 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003479 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003480 * // V8 not locked.
3481 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003482 * v8::Locker locker(isolate);
3483 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484 * // V8 locked.
3485 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003486 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003487 * // V8 still locked (2 levels).
3488 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003489 * isolate->Exit();
3490 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491 * // V8 not locked.
3492 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003493 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003494 * // V8 locked again (2 levels).
3495 * }
3496 * // V8 still locked (1 level).
3497 * }
3498 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003499 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003500 *
3501 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003502 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003503class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003505 /**
3506 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3507 */
3508 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003510 private:
3511 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003512};
3513
3514
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003515class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003517 /**
3518 * Initialize Locker for a given Isolate. NULL means default isolate.
3519 */
3520 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003522
3523 /**
3524 * Start preemption.
3525 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003526 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003527 * that will switch between multiple threads that are in contention
3528 * for the V8 lock.
3529 */
3530 static void StartPreemption(int every_n_ms);
3531
3532 /**
3533 * Stop preemption.
3534 */
3535 static void StopPreemption();
3536
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003537 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00003538 * Returns whether or not the locker for a given isolate, or default isolate if NULL is given,
3539 * is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003540 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003541 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003542
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003543 /**
3544 * Returns whether v8::Locker is being used by this V8 instance.
3545 */
3546 static bool IsActive() { return active_; }
3547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003548 private:
3549 bool has_lock_;
3550 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003551 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003552
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003553 static bool active_;
3554
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003555 // Disallow copying and assigning.
3556 Locker(const Locker&);
3557 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003558};
3559
3560
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003561/**
3562 * An interface for exporting data from V8, using "push" model.
3563 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003564class V8EXPORT OutputStream { // NOLINT
3565 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003566 enum OutputEncoding {
3567 kAscii = 0 // 7-bit ASCII.
3568 };
3569 enum WriteResult {
3570 kContinue = 0,
3571 kAbort = 1
3572 };
3573 virtual ~OutputStream() {}
3574 /** Notify about the end of stream. */
3575 virtual void EndOfStream() = 0;
3576 /** Get preferred output chunk size. Called only once. */
3577 virtual int GetChunkSize() { return 1024; }
3578 /** Get preferred output encoding. Called only once. */
3579 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3580 /**
3581 * Writes the next chunk of snapshot data into the stream. Writing
3582 * can be stopped by returning kAbort as function result. EndOfStream
3583 * will not be called in case writing was aborted.
3584 */
3585 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3586};
3587
3588
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003589/**
3590 * An interface for reporting progress and controlling long-running
3591 * activities.
3592 */
3593class V8EXPORT ActivityControl { // NOLINT
3594 public:
3595 enum ControlOption {
3596 kContinue = 0,
3597 kAbort = 1
3598 };
3599 virtual ~ActivityControl() {}
3600 /**
3601 * Notify about current progress. The activity can be stopped by
3602 * returning kAbort as the callback result.
3603 */
3604 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3605};
3606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003607
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003608// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003609
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003610
3611namespace internal {
3612
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003613static const int kApiPointerSize = sizeof(void*); // NOLINT
3614static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003615
3616// Tag information for HeapObject.
3617const int kHeapObjectTag = 1;
3618const int kHeapObjectTagSize = 2;
3619const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3620
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003621// Tag information for Smi.
3622const int kSmiTag = 0;
3623const int kSmiTagSize = 1;
3624const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3625
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003626template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003627
3628// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003629template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003630 static const int kSmiShiftSize = 0;
3631 static const int kSmiValueSize = 31;
3632 static inline int SmiToInt(internal::Object* value) {
3633 int shift_bits = kSmiTagSize + kSmiShiftSize;
3634 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3635 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3636 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003637
3638 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3639 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003640 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003641 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003642};
3643
3644// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003645template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003646 static const int kSmiShiftSize = 31;
3647 static const int kSmiValueSize = 32;
3648 static inline int SmiToInt(internal::Object* value) {
3649 int shift_bits = kSmiTagSize + kSmiShiftSize;
3650 // Shift down and throw away top 32 bits.
3651 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3652 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003653
3654 // To maximize the range of pointers that can be encoded
3655 // in the available 32 bits, we require them to be 8 bytes aligned.
3656 // This gives 2 ^ (32 + 3) = 32G address space covered.
3657 // It might be not enough to cover stack allocated objects on some platforms.
3658 static const int kPointerAlignment = 3;
3659
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003660 static const uintptr_t kEncodablePointerMask =
3661 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003662
3663 static const int kPointerToSmiShift =
3664 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003665};
3666
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003667typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3668const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3669const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003670const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003671 PlatformSmiTagging::kEncodablePointerMask;
3672const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003673
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003674template <size_t ptr_size> struct InternalConstants;
3675
3676// Internal constants for 32-bit systems.
3677template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003678 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003679};
3680
3681// Internal constants for 64-bit systems.
3682template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003683 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003684};
3685
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003686/**
3687 * This class exports constants and functionality from within v8 that
3688 * is necessary to implement inline functions in the v8 api. Don't
3689 * depend on functions and constants defined here.
3690 */
3691class Internals {
3692 public:
3693
3694 // These values match non-compiler-dependent values defined within
3695 // the implementation of v8.
3696 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003697 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003698 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003699 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003700
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003701 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003702 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003703 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003704 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003705
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003706 static const int kJSObjectType = 0xa2;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003707 static const int kFirstNonstringType = 0x80;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003708 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003709
3710 static inline bool HasHeapObjectTag(internal::Object* value) {
3711 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3712 kHeapObjectTag);
3713 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003714
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003715 static inline bool HasSmiTag(internal::Object* value) {
3716 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3717 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003718
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003719 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003720 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003721 }
3722
3723 static inline int GetInstanceType(internal::Object* obj) {
3724 typedef internal::Object O;
3725 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3726 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3727 }
3728
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003729 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003730 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003731 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3732 }
3733
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003734 static inline void* GetExternalPointer(internal::Object* obj) {
3735 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003736 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003737 } else if (GetInstanceType(obj) == kForeignType) {
3738 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003739 } else {
3740 return NULL;
3741 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003742 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003743
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003744 static inline bool IsExternalTwoByteString(int instance_type) {
3745 int representation = (instance_type & kFullStringRepresentationMask);
3746 return representation == kExternalTwoByteRepresentationTag;
3747 }
3748
3749 template <typename T>
3750 static inline T ReadField(Object* ptr, int offset) {
3751 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3752 return *reinterpret_cast<T*>(addr);
3753 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003754
3755 static inline bool CanCastToHeapObject(void* o) { return false; }
3756 static inline bool CanCastToHeapObject(Context* o) { return true; }
3757 static inline bool CanCastToHeapObject(String* o) { return true; }
3758 static inline bool CanCastToHeapObject(Object* o) { return true; }
3759 static inline bool CanCastToHeapObject(Message* o) { return true; }
3760 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3761 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003762};
3763
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003764} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003765
3766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767template <class T>
3768Handle<T>::Handle() : val_(0) { }
3769
3770
3771template <class T>
3772Local<T>::Local() : Handle<T>() { }
3773
3774
3775template <class T>
3776Local<T> Local<T>::New(Handle<T> that) {
3777 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003778 T* that_ptr = *that;
3779 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3780 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3781 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3782 reinterpret_cast<internal::HeapObject*>(*p))));
3783 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3785}
3786
3787
3788template <class T>
3789Persistent<T> Persistent<T>::New(Handle<T> that) {
3790 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003791 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3793}
3794
3795
3796template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003797bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003799 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003800}
3801
3802
3803template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003804bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003805 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003806 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807}
3808
3809
3810template <class T>
3811void Persistent<T>::Dispose() {
3812 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003813 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814}
3815
3816
3817template <class T>
3818Persistent<T>::Persistent() : Handle<T>() { }
3819
3820template <class T>
3821void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003822 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3823 parameters,
3824 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825}
3826
3827template <class T>
3828void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003829 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830}
3831
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003832template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003833void Persistent<T>::MarkIndependent() {
3834 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
3835}
3836
3837template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003838void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
3839 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
3840}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003841
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003842Arguments::Arguments(internal::Object** implicit_args,
3843 internal::Object** values, int length,
3844 bool is_construct_call)
3845 : implicit_args_(implicit_args),
3846 values_(values),
3847 length_(length),
3848 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003849
3850
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003851Local<Value> Arguments::operator[](int i) const {
3852 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3853 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3854}
3855
3856
3857Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003858 return Local<Function>(reinterpret_cast<Function*>(
3859 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003860}
3861
3862
3863Local<Object> Arguments::This() const {
3864 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3865}
3866
3867
3868Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003869 return Local<Object>(reinterpret_cast<Object*>(
3870 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871}
3872
3873
3874Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003875 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876}
3877
3878
3879bool Arguments::IsConstructCall() const {
3880 return is_construct_call_;
3881}
3882
3883
3884int Arguments::Length() const {
3885 return length_;
3886}
3887
3888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889template <class T>
3890Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003891 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3892 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003893 return Local<T>(reinterpret_cast<T*>(after));
3894}
3895
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003896Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003897 return resource_name_;
3898}
3899
3900
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003901Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003902 return resource_line_offset_;
3903}
3904
3905
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003906Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 return resource_column_offset_;
3908}
3909
3910
3911Handle<Boolean> Boolean::New(bool value) {
3912 return value ? True() : False();
3913}
3914
3915
3916void Template::Set(const char* name, v8::Handle<Data> value) {
3917 Set(v8::String::New(name), value);
3918}
3919
3920
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003921Local<Value> Object::GetInternalField(int index) {
3922#ifndef V8_ENABLE_CHECKS
3923 Local<Value> quick_result = UncheckedGetInternalField(index);
3924 if (!quick_result.IsEmpty()) return quick_result;
3925#endif
3926 return CheckedGetInternalField(index);
3927}
3928
3929
3930Local<Value> Object::UncheckedGetInternalField(int index) {
3931 typedef internal::Object O;
3932 typedef internal::Internals I;
3933 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003934 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003935 // If the object is a plain JSObject, which is the common case,
3936 // we know where to find the internal fields and can return the
3937 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003938 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003939 O* value = I::ReadField<O*>(obj, offset);
3940 O** result = HandleScope::CreateHandle(value);
3941 return Local<Value>(reinterpret_cast<Value*>(result));
3942 } else {
3943 return Local<Value>();
3944 }
3945}
3946
3947
3948void* External::Unwrap(Handle<v8::Value> obj) {
3949#ifdef V8_ENABLE_CHECKS
3950 return FullUnwrap(obj);
3951#else
3952 return QuickUnwrap(obj);
3953#endif
3954}
3955
3956
3957void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3958 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003959 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003960 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003961}
3962
3963
3964void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003965 typedef internal::Object O;
3966 typedef internal::Internals I;
3967
3968 O* obj = *reinterpret_cast<O**>(this);
3969
3970 if (I::GetInstanceType(obj) == I::kJSObjectType) {
3971 // If the object is a plain JSObject, which is the common case,
3972 // we know where to find the internal fields and can return the
3973 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003974 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003975 O* value = I::ReadField<O*>(obj, offset);
3976 return I::GetExternalPointer(value);
3977 }
3978
3979 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003980}
3981
3982
3983String* String::Cast(v8::Value* value) {
3984#ifdef V8_ENABLE_CHECKS
3985 CheckCast(value);
3986#endif
3987 return static_cast<String*>(value);
3988}
3989
3990
3991String::ExternalStringResource* String::GetExternalStringResource() const {
3992 typedef internal::Object O;
3993 typedef internal::Internals I;
3994 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003995 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003996 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003997 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
3998 result = reinterpret_cast<String::ExternalStringResource*>(value);
3999 } else {
4000 result = NULL;
4001 }
4002#ifdef V8_ENABLE_CHECKS
4003 VerifyExternalStringResource(result);
4004#endif
4005 return result;
4006}
4007
4008
4009bool Value::IsString() const {
4010#ifdef V8_ENABLE_CHECKS
4011 return FullIsString();
4012#else
4013 return QuickIsString();
4014#endif
4015}
4016
4017bool Value::QuickIsString() const {
4018 typedef internal::Object O;
4019 typedef internal::Internals I;
4020 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4021 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004022 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004023}
4024
4025
4026Number* Number::Cast(v8::Value* value) {
4027#ifdef V8_ENABLE_CHECKS
4028 CheckCast(value);
4029#endif
4030 return static_cast<Number*>(value);
4031}
4032
4033
4034Integer* Integer::Cast(v8::Value* value) {
4035#ifdef V8_ENABLE_CHECKS
4036 CheckCast(value);
4037#endif
4038 return static_cast<Integer*>(value);
4039}
4040
4041
4042Date* Date::Cast(v8::Value* value) {
4043#ifdef V8_ENABLE_CHECKS
4044 CheckCast(value);
4045#endif
4046 return static_cast<Date*>(value);
4047}
4048
4049
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004050RegExp* RegExp::Cast(v8::Value* value) {
4051#ifdef V8_ENABLE_CHECKS
4052 CheckCast(value);
4053#endif
4054 return static_cast<RegExp*>(value);
4055}
4056
4057
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004058Object* Object::Cast(v8::Value* value) {
4059#ifdef V8_ENABLE_CHECKS
4060 CheckCast(value);
4061#endif
4062 return static_cast<Object*>(value);
4063}
4064
4065
4066Array* Array::Cast(v8::Value* value) {
4067#ifdef V8_ENABLE_CHECKS
4068 CheckCast(value);
4069#endif
4070 return static_cast<Array*>(value);
4071}
4072
4073
4074Function* Function::Cast(v8::Value* value) {
4075#ifdef V8_ENABLE_CHECKS
4076 CheckCast(value);
4077#endif
4078 return static_cast<Function*>(value);
4079}
4080
4081
4082External* External::Cast(v8::Value* value) {
4083#ifdef V8_ENABLE_CHECKS
4084 CheckCast(value);
4085#endif
4086 return static_cast<External*>(value);
4087}
4088
4089
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004090Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004091 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004092}
4093
4094
4095Local<Object> AccessorInfo::This() const {
4096 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4097}
4098
4099
4100Local<Object> AccessorInfo::Holder() const {
4101 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4102}
4103
4104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004105/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004106 * \example shell.cc
4107 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 * command-line and executes them.
4109 */
4110
4111
4112/**
4113 * \example process.cc
4114 */
4115
4116
4117} // namespace v8
4118
4119
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004120#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004121#undef TYPE_CHECK
4122
4123
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004124#endif // V8_H_