blob: e4ac1a3e1820c5c4b2f57ff3be65e7d88018bfc9 [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:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000168 /**
169 * Creates an empty handle.
170 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000171 inline Handle();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172
173 /**
174 * Creates a new handle for the specified value.
175 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000176 inline explicit Handle(T* val) : val_(val) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177
178 /**
179 * Creates a handle for the contents of the specified handle. This
180 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000181 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000183 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 * between compatible handles, for instance assigning a
185 * Handle<String> to a variable declared as Handle<Value>, is legal
186 * because String is a subclass of Value.
187 */
188 template <class S> inline Handle(Handle<S> that)
189 : val_(reinterpret_cast<T*>(*that)) {
190 /**
191 * This check fails when trying to convert between incompatible
192 * handles. For example, converting from a Handle<String> to a
193 * Handle<Number>.
194 */
195 TYPE_CHECK(T, S);
196 }
197
198 /**
199 * Returns true if the handle is empty.
200 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000201 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000203 inline T* operator->() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000205 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
207 /**
208 * Sets the handle to be empty. IsEmpty() will then return true.
209 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000210 inline void Clear() { this->val_ = 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211
212 /**
213 * Checks whether two handles are the same.
214 * Returns true if both are empty, or if the objects
215 * to which they refer are identical.
216 * The handles' references are not checked.
217 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000218 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000219 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
220 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 if (a == 0) return b == 0;
222 if (b == 0) return false;
223 return *a == *b;
224 }
225
226 /**
227 * Checks whether two handles are different.
228 * Returns true if only one of the handles is empty, or if
229 * the objects to which they refer are different.
230 * The handles' references are not checked.
231 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000232 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 return !operator==(that);
234 }
235
236 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000237#ifdef V8_ENABLE_CHECKS
238 // If we're going to perform the type check then we have to check
239 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000241#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 return Handle<T>(T::Cast(*that));
243 }
244
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000245 template <class S> inline Handle<S> As() {
246 return Handle<S>::Cast(*this);
247 }
248
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 private:
250 T* val_;
251};
252
253
254/**
255 * A light-weight stack-allocated object handle. All operations
256 * that return objects from within v8 return them in local handles. They
257 * are created within HandleScopes, and all local handles allocated within a
258 * handle scope are destroyed when the handle scope is destroyed. Hence it
259 * is not necessary to explicitly deallocate local handles.
260 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000261template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000263 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 template <class S> inline Local(Local<S> that)
265 : Handle<T>(reinterpret_cast<T*>(*that)) {
266 /**
267 * This check fails when trying to convert between incompatible
268 * handles. For example, converting from a Handle<String> to a
269 * Handle<Number>.
270 */
271 TYPE_CHECK(T, S);
272 }
273 template <class S> inline Local(S* that) : Handle<T>(that) { }
274 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000275#ifdef V8_ENABLE_CHECKS
276 // If we're going to perform the type check then we have to check
277 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000279#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 return Local<T>(T::Cast(*that));
281 }
282
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000283 template <class S> inline Local<S> As() {
284 return Local<S>::Cast(*this);
285 }
286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 /** Create a local handle for the content of another handle.
288 * The referee is kept alive by the local handle even when
289 * the original handle is destroyed/disposed.
290 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000291 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292};
293
294
295/**
296 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000297 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 * allocated, a Persistent handle remains valid until it is explicitly
299 * disposed.
300 *
301 * A persistent handle contains a reference to a storage cell within
302 * the v8 engine which holds an object value and which is updated by
303 * the garbage collector whenever the object is moved. A new storage
304 * cell can be created using Persistent::New and existing handles can
305 * be disposed using Persistent::Dispose. Since persistent handles
306 * are passed by value you may have many persistent handle objects
307 * that point to the same storage cell. For instance, if you pass a
308 * persistent handle as an argument to a function you will not get two
309 * different storage cells but rather two references to the same
310 * storage cell.
311 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000312template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 /**
315 * Creates an empty persistent handle that doesn't point to any
316 * storage cell.
317 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000318 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319
320 /**
321 * Creates a persistent handle for the same storage cell as the
322 * specified handle. This constructor allows you to pass persistent
323 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000324 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000326 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 * between compatible persistent handles, for instance assigning a
328 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000329 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 */
331 template <class S> inline Persistent(Persistent<S> that)
332 : Handle<T>(reinterpret_cast<T*>(*that)) {
333 /**
334 * This check fails when trying to convert between incompatible
335 * handles. For example, converting from a Handle<String> to a
336 * Handle<Number>.
337 */
338 TYPE_CHECK(T, S);
339 }
340
341 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
342
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000343 /**
344 * "Casts" a plain handle which is known to be a persistent handle
345 * to a persistent handle.
346 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000347 template <class S> explicit inline Persistent(Handle<S> that)
348 : Handle<T>(*that) { }
349
350 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000351#ifdef V8_ENABLE_CHECKS
352 // If we're going to perform the type check then we have to check
353 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000355#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 return Persistent<T>(T::Cast(*that));
357 }
358
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000359 template <class S> inline Persistent<S> As() {
360 return Persistent<S>::Cast(*this);
361 }
362
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000364 * Creates a new persistent handle for an existing local or
365 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000367 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368
369 /**
370 * Releases the storage cell referenced by this persistent handle.
371 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000372 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 * cell remain and IsEmpty will still return false.
374 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000375 inline void Dispose();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376
377 /**
378 * Make the reference to this object weak. When only weak handles
379 * refer to the object, the garbage collector will perform a
380 * callback to the given V8::WeakReferenceCallback function, passing
381 * it the object reference and the given parameters.
382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /** Clears the weak reference to this object.*/
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000386 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000389 * Marks the reference to this object independent. Garbage collector
390 * is free to ignore any object groups containing this object.
391 * Weak callback for an independent handle should not
392 * assume that it will be preceded by a global GC prologue callback
393 * or followed by a global GC epilogue callback.
394 */
395 inline void MarkIndependent();
396
397 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000398 *Checks if the handle holds the only reference to an object.
399 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000400 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401
402 /**
403 * Returns true if the handle's reference is weak.
404 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000405 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000407 /**
408 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
409 * interface description in v8-profiler.h for details.
410 */
411 inline void SetWrapperClassId(uint16_t class_id);
412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 private:
414 friend class ImplementationUtilities;
415 friend class ObjectTemplate;
416};
417
418
v8.team.kasperl727e9952008-09-02 14:56:44 +0000419 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000420 * A stack-allocated class that governs a number of local handles.
421 * After a handle scope has been created, all local handles will be
422 * allocated within that handle scope until either the handle scope is
423 * deleted or another handle scope is created. If there is already a
424 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000425 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426 * new handles will again be allocated in the original handle scope.
427 *
428 * After the handle scope of a local handle has been deleted the
429 * garbage collector will no longer track the object stored in the
430 * handle and may deallocate it. The behavior of accessing a handle
431 * for which the handle scope has been deleted is undefined.
432 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000433class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000434 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000435 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000437 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
439 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440 * Closes the handle scope and returns the value as a handle in the
441 * previous scope, which is the new current scope after the call.
442 */
443 template <class T> Local<T> Close(Handle<T> value);
444
445 /**
446 * Counts the number of allocated handles.
447 */
448 static int NumberOfHandles();
449
450 /**
451 * Creates a new handle with the given value.
452 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000453 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000454 // Faster version, uses HeapObject to obtain the current Isolate.
455 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456
457 private:
458 // Make it impossible to create heap-allocated or illegal handle
459 // scopes by disallowing certain operations.
460 HandleScope(const HandleScope&);
461 void operator=(const HandleScope&);
462 void* operator new(size_t size);
463 void operator delete(void*, size_t);
464
ager@chromium.org3811b432009-10-28 14:53:37 +0000465 // This Data class is accessible internally as HandleScopeData through a
466 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000467 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000469 internal::Object** next;
470 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000471 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000474 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 }
476 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000477
lrn@chromium.org303ada72010-10-27 09:33:13 +0000478 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000480 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000481 internal::Object** prev_next_;
482 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000484 // Allow for the active closing of HandleScopes which allows to pass a handle
485 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000487 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489 friend class ImplementationUtilities;
490};
491
492
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000493// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494
495
496/**
497 * The superclass of values and API object templates.
498 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000499class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 private:
501 Data();
502};
503
504
505/**
506 * Pre-compilation data that can be associated with a script. This
507 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000508 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000509 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000511class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 public:
513 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000514
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000515 /**
516 * Pre-compiles the specified script (context-independent).
517 *
518 * \param input Pointer to UTF-8 script source code.
519 * \param length Length of UTF-8 script source code.
520 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000523 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000524 * Pre-compiles the specified script (context-independent).
525 *
526 * NOTE: Pre-compilation using this method cannot happen on another thread
527 * without using Lockers.
528 *
529 * \param source Script source code.
530 */
531 static ScriptData* PreCompile(Handle<String> source);
532
533 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000534 * Load previous pre-compilation data.
535 *
536 * \param data Pointer to data returned by a call to Data() of a previous
537 * ScriptData. Ownership is not transferred.
538 * \param length Length of data.
539 */
540 static ScriptData* New(const char* data, int length);
541
542 /**
543 * Returns the length of Data().
544 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000546
547 /**
548 * Returns a serialized representation of this ScriptData that can later be
549 * passed to New(). NOTE: Serialized data is platform-dependent.
550 */
551 virtual const char* Data() = 0;
552
553 /**
554 * Returns true if the source code could not be parsed.
555 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000556 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557};
558
559
560/**
561 * The origin, within a file, of a script.
562 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000563class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000565 inline ScriptOrigin(
566 Handle<Value> resource_name,
567 Handle<Integer> resource_line_offset = Handle<Integer>(),
568 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 : resource_name_(resource_name),
570 resource_line_offset_(resource_line_offset),
571 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000572 inline Handle<Value> ResourceName() const;
573 inline Handle<Integer> ResourceLineOffset() const;
574 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000576 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 Handle<Integer> resource_line_offset_;
578 Handle<Integer> resource_column_offset_;
579};
580
581
582/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000583 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000585class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000588 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000589 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000590 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000591 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000592 * when New() returns
593 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
594 * using pre_data speeds compilation if it's done multiple times.
595 * Owned by caller, no references are kept when New() returns.
596 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000597 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000598 * available to compile event handlers.
599 * \return Compiled script object (context independent; when run it
600 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 static Local<Script> New(Handle<String> source,
603 ScriptOrigin* origin = NULL,
604 ScriptData* pre_data = NULL,
605 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
mads.s.agercbaa0602008-08-14 13:41:48 +0000607 /**
608 * Compiles the specified script using the specified file name
609 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000610 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000611 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000612 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000613 * as the script's origin.
614 * \return Compiled script object (context independent; when run it
615 * will use the currently entered context).
616 */
617 static Local<Script> New(Handle<String> source,
618 Handle<Value> file_name);
619
620 /**
621 * Compiles the specified script (bound to current context).
622 *
623 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000624 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 * when Compile() returns
626 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
627 * using pre_data speeds compilation if it's done multiple times.
628 * Owned by caller, no references are kept when Compile() returns.
629 * \param script_data Arbitrary data associated with script. Using
630 * this has same effect as calling SetData(), but makes data available
631 * earlier (i.e. to compile event handlers).
632 * \return Compiled script object, bound to the context that was active
633 * when this function was called. When run it will always use this
634 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000635 */
636 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000637 ScriptOrigin* origin = NULL,
638 ScriptData* pre_data = NULL,
639 Handle<String> script_data = Handle<String>());
640
641 /**
642 * Compiles the specified script using the specified file name
643 * object (typically a string) as the script's origin.
644 *
645 * \param source Script source code.
646 * \param file_name File name to use as script's origin
647 * \param script_data Arbitrary data associated with script. Using
648 * this has same effect as calling SetData(), but makes data available
649 * earlier (i.e. to compile event handlers).
650 * \return Compiled script object, bound to the context that was active
651 * when this function was called. When run it will always use this
652 * context.
653 */
654 static Local<Script> Compile(Handle<String> source,
655 Handle<Value> file_name,
656 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000657
v8.team.kasperl727e9952008-09-02 14:56:44 +0000658 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000659 * Runs the script returning the resulting value. If the script is
660 * context independent (created using ::New) it will be run in the
661 * currently entered context. If it is context specific (created
662 * using ::Compile) it will be run in the context in which it was
663 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000664 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000666
667 /**
668 * Returns the script id value.
669 */
670 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000671
672 /**
673 * Associate an additional data object with the script. This is mainly used
674 * with the debugger as this data object is only available through the
675 * debugger API.
676 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000677 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678};
679
680
681/**
682 * An error message.
683 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000684class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000685 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000686 Local<String> Get() const;
687 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000688
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000689 /**
690 * Returns the resource name for the script from where the function causing
691 * the error originates.
692 */
ager@chromium.org32912102009-01-16 10:38:43 +0000693 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000695 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000696 * Returns the resource data for the script from where the function causing
697 * the error originates.
698 */
699 Handle<Value> GetScriptData() const;
700
701 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000702 * Exception stack trace. By default stack traces are not captured for
703 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
704 * to change this option.
705 */
706 Handle<StackTrace> GetStackTrace() const;
707
708 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000709 * Returns the number, 1-based, of the line where the error occurred.
710 */
ager@chromium.org32912102009-01-16 10:38:43 +0000711 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000713 /**
714 * Returns the index within the script of the first character where
715 * the error occurred.
716 */
ager@chromium.org32912102009-01-16 10:38:43 +0000717 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000718
719 /**
720 * Returns the index within the script of the last character where
721 * the error occurred.
722 */
ager@chromium.org32912102009-01-16 10:38:43 +0000723 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000724
725 /**
726 * Returns the index within the line of the first character where
727 * the error occurred.
728 */
ager@chromium.org32912102009-01-16 10:38:43 +0000729 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000730
731 /**
732 * Returns the index within the line of the last character where
733 * the error occurred.
734 */
ager@chromium.org32912102009-01-16 10:38:43 +0000735 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737 // TODO(1245381): Print to a string instead of on a FILE.
738 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000739
740 static const int kNoLineNumberInfo = 0;
741 static const int kNoColumnInfo = 0;
742};
743
744
745/**
746 * Representation of a JavaScript stack trace. The information collected is a
747 * snapshot of the execution stack and the information remains valid after
748 * execution continues.
749 */
750class V8EXPORT StackTrace {
751 public:
752 /**
753 * Flags that determine what information is placed captured for each
754 * StackFrame when grabbing the current stack trace.
755 */
756 enum StackTraceOptions {
757 kLineNumber = 1,
758 kColumnOffset = 1 << 1 | kLineNumber,
759 kScriptName = 1 << 2,
760 kFunctionName = 1 << 3,
761 kIsEval = 1 << 4,
762 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000763 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000764 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000765 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000766 };
767
768 /**
769 * Returns a StackFrame at a particular index.
770 */
771 Local<StackFrame> GetFrame(uint32_t index) const;
772
773 /**
774 * Returns the number of StackFrames.
775 */
776 int GetFrameCount() const;
777
778 /**
779 * Returns StackTrace as a v8::Array that contains StackFrame objects.
780 */
781 Local<Array> AsArray();
782
783 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000784 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000785 *
786 * \param frame_limit The maximum number of stack frames we want to capture.
787 * \param options Enumerates the set of things we will capture for each
788 * StackFrame.
789 */
790 static Local<StackTrace> CurrentStackTrace(
791 int frame_limit,
792 StackTraceOptions options = kOverview);
793};
794
795
796/**
797 * A single JavaScript stack frame.
798 */
799class V8EXPORT StackFrame {
800 public:
801 /**
802 * Returns the number, 1-based, of the line for the associate function call.
803 * This method will return Message::kNoLineNumberInfo if it is unable to
804 * retrieve the line number, or if kLineNumber was not passed as an option
805 * when capturing the StackTrace.
806 */
807 int GetLineNumber() const;
808
809 /**
810 * Returns the 1-based column offset on the line for the associated function
811 * call.
812 * This method will return Message::kNoColumnInfo if it is unable to retrieve
813 * the column number, or if kColumnOffset was not passed as an option when
814 * capturing the StackTrace.
815 */
816 int GetColumn() const;
817
818 /**
819 * Returns the name of the resource that contains the script for the
820 * function for this StackFrame.
821 */
822 Local<String> GetScriptName() const;
823
824 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000825 * Returns the name of the resource that contains the script for the
826 * function for this StackFrame or sourceURL value if the script name
827 * is undefined and its source ends with //@ sourceURL=... string.
828 */
829 Local<String> GetScriptNameOrSourceURL() const;
830
831 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000832 * Returns the name of the function associated with this stack frame.
833 */
834 Local<String> GetFunctionName() const;
835
836 /**
837 * Returns whether or not the associated function is compiled via a call to
838 * eval().
839 */
840 bool IsEval() const;
841
842 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000843 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000844 * constructor via "new".
845 */
846 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000847};
848
849
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000850// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
852
853/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000854 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000856class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 /**
859 * Returns true if this value is the undefined value. See ECMA-262
860 * 4.3.10.
861 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000862 V8EXPORT bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863
864 /**
865 * Returns true if this value is the null value. See ECMA-262
866 * 4.3.11.
867 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000868 V8EXPORT bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
870 /**
871 * Returns true if this value is true.
872 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000873 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875 /**
876 * Returns true if this value is false.
877 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000878 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
880 /**
881 * Returns true if this value is an instance of the String type.
882 * See ECMA-262 8.4.
883 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000884 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885
886 /**
887 * Returns true if this value is a function.
888 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000889 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
891 /**
892 * Returns true if this value is an array.
893 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000894 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
v8.team.kasperl727e9952008-09-02 14:56:44 +0000896 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 * Returns true if this value is an object.
898 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000899 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
v8.team.kasperl727e9952008-09-02 14:56:44 +0000901 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 * Returns true if this value is boolean.
903 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000904 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000905
v8.team.kasperl727e9952008-09-02 14:56:44 +0000906 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907 * Returns true if this value is a number.
908 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000909 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
v8.team.kasperl727e9952008-09-02 14:56:44 +0000911 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 * Returns true if this value is external.
913 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000914 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
v8.team.kasperl727e9952008-09-02 14:56:44 +0000916 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 * Returns true if this value is a 32-bit signed integer.
918 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000919 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000921 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000922 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000923 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000924 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000925
926 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000927 * Returns true if this value is a Date.
928 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000929 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000930
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000931 /**
932 * Returns true if this value is a RegExp.
933 */
934 V8EXPORT bool IsRegExp() const;
935
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000936 V8EXPORT Local<Boolean> ToBoolean() const;
937 V8EXPORT Local<Number> ToNumber() const;
938 V8EXPORT Local<String> ToString() const;
939 V8EXPORT Local<String> ToDetailString() const;
940 V8EXPORT Local<Object> ToObject() const;
941 V8EXPORT Local<Integer> ToInteger() const;
942 V8EXPORT Local<Uint32> ToUint32() const;
943 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
945 /**
946 * Attempts to convert a string to an array index.
947 * Returns an empty handle if the conversion fails.
948 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000949 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000951 V8EXPORT bool BooleanValue() const;
952 V8EXPORT double NumberValue() const;
953 V8EXPORT int64_t IntegerValue() const;
954 V8EXPORT uint32_t Uint32Value() const;
955 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
957 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000958 V8EXPORT bool Equals(Handle<Value> that) const;
959 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000960
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000961 private:
962 inline bool QuickIsString() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000963 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964};
965
966
967/**
968 * The superclass of primitive values. See ECMA-262 4.3.2.
969 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000970class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
972
973/**
974 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
975 * or false value.
976 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000977class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000979 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 static inline Handle<Boolean> New(bool value);
981};
982
983
984/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000985 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000987class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 public:
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000989 /**
990 * Returns the number of characters in this string.
991 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000992 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993
v8.team.kasperl727e9952008-09-02 14:56:44 +0000994 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000995 * Returns the number of bytes in the UTF-8 encoded
996 * representation of this string.
997 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000998 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000999
1000 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001001 * Write the contents of the string to an external buffer.
1002 * If no arguments are given, expects the buffer to be large
1003 * enough to hold the entire string and NULL terminator. Copies
1004 * the contents of the string and the NULL terminator into the
1005 * buffer.
1006 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001007 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1008 * before the end of the buffer.
1009 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001010 * Copies up to length characters into the output buffer.
1011 * Only null-terminates if there is enough space in the buffer.
1012 *
1013 * \param buffer The buffer into which the string will be copied.
1014 * \param start The starting position within the string at which
1015 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001016 * \param length The number of characters to copy from the string. For
1017 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001018 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001019 * \param hints Various hints that might affect performance of this or
1020 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001021 * \return The number of characters copied to the buffer excluding the null
1022 * terminator. For WriteUtf8: The number of bytes copied to the buffer
1023 * including the null terminator.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001024 */
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001025 enum WriteHints {
1026 NO_HINTS = 0,
1027 HINT_MANY_WRITES_EXPECTED = 1
1028 };
1029
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001030 V8EXPORT int Write(uint16_t* buffer,
1031 int start = 0,
1032 int length = -1,
1033 WriteHints hints = NO_HINTS) const; // UTF-16
1034 V8EXPORT int WriteAscii(char* buffer,
1035 int start = 0,
1036 int length = -1,
1037 WriteHints hints = NO_HINTS) const; // ASCII
1038 V8EXPORT int WriteUtf8(char* buffer,
1039 int length = -1,
1040 int* nchars_ref = NULL,
1041 WriteHints hints = NO_HINTS) const; // UTF-8
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042
v8.team.kasperl727e9952008-09-02 14:56:44 +00001043 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001044 * A zero length string.
1045 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001046 V8EXPORT static v8::Local<v8::String> Empty();
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001047
1048 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001049 * Returns true if the string is external
1050 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001051 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
v8.team.kasperl727e9952008-09-02 14:56:44 +00001053 /**
1054 * Returns true if the string is both external and ascii
1055 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001056 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001057
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001058 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001059 public:
1060 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001061
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001062 protected:
1063 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001064
1065 /**
1066 * Internally V8 will call this Dispose method when the external string
1067 * resource is no longer needed. The default implementation will use the
1068 * delete operator. This method can be overridden in subclasses to
1069 * control how allocated external string resources are disposed.
1070 */
1071 virtual void Dispose() { delete this; }
1072
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001073 private:
1074 // Disallow copying and assigning.
1075 ExternalStringResourceBase(const ExternalStringResourceBase&);
1076 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001077
1078 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001079 };
1080
v8.team.kasperl727e9952008-09-02 14:56:44 +00001081 /**
1082 * An ExternalStringResource is a wrapper around a two-byte string
1083 * buffer that resides outside V8's heap. Implement an
1084 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001085 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001086 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001087 class V8EXPORT ExternalStringResource
1088 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 public:
1090 /**
1091 * Override the destructor to manage the life cycle of the underlying
1092 * buffer.
1093 */
1094 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001095
1096 /**
1097 * The string data from the underlying buffer.
1098 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001100
1101 /**
1102 * The length of the string. That is, the number of two-byte characters.
1103 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 protected:
1107 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 };
1109
1110 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001111 * An ExternalAsciiStringResource is a wrapper around an ascii
1112 * string buffer that resides outside V8's heap. Implement an
1113 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001114 * underlying buffer. Note that the string data must be immutable
1115 * and that the data must be strict 7-bit ASCII, not Latin1 or
1116 * UTF-8, which would require special treatment internally in the
1117 * engine and, in the case of UTF-8, do not allow efficient indexing.
1118 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001119 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001121 class V8EXPORT ExternalAsciiStringResource
1122 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123 public:
1124 /**
1125 * Override the destructor to manage the life cycle of the underlying
1126 * buffer.
1127 */
1128 virtual ~ExternalAsciiStringResource() {}
1129 /** The string data from the underlying buffer.*/
1130 virtual const char* data() const = 0;
1131 /** The number of ascii characters in the string.*/
1132 virtual size_t length() const = 0;
1133 protected:
1134 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 };
1136
1137 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001138 * Get the ExternalStringResource for an external string. Returns
1139 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001141 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142
1143 /**
1144 * Get the ExternalAsciiStringResource for an external ascii string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001145 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001146 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001147 V8EXPORT ExternalAsciiStringResource* GetExternalAsciiStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001149 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150
1151 /**
1152 * Allocates a new string from either utf-8 encoded or ascii data.
1153 * The second parameter 'length' gives the buffer length.
1154 * If the data is utf-8 encoded, the caller must
1155 * be careful to supply the length parameter.
1156 * If it is not given, the function calls
1157 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001158 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001160 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161
1162 /** Allocates a new string from utf16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001163 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164
1165 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001166 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
v8.team.kasperl727e9952008-09-02 14:56:44 +00001168 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001169 * Creates a new string by concatenating the left and the right strings
1170 * passed in as parameters.
1171 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001172 V8EXPORT static Local<String> Concat(Handle<String> left,
1173 Handle<String>right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001174
1175 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001176 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001177 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001178 * resource will be disposed by calling its Dispose method. The caller of
1179 * this function should not otherwise delete or modify the resource. Neither
1180 * should the underlying buffer be deallocated or modified except through the
1181 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001182 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001183 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001184
ager@chromium.org6f10e412009-02-13 10:11:16 +00001185 /**
1186 * Associate an external string resource with this string by transforming it
1187 * in place so that existing references to this string in the JavaScript heap
1188 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001189 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001190 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001191 * The string is not modified if the operation fails. See NewExternal for
1192 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001193 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001194 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195
v8.team.kasperl727e9952008-09-02 14:56:44 +00001196 /**
1197 * Creates a new external string using the ascii data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001198 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001199 * resource will be disposed by calling its Dispose method. The caller of
1200 * this function should not otherwise delete or modify the resource. Neither
1201 * should the underlying buffer be deallocated or modified except through the
1202 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001203 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001204 V8EXPORT static Local<String> NewExternal(
1205 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001206
ager@chromium.org6f10e412009-02-13 10:11:16 +00001207 /**
1208 * Associate an external string resource with this string by transforming it
1209 * in place so that existing references to this string in the JavaScript heap
1210 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001211 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001212 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001213 * The string is not modified if the operation fails. See NewExternal for
1214 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001215 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001216 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001218 /**
1219 * Returns true if this string can be made external.
1220 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001221 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001222
kasper.lund7276f142008-07-30 08:49:36 +00001223 /** Creates an undetectable string from the supplied ascii or utf-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001224 V8EXPORT static Local<String> NewUndetectable(const char* data,
1225 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226
kasper.lund7276f142008-07-30 08:49:36 +00001227 /** Creates an undetectable string from the supplied utf-16 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001228 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1229 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230
1231 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001232 * Converts an object to a utf8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001233 * you want to print the object. If conversion to a string fails
1234 * (eg. due to an exception in the toString() method of the object)
1235 * then the length() method returns 0 and the * operator returns
1236 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001237 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001238 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001239 public:
1240 explicit Utf8Value(Handle<v8::Value> obj);
1241 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001242 char* operator*() { return str_; }
1243 const char* operator*() const { return str_; }
1244 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001245 private:
1246 char* str_;
1247 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001248
1249 // Disallow copying and assigning.
1250 Utf8Value(const Utf8Value&);
1251 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001252 };
1253
1254 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255 * Converts an object to an ascii string.
1256 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001257 * If conversion to a string fails (eg. due to an exception in the toString()
1258 * method of the object) then the length() method returns 0 and the * operator
1259 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001261 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 public:
1263 explicit AsciiValue(Handle<v8::Value> obj);
1264 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001265 char* operator*() { return str_; }
1266 const char* operator*() const { return str_; }
1267 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 private:
1269 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001270 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001271
1272 // Disallow copying and assigning.
1273 AsciiValue(const AsciiValue&);
1274 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 };
1276
1277 /**
1278 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001279 * If conversion to a string fails (eg. due to an exception in the toString()
1280 * method of the object) then the length() method returns 0 and the * operator
1281 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001283 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 public:
1285 explicit Value(Handle<v8::Value> obj);
1286 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001287 uint16_t* operator*() { return str_; }
1288 const uint16_t* operator*() const { return str_; }
1289 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 private:
1291 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001292 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001293
1294 // Disallow copying and assigning.
1295 Value(const Value&);
1296 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001298
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001299 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001300 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1301 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302};
1303
1304
1305/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001306 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001308class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001310 V8EXPORT double Value() const;
1311 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001312 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001314 V8EXPORT Number();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001315 static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316};
1317
1318
1319/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001320 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001322class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001324 V8EXPORT static Local<Integer> New(int32_t value);
1325 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
1326 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001327 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001329 V8EXPORT Integer();
1330 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331};
1332
1333
1334/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001335 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001337class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001339 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001341 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342};
1343
1344
1345/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001346 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001348class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001350 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001352 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353};
1354
1355
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356enum PropertyAttribute {
1357 None = 0,
1358 ReadOnly = 1 << 0,
1359 DontEnum = 1 << 1,
1360 DontDelete = 1 << 2
1361};
1362
ager@chromium.org3811b432009-10-28 14:53:37 +00001363enum ExternalArrayType {
1364 kExternalByteArray = 1,
1365 kExternalUnsignedByteArray,
1366 kExternalShortArray,
1367 kExternalUnsignedShortArray,
1368 kExternalIntArray,
1369 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001370 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001371 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001372 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001373};
1374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001376 * Accessor[Getter|Setter] are used as callback functions when
1377 * setting|getting a particular property. See Object and ObjectTemplate's
1378 * method SetAccessor.
1379 */
1380typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1381 const AccessorInfo& info);
1382
1383
1384typedef void (*AccessorSetter)(Local<String> property,
1385 Local<Value> value,
1386 const AccessorInfo& info);
1387
1388
1389/**
1390 * Access control specifications.
1391 *
1392 * Some accessors should be accessible across contexts. These
1393 * accessors have an explicit access control parameter which specifies
1394 * the kind of cross-context access that should be allowed.
1395 *
1396 * Additionally, for security, accessors can prohibit overwriting by
1397 * accessors defined in JavaScript. For objects that have such
1398 * accessors either locally or in their prototype chain it is not
1399 * possible to overwrite the accessor by using __defineGetter__ or
1400 * __defineSetter__ from JavaScript code.
1401 */
1402enum AccessControl {
1403 DEFAULT = 0,
1404 ALL_CAN_READ = 1,
1405 ALL_CAN_WRITE = 1 << 1,
1406 PROHIBITS_OVERWRITING = 1 << 2
1407};
1408
1409
1410/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001411 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001413class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001415 V8EXPORT bool Set(Handle<Value> key,
1416 Handle<Value> value,
1417 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001418
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001419 V8EXPORT bool Set(uint32_t index,
1420 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001421
ager@chromium.orge2902be2009-06-08 12:21:35 +00001422 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001423 // overriding accessors or read-only properties.
1424 //
1425 // Note that if the object has an interceptor the property will be set
1426 // locally, but since the interceptor takes precedence the local property
1427 // will only be returned if the interceptor doesn't return a value.
1428 //
1429 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001430 V8EXPORT bool ForceSet(Handle<Value> key,
1431 Handle<Value> value,
1432 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001433
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001434 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001436 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 // TODO(1245389): Replace the type-specific versions of these
1439 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001440 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001441
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001442 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001443
1444 // Delete a property on this object bypassing interceptors and
1445 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001446 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001447
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001448 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001449
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001450 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001452 V8EXPORT bool SetAccessor(Handle<String> name,
1453 AccessorGetter getter,
1454 AccessorSetter setter = 0,
1455 Handle<Value> data = Handle<Value>(),
1456 AccessControl settings = DEFAULT,
1457 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001460 * Returns an array containing the names of the enumerable properties
1461 * of this object, including properties from prototype objects. The
1462 * array returned by this method contains the same values as would
1463 * be enumerated by a for-in statement over this object.
1464 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001465 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001466
1467 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001468 * This function has the same functionality as GetPropertyNames but
1469 * the returned array doesn't contain the names of properties from
1470 * prototype objects.
1471 */
1472 V8EXPORT Local<Array> GetOwnPropertyNames();
1473
1474 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 * Get the prototype object. This does not skip objects marked to
1476 * be skipped by __proto__ and it does not consult the security
1477 * handler.
1478 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001479 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480
1481 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001482 * Set the prototype object. This does not skip objects marked to
1483 * be skipped by __proto__ and it does not consult the security
1484 * handler.
1485 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001486 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001487
1488 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001489 * Finds an instance of the given function template in the prototype
1490 * chain.
1491 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001492 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1493 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001494
1495 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 * Call builtin Object.prototype.toString on this object.
1497 * This is different from Value::ToString() that may call
1498 * user-defined toString function. This one does not.
1499 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001500 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001502 /**
1503 * Returns the name of the function invoked as a constructor for this object.
1504 */
1505 V8EXPORT Local<String> GetConstructorName();
1506
kasper.lund212ac232008-07-16 07:07:30 +00001507 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001508 V8EXPORT int InternalFieldCount();
kasper.lund212ac232008-07-16 07:07:30 +00001509 /** Gets the value in an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001510 inline Local<Value> GetInternalField(int index);
kasper.lund212ac232008-07-16 07:07:30 +00001511 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001512 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001514 /** Gets a native pointer from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001515 inline void* GetPointerFromInternalField(int index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001516
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001517 /** Sets a native pointer in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001518 V8EXPORT void SetPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001519
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001521 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001522 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1523 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1524 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525
1526 /**
1527 * If result.IsEmpty() no real property was located in the prototype chain.
1528 * This means interceptors in the prototype chain are not called.
1529 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001530 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1531 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001532
1533 /**
1534 * If result.IsEmpty() no real property was located on the object or
1535 * in the prototype chain.
1536 * This means interceptors in the prototype chain are not called.
1537 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001538 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539
1540 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001541 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542
kasper.lund212ac232008-07-16 07:07:30 +00001543 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001544 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001546 /**
1547 * Turns on access check on the object if the object is an instance of
1548 * a template that has access check callbacks. If an object has no
1549 * access check info, the object cannot be accessed by anyone.
1550 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001551 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001552
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001553 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001554 * Returns the identity hash for this object. The current implementation
1555 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001556 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001557 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001558 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001559 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001560 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001561
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001562 /**
1563 * Access hidden properties on JavaScript objects. These properties are
1564 * hidden from the executing JavaScript and only accessible through the V8
1565 * C++ API. Hidden properties introduced by V8 internally (for example the
1566 * identity hash) are prefixed with "v8::".
1567 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001568 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1569 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1570 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001571
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001572 /**
1573 * Returns true if this is an instance of an api function (one
1574 * created from a function created from a function template) and has
1575 * been modified since it was created. Note that this method is
1576 * conservative and may return true for objects that haven't actually
1577 * been modified.
1578 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001579 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001580
1581 /**
1582 * Clone this object with a fast but shallow copy. Values will point
1583 * to the same values as the original object.
1584 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001585 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001587 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001588 * Returns the context in which the object was created.
1589 */
1590 V8EXPORT Local<Context> CreationContext();
1591
1592 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001593 * Set the backing store of the indexed properties to be managed by the
1594 * embedding layer. Access to the indexed properties will follow the rules
1595 * spelled out in CanvasPixelArray.
1596 * Note: The embedding program still owns the data and needs to ensure that
1597 * the backing store is preserved while V8 has a reference.
1598 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001599 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001600 V8EXPORT bool HasIndexedPropertiesInPixelData();
1601 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1602 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001603
ager@chromium.org3811b432009-10-28 14:53:37 +00001604 /**
1605 * Set the backing store of the indexed properties to be managed by the
1606 * embedding layer. Access to the indexed properties will follow the rules
1607 * spelled out for the CanvasArray subtypes in the WebGL specification.
1608 * Note: The embedding program still owns the data and needs to ensure that
1609 * the backing store is preserved while V8 has a reference.
1610 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001611 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1612 void* data,
1613 ExternalArrayType array_type,
1614 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001615 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1616 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1617 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1618 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001619
lrn@chromium.org1c092762011-05-09 09:42:16 +00001620 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001621 * Checks whether a callback is set by the
1622 * ObjectTemplate::SetCallAsFunctionHandler method.
1623 * When an Object is callable this method returns true.
1624 */
1625 V8EXPORT bool IsCallable();
1626
1627 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00001628 * Call an Object as a function if a callback is set by the
1629 * ObjectTemplate::SetCallAsFunctionHandler method.
1630 */
1631 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1632 int argc,
1633 Handle<Value> argv[]);
1634
1635 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001636 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001637 * ObjectTemplate::SetCallAsFunctionHandler method.
1638 * Note: This method behaves like the Function::NewInstance method.
1639 */
1640 V8EXPORT Local<Value> CallAsConstructor(int argc,
1641 Handle<Value> argv[]);
1642
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001643 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001644 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001647 V8EXPORT Object();
1648 V8EXPORT static void CheckCast(Value* obj);
1649 V8EXPORT Local<Value> CheckedGetInternalField(int index);
1650 V8EXPORT void* SlowGetPointerFromInternalField(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001651
1652 /**
1653 * If quick access to the internal field is possible this method
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001654 * returns the value. Otherwise an empty handle is returned.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001655 */
1656 inline Local<Value> UncheckedGetInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657};
1658
1659
1660/**
1661 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1662 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001663class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001665 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666
ager@chromium.org3e875802009-06-29 08:26:34 +00001667 /**
1668 * Clones an element at index |index|. Returns an empty
1669 * handle if cloning fails (for any reason).
1670 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001671 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001672
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001673 /**
1674 * Creates a JavaScript array with the given length. If the length
1675 * is negative the returned array will have length 0.
1676 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001677 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001678
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001679 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001681 V8EXPORT Array();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001682 static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683};
1684
1685
1686/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001687 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001689class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001691 V8EXPORT Local<Object> NewInstance() const;
1692 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1693 V8EXPORT Local<Value> Call(Handle<Object> recv,
1694 int argc,
1695 Handle<Value> argv[]);
1696 V8EXPORT void SetName(Handle<String> name);
1697 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001698
1699 /**
1700 * Returns zero based line number of function body and
1701 * kLineOffsetNotFound if no information available.
1702 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001703 V8EXPORT int GetScriptLineNumber() const;
1704 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001705 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001706 V8EXPORT static const int kLineOffsetNotFound;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001708 V8EXPORT Function();
1709 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710};
1711
1712
1713/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001714 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1715 */
1716class Date : public Object {
1717 public:
1718 V8EXPORT static Local<Value> New(double time);
1719
1720 /**
1721 * A specialization of Value::NumberValue that is more efficient
1722 * because we know the structure of this object.
1723 */
1724 V8EXPORT double NumberValue() const;
1725
1726 static inline Date* Cast(v8::Value* obj);
1727
1728 /**
1729 * Notification that the embedder has changed the time zone,
1730 * daylight savings time, or other date / time configuration
1731 * parameters. V8 keeps a cache of various values used for
1732 * date / time computation. This notification will reset
1733 * those cached values for the current context so that date /
1734 * time configuration changes would be reflected in the Date
1735 * object.
1736 *
1737 * This API should not be called more than needed as it will
1738 * negatively impact the performance of date operations.
1739 */
1740 V8EXPORT static void DateTimeConfigurationChangeNotification();
1741
1742 private:
1743 V8EXPORT static void CheckCast(v8::Value* obj);
1744};
1745
1746
1747/**
1748 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1749 */
1750class RegExp : public Object {
1751 public:
1752 /**
1753 * Regular expression flag bits. They can be or'ed to enable a set
1754 * of flags.
1755 */
1756 enum Flags {
1757 kNone = 0,
1758 kGlobal = 1,
1759 kIgnoreCase = 2,
1760 kMultiline = 4
1761 };
1762
1763 /**
1764 * Creates a regular expression from the given pattern string and
1765 * the flags bit field. May throw a JavaScript exception as
1766 * described in ECMA-262, 15.10.4.1.
1767 *
1768 * For example,
1769 * RegExp::New(v8::String::New("foo"),
1770 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1771 * is equivalent to evaluating "/foo/gm".
1772 */
1773 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1774 Flags flags);
1775
1776 /**
1777 * Returns the value of the source property: a string representing
1778 * the regular expression.
1779 */
1780 V8EXPORT Local<String> GetSource() const;
1781
1782 /**
1783 * Returns the flags bit field.
1784 */
1785 V8EXPORT Flags GetFlags() const;
1786
1787 static inline RegExp* Cast(v8::Value* obj);
1788
1789 private:
1790 V8EXPORT static void CheckCast(v8::Value* obj);
1791};
1792
1793
1794/**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001795 * A JavaScript value that wraps a C++ void*. This type of value is
1796 * mainly used to associate C++ data structures with JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 * objects.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001798 *
1799 * The Wrap function V8 will return the most optimal Value object wrapping the
1800 * C++ void*. The type of the value is not guaranteed to be an External object
1801 * and no assumptions about its type should be made. To access the wrapped
1802 * value Unwrap should be used, all other operations on that object will lead
1803 * to unpredictable results.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001805class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001807 V8EXPORT static Local<Value> Wrap(void* data);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001808 static inline void* Unwrap(Handle<Value> obj);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001809
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001810 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001811 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001812 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001814 V8EXPORT External();
1815 V8EXPORT static void CheckCast(v8::Value* obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001816 static inline void* QuickUnwrap(Handle<v8::Value> obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001817 V8EXPORT static void* FullUnwrap(Handle<v8::Value> obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818};
1819
1820
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001821// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
1823
1824/**
1825 * The superclass of object and function templates.
1826 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001827class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 public:
1829 /** Adds a property to each instance created by this template.*/
1830 void Set(Handle<String> name, Handle<Data> value,
1831 PropertyAttribute attributes = None);
1832 inline void Set(const char* name, Handle<Data> value);
1833 private:
1834 Template();
1835
1836 friend class ObjectTemplate;
1837 friend class FunctionTemplate;
1838};
1839
1840
1841/**
1842 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00001843 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 * including the receiver, the number and values of arguments, and
1845 * the holder of the function.
1846 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001847class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 public:
1849 inline int Length() const;
1850 inline Local<Value> operator[](int i) const;
1851 inline Local<Function> Callee() const;
1852 inline Local<Object> This() const;
1853 inline Local<Object> Holder() const;
1854 inline bool IsConstructCall() const;
1855 inline Local<Value> Data() const;
1856 private:
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001857 static const int kDataIndex = 0;
1858 static const int kCalleeIndex = -1;
1859 static const int kHolderIndex = -2;
1860
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001862 inline Arguments(internal::Object** implicit_args,
1863 internal::Object** values,
1864 int length,
1865 bool is_construct_call);
1866 internal::Object** implicit_args_;
1867 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00001869 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870};
1871
1872
1873/**
1874 * The information passed to an accessor callback about the context
1875 * of the property access.
1876 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001877class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001879 inline AccessorInfo(internal::Object** args)
1880 : args_(args) { }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 inline Local<Value> Data() const;
1882 inline Local<Object> This() const;
1883 inline Local<Object> Holder() const;
1884 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001885 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886};
1887
1888
1889typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
1890
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 * NamedProperty[Getter|Setter] are used as interceptors on object.
1893 * See ObjectTemplate::SetNamedPropertyHandler.
1894 */
1895typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
1896 const AccessorInfo& info);
1897
1898
1899/**
1900 * Returns the value if the setter intercepts the request.
1901 * Otherwise, returns an empty handle.
1902 */
1903typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
1904 Local<Value> value,
1905 const AccessorInfo& info);
1906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907/**
1908 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001909 * The result is an integer encoding property attributes (like v8::None,
1910 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001912typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
1913 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914
1915
1916/**
1917 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001918 * The return value is true if the property could be deleted and false
1919 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001920 */
1921typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
1922 const AccessorInfo& info);
1923
1924/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001925 * Returns an array containing the names of the properties the named
1926 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 */
1928typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
1929
v8.team.kasperl727e9952008-09-02 14:56:44 +00001930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001932 * Returns the value of the property if the getter intercepts the
1933 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 */
1935typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
1936 const AccessorInfo& info);
1937
1938
1939/**
1940 * Returns the value if the setter intercepts the request.
1941 * Otherwise, returns an empty handle.
1942 */
1943typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
1944 Local<Value> value,
1945 const AccessorInfo& info);
1946
1947
1948/**
1949 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001950 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001952typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
1953 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954
1955/**
1956 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001957 * The return value is true if the property could be deleted and false
1958 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 */
1960typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
1961 const AccessorInfo& info);
1962
v8.team.kasperl727e9952008-09-02 14:56:44 +00001963/**
1964 * Returns an array containing the indices of the properties the
1965 * indexed property getter intercepts.
1966 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
1968
1969
1970/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001971 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972 */
1973enum AccessType {
1974 ACCESS_GET,
1975 ACCESS_SET,
1976 ACCESS_HAS,
1977 ACCESS_DELETE,
1978 ACCESS_KEYS
1979};
1980
v8.team.kasperl727e9952008-09-02 14:56:44 +00001981
1982/**
1983 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001984 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001985 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001986typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 Local<Value> key,
1988 AccessType type,
1989 Local<Value> data);
1990
v8.team.kasperl727e9952008-09-02 14:56:44 +00001991
1992/**
1993 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001994 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001995 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001996typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 uint32_t index,
1998 AccessType type,
1999 Local<Value> data);
2000
2001
2002/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002003 * A FunctionTemplate is used to create functions at runtime. There
2004 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002005 * context. The lifetime of the created function is equal to the
2006 * lifetime of the context. So in case the embedder needs to create
2007 * temporary functions that can be collected using Scripts is
2008 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 *
2010 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002011 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002013 * A FunctionTemplate has a corresponding instance template which is
2014 * used to create object instances when the function is used as a
2015 * constructor. Properties added to the instance template are added to
2016 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 *
2018 * A FunctionTemplate can have a prototype template. The prototype template
2019 * is used to create the prototype object of the function.
2020 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002021 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002023 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2025 * t->Set("func_property", v8::Number::New(1));
2026 *
2027 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2028 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2029 * proto_t->Set("proto_const", v8::Number::New(2));
2030 *
2031 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2032 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2033 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2034 * instance_t->Set("instance_property", Number::New(3));
2035 *
2036 * v8::Local<v8::Function> function = t->GetFunction();
2037 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002038 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 *
2040 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002041 * and "instance" for the instance object created above. The function
2042 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002044 * \code
2045 * func_property in function == true;
2046 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002048 * function.prototype.proto_method() invokes 'InvokeCallback'
2049 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002051 * instance instanceof function == true;
2052 * instance.instance_accessor calls 'InstanceAccessorCallback'
2053 * instance.instance_property == 3;
2054 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002056 * A FunctionTemplate can inherit from another one by calling the
2057 * FunctionTemplate::Inherit method. The following graph illustrates
2058 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002060 * \code
2061 * FunctionTemplate Parent -> Parent() . prototype -> { }
2062 * ^ ^
2063 * | Inherit(Parent) | .__proto__
2064 * | |
2065 * FunctionTemplate Child -> Child() . prototype -> { }
2066 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002068 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2069 * object of the Child() function has __proto__ pointing to the
2070 * Parent() function's prototype object. An instance of the Child
2071 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002073 * Let Parent be the FunctionTemplate initialized in the previous
2074 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002076 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 * Local<FunctionTemplate> parent = t;
2078 * Local<FunctionTemplate> child = FunctionTemplate::New();
2079 * child->Inherit(parent);
2080 *
2081 * Local<Function> child_function = child->GetFunction();
2082 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002083 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002085 * The Child function and Child instance will have the following
2086 * properties:
2087 *
2088 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002090 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002092 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002094class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 public:
2096 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002097 static Local<FunctionTemplate> New(
2098 InvocationCallback callback = 0,
2099 Handle<Value> data = Handle<Value>(),
2100 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 /** Returns the unique function instance in the current execution context.*/
2102 Local<Function> GetFunction();
2103
v8.team.kasperl727e9952008-09-02 14:56:44 +00002104 /**
2105 * Set the call-handler callback for a FunctionTemplate. This
2106 * callback is called whenever the function created from this
2107 * FunctionTemplate is called.
2108 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 void SetCallHandler(InvocationCallback callback,
2110 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111
v8.team.kasperl727e9952008-09-02 14:56:44 +00002112 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 Local<ObjectTemplate> InstanceTemplate();
2114
2115 /** Causes the function template to inherit from a parent function template.*/
2116 void Inherit(Handle<FunctionTemplate> parent);
2117
2118 /**
2119 * A PrototypeTemplate is the template used to create the prototype object
2120 * of the function created by this template.
2121 */
2122 Local<ObjectTemplate> PrototypeTemplate();
2123
v8.team.kasperl727e9952008-09-02 14:56:44 +00002124
2125 /**
2126 * Set the class name of the FunctionTemplate. This is used for
2127 * printing objects created with the function created from the
2128 * FunctionTemplate as its constructor.
2129 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 void SetClassName(Handle<String> name);
2131
2132 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002133 * Determines whether the __proto__ accessor ignores instances of
2134 * the function template. If instances of the function template are
2135 * ignored, __proto__ skips all instances and instead returns the
2136 * next object in the prototype chain.
2137 *
2138 * Call with a value of true to make the __proto__ accessor ignore
2139 * instances of the function template. Call with a value of false
2140 * to make the __proto__ accessor not ignore instances of the
2141 * function template. By default, instances of a function template
2142 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 */
2144 void SetHiddenPrototype(bool value);
2145
2146 /**
ager@chromium.org04921a82011-06-27 13:21:41 +00002147 * Sets the property attributes of the 'prototype' property of functions
2148 * created from this FunctionTemplate. Can be any combination of ReadOnly,
2149 * DontEnum and DontDelete.
2150 */
2151 void SetPrototypeAttributes(int attributes);
2152
2153 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002154 * Returns true if the given object is an instance of this function
2155 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 */
2157 bool HasInstance(Handle<Value> object);
2158
2159 private:
2160 FunctionTemplate();
2161 void AddInstancePropertyAccessor(Handle<String> name,
2162 AccessorGetter getter,
2163 AccessorSetter setter,
2164 Handle<Value> data,
2165 AccessControl settings,
2166 PropertyAttribute attributes);
2167 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2168 NamedPropertySetter setter,
2169 NamedPropertyQuery query,
2170 NamedPropertyDeleter remover,
2171 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002172 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2174 IndexedPropertySetter setter,
2175 IndexedPropertyQuery query,
2176 IndexedPropertyDeleter remover,
2177 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002178 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2180 Handle<Value> data);
2181
2182 friend class Context;
2183 friend class ObjectTemplate;
2184};
2185
2186
2187/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002188 * An ObjectTemplate is used to create objects at runtime.
2189 *
2190 * Properties added to an ObjectTemplate are added to each object
2191 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002193class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002197
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 /** Creates a new instance of this template.*/
2199 Local<Object> NewInstance();
2200
2201 /**
2202 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002203 *
2204 * Whenever the property with the given name is accessed on objects
2205 * created from this ObjectTemplate the getter and setter callbacks
2206 * are called instead of getting and setting the property directly
2207 * on the JavaScript object.
2208 *
2209 * \param name The name of the property for which an accessor is added.
2210 * \param getter The callback to invoke when getting the property.
2211 * \param setter The callback to invoke when setting the property.
2212 * \param data A piece of data that will be passed to the getter and setter
2213 * callbacks whenever they are invoked.
2214 * \param settings Access control settings for the accessor. This is a bit
2215 * field consisting of one of more of
2216 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2217 * The default is to not allow cross-context access.
2218 * ALL_CAN_READ means that all cross-context reads are allowed.
2219 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2220 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2221 * cross-context access.
2222 * \param attribute The attributes of the property for which an accessor
2223 * is added.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 */
2225 void SetAccessor(Handle<String> name,
2226 AccessorGetter getter,
2227 AccessorSetter setter = 0,
2228 Handle<Value> data = Handle<Value>(),
2229 AccessControl settings = DEFAULT,
2230 PropertyAttribute attribute = None);
2231
2232 /**
2233 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002234 *
2235 * Whenever a named property is accessed on objects created from
2236 * this object template, the provided callback is invoked instead of
2237 * accessing the property directly on the JavaScript object.
2238 *
2239 * \param getter The callback to invoke when getting a property.
2240 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002241 * \param query The callback to invoke to check if a property is present,
2242 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * \param deleter The callback to invoke when deleting a property.
2244 * \param enumerator The callback to invoke to enumerate all the named
2245 * properties of an object.
2246 * \param data A piece of data that will be passed to the callbacks
2247 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 */
2249 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2250 NamedPropertySetter setter = 0,
2251 NamedPropertyQuery query = 0,
2252 NamedPropertyDeleter deleter = 0,
2253 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002254 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255
2256 /**
2257 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002258 *
2259 * Whenever an indexed property is accessed on objects created from
2260 * this object template, the provided callback is invoked instead of
2261 * accessing the property directly on the JavaScript object.
2262 *
2263 * \param getter The callback to invoke when getting a property.
2264 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002265 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002266 * \param deleter The callback to invoke when deleting a property.
2267 * \param enumerator The callback to invoke to enumerate all the indexed
2268 * properties of an object.
2269 * \param data A piece of data that will be passed to the callbacks
2270 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 */
2272 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2273 IndexedPropertySetter setter = 0,
2274 IndexedPropertyQuery query = 0,
2275 IndexedPropertyDeleter deleter = 0,
2276 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002277 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 /**
2280 * Sets the callback to be used when calling instances created from
2281 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002282 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 * function.
2284 */
2285 void SetCallAsFunctionHandler(InvocationCallback callback,
2286 Handle<Value> data = Handle<Value>());
2287
v8.team.kasperl727e9952008-09-02 14:56:44 +00002288 /**
2289 * Mark object instances of the template as undetectable.
2290 *
2291 * In many ways, undetectable objects behave as though they are not
2292 * there. They behave like 'undefined' in conditionals and when
2293 * printed. However, properties can be accessed and called as on
2294 * normal objects.
2295 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 void MarkAsUndetectable();
2297
v8.team.kasperl727e9952008-09-02 14:56:44 +00002298 /**
2299 * Sets access check callbacks on the object template.
2300 *
2301 * When accessing properties on instances of this object template,
2302 * the access check callback will be called to determine whether or
2303 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002304 * The last parameter specifies whether access checks are turned
2305 * on by default on instances. If access checks are off by default,
2306 * they can be turned on on individual instances by calling
2307 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002308 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2310 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002311 Handle<Value> data = Handle<Value>(),
2312 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313
kasper.lund212ac232008-07-16 07:07:30 +00002314 /**
2315 * Gets the number of internal fields for objects generated from
2316 * this template.
2317 */
2318 int InternalFieldCount();
2319
2320 /**
2321 * Sets the number of internal fields for objects generated from
2322 * this template.
2323 */
2324 void SetInternalFieldCount(int value);
2325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 private:
2327 ObjectTemplate();
2328 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2329 friend class FunctionTemplate;
2330};
2331
2332
2333/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002334 * A Signature specifies which receivers and arguments a function can
2335 * legally be called with.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002337class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338 public:
2339 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2340 Handle<FunctionTemplate>(),
2341 int argc = 0,
2342 Handle<FunctionTemplate> argv[] = 0);
2343 private:
2344 Signature();
2345};
2346
2347
2348/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002349 * A utility for determining the type of objects based on the template
2350 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002352class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 public:
2354 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2355 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2356 int match(Handle<Value> value);
2357 private:
2358 TypeSwitch();
2359};
2360
2361
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002362// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002363
2364
2365/**
2366 * Ignore
2367 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002368class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 public:
2370 Extension(const char* name,
2371 const char* source = 0,
2372 int dep_count = 0,
2373 const char** deps = 0);
2374 virtual ~Extension() { }
2375 virtual v8::Handle<v8::FunctionTemplate>
2376 GetNativeFunction(v8::Handle<v8::String> name) {
2377 return v8::Handle<v8::FunctionTemplate>();
2378 }
2379
2380 const char* name() { return name_; }
2381 const char* source() { return source_; }
2382 int dependency_count() { return dep_count_; }
2383 const char** dependencies() { return deps_; }
2384 void set_auto_enable(bool value) { auto_enable_ = value; }
2385 bool auto_enable() { return auto_enable_; }
2386
2387 private:
2388 const char* name_;
2389 const char* source_;
2390 int dep_count_;
2391 const char** deps_;
2392 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002393
2394 // Disallow copying and assigning.
2395 Extension(const Extension&);
2396 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397};
2398
2399
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002400void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401
2402
2403/**
2404 * Ignore
2405 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002406class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407 public:
2408 inline DeclareExtension(Extension* extension) {
2409 RegisterExtension(extension);
2410 }
2411};
2412
2413
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002414// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002415
2416
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002417Handle<Primitive> V8EXPORT Undefined();
2418Handle<Primitive> V8EXPORT Null();
2419Handle<Boolean> V8EXPORT True();
2420Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421
2422
2423/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002424 * A set of constraints that specifies the limits of the runtime's memory use.
2425 * You must set the heap size before initializing the VM - the size cannot be
2426 * adjusted after the VM is initialized.
2427 *
2428 * If you are using threads then you should hold the V8::Locker lock while
2429 * setting the stack limit and you must set a non-default stack limit separately
2430 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002432class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 public:
2434 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002435 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002437 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002439 int max_executable_size() { return max_executable_size_; }
2440 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002441 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002442 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002443 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2444 private:
2445 int max_young_space_size_;
2446 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002447 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448 uint32_t* stack_limit_;
2449};
2450
2451
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002452bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002453
2454
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002455// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002456
2457
2458typedef void (*FatalErrorCallback)(const char* location, const char* message);
2459
2460
2461typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> data);
2462
2463
2464/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002465 * Schedules an exception to be thrown when returning to JavaScript. When an
2466 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002468 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002470Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471
2472/**
2473 * Create new error objects by calling the corresponding error object
2474 * constructor with the message.
2475 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002476class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477 public:
2478 static Local<Value> RangeError(Handle<String> message);
2479 static Local<Value> ReferenceError(Handle<String> message);
2480 static Local<Value> SyntaxError(Handle<String> message);
2481 static Local<Value> TypeError(Handle<String> message);
2482 static Local<Value> Error(Handle<String> message);
2483};
2484
2485
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002486// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002488typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002490typedef void* (*CreateHistogramCallback)(const char* name,
2491 int min,
2492 int max,
2493 size_t buckets);
2494
2495typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2496
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002497// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002498 enum ObjectSpace {
2499 kObjectSpaceNewSpace = 1 << 0,
2500 kObjectSpaceOldPointerSpace = 1 << 1,
2501 kObjectSpaceOldDataSpace = 1 << 2,
2502 kObjectSpaceCodeSpace = 1 << 3,
2503 kObjectSpaceMapSpace = 1 << 4,
2504 kObjectSpaceLoSpace = 1 << 5,
2505
2506 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2507 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2508 kObjectSpaceLoSpace
2509 };
2510
2511 enum AllocationAction {
2512 kAllocationActionAllocate = 1 << 0,
2513 kAllocationActionFree = 1 << 1,
2514 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2515 };
2516
2517typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2518 AllocationAction action,
2519 int size);
2520
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002521// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002522typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2523 AccessType type,
2524 Local<Value> data);
2525
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002526// --- AllowCodeGenerationFromStrings callbacks ---
2527
2528/**
2529 * Callback to check if code generation from strings is allowed. See
2530 * Context::AllowCodeGenerationFromStrings.
2531 */
2532typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2533
2534// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535
2536/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002537 * Applications can register callback functions which will be called
2538 * before and after a garbage collection. Allocations are not
2539 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002540 * objects (set or delete properties for example) since it is possible
2541 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002542 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002543enum GCType {
2544 kGCTypeScavenge = 1 << 0,
2545 kGCTypeMarkSweepCompact = 1 << 1,
2546 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2547};
2548
2549enum GCCallbackFlags {
2550 kNoGCCallbackFlags = 0,
2551 kGCCallbackFlagCompacted = 1 << 0
2552};
2553
2554typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2555typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2556
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557typedef void (*GCCallback)();
2558
2559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560/**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002561 * Profiler modules.
2562 *
ager@chromium.org04921a82011-06-27 13:21:41 +00002563 * In V8, profiler consists of several modules. Each can be turned on / off
2564 * independently.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002565 */
2566enum ProfilerModules {
2567 PROFILER_MODULE_NONE = 0,
ager@chromium.org04921a82011-06-27 13:21:41 +00002568 PROFILER_MODULE_CPU = 1
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002569};
2570
2571
2572/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002573 * Collection of V8 heap information.
2574 *
2575 * Instances of this class can be passed to v8::V8::HeapStatistics to
2576 * get heap statistics from V8.
2577 */
2578class V8EXPORT HeapStatistics {
2579 public:
2580 HeapStatistics();
2581 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002582 size_t total_heap_size_executable() { return total_heap_size_executable_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002583 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002584 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002585
2586 private:
2587 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002588 void set_total_heap_size_executable(size_t size) {
2589 total_heap_size_executable_ = size;
2590 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002591 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002592 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002593
2594 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002595 size_t total_heap_size_executable_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002596 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002597 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002598
2599 friend class V8;
2600};
2601
2602
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002603class RetainedObjectInfo;
2604
ager@chromium.org3811b432009-10-28 14:53:37 +00002605/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002606 * Isolate represents an isolated instance of the V8 engine. V8
2607 * isolates have completely separate states. Objects from one isolate
2608 * must not be used in other isolates. When V8 is initialized a
2609 * default isolate is implicitly created and entered. The embedder
2610 * can create additional isolates and use them in parallel in multiple
2611 * threads. An isolate can be entered by at most one thread at any
2612 * given time. The Locker/Unlocker API can be used to synchronize.
2613 */
2614class V8EXPORT Isolate {
2615 public:
2616 /**
2617 * Stack-allocated class which sets the isolate for all operations
2618 * executed within a local scope.
2619 */
2620 class V8EXPORT Scope {
2621 public:
2622 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2623 isolate->Enter();
2624 }
2625
2626 ~Scope() { isolate_->Exit(); }
2627
2628 private:
2629 Isolate* const isolate_;
2630
2631 // Prevent copying of Scope objects.
2632 Scope(const Scope&);
2633 Scope& operator=(const Scope&);
2634 };
2635
2636 /**
2637 * Creates a new isolate. Does not change the currently entered
2638 * isolate.
2639 *
2640 * When an isolate is no longer used its resources should be freed
2641 * by calling Dispose(). Using the delete operator is not allowed.
2642 */
2643 static Isolate* New();
2644
2645 /**
2646 * Returns the entered isolate for the current thread or NULL in
2647 * case there is no current isolate.
2648 */
2649 static Isolate* GetCurrent();
2650
2651 /**
2652 * Methods below this point require holding a lock (using Locker) in
2653 * a multi-threaded environment.
2654 */
2655
2656 /**
2657 * Sets this isolate as the entered one for the current thread.
2658 * Saves the previously entered one (if any), so that it can be
2659 * restored when exiting. Re-entering an isolate is allowed.
2660 */
2661 void Enter();
2662
2663 /**
2664 * Exits this isolate by restoring the previously entered one in the
2665 * current thread. The isolate may still stay the same, if it was
2666 * entered more than once.
2667 *
2668 * Requires: this == Isolate::GetCurrent().
2669 */
2670 void Exit();
2671
2672 /**
2673 * Disposes the isolate. The isolate must not be entered by any
2674 * thread to be disposable.
2675 */
2676 void Dispose();
2677
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002678 /**
2679 * Associate embedder-specific data with the isolate
2680 */
2681 void SetData(void* data);
2682
2683 /**
2684 * Retrive embedder-specific data from the isolate.
2685 * Returns NULL if SetData has never been called.
2686 */
2687 void* GetData();
2688
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002689 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002690 Isolate();
2691 Isolate(const Isolate&);
2692 ~Isolate();
2693 Isolate& operator=(const Isolate&);
2694 void* operator new(size_t size);
2695 void operator delete(void*, size_t);
2696};
2697
2698
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002699class StartupData {
2700 public:
2701 enum CompressionAlgorithm {
2702 kUncompressed,
2703 kBZip2
2704 };
2705
2706 const char* data;
2707 int compressed_size;
2708 int raw_size;
2709};
2710
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002711
2712/**
2713 * A helper class for driving V8 startup data decompression. It is based on
2714 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2715 * for an embedder to use this class, instead, API functions can be used
2716 * directly.
2717 *
2718 * For an example of the class usage, see the "shell.cc" sample application.
2719 */
2720class V8EXPORT StartupDataDecompressor { // NOLINT
2721 public:
2722 StartupDataDecompressor();
2723 virtual ~StartupDataDecompressor();
2724 int Decompress();
2725
2726 protected:
2727 virtual int DecompressData(char* raw_data,
2728 int* raw_data_size,
2729 const char* compressed_data,
2730 int compressed_data_size) = 0;
2731
2732 private:
2733 char** raw_data;
2734};
2735
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002736/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737 * Container class for static utility functions.
2738 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002739class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002741 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002742 static void SetFatalErrorHandler(FatalErrorCallback that);
2743
v8.team.kasperl727e9952008-09-02 14:56:44 +00002744 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002745 * Set the callback to invoke to check if code generation from
2746 * strings should be allowed.
2747 */
2748 static void SetAllowCodeGenerationFromStringsCallback(
2749 AllowCodeGenerationFromStringsCallback that);
2750
2751 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002752 * Ignore out-of-memory exceptions.
2753 *
2754 * V8 running out of memory is treated as a fatal error by default.
2755 * This means that the fatal error handler is called and that V8 is
2756 * terminated.
2757 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002758 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00002759 * out-of-memory situation as a fatal error. This way, the contexts
2760 * that did not cause the out of memory problem might be able to
2761 * continue execution.
2762 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 static void IgnoreOutOfMemoryException();
2764
v8.team.kasperl727e9952008-09-02 14:56:44 +00002765 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002766 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00002767 * fatal errors such as out-of-memory situations.
2768 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002769 static bool IsDead();
2770
2771 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002772 * The following 4 functions are to be used when V8 is built with
2773 * the 'compress_startup_data' flag enabled. In this case, the
2774 * embedder must decompress startup data prior to initializing V8.
2775 *
2776 * This is how interaction with V8 should look like:
2777 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
2778 * v8::StartupData* compressed_data =
2779 * new v8::StartupData[compressed_data_count];
2780 * v8::V8::GetCompressedStartupData(compressed_data);
2781 * ... decompress data (compressed_data can be updated in-place) ...
2782 * v8::V8::SetDecompressedStartupData(compressed_data);
2783 * ... now V8 can be initialized
2784 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002785 *
2786 * A helper class StartupDataDecompressor is provided. It implements
2787 * the protocol of the interaction described above, and can be used in
2788 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002789 */
2790 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
2791 static int GetCompressedStartupDataCount();
2792 static void GetCompressedStartupData(StartupData* compressed_data);
2793 static void SetDecompressedStartupData(StartupData* decompressed_data);
2794
2795 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002796 * Adds a message listener.
2797 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002798 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00002799 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 */
2801 static bool AddMessageListener(MessageCallback that,
2802 Handle<Value> data = Handle<Value>());
2803
2804 /**
2805 * Remove all message listeners from the specified callback function.
2806 */
2807 static void RemoveMessageListeners(MessageCallback that);
2808
2809 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002810 * Tells V8 to capture current stack trace when uncaught exception occurs
2811 * and report it to the message listeners. The option is off by default.
2812 */
2813 static void SetCaptureStackTraceForUncaughtExceptions(
2814 bool capture,
2815 int frame_limit = 10,
2816 StackTrace::StackTraceOptions options = StackTrace::kOverview);
2817
2818 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002819 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820 */
2821 static void SetFlagsFromString(const char* str, int length);
2822
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002823 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002824 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002825 */
2826 static void SetFlagsFromCommandLine(int* argc,
2827 char** argv,
2828 bool remove_flags);
2829
kasper.lund7276f142008-07-30 08:49:36 +00002830 /** Get the version string. */
2831 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832
2833 /**
2834 * Enables the host application to provide a mechanism for recording
2835 * statistics counters.
2836 */
2837 static void SetCounterFunction(CounterLookupCallback);
2838
2839 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002840 * Enables the host application to provide a mechanism for recording
2841 * histograms. The CreateHistogram function returns a
2842 * histogram which will later be passed to the AddHistogramSample
2843 * function.
2844 */
2845 static void SetCreateHistogramFunction(CreateHistogramCallback);
2846 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
2847
2848 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849 * Enables the computation of a sliding window of states. The sliding
2850 * window information is recorded in statistics counters.
2851 */
2852 static void EnableSlidingStateWindow();
2853
2854 /** Callback function for reporting failed access checks.*/
2855 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
2856
2857 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002858 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002859 * garbage collection. Allocations are not allowed in the
2860 * callback function, you therefore cannot manipulate objects (set
2861 * or delete properties for example) since it is possible such
2862 * operations will result in the allocation of objects. It is possible
2863 * to specify the GCType filter for your callback. But it is not possible to
2864 * register the same callback function two times with different
2865 * GCType filters.
2866 */
2867 static void AddGCPrologueCallback(
2868 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
2869
2870 /**
2871 * This function removes callback which was installed by
2872 * AddGCPrologueCallback function.
2873 */
2874 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
2875
2876 /**
2877 * The function is deprecated. Please use AddGCPrologueCallback instead.
2878 * Enables the host application to receive a notification before a
2879 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002880 * callback function, you therefore cannot manipulate objects (set
2881 * or delete properties for example) since it is possible such
2882 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883 */
2884 static void SetGlobalGCPrologueCallback(GCCallback);
2885
2886 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002887 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002888 * garbage collection. Allocations are not allowed in the
2889 * callback function, you therefore cannot manipulate objects (set
2890 * or delete properties for example) since it is possible such
2891 * operations will result in the allocation of objects. It is possible
2892 * to specify the GCType filter for your callback. But it is not possible to
2893 * register the same callback function two times with different
2894 * GCType filters.
2895 */
2896 static void AddGCEpilogueCallback(
2897 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
2898
2899 /**
2900 * This function removes callback which was installed by
2901 * AddGCEpilogueCallback function.
2902 */
2903 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
2904
2905 /**
2906 * The function is deprecated. Please use AddGCEpilogueCallback instead.
2907 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00002908 * major garbage collection. Allocations are not allowed in the
2909 * callback function, you therefore cannot manipulate objects (set
2910 * or delete properties for example) since it is possible such
2911 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 */
2913 static void SetGlobalGCEpilogueCallback(GCCallback);
2914
2915 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002916 * Enables the host application to provide a mechanism to be notified
2917 * and perform custom logging when V8 Allocates Executable Memory.
2918 */
2919 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
2920 ObjectSpace space,
2921 AllocationAction action);
2922
2923 /**
2924 * This function removes callback which was installed by
2925 * AddMemoryAllocationCallback function.
2926 */
2927 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
2928
2929 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002930 * Allows the host application to group objects together. If one
2931 * object in the group is alive, all objects in the group are alive.
2932 * After each garbage collection, object groups are removed. It is
2933 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00002934 * function, for instance to simulate DOM tree connections among JS
v8.team.kasperl727e9952008-09-02 14:56:44 +00002935 * wrapper objects.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002936 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002938 static void AddObjectGroup(Persistent<Value>* objects,
2939 size_t length,
2940 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941
2942 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00002943 * Allows the host application to declare implicit references between
2944 * the objects: if |parent| is alive, all |children| are alive too.
2945 * After each garbage collection, all implicit references
2946 * are removed. It is intended to be used in the before-garbage-collection
2947 * callback function.
2948 */
2949 static void AddImplicitReferences(Persistent<Object> parent,
2950 Persistent<Value>* children,
2951 size_t length);
2952
2953 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002954 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002955 * initialize from scratch. This function is called implicitly if
2956 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 */
2958 static bool Initialize();
2959
kasper.lund7276f142008-07-30 08:49:36 +00002960 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002961 * Adjusts the amount of registered external memory. Used to give
2962 * V8 an indication of the amount of externally allocated memory
2963 * that is kept alive by JavaScript objects. V8 uses this to decide
2964 * when to perform global garbage collections. Registering
2965 * externally allocated memory will trigger global garbage
2966 * collections more often than otherwise in an attempt to garbage
2967 * collect the JavaScript objects keeping the externally allocated
2968 * memory alive.
2969 *
2970 * \param change_in_bytes the change in externally allocated memory
2971 * that is kept alive by JavaScript objects.
2972 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00002973 */
2974 static int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
2975
iposva@chromium.org245aa852009-02-10 00:49:54 +00002976 /**
2977 * Suspends recording of tick samples in the profiler.
2978 * When the V8 profiling mode is enabled (usually via command line
2979 * switches) this function suspends recording of tick samples.
2980 * Profiling ticks are discarded until ResumeProfiler() is called.
2981 *
2982 * See also the --prof and --prof_auto command line switches to
2983 * enable V8 profiling.
2984 */
2985 static void PauseProfiler();
2986
2987 /**
2988 * Resumes recording of tick samples in the profiler.
2989 * See also PauseProfiler().
2990 */
2991 static void ResumeProfiler();
2992
ager@chromium.org41826e72009-03-30 13:30:57 +00002993 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002994 * Return whether profiler is currently paused.
2995 */
2996 static bool IsProfilerPaused();
2997
2998 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00002999 * Resumes specified profiler modules. Can be called several times to
3000 * mark the opening of a profiler events block with the given tag.
3001 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003002 * "ResumeProfiler" is equivalent to "ResumeProfilerEx(PROFILER_MODULE_CPU)".
3003 * See ProfilerModules enum.
3004 *
3005 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00003006 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003007 */
ager@chromium.org5c838252010-02-19 08:53:10 +00003008 static void ResumeProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003009
3010 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00003011 * Pauses specified profiler modules. Each call to "PauseProfilerEx" closes
3012 * a block of profiler events opened by a call to "ResumeProfilerEx" with the
3013 * same tag value. There is no need for blocks to be properly nested.
3014 * The profiler is paused when the last opened block is closed.
3015 *
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003016 * "PauseProfiler" is equivalent to "PauseProfilerEx(PROFILER_MODULE_CPU)".
3017 * See ProfilerModules enum.
3018 *
3019 * \param flags Flags specifying profiler modules.
ager@chromium.org5c838252010-02-19 08:53:10 +00003020 * \param tag Profile tag.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003021 */
ager@chromium.org5c838252010-02-19 08:53:10 +00003022 static void PauseProfilerEx(int flags, int tag = 0);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00003023
3024 /**
3025 * Returns active (resumed) profiler modules.
3026 * See ProfilerModules enum.
3027 *
3028 * \returns active profiler modules.
3029 */
3030 static int GetActiveProfilerModules();
3031
3032 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003033 * If logging is performed into a memory buffer (via --logfile=*), allows to
3034 * retrieve previously written messages. This can be used for retrieving
3035 * profiler log data in the application. This function is thread-safe.
3036 *
3037 * Caller provides a destination buffer that must exist during GetLogLines
3038 * call. Only whole log lines are copied into the buffer.
3039 *
3040 * \param from_pos specified a point in a buffer to read from, 0 is the
3041 * beginning of a buffer. It is assumed that caller updates its current
3042 * position using returned size value from the previous call.
3043 * \param dest_buf destination buffer for log data.
3044 * \param max_size size of the destination buffer.
3045 * \returns actual size of log data copied into buffer.
3046 */
3047 static int GetLogLines(int from_pos, char* dest_buf, int max_size);
3048
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003049 /**
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003050 * The minimum allowed size for a log lines buffer. If the size of
3051 * the buffer given will not be enough to hold a line of the maximum
3052 * length, an attempt to find a log line end in GetLogLines will
3053 * fail, and an empty result will be returned.
3054 */
3055 static const int kMinimumSizeForLogLinesBuffer = 2048;
3056
3057 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003058 * Retrieve the V8 thread id of the calling thread.
3059 *
3060 * The thread id for a thread should only be retrieved after the V8
3061 * lock has been acquired with a Locker object with that thread.
3062 */
3063 static int GetCurrentThreadId();
3064
3065 /**
3066 * Forcefully terminate execution of a JavaScript thread. This can
3067 * be used to terminate long-running scripts.
3068 *
3069 * TerminateExecution should only be called when then V8 lock has
3070 * been acquired with a Locker object. Therefore, in order to be
3071 * able to terminate long-running threads, preemption must be
3072 * enabled to allow the user of TerminateExecution to acquire the
3073 * lock.
3074 *
3075 * The termination is achieved by throwing an exception that is
3076 * uncatchable by JavaScript exception handlers. Termination
3077 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003078 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003079 * exception handler that catches an exception should check if that
3080 * exception is a termination exception and immediately return if
3081 * that is the case. Returning immediately in that case will
3082 * continue the propagation of the termination exception if needed.
3083 *
3084 * The thread id passed to TerminateExecution must have been
3085 * obtained by calling GetCurrentThreadId on the thread in question.
3086 *
3087 * \param thread_id The thread id of the thread to terminate.
3088 */
3089 static void TerminateExecution(int thread_id);
3090
3091 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003092 * Forcefully terminate the current thread of JavaScript execution
3093 * in the given isolate. If no isolate is provided, the default
3094 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003095 *
3096 * This method can be used by any thread even if that thread has not
3097 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003098 *
3099 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003100 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003101 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003102
3103 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003104 * Is V8 terminating JavaScript execution.
3105 *
3106 * Returns true if JavaScript execution is currently terminating
3107 * because of a call to TerminateExecution. In that case there are
3108 * still JavaScript frames on the stack and the termination
3109 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003110 *
3111 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003112 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003113 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003114
3115 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003116 * Releases any resources used by v8 and stops any utility threads
3117 * that may be running. Note that disposing v8 is permanent, it
3118 * cannot be reinitialized.
3119 *
3120 * It should generally not be necessary to dispose v8 before exiting
3121 * a process, this should happen automatically. It is only necessary
3122 * to use if the process needs the resources taken up by v8.
3123 */
3124 static bool Dispose();
3125
ager@chromium.org3811b432009-10-28 14:53:37 +00003126 /**
3127 * Get statistics about the heap memory usage.
3128 */
3129 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003130
3131 /**
3132 * Optional notification that the embedder is idle.
3133 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003134 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003135 * Returns true if the embedder should stop calling IdleNotification
3136 * until real work has been done. This indicates that V8 has done
3137 * as much cleanup as it will be able to do.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003138 */
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003139 static bool IdleNotification();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003140
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003141 /**
3142 * Optional notification that the system is running low on memory.
3143 * V8 uses these notifications to attempt to free memory.
3144 */
3145 static void LowMemoryNotification();
3146
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003147 /**
3148 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003149 * these notifications to guide the GC heuristic. Returns the number
3150 * of context disposals - including this one - since the last time
3151 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003152 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003153 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003155 private:
3156 V8();
3157
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003158 static internal::Object** GlobalizeReference(internal::Object** handle);
3159 static void DisposeGlobal(internal::Object** global_handle);
3160 static void MakeWeak(internal::Object** global_handle,
3161 void* data,
3162 WeakReferenceCallback);
3163 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003164 static void MarkIndependent(internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003165 static bool IsGlobalNearDeath(internal::Object** global_handle);
3166 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003167 static void SetWrapperClassId(internal::Object** global_handle,
3168 uint16_t class_id);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169
3170 template <class T> friend class Handle;
3171 template <class T> friend class Local;
3172 template <class T> friend class Persistent;
3173 friend class Context;
3174};
3175
3176
3177/**
3178 * An external exception handler.
3179 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003180class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003181 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182 /**
3183 * Creates a new try/catch block and registers it with v8.
3184 */
3185 TryCatch();
3186
3187 /**
3188 * Unregisters and deletes this try/catch block.
3189 */
3190 ~TryCatch();
3191
3192 /**
3193 * Returns true if an exception has been caught by this try/catch block.
3194 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003195 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003196
3197 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003198 * For certain types of exceptions, it makes no sense to continue
3199 * execution.
3200 *
3201 * Currently, the only type of exception that can be caught by a
3202 * TryCatch handler and for which it does not make sense to continue
3203 * is termination exception. Such exceptions are thrown when the
3204 * TerminateExecution methods are called to terminate a long-running
3205 * script.
3206 *
3207 * If CanContinue returns false, the correct action is to perform
3208 * any C++ cleanup needed and then return.
3209 */
3210 bool CanContinue() const;
3211
3212 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003213 * Throws the exception caught by this TryCatch in a way that avoids
3214 * it being caught again by this same TryCatch. As with ThrowException
3215 * it is illegal to execute any JavaScript operations after calling
3216 * ReThrow; the caller must return immediately to where the exception
3217 * is caught.
3218 */
3219 Handle<Value> ReThrow();
3220
3221 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003222 * Returns the exception caught by this try/catch block. If no exception has
3223 * been caught an empty handle is returned.
3224 *
3225 * The returned handle is valid until this TryCatch block has been destroyed.
3226 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003227 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003228
3229 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003230 * Returns the .stack property of the thrown object. If no .stack
3231 * property is present an empty handle is returned.
3232 */
3233 Local<Value> StackTrace() const;
3234
3235 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003236 * Returns the message associated with this exception. If there is
3237 * no message associated an empty handle is returned.
3238 *
3239 * The returned handle is valid until this TryCatch block has been
3240 * destroyed.
3241 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003242 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003243
3244 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003245 * Clears any exceptions that may have been caught by this try/catch block.
3246 * After this method has been called, HasCaught() will return false.
3247 *
3248 * It is not necessary to clear a try/catch block before using it again; if
3249 * another exception is thrown the previously caught exception will just be
3250 * overwritten. However, it is often a good idea since it makes it easier
3251 * to determine which operation threw a given exception.
3252 */
3253 void Reset();
3254
v8.team.kasperl727e9952008-09-02 14:56:44 +00003255 /**
3256 * Set verbosity of the external exception handler.
3257 *
3258 * By default, exceptions that are caught by an external exception
3259 * handler are not reported. Call SetVerbose with true on an
3260 * external exception handler to have exceptions caught by the
3261 * handler reported as if they were not caught.
3262 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263 void SetVerbose(bool value);
3264
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003265 /**
3266 * Set whether or not this TryCatch should capture a Message object
3267 * which holds source information about where the exception
3268 * occurred. True by default.
3269 */
3270 void SetCaptureMessage(bool value);
3271
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003272 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003273 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003274 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003275 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003276 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003277 bool is_verbose_ : 1;
3278 bool can_continue_ : 1;
3279 bool capture_message_ : 1;
3280 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003281
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003282 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003283};
3284
3285
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003286// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287
3288
3289/**
3290 * Ignore
3291 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003292class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 public:
3294 ExtensionConfiguration(int name_count, const char* names[])
3295 : name_count_(name_count), names_(names) { }
3296 private:
3297 friend class ImplementationUtilities;
3298 int name_count_;
3299 const char** names_;
3300};
3301
3302
3303/**
3304 * A sandboxed execution context with its own set of built-in objects
3305 * and functions.
3306 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003307class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003308 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003309 /**
3310 * Returns the global proxy object or global object itself for
3311 * detached contexts.
3312 *
3313 * Global proxy object is a thin wrapper whose prototype points to
3314 * actual context's global object with the properties like Object, etc.
3315 * This is done that way for security reasons (for more details see
3316 * https://wiki.mozilla.org/Gecko:SplitWindow).
3317 *
3318 * Please note that changes to global proxy object prototype most probably
3319 * would break VM---v8 expects only global object as a prototype of
3320 * global proxy object.
3321 *
3322 * If DetachGlobal() has been invoked, Global() would return actual global
3323 * object until global is reattached with ReattachGlobal().
3324 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003325 Local<Object> Global();
3326
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003327 /**
3328 * Detaches the global object from its context before
3329 * the global object can be reused to create a new context.
3330 */
3331 void DetachGlobal();
3332
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003333 /**
3334 * Reattaches a global object to a context. This can be used to
3335 * restore the connection between a global object and a context
3336 * after DetachGlobal has been called.
3337 *
3338 * \param global_object The global object to reattach to the
3339 * context. For this to work, the global object must be the global
3340 * object that was associated with this context before a call to
3341 * DetachGlobal.
3342 */
3343 void ReattachGlobal(Handle<Object> global_object);
3344
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003345 /** Creates a new context.
3346 *
3347 * Returns a persistent handle to the newly allocated context. This
3348 * persistent handle has to be disposed when the context is no
3349 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003350 *
3351 * \param extensions An optional extension configuration containing
3352 * the extensions to be installed in the newly created context.
3353 *
3354 * \param global_template An optional object template from which the
3355 * global object for the newly created context will be created.
3356 *
3357 * \param global_object An optional global object to be reused for
3358 * the newly created context. This global object must have been
3359 * created by a previous call to Context::New with the same global
3360 * template. The state of the global object will be completely reset
3361 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003362 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003363 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003364 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003365 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3366 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003367
kasper.lund44510672008-07-25 07:37:58 +00003368 /** Returns the last entered context. */
3369 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370
kasper.lund44510672008-07-25 07:37:58 +00003371 /** Returns the context that is on the top of the stack. */
3372 static Local<Context> GetCurrent();
3373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003374 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003375 * Returns the context of the calling JavaScript code. That is the
3376 * context of the top-most JavaScript frame. If there are no
3377 * JavaScript frames an empty handle is returned.
3378 */
3379 static Local<Context> GetCalling();
3380
3381 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382 * Sets the security token for the context. To access an object in
3383 * another context, the security tokens must match.
3384 */
3385 void SetSecurityToken(Handle<Value> token);
3386
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003387 /** Restores the security token to the default value. */
3388 void UseDefaultSecurityToken();
3389
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390 /** Returns the security token of this context.*/
3391 Handle<Value> GetSecurityToken();
3392
v8.team.kasperl727e9952008-09-02 14:56:44 +00003393 /**
3394 * Enter this context. After entering a context, all code compiled
3395 * and run is compiled and run in this context. If another context
3396 * is already entered, this old context is saved so it can be
3397 * restored when the new context is exited.
3398 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003399 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003400
3401 /**
3402 * Exit this context. Exiting the current context restores the
3403 * context that was in place when entering the current context.
3404 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 void Exit();
3406
v8.team.kasperl727e9952008-09-02 14:56:44 +00003407 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003408 bool HasOutOfMemoryException();
3409
v8.team.kasperl727e9952008-09-02 14:56:44 +00003410 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411 static bool InContext();
3412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003413 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00003414 * Associate an additional data object with the context. This is mainly used
3415 * with the debugger to provide additional information on the context through
3416 * the debugger API.
3417 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00003418 void SetData(Handle<String> data);
ager@chromium.org9085a012009-05-11 19:22:57 +00003419 Local<Value> GetData();
3420
3421 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003422 * Control whether code generation from strings is allowed. Calling
3423 * this method with false will disable 'eval' and the 'Function'
3424 * constructor for code running in this context. If 'eval' or the
3425 * 'Function' constructor are used an exception will be thrown.
3426 *
3427 * If code generation from strings is not allowed the
3428 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3429 * set before blocking the call to 'eval' or the 'Function'
3430 * constructor. If that callback returns true, the call will be
3431 * allowed, otherwise an exception will be thrown. If no callback is
3432 * set an exception will be thrown.
3433 */
3434 void AllowCodeGenerationFromStrings(bool allow);
3435
3436 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003437 * Stack-allocated class which sets the execution context for all
3438 * operations executed within a local scope.
3439 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003440 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003441 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003442 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443 context_->Enter();
3444 }
3445 inline ~Scope() { context_->Exit(); }
3446 private:
3447 Handle<Context> context_;
3448 };
3449
3450 private:
3451 friend class Value;
3452 friend class Script;
3453 friend class Object;
3454 friend class Function;
3455};
3456
3457
3458/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003459 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003460 * is allowed to use any given V8 isolate. See Isolate class
3461 * comments. The definition of 'using V8 isolate' includes
3462 * accessing handles or holding onto object pointers obtained
3463 * from V8 handles while in the particular V8 isolate. It is up
3464 * to the user of V8 to ensure (perhaps with locking) that this
3465 * constraint is not violated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003466 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003467 * v8::Locker is a scoped lock object. While it's
3468 * active (i.e. between its construction and destruction) the current thread is
3469 * allowed to use the locked isolate. V8 guarantees that an isolate can be locked
3470 * by at most one thread at any time. In other words, the scope of a v8::Locker is
3471 * a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003472 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003473 * Sample usage:
3474* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003475 * ...
3476 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003477 * v8::Locker locker(isolate);
3478 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003479 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003480 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481 * ...
3482 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003483 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003485 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003486 * by destroying the v8::Locker object as above or by constructing a
3487 * v8::Unlocker object:
3488 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003489 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003490 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003491 * isolate->Exit();
3492 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493 * ...
3494 * // Code not using V8 goes here while V8 can run in another thread.
3495 * ...
3496 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003497 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003498 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003499 *
3500 * The Unlocker object is intended for use in a long-running callback
3501 * from V8, where you want to release the V8 lock for other threads to
3502 * use.
3503 *
3504 * The v8::Locker is a recursive lock. That is, you can lock more than
3505 * once in a given thread. This can be useful if you have code that can
3506 * be called either from code that holds the lock or from code that does
3507 * not. The Unlocker is not recursive so you can not have several
3508 * Unlockers on the stack at once, and you can not use an Unlocker in a
3509 * thread that is not inside a Locker's scope.
3510 *
3511 * An unlocker will unlock several lockers if it has to and reinstate
3512 * the correct depth of locking on its destruction. eg.:
3513 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003514 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003515 * // V8 not locked.
3516 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003517 * v8::Locker locker(isolate);
3518 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003519 * // V8 locked.
3520 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003521 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003522 * // V8 still locked (2 levels).
3523 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003524 * isolate->Exit();
3525 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526 * // V8 not locked.
3527 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003528 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529 * // V8 locked again (2 levels).
3530 * }
3531 * // V8 still locked (1 level).
3532 * }
3533 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003534 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003535 *
3536 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003538class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003540 /**
3541 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3542 */
3543 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003544 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003545 private:
3546 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003547};
3548
3549
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003550class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003552 /**
3553 * Initialize Locker for a given Isolate. NULL means default isolate.
3554 */
3555 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003557
3558 /**
3559 * Start preemption.
3560 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003561 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003562 * that will switch between multiple threads that are in contention
3563 * for the V8 lock.
3564 */
3565 static void StartPreemption(int every_n_ms);
3566
3567 /**
3568 * Stop preemption.
3569 */
3570 static void StopPreemption();
3571
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003572 /**
lrn@chromium.org1c092762011-05-09 09:42:16 +00003573 * Returns whether or not the locker for a given isolate, or default isolate if NULL is given,
3574 * is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003575 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00003576 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003577
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003578 /**
3579 * Returns whether v8::Locker is being used by this V8 instance.
3580 */
3581 static bool IsActive() { return active_; }
3582
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583 private:
3584 bool has_lock_;
3585 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003586 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003587
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003588 static bool active_;
3589
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003590 // Disallow copying and assigning.
3591 Locker(const Locker&);
3592 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593};
3594
3595
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003596/**
3597 * An interface for exporting data from V8, using "push" model.
3598 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003599class V8EXPORT OutputStream { // NOLINT
3600 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003601 enum OutputEncoding {
3602 kAscii = 0 // 7-bit ASCII.
3603 };
3604 enum WriteResult {
3605 kContinue = 0,
3606 kAbort = 1
3607 };
3608 virtual ~OutputStream() {}
3609 /** Notify about the end of stream. */
3610 virtual void EndOfStream() = 0;
3611 /** Get preferred output chunk size. Called only once. */
3612 virtual int GetChunkSize() { return 1024; }
3613 /** Get preferred output encoding. Called only once. */
3614 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
3615 /**
3616 * Writes the next chunk of snapshot data into the stream. Writing
3617 * can be stopped by returning kAbort as function result. EndOfStream
3618 * will not be called in case writing was aborted.
3619 */
3620 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
3621};
3622
3623
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003624/**
3625 * An interface for reporting progress and controlling long-running
3626 * activities.
3627 */
3628class V8EXPORT ActivityControl { // NOLINT
3629 public:
3630 enum ControlOption {
3631 kContinue = 0,
3632 kAbort = 1
3633 };
3634 virtual ~ActivityControl() {}
3635 /**
3636 * Notify about current progress. The activity can be stopped by
3637 * returning kAbort as the callback result.
3638 */
3639 virtual ControlOption ReportProgressValue(int done, int total) = 0;
3640};
3641
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003642
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003643// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003645
3646namespace internal {
3647
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003648static const int kApiPointerSize = sizeof(void*); // NOLINT
3649static const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003650
3651// Tag information for HeapObject.
3652const int kHeapObjectTag = 1;
3653const int kHeapObjectTagSize = 2;
3654const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3655
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003656// Tag information for Smi.
3657const int kSmiTag = 0;
3658const int kSmiTagSize = 1;
3659const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3660
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003661template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003662
3663// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003664template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003665 static const int kSmiShiftSize = 0;
3666 static const int kSmiValueSize = 31;
3667 static inline int SmiToInt(internal::Object* value) {
3668 int shift_bits = kSmiTagSize + kSmiShiftSize;
3669 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3670 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3671 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003672
3673 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3674 // with a plain reinterpret_cast.
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003675 static const uintptr_t kEncodablePointerMask = 0x1;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003676 static const int kPointerToSmiShift = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003677};
3678
3679// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003680template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003681 static const int kSmiShiftSize = 31;
3682 static const int kSmiValueSize = 32;
3683 static inline int SmiToInt(internal::Object* value) {
3684 int shift_bits = kSmiTagSize + kSmiShiftSize;
3685 // Shift down and throw away top 32 bits.
3686 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3687 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003688
3689 // To maximize the range of pointers that can be encoded
3690 // in the available 32 bits, we require them to be 8 bytes aligned.
3691 // This gives 2 ^ (32 + 3) = 32G address space covered.
3692 // It might be not enough to cover stack allocated objects on some platforms.
3693 static const int kPointerAlignment = 3;
3694
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003695 static const uintptr_t kEncodablePointerMask =
3696 ~(uintptr_t(0xffffffff) << kPointerAlignment);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003697
3698 static const int kPointerToSmiShift =
3699 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003700};
3701
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003702typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
3703const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
3704const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003705const uintptr_t kEncodablePointerMask =
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003706 PlatformSmiTagging::kEncodablePointerMask;
3707const int kPointerToSmiShift = PlatformSmiTagging::kPointerToSmiShift;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003708
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003709template <size_t ptr_size> struct InternalConstants;
3710
3711// Internal constants for 32-bit systems.
3712template <> struct InternalConstants<4> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003713 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003714};
3715
3716// Internal constants for 64-bit systems.
3717template <> struct InternalConstants<8> {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003718 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003719};
3720
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003721/**
3722 * This class exports constants and functionality from within v8 that
3723 * is necessary to implement inline functions in the v8 api. Don't
3724 * depend on functions and constants defined here.
3725 */
3726class Internals {
3727 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003728 // These values match non-compiler-dependent values defined within
3729 // the implementation of v8.
3730 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003731 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003732 static const int kStringResourceOffset =
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003733 InternalConstants<kApiPointerSize>::kStringResourceOffset;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003734
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003735 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003736 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003737 static const int kFullStringRepresentationMask = 0x07;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00003738 static const int kExternalTwoByteRepresentationTag = 0x02;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003739
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003740 static const int kJSObjectType = 0xa3;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00003741 static const int kFirstNonstringType = 0x80;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003742 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003743
3744 static inline bool HasHeapObjectTag(internal::Object* value) {
3745 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3746 kHeapObjectTag);
3747 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003748
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003749 static inline bool HasSmiTag(internal::Object* value) {
3750 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3751 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003752
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003753 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003754 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003755 }
3756
3757 static inline int GetInstanceType(internal::Object* obj) {
3758 typedef internal::Object O;
3759 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3760 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3761 }
3762
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003763 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00003764 const uintptr_t address = reinterpret_cast<uintptr_t>(value);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003765 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3766 }
3767
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003768 static inline void* GetExternalPointer(internal::Object* obj) {
3769 if (HasSmiTag(obj)) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003770 return GetExternalPointerFromSmi(obj);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00003771 } else if (GetInstanceType(obj) == kForeignType) {
3772 return ReadField<void*>(obj, kForeignAddressOffset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003773 } else {
3774 return NULL;
3775 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003776 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003777
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003778 static inline bool IsExternalTwoByteString(int instance_type) {
3779 int representation = (instance_type & kFullStringRepresentationMask);
3780 return representation == kExternalTwoByteRepresentationTag;
3781 }
3782
3783 template <typename T>
3784 static inline T ReadField(Object* ptr, int offset) {
3785 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
3786 return *reinterpret_cast<T*>(addr);
3787 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003788
3789 static inline bool CanCastToHeapObject(void* o) { return false; }
3790 static inline bool CanCastToHeapObject(Context* o) { return true; }
3791 static inline bool CanCastToHeapObject(String* o) { return true; }
3792 static inline bool CanCastToHeapObject(Object* o) { return true; }
3793 static inline bool CanCastToHeapObject(Message* o) { return true; }
3794 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
3795 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003796};
3797
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00003798} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003799
3800
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003801template <class T>
3802Handle<T>::Handle() : val_(0) { }
3803
3804
3805template <class T>
3806Local<T>::Local() : Handle<T>() { }
3807
3808
3809template <class T>
3810Local<T> Local<T>::New(Handle<T> that) {
3811 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003812 T* that_ptr = *that;
3813 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
3814 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
3815 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
3816 reinterpret_cast<internal::HeapObject*>(*p))));
3817 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003818 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
3819}
3820
3821
3822template <class T>
3823Persistent<T> Persistent<T>::New(Handle<T> that) {
3824 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003825 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003826 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
3827}
3828
3829
3830template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003831bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003833 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834}
3835
3836
3837template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00003838bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003839 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003840 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841}
3842
3843
3844template <class T>
3845void Persistent<T>::Dispose() {
3846 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003847 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003848}
3849
3850
3851template <class T>
3852Persistent<T>::Persistent() : Handle<T>() { }
3853
3854template <class T>
3855void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003856 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
3857 parameters,
3858 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003859}
3860
3861template <class T>
3862void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003863 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003864}
3865
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003866template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003867void Persistent<T>::MarkIndependent() {
3868 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
3869}
3870
3871template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003872void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
3873 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
3874}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003875
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003876Arguments::Arguments(internal::Object** implicit_args,
3877 internal::Object** values, int length,
3878 bool is_construct_call)
3879 : implicit_args_(implicit_args),
3880 values_(values),
3881 length_(length),
3882 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003883
3884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003885Local<Value> Arguments::operator[](int i) const {
3886 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
3887 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
3888}
3889
3890
3891Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003892 return Local<Function>(reinterpret_cast<Function*>(
3893 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003894}
3895
3896
3897Local<Object> Arguments::This() const {
3898 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
3899}
3900
3901
3902Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003903 return Local<Object>(reinterpret_cast<Object*>(
3904 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003905}
3906
3907
3908Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00003909 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003910}
3911
3912
3913bool Arguments::IsConstructCall() const {
3914 return is_construct_call_;
3915}
3916
3917
3918int Arguments::Length() const {
3919 return length_;
3920}
3921
3922
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923template <class T>
3924Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003925 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
3926 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927 return Local<T>(reinterpret_cast<T*>(after));
3928}
3929
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003930Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 return resource_name_;
3932}
3933
3934
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003935Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 return resource_line_offset_;
3937}
3938
3939
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003940Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941 return resource_column_offset_;
3942}
3943
3944
3945Handle<Boolean> Boolean::New(bool value) {
3946 return value ? True() : False();
3947}
3948
3949
3950void Template::Set(const char* name, v8::Handle<Data> value) {
3951 Set(v8::String::New(name), value);
3952}
3953
3954
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003955Local<Value> Object::GetInternalField(int index) {
3956#ifndef V8_ENABLE_CHECKS
3957 Local<Value> quick_result = UncheckedGetInternalField(index);
3958 if (!quick_result.IsEmpty()) return quick_result;
3959#endif
3960 return CheckedGetInternalField(index);
3961}
3962
3963
3964Local<Value> Object::UncheckedGetInternalField(int index) {
3965 typedef internal::Object O;
3966 typedef internal::Internals I;
3967 O* obj = *reinterpret_cast<O**>(this);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003968 if (I::GetInstanceType(obj) == I::kJSObjectType) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003969 // If the object is a plain JSObject, which is the common case,
3970 // we know where to find the internal fields and can return the
3971 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00003972 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003973 O* value = I::ReadField<O*>(obj, offset);
3974 O** result = HandleScope::CreateHandle(value);
3975 return Local<Value>(reinterpret_cast<Value*>(result));
3976 } else {
3977 return Local<Value>();
3978 }
3979}
3980
3981
3982void* External::Unwrap(Handle<v8::Value> obj) {
3983#ifdef V8_ENABLE_CHECKS
3984 return FullUnwrap(obj);
3985#else
3986 return QuickUnwrap(obj);
3987#endif
3988}
3989
3990
3991void* External::QuickUnwrap(Handle<v8::Value> wrapper) {
3992 typedef internal::Object O;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003993 O* obj = *reinterpret_cast<O**>(const_cast<v8::Value*>(*wrapper));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003994 return internal::Internals::GetExternalPointer(obj);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003995}
3996
3997
3998void* Object::GetPointerFromInternalField(int index) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003999 typedef internal::Object O;
4000 typedef internal::Internals I;
4001
4002 O* obj = *reinterpret_cast<O**>(this);
4003
4004 if (I::GetInstanceType(obj) == I::kJSObjectType) {
4005 // If the object is a plain JSObject, which is the common case,
4006 // we know where to find the internal fields and can return the
4007 // value directly.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004008 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004009 O* value = I::ReadField<O*>(obj, offset);
4010 return I::GetExternalPointer(value);
4011 }
4012
4013 return SlowGetPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004014}
4015
4016
4017String* String::Cast(v8::Value* value) {
4018#ifdef V8_ENABLE_CHECKS
4019 CheckCast(value);
4020#endif
4021 return static_cast<String*>(value);
4022}
4023
4024
4025String::ExternalStringResource* String::GetExternalStringResource() const {
4026 typedef internal::Object O;
4027 typedef internal::Internals I;
4028 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004029 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004030 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004031 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4032 result = reinterpret_cast<String::ExternalStringResource*>(value);
4033 } else {
4034 result = NULL;
4035 }
4036#ifdef V8_ENABLE_CHECKS
4037 VerifyExternalStringResource(result);
4038#endif
4039 return result;
4040}
4041
4042
4043bool Value::IsString() const {
4044#ifdef V8_ENABLE_CHECKS
4045 return FullIsString();
4046#else
4047 return QuickIsString();
4048#endif
4049}
4050
4051bool Value::QuickIsString() const {
4052 typedef internal::Object O;
4053 typedef internal::Internals I;
4054 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4055 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004056 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004057}
4058
4059
4060Number* Number::Cast(v8::Value* value) {
4061#ifdef V8_ENABLE_CHECKS
4062 CheckCast(value);
4063#endif
4064 return static_cast<Number*>(value);
4065}
4066
4067
4068Integer* Integer::Cast(v8::Value* value) {
4069#ifdef V8_ENABLE_CHECKS
4070 CheckCast(value);
4071#endif
4072 return static_cast<Integer*>(value);
4073}
4074
4075
4076Date* Date::Cast(v8::Value* value) {
4077#ifdef V8_ENABLE_CHECKS
4078 CheckCast(value);
4079#endif
4080 return static_cast<Date*>(value);
4081}
4082
4083
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004084RegExp* RegExp::Cast(v8::Value* value) {
4085#ifdef V8_ENABLE_CHECKS
4086 CheckCast(value);
4087#endif
4088 return static_cast<RegExp*>(value);
4089}
4090
4091
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004092Object* Object::Cast(v8::Value* value) {
4093#ifdef V8_ENABLE_CHECKS
4094 CheckCast(value);
4095#endif
4096 return static_cast<Object*>(value);
4097}
4098
4099
4100Array* Array::Cast(v8::Value* value) {
4101#ifdef V8_ENABLE_CHECKS
4102 CheckCast(value);
4103#endif
4104 return static_cast<Array*>(value);
4105}
4106
4107
4108Function* Function::Cast(v8::Value* value) {
4109#ifdef V8_ENABLE_CHECKS
4110 CheckCast(value);
4111#endif
4112 return static_cast<Function*>(value);
4113}
4114
4115
4116External* External::Cast(v8::Value* value) {
4117#ifdef V8_ENABLE_CHECKS
4118 CheckCast(value);
4119#endif
4120 return static_cast<External*>(value);
4121}
4122
4123
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004124Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004125 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004126}
4127
4128
4129Local<Object> AccessorInfo::This() const {
4130 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4131}
4132
4133
4134Local<Object> AccessorInfo::Holder() const {
4135 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4136}
4137
4138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004140 * \example shell.cc
4141 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004142 * command-line and executes them.
4143 */
4144
4145
4146/**
4147 * \example process.cc
4148 */
4149
4150
4151} // namespace v8
4152
4153
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004154#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004155#undef TYPE_CHECK
4156
4157
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004158#endif // V8_H_