blob: 74f489d2ab7870bba38e7bb46dfd10b1bba93401 [file] [log] [blame]
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001// Copyright 2012 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
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000065// Setup for Linux shared library export.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000066#if defined(__GNUC__) && ((__GNUC__ >= 4) || \
67 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED)
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000068#ifdef BUILDING_V8_SHARED
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000069#define V8EXPORT __attribute__ ((visibility("default")))
ulan@chromium.orgd6899c32012-05-18 14:12:25 +000070#else
71#define V8EXPORT
72#endif
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000073#else
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000074#define V8EXPORT
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +000075#endif
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000076
77#endif // _WIN32
78
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000079#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000080#define V8_DEPRECATED(func) func __attribute__ ((deprecated))
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000081#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000082#define V8_DEPRECATED(func) __declspec(deprecated) func
83#else
84#define V8_DEPRECATED(func) func
85#endif
86
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000088 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 */
90namespace v8 {
91
92class Context;
93class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000094class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095class Value;
96class Utils;
97class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000098class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099class Object;
100class Array;
101class Int32;
102class Uint32;
103class External;
104class Primitive;
105class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000106class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107class Integer;
108class Function;
109class Date;
110class ImplementationUtilities;
111class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000112class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113template <class T> class Handle;
114template <class T> class Local;
115template <class T> class Persistent;
116class FunctionTemplate;
117class ObjectTemplate;
118class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000119class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000120class StackTrace;
121class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000122class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000124namespace internal {
125
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000126class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000127class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000128class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000129class HeapObject;
130class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000131}
132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000134// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000135
136
137/**
138 * A weak reference callback function.
139 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000140 * This callback should either explicitly invoke Dispose on |object| if
141 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
142 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 * \param object the weak global object to be reclaimed by the garbage collector
144 * \param parameter the value passed in when making the weak global object
145 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000146typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 void* parameter);
148
149
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000150// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000152#define TYPE_CHECK(T, S) \
153 while (false) { \
154 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 }
156
157/**
158 * An object reference managed by the v8 garbage collector.
159 *
160 * All objects returned from v8 have to be tracked by the garbage
161 * collector so that it knows that the objects are still alive. Also,
162 * because the garbage collector may move objects, it is unsafe to
163 * point directly to an object. Instead, all objects are stored in
164 * handles which are known by the garbage collector and updated
165 * whenever an object moves. Handles should always be passed by value
166 * (except in cases like out-parameters) and they should never be
167 * allocated on the heap.
168 *
169 * There are two types of handles: local and persistent handles.
170 * Local handles are light-weight and transient and typically used in
171 * local operations. They are managed by HandleScopes. Persistent
172 * handles can be used when storing objects across several independent
173 * operations and have to be explicitly deallocated when they're no
174 * longer used.
175 *
176 * It is safe to extract the object stored in the handle by
177 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000178 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 * behind the scenes and the same rules apply to these values as to
180 * their handles.
181 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000182template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 /**
185 * Creates an empty handle.
186 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000187 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
189 /**
190 * Creates a new handle for the specified value.
191 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000192 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193
194 /**
195 * Creates a handle for the contents of the specified handle. This
196 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000197 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000199 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000200 * between compatible handles, for instance assigning a
201 * Handle<String> to a variable declared as Handle<Value>, is legal
202 * because String is a subclass of Value.
203 */
204 template <class S> inline Handle(Handle<S> that)
205 : val_(reinterpret_cast<T*>(*that)) {
206 /**
207 * This check fails when trying to convert between incompatible
208 * handles. For example, converting from a Handle<String> to a
209 * Handle<Number>.
210 */
211 TYPE_CHECK(T, S);
212 }
213
214 /**
215 * Returns true if the handle is empty.
216 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000217 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 /**
220 * Sets the handle to be empty. IsEmpty() will then return true.
221 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000222 inline void Clear() { val_ = 0; }
223
224 inline T* operator->() const { return val_; }
225
226 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227
228 /**
229 * Checks whether two handles are the same.
230 * Returns true if both are empty, or if the objects
231 * to which they refer are identical.
232 * The handles' references are not checked.
233 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000234 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000235 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
236 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 if (a == 0) return b == 0;
238 if (b == 0) return false;
239 return *a == *b;
240 }
241
242 /**
243 * Checks whether two handles are different.
244 * Returns true if only one of the handles is empty, or if
245 * the objects to which they refer are different.
246 * The handles' references are not checked.
247 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000248 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 return !operator==(that);
250 }
251
252 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000253#ifdef V8_ENABLE_CHECKS
254 // If we're going to perform the type check then we have to check
255 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000257#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 return Handle<T>(T::Cast(*that));
259 }
260
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000261 template <class S> inline Handle<S> As() {
262 return Handle<S>::Cast(*this);
263 }
264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 private:
266 T* val_;
267};
268
269
270/**
271 * A light-weight stack-allocated object handle. All operations
272 * that return objects from within v8 return them in local handles. They
273 * are created within HandleScopes, and all local handles allocated within a
274 * handle scope are destroyed when the handle scope is destroyed. Hence it
275 * is not necessary to explicitly deallocate local handles.
276 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000277template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000279 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 template <class S> inline Local(Local<S> that)
281 : Handle<T>(reinterpret_cast<T*>(*that)) {
282 /**
283 * This check fails when trying to convert between incompatible
284 * handles. For example, converting from a Handle<String> to a
285 * Handle<Number>.
286 */
287 TYPE_CHECK(T, S);
288 }
289 template <class S> inline Local(S* that) : Handle<T>(that) { }
290 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000291#ifdef V8_ENABLE_CHECKS
292 // If we're going to perform the type check then we have to check
293 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000295#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 return Local<T>(T::Cast(*that));
297 }
298
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000299 template <class S> inline Local<S> As() {
300 return Local<S>::Cast(*this);
301 }
302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 /** Create a local handle for the content of another handle.
304 * The referee is kept alive by the local handle even when
305 * the original handle is destroyed/disposed.
306 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000307 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308};
309
310
311/**
312 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000313 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 * allocated, a Persistent handle remains valid until it is explicitly
315 * disposed.
316 *
317 * A persistent handle contains a reference to a storage cell within
318 * the v8 engine which holds an object value and which is updated by
319 * the garbage collector whenever the object is moved. A new storage
320 * cell can be created using Persistent::New and existing handles can
321 * be disposed using Persistent::Dispose. Since persistent handles
322 * are passed by value you may have many persistent handle objects
323 * that point to the same storage cell. For instance, if you pass a
324 * persistent handle as an argument to a function you will not get two
325 * different storage cells but rather two references to the same
326 * storage cell.
327 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000328template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 /**
331 * Creates an empty persistent handle that doesn't point to any
332 * storage cell.
333 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000334 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335
336 /**
337 * Creates a persistent handle for the same storage cell as the
338 * specified handle. This constructor allows you to pass persistent
339 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000340 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000342 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 * between compatible persistent handles, for instance assigning a
344 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000345 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 */
347 template <class S> inline Persistent(Persistent<S> that)
348 : Handle<T>(reinterpret_cast<T*>(*that)) {
349 /**
350 * This check fails when trying to convert between incompatible
351 * handles. For example, converting from a Handle<String> to a
352 * Handle<Number>.
353 */
354 TYPE_CHECK(T, S);
355 }
356
357 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
358
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000359 /**
360 * "Casts" a plain handle which is known to be a persistent handle
361 * to a persistent handle.
362 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000363 template <class S> explicit inline Persistent(Handle<S> that)
364 : Handle<T>(*that) { }
365
366 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000367#ifdef V8_ENABLE_CHECKS
368 // If we're going to perform the type check then we have to check
369 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000371#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372 return Persistent<T>(T::Cast(*that));
373 }
374
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000375 template <class S> inline Persistent<S> As() {
376 return Persistent<S>::Cast(*this);
377 }
378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000380 * Creates a new persistent handle for an existing local or
381 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000383 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
385 /**
386 * Releases the storage cell referenced by this persistent handle.
387 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000388 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389 * cell remain and IsEmpty will still return false.
390 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000391 inline void Dispose();
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +0000392 inline void Dispose(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
394 /**
395 * Make the reference to this object weak. When only weak handles
396 * refer to the object, the garbage collector will perform a
397 * callback to the given V8::WeakReferenceCallback function, passing
398 * it the object reference and the given parameters.
399 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000400 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000402 /** Clears the weak reference to this object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000403 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
405 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000406 * Marks the reference to this object independent. Garbage collector
407 * is free to ignore any object groups containing this object.
408 * Weak callback for an independent handle should not
409 * assume that it will be preceded by a global GC prologue callback
410 * or followed by a global GC epilogue callback.
411 */
412 inline void MarkIndependent();
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000413 inline void MarkIndependent(Isolate* isolate);
414
415 /**
416 * Marks the reference to this object partially dependent. Partially
417 * dependent handles only depend on other partially dependent handles and
418 * these dependencies are provided through object groups. It provides a way
419 * to build smaller object groups for young objects that represent only a
420 * subset of all external dependencies. This mark is automatically cleared
421 * after each garbage collection.
422 */
423 inline void MarkPartiallyDependent();
424 inline void MarkPartiallyDependent(Isolate* isolate);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000425
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000426 /** Returns true if this handle was previously marked as independent. */
427 inline bool IsIndependent() const;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +0000428 inline bool IsIndependent(Isolate* isolate) const;
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000429
430 /** Checks if the handle holds the only reference to an object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000431 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000433 /** Returns true if the handle's reference is weak. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000434 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000436 /**
437 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
438 * interface description in v8-profiler.h for details.
439 */
440 inline void SetWrapperClassId(uint16_t class_id);
441
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000442 /**
443 * Returns the class ID previously assigned to this handle or 0 if no class
444 * ID was previously assigned.
445 */
446 inline uint16_t WrapperClassId() const;
447
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448 private:
449 friend class ImplementationUtilities;
450 friend class ObjectTemplate;
451};
452
453
v8.team.kasperl727e9952008-09-02 14:56:44 +0000454 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 * A stack-allocated class that governs a number of local handles.
456 * After a handle scope has been created, all local handles will be
457 * allocated within that handle scope until either the handle scope is
458 * deleted or another handle scope is created. If there is already a
459 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000460 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 * new handles will again be allocated in the original handle scope.
462 *
463 * After the handle scope of a local handle has been deleted the
464 * garbage collector will no longer track the object stored in the
465 * handle and may deallocate it. The behavior of accessing a handle
466 * for which the handle scope has been deleted is undefined.
467 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000468class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000470 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000472 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
474 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 * Closes the handle scope and returns the value as a handle in the
476 * previous scope, which is the new current scope after the call.
477 */
478 template <class T> Local<T> Close(Handle<T> value);
479
480 /**
481 * Counts the number of allocated handles.
482 */
483 static int NumberOfHandles();
484
485 /**
486 * Creates a new handle with the given value.
487 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000488 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000489 // Faster version, uses HeapObject to obtain the current Isolate.
490 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
492 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +0000493 // Make it hard to create heap-allocated or illegal handle scopes by
494 // disallowing certain operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 HandleScope(const HandleScope&);
496 void operator=(const HandleScope&);
497 void* operator new(size_t size);
498 void operator delete(void*, size_t);
499
ager@chromium.org3811b432009-10-28 14:53:37 +0000500 // This Data class is accessible internally as HandleScopeData through a
501 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000502 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000504 internal::Object** next;
505 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000506 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000509 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 }
511 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000512
lrn@chromium.org303ada72010-10-27 09:33:13 +0000513 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000516 internal::Object** prev_next_;
517 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000519 // Allow for the active closing of HandleScopes which allows to pass a handle
520 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000522 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000523
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 friend class ImplementationUtilities;
525};
526
527
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000528// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529
530
531/**
532 * The superclass of values and API object templates.
533 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000534class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000535 private:
536 Data();
537};
538
539
540/**
541 * Pre-compilation data that can be associated with a script. This
542 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000543 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000544 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000546class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 public:
548 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000549
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000550 /**
551 * Pre-compiles the specified script (context-independent).
552 *
553 * \param input Pointer to UTF-8 script source code.
554 * \param length Length of UTF-8 script source code.
555 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000558 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000559 * Pre-compiles the specified script (context-independent).
560 *
561 * NOTE: Pre-compilation using this method cannot happen on another thread
562 * without using Lockers.
563 *
564 * \param source Script source code.
565 */
566 static ScriptData* PreCompile(Handle<String> source);
567
568 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000569 * Load previous pre-compilation data.
570 *
571 * \param data Pointer to data returned by a call to Data() of a previous
572 * ScriptData. Ownership is not transferred.
573 * \param length Length of data.
574 */
575 static ScriptData* New(const char* data, int length);
576
577 /**
578 * Returns the length of Data().
579 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000581
582 /**
583 * Returns a serialized representation of this ScriptData that can later be
584 * passed to New(). NOTE: Serialized data is platform-dependent.
585 */
586 virtual const char* Data() = 0;
587
588 /**
589 * Returns true if the source code could not be parsed.
590 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000591 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592};
593
594
595/**
596 * The origin, within a file, of a script.
597 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000598class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000600 inline ScriptOrigin(
601 Handle<Value> resource_name,
602 Handle<Integer> resource_line_offset = Handle<Integer>(),
603 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 : resource_name_(resource_name),
605 resource_line_offset_(resource_line_offset),
606 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000607 inline Handle<Value> ResourceName() const;
608 inline Handle<Integer> ResourceLineOffset() const;
609 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000611 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612 Handle<Integer> resource_line_offset_;
613 Handle<Integer> resource_column_offset_;
614};
615
616
617/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000618 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000620class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000623 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000624 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000626 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000627 * when New() returns
628 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
629 * using pre_data speeds compilation if it's done multiple times.
630 * Owned by caller, no references are kept when New() returns.
631 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000632 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000633 * available to compile event handlers.
634 * \return Compiled script object (context independent; when run it
635 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000637 static Local<Script> New(Handle<String> source,
638 ScriptOrigin* origin = NULL,
639 ScriptData* pre_data = NULL,
640 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
mads.s.agercbaa0602008-08-14 13:41:48 +0000642 /**
643 * Compiles the specified script using the specified file name
644 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000645 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000646 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000647 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000648 * as the script's origin.
649 * \return Compiled script object (context independent; when run it
650 * will use the currently entered context).
651 */
652 static Local<Script> New(Handle<String> source,
653 Handle<Value> file_name);
654
655 /**
656 * Compiles the specified script (bound to current context).
657 *
658 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000659 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000660 * when Compile() returns
661 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
662 * using pre_data speeds compilation if it's done multiple times.
663 * Owned by caller, no references are kept when Compile() returns.
664 * \param script_data Arbitrary data associated with script. Using
665 * this has same effect as calling SetData(), but makes data available
666 * earlier (i.e. to compile event handlers).
667 * \return Compiled script object, bound to the context that was active
668 * when this function was called. When run it will always use this
669 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000670 */
671 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000672 ScriptOrigin* origin = NULL,
673 ScriptData* pre_data = NULL,
674 Handle<String> script_data = Handle<String>());
675
676 /**
677 * Compiles the specified script using the specified file name
678 * object (typically a string) as the script's origin.
679 *
680 * \param source Script source code.
681 * \param file_name File name to use as script's origin
682 * \param script_data Arbitrary data associated with script. Using
683 * this has same effect as calling SetData(), but makes data available
684 * earlier (i.e. to compile event handlers).
685 * \return Compiled script object, bound to the context that was active
686 * when this function was called. When run it will always use this
687 * context.
688 */
689 static Local<Script> Compile(Handle<String> source,
690 Handle<Value> file_name,
691 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000692
v8.team.kasperl727e9952008-09-02 14:56:44 +0000693 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000694 * Runs the script returning the resulting value. If the script is
695 * context independent (created using ::New) it will be run in the
696 * currently entered context. If it is context specific (created
697 * using ::Compile) it will be run in the context in which it was
698 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000699 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000701
702 /**
703 * Returns the script id value.
704 */
705 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000706
707 /**
708 * Associate an additional data object with the script. This is mainly used
709 * with the debugger as this data object is only available through the
710 * debugger API.
711 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000712 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713};
714
715
716/**
717 * An error message.
718 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000719class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000721 Local<String> Get() const;
722 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000724 /**
725 * Returns the resource name for the script from where the function causing
726 * the error originates.
727 */
ager@chromium.org32912102009-01-16 10:38:43 +0000728 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000729
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000730 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000731 * Returns the resource data for the script from where the function causing
732 * the error originates.
733 */
734 Handle<Value> GetScriptData() const;
735
736 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000737 * Exception stack trace. By default stack traces are not captured for
738 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
739 * to change this option.
740 */
741 Handle<StackTrace> GetStackTrace() const;
742
743 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000744 * Returns the number, 1-based, of the line where the error occurred.
745 */
ager@chromium.org32912102009-01-16 10:38:43 +0000746 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000748 /**
749 * Returns the index within the script of the first character where
750 * the error occurred.
751 */
ager@chromium.org32912102009-01-16 10:38:43 +0000752 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000753
754 /**
755 * Returns the index within the script of the last character where
756 * the error occurred.
757 */
ager@chromium.org32912102009-01-16 10:38:43 +0000758 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000759
760 /**
761 * Returns the index within the line of the first character where
762 * the error occurred.
763 */
ager@chromium.org32912102009-01-16 10:38:43 +0000764 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000765
766 /**
767 * Returns the index within the line of the last character where
768 * the error occurred.
769 */
ager@chromium.org32912102009-01-16 10:38:43 +0000770 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 // TODO(1245381): Print to a string instead of on a FILE.
773 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000774
775 static const int kNoLineNumberInfo = 0;
776 static const int kNoColumnInfo = 0;
777};
778
779
780/**
781 * Representation of a JavaScript stack trace. The information collected is a
782 * snapshot of the execution stack and the information remains valid after
783 * execution continues.
784 */
785class V8EXPORT StackTrace {
786 public:
787 /**
788 * Flags that determine what information is placed captured for each
789 * StackFrame when grabbing the current stack trace.
790 */
791 enum StackTraceOptions {
792 kLineNumber = 1,
793 kColumnOffset = 1 << 1 | kLineNumber,
794 kScriptName = 1 << 2,
795 kFunctionName = 1 << 3,
796 kIsEval = 1 << 4,
797 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000798 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000799 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000800 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000801 };
802
803 /**
804 * Returns a StackFrame at a particular index.
805 */
806 Local<StackFrame> GetFrame(uint32_t index) const;
807
808 /**
809 * Returns the number of StackFrames.
810 */
811 int GetFrameCount() const;
812
813 /**
814 * Returns StackTrace as a v8::Array that contains StackFrame objects.
815 */
816 Local<Array> AsArray();
817
818 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000819 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000820 *
821 * \param frame_limit The maximum number of stack frames we want to capture.
822 * \param options Enumerates the set of things we will capture for each
823 * StackFrame.
824 */
825 static Local<StackTrace> CurrentStackTrace(
826 int frame_limit,
827 StackTraceOptions options = kOverview);
828};
829
830
831/**
832 * A single JavaScript stack frame.
833 */
834class V8EXPORT StackFrame {
835 public:
836 /**
837 * Returns the number, 1-based, of the line for the associate function call.
838 * This method will return Message::kNoLineNumberInfo if it is unable to
839 * retrieve the line number, or if kLineNumber was not passed as an option
840 * when capturing the StackTrace.
841 */
842 int GetLineNumber() const;
843
844 /**
845 * Returns the 1-based column offset on the line for the associated function
846 * call.
847 * This method will return Message::kNoColumnInfo if it is unable to retrieve
848 * the column number, or if kColumnOffset was not passed as an option when
849 * capturing the StackTrace.
850 */
851 int GetColumn() const;
852
853 /**
854 * Returns the name of the resource that contains the script for the
855 * function for this StackFrame.
856 */
857 Local<String> GetScriptName() const;
858
859 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000860 * Returns the name of the resource that contains the script for the
861 * function for this StackFrame or sourceURL value if the script name
862 * is undefined and its source ends with //@ sourceURL=... string.
863 */
864 Local<String> GetScriptNameOrSourceURL() const;
865
866 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000867 * Returns the name of the function associated with this stack frame.
868 */
869 Local<String> GetFunctionName() const;
870
871 /**
872 * Returns whether or not the associated function is compiled via a call to
873 * eval().
874 */
875 bool IsEval() const;
876
877 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000878 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000879 * constructor via "new".
880 */
881 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882};
883
884
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000885// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
887
888/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000889 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000891class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 /**
894 * Returns true if this value is the undefined value. See ECMA-262
895 * 4.3.10.
896 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000897 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
899 /**
900 * Returns true if this value is the null value. See ECMA-262
901 * 4.3.11.
902 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000903 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904
905 /**
906 * Returns true if this value is true.
907 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000908 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909
910 /**
911 * Returns true if this value is false.
912 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000913 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914
915 /**
916 * Returns true if this value is an instance of the String type.
917 * See ECMA-262 8.4.
918 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000919 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920
921 /**
922 * Returns true if this value is a function.
923 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000924 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925
926 /**
927 * Returns true if this value is an array.
928 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000929 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930
v8.team.kasperl727e9952008-09-02 14:56:44 +0000931 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000932 * Returns true if this value is an object.
933 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000934 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935
v8.team.kasperl727e9952008-09-02 14:56:44 +0000936 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937 * Returns true if this value is boolean.
938 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000939 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000940
v8.team.kasperl727e9952008-09-02 14:56:44 +0000941 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000942 * Returns true if this value is a number.
943 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000944 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945
v8.team.kasperl727e9952008-09-02 14:56:44 +0000946 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947 * Returns true if this value is external.
948 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000949 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
v8.team.kasperl727e9952008-09-02 14:56:44 +0000951 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000952 * Returns true if this value is a 32-bit signed integer.
953 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000954 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000956 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000957 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000958 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000959 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000960
961 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000962 * Returns true if this value is a Date.
963 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000964 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000965
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000966 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000967 * Returns true if this value is a Boolean object.
968 */
969 V8EXPORT bool IsBooleanObject() const;
970
971 /**
972 * Returns true if this value is a Number object.
973 */
974 V8EXPORT bool IsNumberObject() const;
975
976 /**
977 * Returns true if this value is a String object.
978 */
979 V8EXPORT bool IsStringObject() const;
980
981 /**
982 * Returns true if this value is a NativeError.
983 */
984 V8EXPORT bool IsNativeError() const;
985
986 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000987 * Returns true if this value is a RegExp.
988 */
989 V8EXPORT bool IsRegExp() const;
990
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000991 V8EXPORT Local<Boolean> ToBoolean() const;
992 V8EXPORT Local<Number> ToNumber() const;
993 V8EXPORT Local<String> ToString() const;
994 V8EXPORT Local<String> ToDetailString() const;
995 V8EXPORT Local<Object> ToObject() const;
996 V8EXPORT Local<Integer> ToInteger() const;
997 V8EXPORT Local<Uint32> ToUint32() const;
998 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
1000 /**
1001 * Attempts to convert a string to an array index.
1002 * Returns an empty handle if the conversion fails.
1003 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001004 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001006 V8EXPORT bool BooleanValue() const;
1007 V8EXPORT double NumberValue() const;
1008 V8EXPORT int64_t IntegerValue() const;
1009 V8EXPORT uint32_t Uint32Value() const;
1010 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011
1012 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001013 V8EXPORT bool Equals(Handle<Value> that) const;
1014 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001015
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001016 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00001017 inline bool QuickIsUndefined() const;
1018 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001019 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00001020 V8EXPORT bool FullIsUndefined() const;
1021 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001022 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023};
1024
1025
1026/**
1027 * The superclass of primitive values. See ECMA-262 4.3.2.
1028 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001029class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001030
1031
1032/**
1033 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1034 * or false value.
1035 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001036class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001038 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 static inline Handle<Boolean> New(bool value);
1040};
1041
1042
1043/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001044 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001046class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001048 enum Encoding {
1049 UNKNOWN_ENCODING = 0x1,
1050 TWO_BYTE_ENCODING = 0x0,
1051 ASCII_ENCODING = 0x4
1052 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001053 /**
1054 * Returns the number of characters in this string.
1055 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001056 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
v8.team.kasperl727e9952008-09-02 14:56:44 +00001058 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001059 * Returns the number of bytes in the UTF-8 encoded
1060 * representation of this string.
1061 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001062 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001063
1064 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001065 * A fast conservative check for non-ASCII characters. May
1066 * return true even for ASCII strings, but if it returns
1067 * false you can be sure that all characters are in the range
1068 * 0-127.
1069 */
1070 V8EXPORT bool MayContainNonAscii() const;
1071
1072 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001073 * Write the contents of the string to an external buffer.
1074 * If no arguments are given, expects the buffer to be large
1075 * enough to hold the entire string and NULL terminator. Copies
1076 * the contents of the string and the NULL terminator into the
1077 * buffer.
1078 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001079 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1080 * before the end of the buffer.
1081 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001082 * Copies up to length characters into the output buffer.
1083 * Only null-terminates if there is enough space in the buffer.
1084 *
1085 * \param buffer The buffer into which the string will be copied.
1086 * \param start The starting position within the string at which
1087 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001088 * \param length The number of characters to copy from the string. For
1089 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001090 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001091 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001092 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001093 * \return The number of characters copied to the buffer excluding the null
1094 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001095 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001096 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001097 enum WriteOptions {
1098 NO_OPTIONS = 0,
1099 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001100 NO_NULL_TERMINATION = 2,
1101 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001102 };
1103
lrn@chromium.org34e60782011-09-15 07:25:40 +00001104 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001105 V8EXPORT int Write(uint16_t* buffer,
1106 int start = 0,
1107 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001108 int options = NO_OPTIONS) const;
1109 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001110 V8EXPORT int WriteAscii(char* buffer,
1111 int start = 0,
1112 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001113 int options = NO_OPTIONS) const;
1114 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001115 V8EXPORT int WriteUtf8(char* buffer,
1116 int length = -1,
1117 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001118 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119
v8.team.kasperl727e9952008-09-02 14:56:44 +00001120 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001121 * A zero length string.
1122 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001123 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001124 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001125
1126 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001127 * Returns true if the string is external
1128 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001129 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130
v8.team.kasperl727e9952008-09-02 14:56:44 +00001131 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001132 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001133 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001134 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001135
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001136 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001137 public:
1138 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001139
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001140 protected:
1141 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001142
1143 /**
1144 * Internally V8 will call this Dispose method when the external string
1145 * resource is no longer needed. The default implementation will use the
1146 * delete operator. This method can be overridden in subclasses to
1147 * control how allocated external string resources are disposed.
1148 */
1149 virtual void Dispose() { delete this; }
1150
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001151 private:
1152 // Disallow copying and assigning.
1153 ExternalStringResourceBase(const ExternalStringResourceBase&);
1154 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001155
1156 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001157 };
1158
v8.team.kasperl727e9952008-09-02 14:56:44 +00001159 /**
1160 * An ExternalStringResource is a wrapper around a two-byte string
1161 * buffer that resides outside V8's heap. Implement an
1162 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001163 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001164 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001165 class V8EXPORT ExternalStringResource
1166 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 public:
1168 /**
1169 * Override the destructor to manage the life cycle of the underlying
1170 * buffer.
1171 */
1172 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001173
1174 /**
1175 * The string data from the underlying buffer.
1176 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001178
1179 /**
1180 * The length of the string. That is, the number of two-byte characters.
1181 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001183
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 protected:
1185 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 };
1187
1188 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001189 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001190 * string buffer that resides outside V8's heap. Implement an
1191 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001192 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001193 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001194 * UTF-8, which would require special treatment internally in the
1195 * engine and, in the case of UTF-8, do not allow efficient indexing.
1196 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001197 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001199 class V8EXPORT ExternalAsciiStringResource
1200 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 public:
1202 /**
1203 * Override the destructor to manage the life cycle of the underlying
1204 * buffer.
1205 */
1206 virtual ~ExternalAsciiStringResource() {}
1207 /** The string data from the underlying buffer.*/
1208 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001209 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 virtual size_t length() const = 0;
1211 protected:
1212 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 };
1214
1215 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001216 * If the string is an external string, return the ExternalStringResourceBase
1217 * regardless of the encoding, otherwise return NULL. The encoding of the
1218 * string is returned in encoding_out.
1219 */
1220 inline ExternalStringResourceBase* GetExternalStringResourceBase(
1221 Encoding* encoding_out) const;
1222
1223 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001224 * Get the ExternalStringResource for an external string. Returns
1225 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001227 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228
1229 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001230 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001231 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001233 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1234 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001236 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237
1238 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001239 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001241 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001242 * be careful to supply the length parameter.
1243 * If it is not given, the function calls
1244 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001245 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001247 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248
lrn@chromium.org34e60782011-09-15 07:25:40 +00001249 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001250 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251
1252 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001253 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254
v8.team.kasperl727e9952008-09-02 14:56:44 +00001255 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001256 * Creates a new string by concatenating the left and the right strings
1257 * passed in as parameters.
1258 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001259 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001260 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001261
1262 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001263 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001264 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001265 * resource will be disposed by calling its Dispose method. The caller of
1266 * this function should not otherwise delete or modify the resource. Neither
1267 * should the underlying buffer be deallocated or modified except through the
1268 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001269 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001270 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001271
ager@chromium.org6f10e412009-02-13 10:11:16 +00001272 /**
1273 * Associate an external string resource with this string by transforming it
1274 * in place so that existing references to this string in the JavaScript heap
1275 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001276 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001277 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001278 * The string is not modified if the operation fails. See NewExternal for
1279 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001280 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001281 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282
v8.team.kasperl727e9952008-09-02 14:56:44 +00001283 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001284 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001285 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001286 * resource will be disposed by calling its Dispose method. The caller of
1287 * this function should not otherwise delete or modify the resource. Neither
1288 * should the underlying buffer be deallocated or modified except through the
1289 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001290 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001291 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001292
ager@chromium.org6f10e412009-02-13 10:11:16 +00001293 /**
1294 * Associate an external string resource with this string by transforming it
1295 * in place so that existing references to this string in the JavaScript heap
1296 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001297 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001298 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001299 * The string is not modified if the operation fails. See NewExternal for
1300 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001301 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001302 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001304 /**
1305 * Returns true if this string can be made external.
1306 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001307 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001308
lrn@chromium.org34e60782011-09-15 07:25:40 +00001309 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001310 V8EXPORT static Local<String> NewUndetectable(const char* data,
1311 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312
lrn@chromium.org34e60782011-09-15 07:25:40 +00001313 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001314 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1315 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316
1317 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001318 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001319 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001320 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001321 * then the length() method returns 0 and the * operator returns
1322 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001323 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001324 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001325 public:
1326 explicit Utf8Value(Handle<v8::Value> obj);
1327 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001328 char* operator*() { return str_; }
1329 const char* operator*() const { return str_; }
1330 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001331 private:
1332 char* str_;
1333 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001334
1335 // Disallow copying and assigning.
1336 Utf8Value(const Utf8Value&);
1337 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001338 };
1339
1340 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001341 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001343 * If conversion to a string fails (eg. due to an exception in the toString()
1344 * method of the object) then the length() method returns 0 and the * operator
1345 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001347 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 public:
1349 explicit AsciiValue(Handle<v8::Value> obj);
1350 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001351 char* operator*() { return str_; }
1352 const char* operator*() const { return str_; }
1353 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 private:
1355 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001356 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001357
1358 // Disallow copying and assigning.
1359 AsciiValue(const AsciiValue&);
1360 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361 };
1362
1363 /**
1364 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001365 * If conversion to a string fails (eg. due to an exception in the toString()
1366 * method of the object) then the length() method returns 0 and the * operator
1367 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001369 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 public:
1371 explicit Value(Handle<v8::Value> obj);
1372 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001373 uint16_t* operator*() { return str_; }
1374 const uint16_t* operator*() const { return str_; }
1375 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 private:
1377 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001378 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001379
1380 // Disallow copying and assigning.
1381 Value(const Value&);
1382 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001384
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001385 private:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001386 V8EXPORT void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1387 Encoding encoding) const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001388 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1389 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390};
1391
1392
1393/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001394 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001396class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001398 V8EXPORT double Value() const;
1399 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001400 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001402 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001403 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404};
1405
1406
1407/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001408 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001410class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001412 V8EXPORT static Local<Integer> New(int32_t value);
1413 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001414 V8EXPORT static Local<Integer> New(int32_t value, Isolate*);
1415 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001416 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001417 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001419 V8EXPORT Integer();
1420 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421};
1422
1423
1424/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001425 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001427class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001429 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001431 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432};
1433
1434
1435/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001436 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001437 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001438class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001439 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001440 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001442 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443};
1444
1445
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446enum PropertyAttribute {
1447 None = 0,
1448 ReadOnly = 1 << 0,
1449 DontEnum = 1 << 1,
1450 DontDelete = 1 << 2
1451};
1452
ager@chromium.org3811b432009-10-28 14:53:37 +00001453enum ExternalArrayType {
1454 kExternalByteArray = 1,
1455 kExternalUnsignedByteArray,
1456 kExternalShortArray,
1457 kExternalUnsignedShortArray,
1458 kExternalIntArray,
1459 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001460 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001461 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001462 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001463};
1464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001466 * Accessor[Getter|Setter] are used as callback functions when
1467 * setting|getting a particular property. See Object and ObjectTemplate's
1468 * method SetAccessor.
1469 */
1470typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1471 const AccessorInfo& info);
1472
1473
1474typedef void (*AccessorSetter)(Local<String> property,
1475 Local<Value> value,
1476 const AccessorInfo& info);
1477
1478
1479/**
1480 * Access control specifications.
1481 *
1482 * Some accessors should be accessible across contexts. These
1483 * accessors have an explicit access control parameter which specifies
1484 * the kind of cross-context access that should be allowed.
1485 *
1486 * Additionally, for security, accessors can prohibit overwriting by
1487 * accessors defined in JavaScript. For objects that have such
1488 * accessors either locally or in their prototype chain it is not
1489 * possible to overwrite the accessor by using __defineGetter__ or
1490 * __defineSetter__ from JavaScript code.
1491 */
1492enum AccessControl {
1493 DEFAULT = 0,
1494 ALL_CAN_READ = 1,
1495 ALL_CAN_WRITE = 1 << 1,
1496 PROHIBITS_OVERWRITING = 1 << 2
1497};
1498
1499
1500/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001501 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001503class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001505 V8EXPORT bool Set(Handle<Value> key,
1506 Handle<Value> value,
1507 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001508
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001509 V8EXPORT bool Set(uint32_t index,
1510 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001511
ager@chromium.orge2902be2009-06-08 12:21:35 +00001512 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001513 // overriding accessors or read-only properties.
1514 //
1515 // Note that if the object has an interceptor the property will be set
1516 // locally, but since the interceptor takes precedence the local property
1517 // will only be returned if the interceptor doesn't return a value.
1518 //
1519 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001520 V8EXPORT bool ForceSet(Handle<Value> key,
1521 Handle<Value> value,
1522 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001523
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001524 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001526 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001527
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001528 /**
1529 * Gets the property attributes of a property which can be None or
1530 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1531 * None when the property doesn't exist.
1532 */
1533 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535 // TODO(1245389): Replace the type-specific versions of these
1536 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001537 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001538
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001539 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001540
1541 // Delete a property on this object bypassing interceptors and
1542 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001543 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001544
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001545 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001546
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001547 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001549 V8EXPORT bool SetAccessor(Handle<String> name,
1550 AccessorGetter getter,
1551 AccessorSetter setter = 0,
1552 Handle<Value> data = Handle<Value>(),
1553 AccessControl settings = DEFAULT,
1554 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001557 * Returns an array containing the names of the enumerable properties
1558 * of this object, including properties from prototype objects. The
1559 * array returned by this method contains the same values as would
1560 * be enumerated by a for-in statement over this object.
1561 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001562 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001563
1564 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001565 * This function has the same functionality as GetPropertyNames but
1566 * the returned array doesn't contain the names of properties from
1567 * prototype objects.
1568 */
1569 V8EXPORT Local<Array> GetOwnPropertyNames();
1570
1571 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572 * Get the prototype object. This does not skip objects marked to
1573 * be skipped by __proto__ and it does not consult the security
1574 * handler.
1575 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001576 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577
1578 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001579 * Set the prototype object. This does not skip objects marked to
1580 * be skipped by __proto__ and it does not consult the security
1581 * handler.
1582 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001583 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001584
1585 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001586 * Finds an instance of the given function template in the prototype
1587 * chain.
1588 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001589 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1590 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001591
1592 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593 * Call builtin Object.prototype.toString on this object.
1594 * This is different from Value::ToString() that may call
1595 * user-defined toString function. This one does not.
1596 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001597 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001599 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001600 * Returns the function invoked as a constructor for this object.
1601 * May be the null value.
1602 */
1603 V8EXPORT Local<Value> GetConstructor();
1604
1605 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001606 * Returns the name of the function invoked as a constructor for this object.
1607 */
1608 V8EXPORT Local<String> GetConstructorName();
1609
kasper.lund212ac232008-07-16 07:07:30 +00001610 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001611 V8EXPORT int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001612
1613 /** Gets the value from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001614 inline Local<Value> GetInternalField(int index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001615
kasper.lund212ac232008-07-16 07:07:30 +00001616 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001617 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001619 /**
1620 * Gets a native pointer from an internal field. Deprecated. If the pointer is
1621 * always 2-byte-aligned, use GetAlignedPointerFromInternalField instead,
1622 * otherwise use a combination of GetInternalField, External::Cast and
1623 * External::Value.
1624 */
1625 V8EXPORT V8_DEPRECATED(void* GetPointerFromInternalField(int index));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001626
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001627 /**
1628 * Sets a native pointer in an internal field. Deprecated. If the pointer is
1629 * always 2-byte aligned, use SetAlignedPointerInInternalField instead,
1630 * otherwise use a combination of External::New and SetInternalField.
1631 */
1632 inline V8_DEPRECATED(void SetPointerInInternalField(int index, void* value));
1633
1634 /**
1635 * Gets a 2-byte-aligned native pointer from an internal field. This field
1636 * must have been set by SetAlignedPointerInInternalField, everything else
1637 * leads to undefined behavior.
1638 */
1639 inline void* GetAlignedPointerFromInternalField(int index);
1640
1641 /**
1642 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1643 * a field, GetAlignedPointerFromInternalField must be used, everything else
1644 * leads to undefined behavior.
1645 */
1646 V8EXPORT void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001649 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001650 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1651 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1652 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653
1654 /**
1655 * If result.IsEmpty() no real property was located in the prototype chain.
1656 * This means interceptors in the prototype chain are not called.
1657 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001658 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1659 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001660
1661 /**
1662 * If result.IsEmpty() no real property was located on the object or
1663 * in the prototype chain.
1664 * This means interceptors in the prototype chain are not called.
1665 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001666 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667
1668 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001669 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670
kasper.lund212ac232008-07-16 07:07:30 +00001671 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001672 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001674 /**
1675 * Turns on access check on the object if the object is an instance of
1676 * a template that has access check callbacks. If an object has no
1677 * access check info, the object cannot be accessed by anyone.
1678 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001679 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001680
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001681 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001682 * Returns the identity hash for this object. The current implementation
1683 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001684 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001685 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001686 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001687 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001688 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001689
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001690 /**
1691 * Access hidden properties on JavaScript objects. These properties are
1692 * hidden from the executing JavaScript and only accessible through the V8
1693 * C++ API. Hidden properties introduced by V8 internally (for example the
1694 * identity hash) are prefixed with "v8::".
1695 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001696 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1697 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1698 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001699
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001700 /**
1701 * Returns true if this is an instance of an api function (one
1702 * created from a function created from a function template) and has
1703 * been modified since it was created. Note that this method is
1704 * conservative and may return true for objects that haven't actually
1705 * been modified.
1706 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001707 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001708
1709 /**
1710 * Clone this object with a fast but shallow copy. Values will point
1711 * to the same values as the original object.
1712 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001713 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001715 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001716 * Returns the context in which the object was created.
1717 */
1718 V8EXPORT Local<Context> CreationContext();
1719
1720 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001721 * Set the backing store of the indexed properties to be managed by the
1722 * embedding layer. Access to the indexed properties will follow the rules
1723 * spelled out in CanvasPixelArray.
1724 * Note: The embedding program still owns the data and needs to ensure that
1725 * the backing store is preserved while V8 has a reference.
1726 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001727 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001728 V8EXPORT bool HasIndexedPropertiesInPixelData();
1729 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1730 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001731
ager@chromium.org3811b432009-10-28 14:53:37 +00001732 /**
1733 * Set the backing store of the indexed properties to be managed by the
1734 * embedding layer. Access to the indexed properties will follow the rules
1735 * spelled out for the CanvasArray subtypes in the WebGL specification.
1736 * Note: The embedding program still owns the data and needs to ensure that
1737 * the backing store is preserved while V8 has a reference.
1738 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001739 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1740 void* data,
1741 ExternalArrayType array_type,
1742 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001743 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1744 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1745 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1746 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001747
lrn@chromium.org1c092762011-05-09 09:42:16 +00001748 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001749 * Checks whether a callback is set by the
1750 * ObjectTemplate::SetCallAsFunctionHandler method.
1751 * When an Object is callable this method returns true.
1752 */
1753 V8EXPORT bool IsCallable();
1754
1755 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001756 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001757 * ObjectTemplate::SetCallAsFunctionHandler method.
1758 */
1759 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1760 int argc,
1761 Handle<Value> argv[]);
1762
1763 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001764 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001765 * ObjectTemplate::SetCallAsFunctionHandler method.
1766 * Note: This method behaves like the Function::NewInstance method.
1767 */
1768 V8EXPORT Local<Value> CallAsConstructor(int argc,
1769 Handle<Value> argv[]);
1770
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001771 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001772 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001773
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001775 V8EXPORT Object();
1776 V8EXPORT static void CheckCast(Value* obj);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001777 V8EXPORT Local<Value> SlowGetInternalField(int index);
1778 V8EXPORT void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779};
1780
1781
1782/**
1783 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1784 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001785class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001787 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788
ager@chromium.org3e875802009-06-29 08:26:34 +00001789 /**
1790 * Clones an element at index |index|. Returns an empty
1791 * handle if cloning fails (for any reason).
1792 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001793 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001794
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001795 /**
1796 * Creates a JavaScript array with the given length. If the length
1797 * is negative the returned array will have length 0.
1798 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001799 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001800
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001801 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001803 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001804 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805};
1806
1807
1808/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001809 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001811class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001813 V8EXPORT Local<Object> NewInstance() const;
1814 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1815 V8EXPORT Local<Value> Call(Handle<Object> recv,
1816 int argc,
1817 Handle<Value> argv[]);
1818 V8EXPORT void SetName(Handle<String> name);
1819 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001820
1821 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001822 * Name inferred from variable or property assignment of this function.
1823 * Used to facilitate debugging and profiling of JavaScript code written
1824 * in an OO style, where many functions are anonymous but are assigned
1825 * to object properties.
1826 */
1827 V8EXPORT Handle<Value> GetInferredName() const;
1828
1829 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001830 * Returns zero based line number of function body and
1831 * kLineOffsetNotFound if no information available.
1832 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001833 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001834 /**
1835 * Returns zero based column number of function body and
1836 * kLineOffsetNotFound if no information available.
1837 */
1838 V8EXPORT int GetScriptColumnNumber() const;
1839 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001840 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001841 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001842 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001843
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001845 V8EXPORT Function();
1846 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847};
1848
1849
1850/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001851 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1852 */
1853class Date : public Object {
1854 public:
1855 V8EXPORT static Local<Value> New(double time);
1856
1857 /**
1858 * A specialization of Value::NumberValue that is more efficient
1859 * because we know the structure of this object.
1860 */
1861 V8EXPORT double NumberValue() const;
1862
1863 static inline Date* Cast(v8::Value* obj);
1864
1865 /**
1866 * Notification that the embedder has changed the time zone,
1867 * daylight savings time, or other date / time configuration
1868 * parameters. V8 keeps a cache of various values used for
1869 * date / time computation. This notification will reset
1870 * those cached values for the current context so that date /
1871 * time configuration changes would be reflected in the Date
1872 * object.
1873 *
1874 * This API should not be called more than needed as it will
1875 * negatively impact the performance of date operations.
1876 */
1877 V8EXPORT static void DateTimeConfigurationChangeNotification();
1878
1879 private:
1880 V8EXPORT static void CheckCast(v8::Value* obj);
1881};
1882
1883
1884/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001885 * A Number object (ECMA-262, 4.3.21).
1886 */
1887class NumberObject : public Object {
1888 public:
1889 V8EXPORT static Local<Value> New(double value);
1890
1891 /**
1892 * Returns the Number held by the object.
1893 */
1894 V8EXPORT double NumberValue() const;
1895
1896 static inline NumberObject* Cast(v8::Value* obj);
1897
1898 private:
1899 V8EXPORT static void CheckCast(v8::Value* obj);
1900};
1901
1902
1903/**
1904 * A Boolean object (ECMA-262, 4.3.15).
1905 */
1906class BooleanObject : public Object {
1907 public:
1908 V8EXPORT static Local<Value> New(bool value);
1909
1910 /**
1911 * Returns the Boolean held by the object.
1912 */
1913 V8EXPORT bool BooleanValue() const;
1914
1915 static inline BooleanObject* Cast(v8::Value* obj);
1916
1917 private:
1918 V8EXPORT static void CheckCast(v8::Value* obj);
1919};
1920
1921
1922/**
1923 * A String object (ECMA-262, 4.3.18).
1924 */
1925class StringObject : public Object {
1926 public:
1927 V8EXPORT static Local<Value> New(Handle<String> value);
1928
1929 /**
1930 * Returns the String held by the object.
1931 */
1932 V8EXPORT Local<String> StringValue() const;
1933
1934 static inline StringObject* Cast(v8::Value* obj);
1935
1936 private:
1937 V8EXPORT static void CheckCast(v8::Value* obj);
1938};
1939
1940
1941/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001942 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1943 */
1944class RegExp : public Object {
1945 public:
1946 /**
1947 * Regular expression flag bits. They can be or'ed to enable a set
1948 * of flags.
1949 */
1950 enum Flags {
1951 kNone = 0,
1952 kGlobal = 1,
1953 kIgnoreCase = 2,
1954 kMultiline = 4
1955 };
1956
1957 /**
1958 * Creates a regular expression from the given pattern string and
1959 * the flags bit field. May throw a JavaScript exception as
1960 * described in ECMA-262, 15.10.4.1.
1961 *
1962 * For example,
1963 * RegExp::New(v8::String::New("foo"),
1964 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1965 * is equivalent to evaluating "/foo/gm".
1966 */
1967 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1968 Flags flags);
1969
1970 /**
1971 * Returns the value of the source property: a string representing
1972 * the regular expression.
1973 */
1974 V8EXPORT Local<String> GetSource() const;
1975
1976 /**
1977 * Returns the flags bit field.
1978 */
1979 V8EXPORT Flags GetFlags() const;
1980
1981 static inline RegExp* Cast(v8::Value* obj);
1982
1983 private:
1984 V8EXPORT static void CheckCast(v8::Value* obj);
1985};
1986
1987
1988/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001989 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
1990 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001992class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 public:
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001994 /** Deprecated, use New instead. */
1995 V8_DEPRECATED(static inline Local<Value> Wrap(void* value));
1996
1997 /** Deprecated, use a combination of Cast and Value instead. */
1998 V8_DEPRECATED(static inline void* Unwrap(Handle<Value> obj));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001999
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002000 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002001 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002002 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002004 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005};
2006
2007
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002008// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009
2010
2011/**
2012 * The superclass of object and function templates.
2013 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002014class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 public:
2016 /** Adds a property to each instance created by this template.*/
2017 void Set(Handle<String> name, Handle<Data> value,
2018 PropertyAttribute attributes = None);
2019 inline void Set(const char* name, Handle<Data> value);
2020 private:
2021 Template();
2022
2023 friend class ObjectTemplate;
2024 friend class FunctionTemplate;
2025};
2026
2027
2028/**
2029 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002030 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 * including the receiver, the number and values of arguments, and
2032 * the holder of the function.
2033 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002034class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 public:
2036 inline int Length() const;
2037 inline Local<Value> operator[](int i) const;
2038 inline Local<Function> Callee() const;
2039 inline Local<Object> This() const;
2040 inline Local<Object> Holder() const;
2041 inline bool IsConstructCall() const;
2042 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002043 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002046 static const int kIsolateIndex = 0;
2047 static const int kDataIndex = -1;
2048 static const int kCalleeIndex = -2;
2049 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002050
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002052 inline Arguments(internal::Object** implicit_args,
2053 internal::Object** values,
2054 int length,
2055 bool is_construct_call);
2056 internal::Object** implicit_args_;
2057 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002059 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060};
2061
2062
2063/**
2064 * The information passed to an accessor callback about the context
2065 * of the property access.
2066 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002067class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002069 inline AccessorInfo(internal::Object** args)
2070 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002071 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 inline Local<Value> Data() const;
2073 inline Local<Object> This() const;
2074 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002077 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078};
2079
2080
2081typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2082
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 * NamedProperty[Getter|Setter] are used as interceptors on object.
2085 * See ObjectTemplate::SetNamedPropertyHandler.
2086 */
2087typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2088 const AccessorInfo& info);
2089
2090
2091/**
2092 * Returns the value if the setter intercepts the request.
2093 * Otherwise, returns an empty handle.
2094 */
2095typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2096 Local<Value> value,
2097 const AccessorInfo& info);
2098
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099/**
2100 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002101 * The result is an integer encoding property attributes (like v8::None,
2102 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002104typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2105 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106
2107
2108/**
2109 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002110 * The return value is true if the property could be deleted and false
2111 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112 */
2113typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2114 const AccessorInfo& info);
2115
2116/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002117 * Returns an array containing the names of the properties the named
2118 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 */
2120typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2121
v8.team.kasperl727e9952008-09-02 14:56:44 +00002122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002124 * Returns the value of the property if the getter intercepts the
2125 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126 */
2127typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2128 const AccessorInfo& info);
2129
2130
2131/**
2132 * Returns the value if the setter intercepts the request.
2133 * Otherwise, returns an empty handle.
2134 */
2135typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2136 Local<Value> value,
2137 const AccessorInfo& info);
2138
2139
2140/**
2141 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002142 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002143 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002144typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2145 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146
2147/**
2148 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002149 * The return value is true if the property could be deleted and false
2150 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 */
2152typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2153 const AccessorInfo& info);
2154
v8.team.kasperl727e9952008-09-02 14:56:44 +00002155/**
2156 * Returns an array containing the indices of the properties the
2157 * indexed property getter intercepts.
2158 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2160
2161
2162/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002163 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 */
2165enum AccessType {
2166 ACCESS_GET,
2167 ACCESS_SET,
2168 ACCESS_HAS,
2169 ACCESS_DELETE,
2170 ACCESS_KEYS
2171};
2172
v8.team.kasperl727e9952008-09-02 14:56:44 +00002173
2174/**
2175 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002176 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002177 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002178typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 Local<Value> key,
2180 AccessType type,
2181 Local<Value> data);
2182
v8.team.kasperl727e9952008-09-02 14:56:44 +00002183
2184/**
2185 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002186 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002187 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002188typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 uint32_t index,
2190 AccessType type,
2191 Local<Value> data);
2192
2193
2194/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002195 * A FunctionTemplate is used to create functions at runtime. There
2196 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002197 * context. The lifetime of the created function is equal to the
2198 * lifetime of the context. So in case the embedder needs to create
2199 * temporary functions that can be collected using Scripts is
2200 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 *
2202 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002203 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002205 * A FunctionTemplate has a corresponding instance template which is
2206 * used to create object instances when the function is used as a
2207 * constructor. Properties added to the instance template are added to
2208 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 *
2210 * A FunctionTemplate can have a prototype template. The prototype template
2211 * is used to create the prototype object of the function.
2212 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002213 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002215 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2217 * t->Set("func_property", v8::Number::New(1));
2218 *
2219 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2220 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2221 * proto_t->Set("proto_const", v8::Number::New(2));
2222 *
2223 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2224 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2225 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2226 * instance_t->Set("instance_property", Number::New(3));
2227 *
2228 * v8::Local<v8::Function> function = t->GetFunction();
2229 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002230 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 *
2232 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002233 * and "instance" for the instance object created above. The function
2234 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002236 * \code
2237 * func_property in function == true;
2238 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002240 * function.prototype.proto_method() invokes 'InvokeCallback'
2241 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * instance instanceof function == true;
2244 * instance.instance_accessor calls 'InstanceAccessorCallback'
2245 * instance.instance_property == 3;
2246 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002248 * A FunctionTemplate can inherit from another one by calling the
2249 * FunctionTemplate::Inherit method. The following graph illustrates
2250 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002252 * \code
2253 * FunctionTemplate Parent -> Parent() . prototype -> { }
2254 * ^ ^
2255 * | Inherit(Parent) | .__proto__
2256 * | |
2257 * FunctionTemplate Child -> Child() . prototype -> { }
2258 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002260 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2261 * object of the Child() function has __proto__ pointing to the
2262 * Parent() function's prototype object. An instance of the Child
2263 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002265 * Let Parent be the FunctionTemplate initialized in the previous
2266 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002268 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 * Local<FunctionTemplate> parent = t;
2270 * Local<FunctionTemplate> child = FunctionTemplate::New();
2271 * child->Inherit(parent);
2272 *
2273 * Local<Function> child_function = child->GetFunction();
2274 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002275 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002277 * The Child function and Child instance will have the following
2278 * properties:
2279 *
2280 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002282 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002284 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002286class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287 public:
2288 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002289 static Local<FunctionTemplate> New(
2290 InvocationCallback callback = 0,
2291 Handle<Value> data = Handle<Value>(),
2292 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 /** Returns the unique function instance in the current execution context.*/
2294 Local<Function> GetFunction();
2295
v8.team.kasperl727e9952008-09-02 14:56:44 +00002296 /**
2297 * Set the call-handler callback for a FunctionTemplate. This
2298 * callback is called whenever the function created from this
2299 * FunctionTemplate is called.
2300 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 void SetCallHandler(InvocationCallback callback,
2302 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303
v8.team.kasperl727e9952008-09-02 14:56:44 +00002304 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 Local<ObjectTemplate> InstanceTemplate();
2306
2307 /** Causes the function template to inherit from a parent function template.*/
2308 void Inherit(Handle<FunctionTemplate> parent);
2309
2310 /**
2311 * A PrototypeTemplate is the template used to create the prototype object
2312 * of the function created by this template.
2313 */
2314 Local<ObjectTemplate> PrototypeTemplate();
2315
v8.team.kasperl727e9952008-09-02 14:56:44 +00002316
2317 /**
2318 * Set the class name of the FunctionTemplate. This is used for
2319 * printing objects created with the function created from the
2320 * FunctionTemplate as its constructor.
2321 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 void SetClassName(Handle<String> name);
2323
2324 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002325 * Determines whether the __proto__ accessor ignores instances of
2326 * the function template. If instances of the function template are
2327 * ignored, __proto__ skips all instances and instead returns the
2328 * next object in the prototype chain.
2329 *
2330 * Call with a value of true to make the __proto__ accessor ignore
2331 * instances of the function template. Call with a value of false
2332 * to make the __proto__ accessor not ignore instances of the
2333 * function template. By default, instances of a function template
2334 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 */
2336 void SetHiddenPrototype(bool value);
2337
2338 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002339 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2340 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002341 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002342 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002343
2344 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002345 * Returns true if the given object is an instance of this function
2346 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 */
2348 bool HasInstance(Handle<Value> object);
2349
2350 private:
2351 FunctionTemplate();
2352 void AddInstancePropertyAccessor(Handle<String> name,
2353 AccessorGetter getter,
2354 AccessorSetter setter,
2355 Handle<Value> data,
2356 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002357 PropertyAttribute attributes,
2358 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2360 NamedPropertySetter setter,
2361 NamedPropertyQuery query,
2362 NamedPropertyDeleter remover,
2363 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002364 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2366 IndexedPropertySetter setter,
2367 IndexedPropertyQuery query,
2368 IndexedPropertyDeleter remover,
2369 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002370 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002371 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2372 Handle<Value> data);
2373
2374 friend class Context;
2375 friend class ObjectTemplate;
2376};
2377
2378
2379/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002380 * An ObjectTemplate is used to create objects at runtime.
2381 *
2382 * Properties added to an ObjectTemplate are added to each object
2383 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002385class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002387 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002389
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 /** Creates a new instance of this template.*/
2391 Local<Object> NewInstance();
2392
2393 /**
2394 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002395 *
2396 * Whenever the property with the given name is accessed on objects
2397 * created from this ObjectTemplate the getter and setter callbacks
2398 * are called instead of getting and setting the property directly
2399 * on the JavaScript object.
2400 *
2401 * \param name The name of the property for which an accessor is added.
2402 * \param getter The callback to invoke when getting the property.
2403 * \param setter The callback to invoke when setting the property.
2404 * \param data A piece of data that will be passed to the getter and setter
2405 * callbacks whenever they are invoked.
2406 * \param settings Access control settings for the accessor. This is a bit
2407 * field consisting of one of more of
2408 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2409 * The default is to not allow cross-context access.
2410 * ALL_CAN_READ means that all cross-context reads are allowed.
2411 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2412 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2413 * cross-context access.
2414 * \param attribute The attributes of the property for which an accessor
2415 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002416 * \param signature The signature describes valid receivers for the accessor
2417 * and is used to perform implicit instance checks against them. If the
2418 * receiver is incompatible (i.e. is not an instance of the constructor as
2419 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2420 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 */
2422 void SetAccessor(Handle<String> name,
2423 AccessorGetter getter,
2424 AccessorSetter setter = 0,
2425 Handle<Value> data = Handle<Value>(),
2426 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002427 PropertyAttribute attribute = None,
2428 Handle<AccessorSignature> signature =
2429 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430
2431 /**
2432 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002433 *
2434 * Whenever a named property is accessed on objects created from
2435 * this object template, the provided callback is invoked instead of
2436 * accessing the property directly on the JavaScript object.
2437 *
2438 * \param getter The callback to invoke when getting a property.
2439 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002440 * \param query The callback to invoke to check if a property is present,
2441 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002442 * \param deleter The callback to invoke when deleting a property.
2443 * \param enumerator The callback to invoke to enumerate all the named
2444 * properties of an object.
2445 * \param data A piece of data that will be passed to the callbacks
2446 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002447 */
2448 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2449 NamedPropertySetter setter = 0,
2450 NamedPropertyQuery query = 0,
2451 NamedPropertyDeleter deleter = 0,
2452 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002453 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002454
2455 /**
2456 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002457 *
2458 * Whenever an indexed property is accessed on objects created from
2459 * this object template, the provided callback is invoked instead of
2460 * accessing the property directly on the JavaScript object.
2461 *
2462 * \param getter The callback to invoke when getting a property.
2463 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002464 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002465 * \param deleter The callback to invoke when deleting a property.
2466 * \param enumerator The callback to invoke to enumerate all the indexed
2467 * properties of an object.
2468 * \param data A piece of data that will be passed to the callbacks
2469 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 */
2471 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2472 IndexedPropertySetter setter = 0,
2473 IndexedPropertyQuery query = 0,
2474 IndexedPropertyDeleter deleter = 0,
2475 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002476 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 /**
2479 * Sets the callback to be used when calling instances created from
2480 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002481 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482 * function.
2483 */
2484 void SetCallAsFunctionHandler(InvocationCallback callback,
2485 Handle<Value> data = Handle<Value>());
2486
v8.team.kasperl727e9952008-09-02 14:56:44 +00002487 /**
2488 * Mark object instances of the template as undetectable.
2489 *
2490 * In many ways, undetectable objects behave as though they are not
2491 * there. They behave like 'undefined' in conditionals and when
2492 * printed. However, properties can be accessed and called as on
2493 * normal objects.
2494 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 void MarkAsUndetectable();
2496
v8.team.kasperl727e9952008-09-02 14:56:44 +00002497 /**
2498 * Sets access check callbacks on the object template.
2499 *
2500 * When accessing properties on instances of this object template,
2501 * the access check callback will be called to determine whether or
2502 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002503 * The last parameter specifies whether access checks are turned
2504 * on by default on instances. If access checks are off by default,
2505 * they can be turned on on individual instances by calling
2506 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002507 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2509 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002510 Handle<Value> data = Handle<Value>(),
2511 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002512
kasper.lund212ac232008-07-16 07:07:30 +00002513 /**
2514 * Gets the number of internal fields for objects generated from
2515 * this template.
2516 */
2517 int InternalFieldCount();
2518
2519 /**
2520 * Sets the number of internal fields for objects generated from
2521 * this template.
2522 */
2523 void SetInternalFieldCount(int value);
2524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 private:
2526 ObjectTemplate();
2527 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2528 friend class FunctionTemplate;
2529};
2530
2531
2532/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002533 * A Signature specifies which receivers and arguments are valid
2534 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002536class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537 public:
2538 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2539 Handle<FunctionTemplate>(),
2540 int argc = 0,
2541 Handle<FunctionTemplate> argv[] = 0);
2542 private:
2543 Signature();
2544};
2545
2546
2547/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002548 * An AccessorSignature specifies which receivers are valid parameters
2549 * to an accessor callback.
2550 */
2551class V8EXPORT AccessorSignature : public Data {
2552 public:
2553 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2554 Handle<FunctionTemplate>());
2555 private:
2556 AccessorSignature();
2557};
2558
2559
2560/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002561 * A utility for determining the type of objects based on the template
2562 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002563 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002564class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 public:
2566 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2567 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2568 int match(Handle<Value> value);
2569 private:
2570 TypeSwitch();
2571};
2572
2573
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002574// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002575
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002576class V8EXPORT ExternalAsciiStringResourceImpl
2577 : public String::ExternalAsciiStringResource {
2578 public:
2579 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2580 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2581 : data_(data), length_(length) {}
2582 const char* data() const { return data_; }
2583 size_t length() const { return length_; }
2584
2585 private:
2586 const char* data_;
2587 size_t length_;
2588};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589
2590/**
2591 * Ignore
2592 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002593class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002595 // Note that the strings passed into this constructor must live as long
2596 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002598 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002599 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002600 const char** deps = 0,
2601 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602 virtual ~Extension() { }
2603 virtual v8::Handle<v8::FunctionTemplate>
2604 GetNativeFunction(v8::Handle<v8::String> name) {
2605 return v8::Handle<v8::FunctionTemplate>();
2606 }
2607
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002608 const char* name() const { return name_; }
2609 size_t source_length() const { return source_length_; }
2610 const String::ExternalAsciiStringResource* source() const {
2611 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 int dependency_count() { return dep_count_; }
2613 const char** dependencies() { return deps_; }
2614 void set_auto_enable(bool value) { auto_enable_ = value; }
2615 bool auto_enable() { return auto_enable_; }
2616
2617 private:
2618 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002619 size_t source_length_; // expected to initialize before source_
2620 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 int dep_count_;
2622 const char** deps_;
2623 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002624
2625 // Disallow copying and assigning.
2626 Extension(const Extension&);
2627 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628};
2629
2630
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002631void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002632
2633
2634/**
2635 * Ignore
2636 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002637class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 public:
2639 inline DeclareExtension(Extension* extension) {
2640 RegisterExtension(extension);
2641 }
2642};
2643
2644
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002645// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646
2647
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002648Handle<Primitive> V8EXPORT Undefined();
2649Handle<Primitive> V8EXPORT Null();
2650Handle<Boolean> V8EXPORT True();
2651Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002653inline Handle<Primitive> Undefined(Isolate* isolate);
2654inline Handle<Primitive> Null(Isolate* isolate);
2655inline Handle<Boolean> True(Isolate* isolate);
2656inline Handle<Boolean> False(Isolate* isolate);
2657
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658
2659/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002660 * A set of constraints that specifies the limits of the runtime's memory use.
2661 * You must set the heap size before initializing the VM - the size cannot be
2662 * adjusted after the VM is initialized.
2663 *
2664 * If you are using threads then you should hold the V8::Locker lock while
2665 * setting the stack limit and you must set a non-default stack limit separately
2666 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002668class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669 public:
2670 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002671 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002673 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002675 int max_executable_size() { return max_executable_size_; }
2676 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002677 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002678 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2680 private:
2681 int max_young_space_size_;
2682 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002683 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 uint32_t* stack_limit_;
2685};
2686
2687
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002688bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689
2690
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002691// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692
2693
2694typedef void (*FatalErrorCallback)(const char* location, const char* message);
2695
2696
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002697typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698
2699
2700/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002701 * Schedules an exception to be thrown when returning to JavaScript. When an
2702 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002704 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002705 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002706Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707
2708/**
2709 * Create new error objects by calling the corresponding error object
2710 * constructor with the message.
2711 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002712class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713 public:
2714 static Local<Value> RangeError(Handle<String> message);
2715 static Local<Value> ReferenceError(Handle<String> message);
2716 static Local<Value> SyntaxError(Handle<String> message);
2717 static Local<Value> TypeError(Handle<String> message);
2718 static Local<Value> Error(Handle<String> message);
2719};
2720
2721
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002722// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002724typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002726typedef void* (*CreateHistogramCallback)(const char* name,
2727 int min,
2728 int max,
2729 size_t buckets);
2730
2731typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2732
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002733// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002734 enum ObjectSpace {
2735 kObjectSpaceNewSpace = 1 << 0,
2736 kObjectSpaceOldPointerSpace = 1 << 1,
2737 kObjectSpaceOldDataSpace = 1 << 2,
2738 kObjectSpaceCodeSpace = 1 << 3,
2739 kObjectSpaceMapSpace = 1 << 4,
2740 kObjectSpaceLoSpace = 1 << 5,
2741
2742 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2743 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2744 kObjectSpaceLoSpace
2745 };
2746
2747 enum AllocationAction {
2748 kAllocationActionAllocate = 1 << 0,
2749 kAllocationActionFree = 1 << 1,
2750 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2751 };
2752
2753typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2754 AllocationAction action,
2755 int size);
2756
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002757// --- Leave Script Callback ---
2758typedef void (*CallCompletedCallback)();
2759
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002760// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2762 AccessType type,
2763 Local<Value> data);
2764
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002765// --- AllowCodeGenerationFromStrings callbacks ---
2766
2767/**
2768 * Callback to check if code generation from strings is allowed. See
2769 * Context::AllowCodeGenerationFromStrings.
2770 */
2771typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2772
2773// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
2775/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002776 * Applications can register callback functions which will be called
2777 * before and after a garbage collection. Allocations are not
2778 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002779 * objects (set or delete properties for example) since it is possible
2780 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002781 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002782enum GCType {
2783 kGCTypeScavenge = 1 << 0,
2784 kGCTypeMarkSweepCompact = 1 << 1,
2785 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2786};
2787
2788enum GCCallbackFlags {
2789 kNoGCCallbackFlags = 0,
2790 kGCCallbackFlagCompacted = 1 << 0
2791};
2792
2793typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2794typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2795
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796typedef void (*GCCallback)();
2797
2798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002800 * Collection of V8 heap information.
2801 *
2802 * Instances of this class can be passed to v8::V8::HeapStatistics to
2803 * get heap statistics from V8.
2804 */
2805class V8EXPORT HeapStatistics {
2806 public:
2807 HeapStatistics();
2808 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002809 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002810 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002811 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002812 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002813
2814 private:
2815 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002816 void set_total_heap_size_executable(size_t size) {
2817 total_heap_size_executable_ = size;
2818 }
danno@chromium.org72204d52012-10-31 10:02:10 +00002819 void set_total_physical_size(size_t size) {
2820 total_physical_size_ = size;
2821 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002822 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002823 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002824
2825 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002826 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002827 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002828 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002829 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002830
2831 friend class V8;
2832};
2833
2834
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002835class RetainedObjectInfo;
2836
ager@chromium.org3811b432009-10-28 14:53:37 +00002837/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002838 * Isolate represents an isolated instance of the V8 engine. V8
2839 * isolates have completely separate states. Objects from one isolate
2840 * must not be used in other isolates. When V8 is initialized a
2841 * default isolate is implicitly created and entered. The embedder
2842 * can create additional isolates and use them in parallel in multiple
2843 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002844 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002845 */
2846class V8EXPORT Isolate {
2847 public:
2848 /**
2849 * Stack-allocated class which sets the isolate for all operations
2850 * executed within a local scope.
2851 */
2852 class V8EXPORT Scope {
2853 public:
2854 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2855 isolate->Enter();
2856 }
2857
2858 ~Scope() { isolate_->Exit(); }
2859
2860 private:
2861 Isolate* const isolate_;
2862
2863 // Prevent copying of Scope objects.
2864 Scope(const Scope&);
2865 Scope& operator=(const Scope&);
2866 };
2867
2868 /**
2869 * Creates a new isolate. Does not change the currently entered
2870 * isolate.
2871 *
2872 * When an isolate is no longer used its resources should be freed
2873 * by calling Dispose(). Using the delete operator is not allowed.
2874 */
2875 static Isolate* New();
2876
2877 /**
2878 * Returns the entered isolate for the current thread or NULL in
2879 * case there is no current isolate.
2880 */
2881 static Isolate* GetCurrent();
2882
2883 /**
2884 * Methods below this point require holding a lock (using Locker) in
2885 * a multi-threaded environment.
2886 */
2887
2888 /**
2889 * Sets this isolate as the entered one for the current thread.
2890 * Saves the previously entered one (if any), so that it can be
2891 * restored when exiting. Re-entering an isolate is allowed.
2892 */
2893 void Enter();
2894
2895 /**
2896 * Exits this isolate by restoring the previously entered one in the
2897 * current thread. The isolate may still stay the same, if it was
2898 * entered more than once.
2899 *
2900 * Requires: this == Isolate::GetCurrent().
2901 */
2902 void Exit();
2903
2904 /**
2905 * Disposes the isolate. The isolate must not be entered by any
2906 * thread to be disposable.
2907 */
2908 void Dispose();
2909
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002910 /**
2911 * Associate embedder-specific data with the isolate
2912 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002913 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002914
2915 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002916 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002917 * Returns NULL if SetData has never been called.
2918 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002919 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002920
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002921 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002922 Isolate();
2923 Isolate(const Isolate&);
2924 ~Isolate();
2925 Isolate& operator=(const Isolate&);
2926 void* operator new(size_t size);
2927 void operator delete(void*, size_t);
2928};
2929
2930
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002931class StartupData {
2932 public:
2933 enum CompressionAlgorithm {
2934 kUncompressed,
2935 kBZip2
2936 };
2937
2938 const char* data;
2939 int compressed_size;
2940 int raw_size;
2941};
2942
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002943
2944/**
2945 * A helper class for driving V8 startup data decompression. It is based on
2946 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2947 * for an embedder to use this class, instead, API functions can be used
2948 * directly.
2949 *
2950 * For an example of the class usage, see the "shell.cc" sample application.
2951 */
2952class V8EXPORT StartupDataDecompressor { // NOLINT
2953 public:
2954 StartupDataDecompressor();
2955 virtual ~StartupDataDecompressor();
2956 int Decompress();
2957
2958 protected:
2959 virtual int DecompressData(char* raw_data,
2960 int* raw_data_size,
2961 const char* compressed_data,
2962 int compressed_data_size) = 0;
2963
2964 private:
2965 char** raw_data;
2966};
2967
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002968
2969/**
2970 * EntropySource is used as a callback function when v8 needs a source
2971 * of entropy.
2972 */
2973typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2974
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002975
2976/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002977 * ReturnAddressLocationResolver is used as a callback function when v8 is
2978 * resolving the location of a return address on the stack. Profilers that
2979 * change the return address on the stack can use this to resolve the stack
2980 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002981 *
2982 * \param return_addr_location points to a location on stack where a machine
2983 * return address resides.
2984 * \returns either return_addr_location, or else a pointer to the profiler's
2985 * copy of the original return address.
2986 *
2987 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002988 */
2989typedef uintptr_t (*ReturnAddressLocationResolver)(
2990 uintptr_t return_addr_location);
2991
2992
2993/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002994 * FunctionEntryHook is the type of the profile entry hook called at entry to
2995 * any generated function when function-level profiling is enabled.
2996 *
2997 * \param function the address of the function that's being entered.
2998 * \param return_addr_location points to a location on stack where the machine
2999 * return address resides. This can be used to identify the caller of
3000 * \p function, and/or modified to divert execution when \p function exits.
3001 *
3002 * \note the entry hook must not cause garbage collection.
3003 */
3004typedef void (*FunctionEntryHook)(uintptr_t function,
3005 uintptr_t return_addr_location);
3006
3007
3008/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003009 * A JIT code event is issued each time code is added, moved or removed.
3010 *
3011 * \note removal events are not currently issued.
3012 */
3013struct JitCodeEvent {
3014 enum EventType {
3015 CODE_ADDED,
3016 CODE_MOVED,
3017 CODE_REMOVED
3018 };
3019
3020 // Type of event.
3021 EventType type;
3022 // Start of the instructions.
3023 void* code_start;
3024 // Size of the instructions.
3025 size_t code_len;
3026
3027 union {
3028 // Only valid for CODE_ADDED.
3029 struct {
3030 // Name of the object associated with the code, note that the string is
3031 // not zero-terminated.
3032 const char* str;
3033 // Number of chars in str.
3034 size_t len;
3035 } name;
3036 // New location of instructions. Only valid for CODE_MOVED.
3037 void* new_code_start;
3038 };
3039};
3040
3041/**
3042 * Option flags passed to the SetJitCodeEventHandler function.
3043 */
3044enum JitCodeEventOptions {
3045 kJitCodeEventDefault = 0,
3046 // Generate callbacks for already existent code.
3047 kJitCodeEventEnumExisting = 1
3048};
3049
3050
3051/**
3052 * Callback function passed to SetJitCodeEventHandler.
3053 *
3054 * \param event code add, move or removal event.
3055 */
3056typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3057
3058
3059/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003060 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003061 */
3062class V8EXPORT ExternalResourceVisitor { // NOLINT
3063 public:
3064 virtual ~ExternalResourceVisitor() {}
3065 virtual void VisitExternalString(Handle<String> string) {}
3066};
3067
3068
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003069/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003070 * Interface for iterating through all the persistent handles in the heap.
3071 */
3072class V8EXPORT PersistentHandleVisitor { // NOLINT
3073 public:
3074 virtual ~PersistentHandleVisitor() {}
3075 virtual void VisitPersistentHandle(Persistent<Value> value,
3076 uint16_t class_id) {}
3077};
3078
3079
3080/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081 * Container class for static utility functions.
3082 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003083class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003085 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086 static void SetFatalErrorHandler(FatalErrorCallback that);
3087
v8.team.kasperl727e9952008-09-02 14:56:44 +00003088 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003089 * Set the callback to invoke to check if code generation from
3090 * strings should be allowed.
3091 */
3092 static void SetAllowCodeGenerationFromStringsCallback(
3093 AllowCodeGenerationFromStringsCallback that);
3094
3095 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003096 * Ignore out-of-memory exceptions.
3097 *
3098 * V8 running out of memory is treated as a fatal error by default.
3099 * This means that the fatal error handler is called and that V8 is
3100 * terminated.
3101 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003102 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003103 * out-of-memory situation as a fatal error. This way, the contexts
3104 * that did not cause the out of memory problem might be able to
3105 * continue execution.
3106 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003107 static void IgnoreOutOfMemoryException();
3108
v8.team.kasperl727e9952008-09-02 14:56:44 +00003109 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003110 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003111 * fatal errors such as out-of-memory situations.
3112 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113 static bool IsDead();
3114
3115 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003116 * The following 4 functions are to be used when V8 is built with
3117 * the 'compress_startup_data' flag enabled. In this case, the
3118 * embedder must decompress startup data prior to initializing V8.
3119 *
3120 * This is how interaction with V8 should look like:
3121 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3122 * v8::StartupData* compressed_data =
3123 * new v8::StartupData[compressed_data_count];
3124 * v8::V8::GetCompressedStartupData(compressed_data);
3125 * ... decompress data (compressed_data can be updated in-place) ...
3126 * v8::V8::SetDecompressedStartupData(compressed_data);
3127 * ... now V8 can be initialized
3128 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003129 *
3130 * A helper class StartupDataDecompressor is provided. It implements
3131 * the protocol of the interaction described above, and can be used in
3132 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003133 */
3134 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3135 static int GetCompressedStartupDataCount();
3136 static void GetCompressedStartupData(StartupData* compressed_data);
3137 static void SetDecompressedStartupData(StartupData* decompressed_data);
3138
3139 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003140 * Adds a message listener.
3141 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003142 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003143 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144 */
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003145 static bool AddMessageListener(MessageCallback that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146
3147 /**
3148 * Remove all message listeners from the specified callback function.
3149 */
3150 static void RemoveMessageListeners(MessageCallback that);
3151
3152 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003153 * Tells V8 to capture current stack trace when uncaught exception occurs
3154 * and report it to the message listeners. The option is off by default.
3155 */
3156 static void SetCaptureStackTraceForUncaughtExceptions(
3157 bool capture,
3158 int frame_limit = 10,
3159 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3160
3161 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003162 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 */
3164 static void SetFlagsFromString(const char* str, int length);
3165
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003166 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003167 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003168 */
3169 static void SetFlagsFromCommandLine(int* argc,
3170 char** argv,
3171 bool remove_flags);
3172
kasper.lund7276f142008-07-30 08:49:36 +00003173 /** Get the version string. */
3174 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175
3176 /**
3177 * Enables the host application to provide a mechanism for recording
3178 * statistics counters.
3179 */
3180 static void SetCounterFunction(CounterLookupCallback);
3181
3182 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003183 * Enables the host application to provide a mechanism for recording
3184 * histograms. The CreateHistogram function returns a
3185 * histogram which will later be passed to the AddHistogramSample
3186 * function.
3187 */
3188 static void SetCreateHistogramFunction(CreateHistogramCallback);
3189 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3190
3191 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003192 * Enables the computation of a sliding window of states. The sliding
3193 * window information is recorded in statistics counters.
3194 */
3195 static void EnableSlidingStateWindow();
3196
3197 /** Callback function for reporting failed access checks.*/
3198 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3199
3200 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003201 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003202 * garbage collection. Allocations are not allowed in the
3203 * callback function, you therefore cannot manipulate objects (set
3204 * or delete properties for example) since it is possible such
3205 * operations will result in the allocation of objects. It is possible
3206 * to specify the GCType filter for your callback. But it is not possible to
3207 * register the same callback function two times with different
3208 * GCType filters.
3209 */
3210 static void AddGCPrologueCallback(
3211 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3212
3213 /**
3214 * This function removes callback which was installed by
3215 * AddGCPrologueCallback function.
3216 */
3217 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3218
3219 /**
3220 * The function is deprecated. Please use AddGCPrologueCallback instead.
3221 * Enables the host application to receive a notification before a
3222 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003223 * callback function, you therefore cannot manipulate objects (set
3224 * or delete properties for example) since it is possible such
3225 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226 */
3227 static void SetGlobalGCPrologueCallback(GCCallback);
3228
3229 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003230 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003231 * garbage collection. Allocations are not allowed in the
3232 * callback function, you therefore cannot manipulate objects (set
3233 * or delete properties for example) since it is possible such
3234 * operations will result in the allocation of objects. It is possible
3235 * to specify the GCType filter for your callback. But it is not possible to
3236 * register the same callback function two times with different
3237 * GCType filters.
3238 */
3239 static void AddGCEpilogueCallback(
3240 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3241
3242 /**
3243 * This function removes callback which was installed by
3244 * AddGCEpilogueCallback function.
3245 */
3246 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3247
3248 /**
3249 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3250 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003251 * major garbage collection. Allocations are not allowed in the
3252 * callback function, you therefore cannot manipulate objects (set
3253 * or delete properties for example) since it is possible such
3254 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003255 */
3256 static void SetGlobalGCEpilogueCallback(GCCallback);
3257
3258 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003259 * Enables the host application to provide a mechanism to be notified
3260 * and perform custom logging when V8 Allocates Executable Memory.
3261 */
3262 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3263 ObjectSpace space,
3264 AllocationAction action);
3265
3266 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003267 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003268 */
3269 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3270
3271 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003272 * Adds a callback to notify the host application when a script finished
3273 * running. If a script re-enters the runtime during executing, the
3274 * CallCompletedCallback is only invoked when the outer-most script
3275 * execution ends. Executing scripts inside the callback do not trigger
3276 * further callbacks.
3277 */
3278 static void AddCallCompletedCallback(CallCompletedCallback callback);
3279
3280 /**
3281 * Removes callback that was installed by AddCallCompletedCallback.
3282 */
3283 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3284
3285 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003286 * Allows the host application to group objects together. If one
3287 * object in the group is alive, all objects in the group are alive.
3288 * After each garbage collection, object groups are removed. It is
3289 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003290 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003291 * wrapper objects. Object groups for all dependent handles need to
3292 * be provided for kGCTypeMarkSweepCompact collections, for all other
3293 * garbage collection types it is sufficient to provide object groups
3294 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003295 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003296 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003297 static void AddObjectGroup(Persistent<Value>* objects,
3298 size_t length,
3299 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003300
3301 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003302 * Allows the host application to declare implicit references between
3303 * the objects: if |parent| is alive, all |children| are alive too.
3304 * After each garbage collection, all implicit references
3305 * are removed. It is intended to be used in the before-garbage-collection
3306 * callback function.
3307 */
3308 static void AddImplicitReferences(Persistent<Object> parent,
3309 Persistent<Value>* children,
3310 size_t length);
3311
3312 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003313 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003314 * initialize from scratch. This function is called implicitly if
3315 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003316 */
3317 static bool Initialize();
3318
kasper.lund7276f142008-07-30 08:49:36 +00003319 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003320 * Allows the host application to provide a callback which can be used
3321 * as a source of entropy for random number generators.
3322 */
3323 static void SetEntropySource(EntropySource source);
3324
3325 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003326 * Allows the host application to provide a callback that allows v8 to
3327 * cooperate with a profiler that rewrites return addresses on stack.
3328 */
3329 static void SetReturnAddressLocationResolver(
3330 ReturnAddressLocationResolver return_address_resolver);
3331
3332 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003333 * Allows the host application to provide the address of a function that's
3334 * invoked on entry to every V8-generated function.
3335 * Note that \p entry_hook is invoked at the very start of each
3336 * generated function.
3337 *
3338 * \param entry_hook a function that will be invoked on entry to every
3339 * V8-generated function.
3340 * \returns true on success on supported platforms, false on failure.
3341 * \note Setting a new entry hook function when one is already active will
3342 * fail.
3343 */
3344 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3345
3346 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003347 * Allows the host application to provide the address of a function that is
3348 * notified each time code is added, moved or removed.
3349 *
3350 * \param options options for the JIT code event handler.
3351 * \param event_handler the JIT code event handler, which will be invoked
3352 * each time code is added, moved or removed.
3353 * \note \p event_handler won't get notified of existent code.
3354 * \note since code removal notifications are not currently issued, the
3355 * \p event_handler may get notifications of code that overlaps earlier
3356 * code notifications. This happens when code areas are reused, and the
3357 * earlier overlapping code areas should therefore be discarded.
3358 * \note the events passed to \p event_handler and the strings they point to
3359 * are not guaranteed to live past each call. The \p event_handler must
3360 * copy strings and other parameters it needs to keep around.
3361 * \note the set of events declared in JitCodeEvent::EventType is expected to
3362 * grow over time, and the JitCodeEvent structure is expected to accrue
3363 * new members. The \p event_handler function must ignore event codes
3364 * it does not recognize to maintain future compatibility.
3365 */
3366 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3367 JitCodeEventHandler event_handler);
3368
3369 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003370 * Adjusts the amount of registered external memory. Used to give
3371 * V8 an indication of the amount of externally allocated memory
3372 * that is kept alive by JavaScript objects. V8 uses this to decide
3373 * when to perform global garbage collections. Registering
3374 * externally allocated memory will trigger global garbage
3375 * collections more often than otherwise in an attempt to garbage
3376 * collect the JavaScript objects keeping the externally allocated
3377 * memory alive.
3378 *
3379 * \param change_in_bytes the change in externally allocated memory
3380 * that is kept alive by JavaScript objects.
3381 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003382 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003383 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3384 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003385
iposva@chromium.org245aa852009-02-10 00:49:54 +00003386 /**
3387 * Suspends recording of tick samples in the profiler.
3388 * When the V8 profiling mode is enabled (usually via command line
3389 * switches) this function suspends recording of tick samples.
3390 * Profiling ticks are discarded until ResumeProfiler() is called.
3391 *
3392 * See also the --prof and --prof_auto command line switches to
3393 * enable V8 profiling.
3394 */
3395 static void PauseProfiler();
3396
3397 /**
3398 * Resumes recording of tick samples in the profiler.
3399 * See also PauseProfiler().
3400 */
3401 static void ResumeProfiler();
3402
ager@chromium.org41826e72009-03-30 13:30:57 +00003403 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003404 * Return whether profiler is currently paused.
3405 */
3406 static bool IsProfilerPaused();
3407
3408 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003409 * Retrieve the V8 thread id of the calling thread.
3410 *
3411 * The thread id for a thread should only be retrieved after the V8
3412 * lock has been acquired with a Locker object with that thread.
3413 */
3414 static int GetCurrentThreadId();
3415
3416 /**
3417 * Forcefully terminate execution of a JavaScript thread. This can
3418 * be used to terminate long-running scripts.
3419 *
3420 * TerminateExecution should only be called when then V8 lock has
3421 * been acquired with a Locker object. Therefore, in order to be
3422 * able to terminate long-running threads, preemption must be
3423 * enabled to allow the user of TerminateExecution to acquire the
3424 * lock.
3425 *
3426 * The termination is achieved by throwing an exception that is
3427 * uncatchable by JavaScript exception handlers. Termination
3428 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003429 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003430 * exception handler that catches an exception should check if that
3431 * exception is a termination exception and immediately return if
3432 * that is the case. Returning immediately in that case will
3433 * continue the propagation of the termination exception if needed.
3434 *
3435 * The thread id passed to TerminateExecution must have been
3436 * obtained by calling GetCurrentThreadId on the thread in question.
3437 *
3438 * \param thread_id The thread id of the thread to terminate.
3439 */
3440 static void TerminateExecution(int thread_id);
3441
3442 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003443 * Forcefully terminate the current thread of JavaScript execution
3444 * in the given isolate. If no isolate is provided, the default
3445 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003446 *
3447 * This method can be used by any thread even if that thread has not
3448 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003449 *
3450 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003451 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003452 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003453
3454 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003455 * Is V8 terminating JavaScript execution.
3456 *
3457 * Returns true if JavaScript execution is currently terminating
3458 * because of a call to TerminateExecution. In that case there are
3459 * still JavaScript frames on the stack and the termination
3460 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003461 *
3462 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003463 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003464 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003465
3466 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003467 * Releases any resources used by v8 and stops any utility threads
3468 * that may be running. Note that disposing v8 is permanent, it
3469 * cannot be reinitialized.
3470 *
3471 * It should generally not be necessary to dispose v8 before exiting
3472 * a process, this should happen automatically. It is only necessary
3473 * to use if the process needs the resources taken up by v8.
3474 */
3475 static bool Dispose();
3476
ager@chromium.org3811b432009-10-28 14:53:37 +00003477 /**
3478 * Get statistics about the heap memory usage.
3479 */
3480 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003481
3482 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003483 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003484 * heap. GC is not invoked prior to iterating, therefore there is no
3485 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003486 */
3487 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3488
3489 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003490 * Iterates through all the persistent handles in the current isolate's heap
3491 * that have class_ids.
3492 */
3493 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3494
3495 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003496 * Optional notification that the embedder is idle.
3497 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003498 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003499 * Returns true if the embedder should stop calling IdleNotification
3500 * until real work has been done. This indicates that V8 has done
3501 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003502 *
3503 * The hint argument specifies the amount of work to be done in the function
3504 * on scale from 1 to 1000. There is no guarantee that the actual work will
3505 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003506 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003507 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003508
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003509 /**
3510 * Optional notification that the system is running low on memory.
3511 * V8 uses these notifications to attempt to free memory.
3512 */
3513 static void LowMemoryNotification();
3514
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003515 /**
3516 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003517 * these notifications to guide the GC heuristic. Returns the number
3518 * of context disposals - including this one - since the last time
3519 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003520 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003521 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003523 private:
3524 V8();
3525
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003526 static internal::Object** GlobalizeReference(internal::Object** handle);
3527 static void DisposeGlobal(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003528 static void DisposeGlobal(internal::Isolate* isolate,
3529 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003530 static void MakeWeak(internal::Object** global_handle,
3531 void* data,
3532 WeakReferenceCallback);
3533 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003534 static void MarkIndependent(internal::Object** global_handle);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003535 static void MarkIndependent(internal::Isolate* isolate,
3536 internal::Object** global_handle);
3537 static void MarkPartiallyDependent(internal::Object** global_handle);
3538 static void MarkPartiallyDependent(internal::Isolate* isolate,
3539 internal::Object** global_handle);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003540 static bool IsGlobalIndependent(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003541 static bool IsGlobalIndependent(internal::Isolate* isolate,
3542 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003543 static bool IsGlobalNearDeath(internal::Object** global_handle);
3544 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003545 static void SetWrapperClassId(internal::Object** global_handle,
3546 uint16_t class_id);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003547 static uint16_t GetWrapperClassId(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003548
3549 template <class T> friend class Handle;
3550 template <class T> friend class Local;
3551 template <class T> friend class Persistent;
3552 friend class Context;
3553};
3554
3555
3556/**
3557 * An external exception handler.
3558 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003559class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003560 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561 /**
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003562 * Creates a new try/catch block and registers it with v8. Note that
3563 * all TryCatch blocks should be stack allocated because the memory
3564 * location itself is compared against JavaScript try/catch blocks.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565 */
3566 TryCatch();
3567
3568 /**
3569 * Unregisters and deletes this try/catch block.
3570 */
3571 ~TryCatch();
3572
3573 /**
3574 * Returns true if an exception has been caught by this try/catch block.
3575 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003576 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577
3578 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003579 * For certain types of exceptions, it makes no sense to continue
3580 * execution.
3581 *
3582 * Currently, the only type of exception that can be caught by a
3583 * TryCatch handler and for which it does not make sense to continue
3584 * is termination exception. Such exceptions are thrown when the
3585 * TerminateExecution methods are called to terminate a long-running
3586 * script.
3587 *
3588 * If CanContinue returns false, the correct action is to perform
3589 * any C++ cleanup needed and then return.
3590 */
3591 bool CanContinue() const;
3592
3593 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003594 * Throws the exception caught by this TryCatch in a way that avoids
3595 * it being caught again by this same TryCatch. As with ThrowException
3596 * it is illegal to execute any JavaScript operations after calling
3597 * ReThrow; the caller must return immediately to where the exception
3598 * is caught.
3599 */
3600 Handle<Value> ReThrow();
3601
3602 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 * Returns the exception caught by this try/catch block. If no exception has
3604 * been caught an empty handle is returned.
3605 *
3606 * The returned handle is valid until this TryCatch block has been destroyed.
3607 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003608 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003609
3610 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003611 * Returns the .stack property of the thrown object. If no .stack
3612 * property is present an empty handle is returned.
3613 */
3614 Local<Value> StackTrace() const;
3615
3616 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003617 * Returns the message associated with this exception. If there is
3618 * no message associated an empty handle is returned.
3619 *
3620 * The returned handle is valid until this TryCatch block has been
3621 * destroyed.
3622 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003623 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003624
3625 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003626 * Clears any exceptions that may have been caught by this try/catch block.
3627 * After this method has been called, HasCaught() will return false.
3628 *
3629 * It is not necessary to clear a try/catch block before using it again; if
3630 * another exception is thrown the previously caught exception will just be
3631 * overwritten. However, it is often a good idea since it makes it easier
3632 * to determine which operation threw a given exception.
3633 */
3634 void Reset();
3635
v8.team.kasperl727e9952008-09-02 14:56:44 +00003636 /**
3637 * Set verbosity of the external exception handler.
3638 *
3639 * By default, exceptions that are caught by an external exception
3640 * handler are not reported. Call SetVerbose with true on an
3641 * external exception handler to have exceptions caught by the
3642 * handler reported as if they were not caught.
3643 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644 void SetVerbose(bool value);
3645
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003646 /**
3647 * Set whether or not this TryCatch should capture a Message object
3648 * which holds source information about where the exception
3649 * occurred. True by default.
3650 */
3651 void SetCaptureMessage(bool value);
3652
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003653 private:
mmassi@chromium.org49a44672012-12-04 13:52:03 +00003654 // Make it hard to create heap-allocated TryCatch blocks.
3655 TryCatch(const TryCatch&);
3656 void operator=(const TryCatch&);
3657 void* operator new(size_t size);
3658 void operator delete(void*, size_t);
3659
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003660 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003661 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003663 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003664 bool is_verbose_ : 1;
3665 bool can_continue_ : 1;
3666 bool capture_message_ : 1;
3667 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003668
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003669 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670};
3671
3672
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003673// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003674
3675
3676/**
3677 * Ignore
3678 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003679class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680 public:
3681 ExtensionConfiguration(int name_count, const char* names[])
3682 : name_count_(name_count), names_(names) { }
3683 private:
3684 friend class ImplementationUtilities;
3685 int name_count_;
3686 const char** names_;
3687};
3688
3689
3690/**
3691 * A sandboxed execution context with its own set of built-in objects
3692 * and functions.
3693 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003694class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003696 /**
3697 * Returns the global proxy object or global object itself for
3698 * detached contexts.
3699 *
3700 * Global proxy object is a thin wrapper whose prototype points to
3701 * actual context's global object with the properties like Object, etc.
3702 * This is done that way for security reasons (for more details see
3703 * https://wiki.mozilla.org/Gecko:SplitWindow).
3704 *
3705 * Please note that changes to global proxy object prototype most probably
3706 * would break VM---v8 expects only global object as a prototype of
3707 * global proxy object.
3708 *
3709 * If DetachGlobal() has been invoked, Global() would return actual global
3710 * object until global is reattached with ReattachGlobal().
3711 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712 Local<Object> Global();
3713
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003714 /**
3715 * Detaches the global object from its context before
3716 * the global object can be reused to create a new context.
3717 */
3718 void DetachGlobal();
3719
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003720 /**
3721 * Reattaches a global object to a context. This can be used to
3722 * restore the connection between a global object and a context
3723 * after DetachGlobal has been called.
3724 *
3725 * \param global_object The global object to reattach to the
3726 * context. For this to work, the global object must be the global
3727 * object that was associated with this context before a call to
3728 * DetachGlobal.
3729 */
3730 void ReattachGlobal(Handle<Object> global_object);
3731
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003732 /** Creates a new context.
3733 *
3734 * Returns a persistent handle to the newly allocated context. This
3735 * persistent handle has to be disposed when the context is no
3736 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003737 *
3738 * \param extensions An optional extension configuration containing
3739 * the extensions to be installed in the newly created context.
3740 *
3741 * \param global_template An optional object template from which the
3742 * global object for the newly created context will be created.
3743 *
3744 * \param global_object An optional global object to be reused for
3745 * the newly created context. This global object must have been
3746 * created by a previous call to Context::New with the same global
3747 * template. The state of the global object will be completely reset
3748 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003749 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003750 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003751 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003752 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3753 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003754
kasper.lund44510672008-07-25 07:37:58 +00003755 /** Returns the last entered context. */
3756 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757
kasper.lund44510672008-07-25 07:37:58 +00003758 /** Returns the context that is on the top of the stack. */
3759 static Local<Context> GetCurrent();
3760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003761 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003762 * Returns the context of the calling JavaScript code. That is the
3763 * context of the top-most JavaScript frame. If there are no
3764 * JavaScript frames an empty handle is returned.
3765 */
3766 static Local<Context> GetCalling();
3767
3768 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769 * Sets the security token for the context. To access an object in
3770 * another context, the security tokens must match.
3771 */
3772 void SetSecurityToken(Handle<Value> token);
3773
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003774 /** Restores the security token to the default value. */
3775 void UseDefaultSecurityToken();
3776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777 /** Returns the security token of this context.*/
3778 Handle<Value> GetSecurityToken();
3779
v8.team.kasperl727e9952008-09-02 14:56:44 +00003780 /**
3781 * Enter this context. After entering a context, all code compiled
3782 * and run is compiled and run in this context. If another context
3783 * is already entered, this old context is saved so it can be
3784 * restored when the new context is exited.
3785 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003787
3788 /**
3789 * Exit this context. Exiting the current context restores the
3790 * context that was in place when entering the current context.
3791 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792 void Exit();
3793
v8.team.kasperl727e9952008-09-02 14:56:44 +00003794 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795 bool HasOutOfMemoryException();
3796
v8.team.kasperl727e9952008-09-02 14:56:44 +00003797 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798 static bool InContext();
3799
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003800 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003801 * Gets embedder data with index 0. Deprecated, use GetEmbedderData with index
3802 * 0 instead.
ager@chromium.org9085a012009-05-11 19:22:57 +00003803 */
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003804 V8_DEPRECATED(inline Local<Value> GetData());
3805
3806 /**
3807 * Sets embedder data with index 0. Deprecated, use SetEmbedderData with index
3808 * 0 instead.
3809 */
3810 V8_DEPRECATED(inline void SetData(Handle<Value> value));
3811
3812 /**
3813 * Gets the embedder data with the given index, which must have been set by a
3814 * previous call to SetEmbedderData with the same index. Note that index 0
3815 * currently has a special meaning for Chrome's debugger.
3816 */
3817 inline Local<Value> GetEmbedderData(int index);
3818
3819 /**
3820 * Sets the embedder data with the given index, growing the data as
3821 * needed. Note that index 0 currently has a special meaning for Chrome's
3822 * debugger.
3823 */
3824 void SetEmbedderData(int index, Handle<Value> value);
3825
3826 /**
3827 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3828 * index, which must have bees set by a previous call to
3829 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3830 * currently has a special meaning for Chrome's debugger.
3831 */
3832 inline void* GetAlignedPointerFromEmbedderData(int index);
3833
3834 /**
3835 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3836 * index, growing the data as needed. Note that index 0 currently has a
3837 * special meaning for Chrome's debugger.
3838 */
3839 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003840
3841 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003842 * Control whether code generation from strings is allowed. Calling
3843 * this method with false will disable 'eval' and the 'Function'
3844 * constructor for code running in this context. If 'eval' or the
3845 * 'Function' constructor are used an exception will be thrown.
3846 *
3847 * If code generation from strings is not allowed the
3848 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3849 * set before blocking the call to 'eval' or the 'Function'
3850 * constructor. If that callback returns true, the call will be
3851 * allowed, otherwise an exception will be thrown. If no callback is
3852 * set an exception will be thrown.
3853 */
3854 void AllowCodeGenerationFromStrings(bool allow);
3855
3856 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003857 * Returns true if code generation from strings is allowed for the context.
3858 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3859 */
3860 bool IsCodeGenerationFromStringsAllowed();
3861
3862 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003863 * Sets the error description for the exception that is thrown when
3864 * code generation from strings is not allowed and 'eval' or the 'Function'
3865 * constructor are called.
3866 */
3867 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3868
3869 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003870 * Stack-allocated class which sets the execution context for all
3871 * operations executed within a local scope.
3872 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003873 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003875 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003876 context_->Enter();
3877 }
3878 inline ~Scope() { context_->Exit(); }
3879 private:
3880 Handle<Context> context_;
3881 };
3882
3883 private:
3884 friend class Value;
3885 friend class Script;
3886 friend class Object;
3887 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003888
3889 Local<Value> SlowGetEmbedderData(int index);
3890 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891};
3892
3893
3894/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003895 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003896 * is allowed to use any given V8 isolate. See Isolate class
3897 * comments. The definition of 'using V8 isolate' includes
3898 * accessing handles or holding onto object pointers obtained
3899 * from V8 handles while in the particular V8 isolate. It is up
3900 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003901 * constraint is not violated. In addition to any other synchronization
3902 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3903 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003905 * v8::Locker is a scoped lock object. While it's
3906 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003907 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3908 * locked by at most one thread at any time. In other words, the scope of a
3909 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003910 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003911 * Sample usage:
3912* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003913 * ...
3914 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003915 * v8::Locker locker(isolate);
3916 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003918 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 * ...
3920 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003921 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003922 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003923 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 * by destroying the v8::Locker object as above or by constructing a
3925 * v8::Unlocker object:
3926 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003927 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003929 * isolate->Exit();
3930 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 * ...
3932 * // Code not using V8 goes here while V8 can run in another thread.
3933 * ...
3934 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003935 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003936 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003937 *
3938 * The Unlocker object is intended for use in a long-running callback
3939 * from V8, where you want to release the V8 lock for other threads to
3940 * use.
3941 *
3942 * The v8::Locker is a recursive lock. That is, you can lock more than
3943 * once in a given thread. This can be useful if you have code that can
3944 * be called either from code that holds the lock or from code that does
3945 * not. The Unlocker is not recursive so you can not have several
3946 * Unlockers on the stack at once, and you can not use an Unlocker in a
3947 * thread that is not inside a Locker's scope.
3948 *
3949 * An unlocker will unlock several lockers if it has to and reinstate
3950 * the correct depth of locking on its destruction. eg.:
3951 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003952 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 * // V8 not locked.
3954 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003955 * v8::Locker locker(isolate);
3956 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 * // V8 locked.
3958 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003959 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 * // V8 still locked (2 levels).
3961 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003962 * isolate->Exit();
3963 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003964 * // V8 not locked.
3965 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003966 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967 * // V8 locked again (2 levels).
3968 * }
3969 * // V8 still locked (1 level).
3970 * }
3971 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003972 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003973 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003974 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003976class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003978 /**
3979 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3980 */
3981 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003983 private:
3984 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003985};
3986
3987
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003988class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003990 /**
3991 * Initialize Locker for a given Isolate. NULL means default isolate.
3992 */
3993 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003994 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003995
3996 /**
3997 * Start preemption.
3998 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003999 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00004000 * that will switch between multiple threads that are in contention
4001 * for the V8 lock.
4002 */
4003 static void StartPreemption(int every_n_ms);
4004
4005 /**
4006 * Stop preemption.
4007 */
4008 static void StopPreemption();
4009
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004010 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004011 * Returns whether or not the locker for a given isolate, or default isolate
4012 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004013 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00004014 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004015
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004016 /**
4017 * Returns whether v8::Locker is being used by this V8 instance.
4018 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004019 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021 private:
4022 bool has_lock_;
4023 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004024 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004025
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004026 static bool active_;
4027
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004028 // Disallow copying and assigning.
4029 Locker(const Locker&);
4030 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031};
4032
4033
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004034/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004035 * A struct for exporting HeapStats data from V8, using "push" model.
4036 */
4037struct HeapStatsUpdate;
4038
4039
4040/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004041 * An interface for exporting data from V8, using "push" model.
4042 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004043class V8EXPORT OutputStream { // NOLINT
4044 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004045 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004046 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004047 };
4048 enum WriteResult {
4049 kContinue = 0,
4050 kAbort = 1
4051 };
4052 virtual ~OutputStream() {}
4053 /** Notify about the end of stream. */
4054 virtual void EndOfStream() = 0;
4055 /** Get preferred output chunk size. Called only once. */
4056 virtual int GetChunkSize() { return 1024; }
4057 /** Get preferred output encoding. Called only once. */
4058 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4059 /**
4060 * Writes the next chunk of snapshot data into the stream. Writing
4061 * can be stopped by returning kAbort as function result. EndOfStream
4062 * will not be called in case writing was aborted.
4063 */
4064 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004065 /**
4066 * Writes the next chunk of heap stats data into the stream. Writing
4067 * can be stopped by returning kAbort as function result. EndOfStream
4068 * will not be called in case writing was aborted.
4069 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004070 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004071 return kAbort;
4072 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004073};
4074
4075
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004076/**
4077 * An interface for reporting progress and controlling long-running
4078 * activities.
4079 */
4080class V8EXPORT ActivityControl { // NOLINT
4081 public:
4082 enum ControlOption {
4083 kContinue = 0,
4084 kAbort = 1
4085 };
4086 virtual ~ActivityControl() {}
4087 /**
4088 * Notify about current progress. The activity can be stopped by
4089 * returning kAbort as the callback result.
4090 */
4091 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4092};
4093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004094
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004095// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004096
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004097
4098namespace internal {
4099
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004100const int kApiPointerSize = sizeof(void*); // NOLINT
4101const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004102
4103// Tag information for HeapObject.
4104const int kHeapObjectTag = 1;
4105const int kHeapObjectTagSize = 2;
4106const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4107
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004108// Tag information for Smi.
4109const int kSmiTag = 0;
4110const int kSmiTagSize = 1;
4111const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4112
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004113template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004114
4115// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004116template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004117 static const int kSmiShiftSize = 0;
4118 static const int kSmiValueSize = 31;
4119 static inline int SmiToInt(internal::Object* value) {
4120 int shift_bits = kSmiTagSize + kSmiShiftSize;
4121 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4122 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4123 }
4124};
4125
4126// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004127template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004128 static const int kSmiShiftSize = 31;
4129 static const int kSmiValueSize = 32;
4130 static inline int SmiToInt(internal::Object* value) {
4131 int shift_bits = kSmiTagSize + kSmiShiftSize;
4132 // Shift down and throw away top 32 bits.
4133 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4134 }
4135};
4136
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004137typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4138const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4139const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004140
4141/**
4142 * This class exports constants and functionality from within v8 that
4143 * is necessary to implement inline functions in the v8 api. Don't
4144 * depend on functions and constants defined here.
4145 */
4146class Internals {
4147 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004148 // These values match non-compiler-dependent values defined within
4149 // the implementation of v8.
4150 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004151 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004152 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004153
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004154 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004155 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004156 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004157 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4158 static const int kContextHeaderSize = 2 * kApiPointerSize;
4159 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004160 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004161 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004162 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004163 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004164
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004165 static const int kIsolateStateOffset = 0;
4166 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4167 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4168 static const int kUndefinedValueRootIndex = 5;
4169 static const int kNullValueRootIndex = 7;
4170 static const int kTrueValueRootIndex = 8;
4171 static const int kFalseValueRootIndex = 9;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004172 static const int kEmptySymbolRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004173
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004174 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004175 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004176 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004177 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004178
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004179 static const int kUndefinedOddballKind = 5;
4180 static const int kNullOddballKind = 3;
4181
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004182 static inline bool HasHeapObjectTag(internal::Object* value) {
4183 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4184 kHeapObjectTag);
4185 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004186
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004187 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004188 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004189 }
4190
4191 static inline int GetInstanceType(internal::Object* obj) {
4192 typedef internal::Object O;
4193 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4194 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4195 }
4196
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004197 static inline int GetOddballKind(internal::Object* obj) {
4198 typedef internal::Object O;
4199 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4200 }
4201
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004202 static inline bool IsExternalTwoByteString(int instance_type) {
4203 int representation = (instance_type & kFullStringRepresentationMask);
4204 return representation == kExternalTwoByteRepresentationTag;
4205 }
4206
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004207 static inline bool IsInitialized(v8::Isolate* isolate) {
4208 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4209 return *reinterpret_cast<int*>(addr) == 1;
4210 }
4211
4212 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4213 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4214 kIsolateEmbedderDataOffset;
4215 *reinterpret_cast<void**>(addr) = data;
4216 }
4217
4218 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4219 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4220 kIsolateEmbedderDataOffset;
4221 return *reinterpret_cast<void**>(addr);
4222 }
4223
4224 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4225 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4226 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4227 }
4228
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004229 template <typename T>
4230 static inline T ReadField(Object* ptr, int offset) {
4231 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4232 return *reinterpret_cast<T*>(addr);
4233 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004234
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004235 template <typename T>
4236 static inline T ReadEmbedderData(Context* context, int index) {
4237 typedef internal::Object O;
4238 typedef internal::Internals I;
4239 O* ctx = *reinterpret_cast<O**>(context);
4240 int embedder_data_offset = I::kContextHeaderSize +
4241 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4242 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4243 int value_offset =
4244 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4245 return I::ReadField<T>(embedder_data, value_offset);
4246 }
4247
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004248 static inline bool CanCastToHeapObject(void* o) { return false; }
4249 static inline bool CanCastToHeapObject(Context* o) { return true; }
4250 static inline bool CanCastToHeapObject(String* o) { return true; }
4251 static inline bool CanCastToHeapObject(Object* o) { return true; }
4252 static inline bool CanCastToHeapObject(Message* o) { return true; }
4253 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4254 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004255};
4256
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004257} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004258
4259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004261Local<T>::Local() : Handle<T>() { }
4262
4263
4264template <class T>
4265Local<T> Local<T>::New(Handle<T> that) {
4266 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004267 T* that_ptr = *that;
4268 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4269 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4270 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4271 reinterpret_cast<internal::HeapObject*>(*p))));
4272 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004273 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4274}
4275
4276
4277template <class T>
4278Persistent<T> Persistent<T>::New(Handle<T> that) {
4279 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004280 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004281 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4282}
4283
4284
4285template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004286bool Persistent<T>::IsIndependent() const {
4287 if (this->IsEmpty()) return false;
4288 return V8::IsGlobalIndependent(reinterpret_cast<internal::Object**>(**this));
4289}
4290
4291
4292template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004293bool Persistent<T>::IsIndependent(Isolate* isolate) const {
4294 if (this->IsEmpty()) return false;
4295 return V8::IsGlobalIndependent(reinterpret_cast<internal::Isolate*>(isolate),
4296 reinterpret_cast<internal::Object**>(**this));
4297}
4298
4299
4300template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004301bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004302 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004303 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004304}
4305
4306
4307template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004308bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004309 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004310 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004311}
4312
4313
4314template <class T>
4315void Persistent<T>::Dispose() {
4316 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004317 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004318}
4319
4320
4321template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004322void Persistent<T>::Dispose(Isolate* isolate) {
4323 if (this->IsEmpty()) return;
4324 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4325 reinterpret_cast<internal::Object**>(**this));
4326}
4327
4328
4329template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004330Persistent<T>::Persistent() : Handle<T>() { }
4331
4332template <class T>
4333void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004334 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4335 parameters,
4336 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004337}
4338
4339template <class T>
4340void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004341 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004342}
4343
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004344template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004345void Persistent<T>::MarkIndependent() {
4346 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4347}
4348
4349template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004350void Persistent<T>::MarkIndependent(Isolate* isolate) {
4351 V8::MarkIndependent(reinterpret_cast<internal::Isolate*>(isolate),
4352 reinterpret_cast<internal::Object**>(**this));
4353}
4354
4355template <class T>
4356void Persistent<T>::MarkPartiallyDependent() {
4357 V8::MarkPartiallyDependent(reinterpret_cast<internal::Object**>(**this));
4358}
4359
4360template <class T>
4361void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
4362 V8::MarkPartiallyDependent(reinterpret_cast<internal::Isolate*>(isolate),
4363 reinterpret_cast<internal::Object**>(**this));
4364}
4365
4366template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004367void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4368 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4369}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004370
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004371template <class T>
4372uint16_t Persistent<T>::WrapperClassId() const {
4373 return V8::GetWrapperClassId(reinterpret_cast<internal::Object**>(**this));
4374}
4375
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004376Arguments::Arguments(internal::Object** implicit_args,
4377 internal::Object** values, int length,
4378 bool is_construct_call)
4379 : implicit_args_(implicit_args),
4380 values_(values),
4381 length_(length),
4382 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004383
4384
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004385Local<Value> Arguments::operator[](int i) const {
4386 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4387 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4388}
4389
4390
4391Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004392 return Local<Function>(reinterpret_cast<Function*>(
4393 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004394}
4395
4396
4397Local<Object> Arguments::This() const {
4398 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4399}
4400
4401
4402Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004403 return Local<Object>(reinterpret_cast<Object*>(
4404 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004405}
4406
4407
4408Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004409 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004410}
4411
4412
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004413Isolate* Arguments::GetIsolate() const {
4414 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4415}
4416
4417
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004418bool Arguments::IsConstructCall() const {
4419 return is_construct_call_;
4420}
4421
4422
4423int Arguments::Length() const {
4424 return length_;
4425}
4426
4427
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004428template <class T>
4429Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004430 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4431 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004432 return Local<T>(reinterpret_cast<T*>(after));
4433}
4434
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004435Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004436 return resource_name_;
4437}
4438
4439
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004440Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004441 return resource_line_offset_;
4442}
4443
4444
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004445Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004446 return resource_column_offset_;
4447}
4448
4449
4450Handle<Boolean> Boolean::New(bool value) {
4451 return value ? True() : False();
4452}
4453
4454
4455void Template::Set(const char* name, v8::Handle<Data> value) {
4456 Set(v8::String::New(name), value);
4457}
4458
4459
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004460Local<Value> Object::GetInternalField(int index) {
4461#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004462 typedef internal::Object O;
4463 typedef internal::Internals I;
4464 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004465 // Fast path: If the object is a plain JSObject, which is the common case, we
4466 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004467 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004468 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004469 O* value = I::ReadField<O*>(obj, offset);
4470 O** result = HandleScope::CreateHandle(value);
4471 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004472 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004473#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004474 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004475}
4476
4477
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004478void Object::SetPointerInInternalField(int index, void* value) {
4479 SetInternalField(index, External::New(value));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004480}
4481
4482
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004483void* Object::GetAlignedPointerFromInternalField(int index) {
4484#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004485 typedef internal::Object O;
4486 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004487 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004488 // Fast path: If the object is a plain JSObject, which is the common case, we
4489 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004490 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004491 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004492 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004493 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004494#endif
4495 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004496}
4497
4498
4499String* String::Cast(v8::Value* value) {
4500#ifdef V8_ENABLE_CHECKS
4501 CheckCast(value);
4502#endif
4503 return static_cast<String*>(value);
4504}
4505
4506
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004507Local<String> String::Empty(Isolate* isolate) {
4508 typedef internal::Object* S;
4509 typedef internal::Internals I;
4510 if (!I::IsInitialized(isolate)) return Empty();
4511 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4512 return Local<String>(reinterpret_cast<String*>(slot));
4513}
4514
4515
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004516String::ExternalStringResource* String::GetExternalStringResource() const {
4517 typedef internal::Object O;
4518 typedef internal::Internals I;
4519 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004520 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004521 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004522 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4523 result = reinterpret_cast<String::ExternalStringResource*>(value);
4524 } else {
4525 result = NULL;
4526 }
4527#ifdef V8_ENABLE_CHECKS
4528 VerifyExternalStringResource(result);
4529#endif
4530 return result;
4531}
4532
4533
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004534String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4535 String::Encoding* encoding_out) const {
4536 typedef internal::Object O;
4537 typedef internal::Internals I;
4538 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4539 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4540 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4541 ExternalStringResourceBase* resource = NULL;
4542 if (type == I::kExternalAsciiRepresentationTag ||
4543 type == I::kExternalTwoByteRepresentationTag) {
4544 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4545 resource = static_cast<ExternalStringResourceBase*>(value);
4546 }
4547#ifdef V8_ENABLE_CHECKS
4548 VerifyExternalStringResourceBase(resource, *encoding_out);
4549#endif
4550 return resource;
4551}
4552
4553
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004554bool Value::IsUndefined() const {
4555#ifdef V8_ENABLE_CHECKS
4556 return FullIsUndefined();
4557#else
4558 return QuickIsUndefined();
4559#endif
4560}
4561
4562bool Value::QuickIsUndefined() const {
4563 typedef internal::Object O;
4564 typedef internal::Internals I;
4565 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4566 if (!I::HasHeapObjectTag(obj)) return false;
4567 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4568 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4569}
4570
4571
4572bool Value::IsNull() const {
4573#ifdef V8_ENABLE_CHECKS
4574 return FullIsNull();
4575#else
4576 return QuickIsNull();
4577#endif
4578}
4579
4580bool Value::QuickIsNull() const {
4581 typedef internal::Object O;
4582 typedef internal::Internals I;
4583 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4584 if (!I::HasHeapObjectTag(obj)) return false;
4585 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4586 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4587}
4588
4589
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004590bool Value::IsString() const {
4591#ifdef V8_ENABLE_CHECKS
4592 return FullIsString();
4593#else
4594 return QuickIsString();
4595#endif
4596}
4597
4598bool Value::QuickIsString() const {
4599 typedef internal::Object O;
4600 typedef internal::Internals I;
4601 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4602 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004603 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004604}
4605
4606
4607Number* Number::Cast(v8::Value* value) {
4608#ifdef V8_ENABLE_CHECKS
4609 CheckCast(value);
4610#endif
4611 return static_cast<Number*>(value);
4612}
4613
4614
4615Integer* Integer::Cast(v8::Value* value) {
4616#ifdef V8_ENABLE_CHECKS
4617 CheckCast(value);
4618#endif
4619 return static_cast<Integer*>(value);
4620}
4621
4622
4623Date* Date::Cast(v8::Value* value) {
4624#ifdef V8_ENABLE_CHECKS
4625 CheckCast(value);
4626#endif
4627 return static_cast<Date*>(value);
4628}
4629
4630
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004631StringObject* StringObject::Cast(v8::Value* value) {
4632#ifdef V8_ENABLE_CHECKS
4633 CheckCast(value);
4634#endif
4635 return static_cast<StringObject*>(value);
4636}
4637
4638
4639NumberObject* NumberObject::Cast(v8::Value* value) {
4640#ifdef V8_ENABLE_CHECKS
4641 CheckCast(value);
4642#endif
4643 return static_cast<NumberObject*>(value);
4644}
4645
4646
4647BooleanObject* BooleanObject::Cast(v8::Value* value) {
4648#ifdef V8_ENABLE_CHECKS
4649 CheckCast(value);
4650#endif
4651 return static_cast<BooleanObject*>(value);
4652}
4653
4654
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004655RegExp* RegExp::Cast(v8::Value* value) {
4656#ifdef V8_ENABLE_CHECKS
4657 CheckCast(value);
4658#endif
4659 return static_cast<RegExp*>(value);
4660}
4661
4662
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004663Object* Object::Cast(v8::Value* value) {
4664#ifdef V8_ENABLE_CHECKS
4665 CheckCast(value);
4666#endif
4667 return static_cast<Object*>(value);
4668}
4669
4670
4671Array* Array::Cast(v8::Value* value) {
4672#ifdef V8_ENABLE_CHECKS
4673 CheckCast(value);
4674#endif
4675 return static_cast<Array*>(value);
4676}
4677
4678
4679Function* Function::Cast(v8::Value* value) {
4680#ifdef V8_ENABLE_CHECKS
4681 CheckCast(value);
4682#endif
4683 return static_cast<Function*>(value);
4684}
4685
4686
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004687Local<Value> External::Wrap(void* value) {
4688 return External::New(value);
4689}
4690
4691
4692void* External::Unwrap(Handle<v8::Value> obj) {
4693 return External::Cast(*obj)->Value();
4694}
4695
4696
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004697External* External::Cast(v8::Value* value) {
4698#ifdef V8_ENABLE_CHECKS
4699 CheckCast(value);
4700#endif
4701 return static_cast<External*>(value);
4702}
4703
4704
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004705Isolate* AccessorInfo::GetIsolate() const {
4706 return *reinterpret_cast<Isolate**>(&args_[-3]);
4707}
4708
4709
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004710Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004711 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004712}
4713
4714
4715Local<Object> AccessorInfo::This() const {
4716 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4717}
4718
4719
4720Local<Object> AccessorInfo::Holder() const {
4721 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4722}
4723
4724
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004725Handle<Primitive> Undefined(Isolate* isolate) {
4726 typedef internal::Object* S;
4727 typedef internal::Internals I;
4728 if (!I::IsInitialized(isolate)) return Undefined();
4729 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4730 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4731}
4732
4733
4734Handle<Primitive> Null(Isolate* isolate) {
4735 typedef internal::Object* S;
4736 typedef internal::Internals I;
4737 if (!I::IsInitialized(isolate)) return Null();
4738 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4739 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4740}
4741
4742
4743Handle<Boolean> True(Isolate* isolate) {
4744 typedef internal::Object* S;
4745 typedef internal::Internals I;
4746 if (!I::IsInitialized(isolate)) return True();
4747 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4748 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4749}
4750
4751
4752Handle<Boolean> False(Isolate* isolate) {
4753 typedef internal::Object* S;
4754 typedef internal::Internals I;
4755 if (!I::IsInitialized(isolate)) return False();
4756 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4757 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4758}
4759
4760
4761void Isolate::SetData(void* data) {
4762 typedef internal::Internals I;
4763 I::SetEmbedderData(this, data);
4764}
4765
4766
4767void* Isolate::GetData() {
4768 typedef internal::Internals I;
4769 return I::GetEmbedderData(this);
4770}
4771
4772
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004773Local<Value> Context::GetData() {
4774 return GetEmbedderData(0);
4775}
4776
4777void Context::SetData(Handle<Value> data) {
4778 SetEmbedderData(0, data);
4779}
4780
4781
4782Local<Value> Context::GetEmbedderData(int index) {
4783#ifndef V8_ENABLE_CHECKS
4784 typedef internal::Object O;
4785 typedef internal::Internals I;
4786 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4787 return Local<Value>(reinterpret_cast<Value*>(result));
4788#else
4789 return SlowGetEmbedderData(index);
4790#endif
4791}
4792
4793
4794void* Context::GetAlignedPointerFromEmbedderData(int index) {
4795#ifndef V8_ENABLE_CHECKS
4796 typedef internal::Internals I;
4797 return I::ReadEmbedderData<void*>(this, index);
4798#else
4799 return SlowGetAlignedPointerFromEmbedderData(index);
4800#endif
4801}
4802
4803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004804/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004805 * \example shell.cc
4806 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004807 * command-line and executes them.
4808 */
4809
4810
4811/**
4812 * \example process.cc
4813 */
4814
4815
4816} // namespace v8
4817
4818
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004819#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004820#undef TYPE_CHECK
4821
4822
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004823#endif // V8_H_