blob: 28bea2e3e4435b39a4b50a140513560b80b3fd85 [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
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +000079// TODO(svenpanne) Remove this when the Chrome's v8 bindings have been adapted.
80#define V8_DISABLE_DEPRECATIONS 1
81
82#if defined(__GNUC__) && !defined(V8_DISABLE_DEPRECATIONS)
83#define V8_DEPRECATED(func) func __attribute__ ((deprecated))
84#elif defined(_MSC_VER) && !defined(V8_DISABLE_DEPRECATIONS)
85#define V8_DEPRECATED(func) __declspec(deprecated) func
86#else
87#define V8_DEPRECATED(func) func
88#endif
89
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090/**
v8.team.kasperl727e9952008-09-02 14:56:44 +000091 * The v8 JavaScript engine.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092 */
93namespace v8 {
94
95class Context;
96class String;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +000097class StringObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098class Value;
99class Utils;
100class Number;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000101class NumberObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102class Object;
103class Array;
104class Int32;
105class Uint32;
106class External;
107class Primitive;
108class Boolean;
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000109class BooleanObject;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110class Integer;
111class Function;
112class Date;
113class ImplementationUtilities;
114class Signature;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000115class AccessorSignature;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116template <class T> class Handle;
117template <class T> class Local;
118template <class T> class Persistent;
119class FunctionTemplate;
120class ObjectTemplate;
121class Data;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000122class AccessorInfo;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000123class StackTrace;
124class StackFrame;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000125class Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000127namespace internal {
128
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000129class Arguments;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000130class Object;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000131class Heap;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132class HeapObject;
133class Isolate;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000134}
135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000137// --- Weak Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139
140/**
141 * A weak reference callback function.
142 *
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000143 * This callback should either explicitly invoke Dispose on |object| if
144 * V8 wrapper is not needed anymore, or 'revive' it by invocation of MakeWeak.
145 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 * \param object the weak global object to be reclaimed by the garbage collector
147 * \param parameter the value passed in when making the weak global object
148 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000149typedef void (*WeakReferenceCallback)(Persistent<Value> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 void* parameter);
151
152
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000153// --- Handles ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000155#define TYPE_CHECK(T, S) \
156 while (false) { \
157 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 }
159
160/**
161 * An object reference managed by the v8 garbage collector.
162 *
163 * All objects returned from v8 have to be tracked by the garbage
164 * collector so that it knows that the objects are still alive. Also,
165 * because the garbage collector may move objects, it is unsafe to
166 * point directly to an object. Instead, all objects are stored in
167 * handles which are known by the garbage collector and updated
168 * whenever an object moves. Handles should always be passed by value
169 * (except in cases like out-parameters) and they should never be
170 * allocated on the heap.
171 *
172 * There are two types of handles: local and persistent handles.
173 * Local handles are light-weight and transient and typically used in
174 * local operations. They are managed by HandleScopes. Persistent
175 * handles can be used when storing objects across several independent
176 * operations and have to be explicitly deallocated when they're no
177 * longer used.
178 *
179 * It is safe to extract the object stored in the handle by
180 * dereferencing the handle (for instance, to extract the Object* from
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000181 * a Handle<Object>); the value will still be governed by a handle
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 * behind the scenes and the same rules apply to these values as to
183 * their handles.
184 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000185template <class T> class Handle {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 /**
188 * Creates an empty handle.
189 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000190 inline Handle() : val_(0) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000191
192 /**
193 * Creates a new handle for the specified value.
194 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000195 inline explicit Handle(T* val) : val_(val) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196
197 /**
198 * Creates a handle for the contents of the specified handle. This
199 * constructor allows you to pass handles as arguments by value and
v8.team.kasperl727e9952008-09-02 14:56:44 +0000200 * to assign between handles. However, if you try to assign between
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 * incompatible handles, for instance from a Handle<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000202 * Handle<Number> it will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 * between compatible handles, for instance assigning a
204 * Handle<String> to a variable declared as Handle<Value>, is legal
205 * because String is a subclass of Value.
206 */
207 template <class S> inline Handle(Handle<S> that)
208 : val_(reinterpret_cast<T*>(*that)) {
209 /**
210 * This check fails when trying to convert between incompatible
211 * handles. For example, converting from a Handle<String> to a
212 * Handle<Number>.
213 */
214 TYPE_CHECK(T, S);
215 }
216
217 /**
218 * Returns true if the handle is empty.
219 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000220 inline bool IsEmpty() const { return val_ == 0; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 /**
223 * Sets the handle to be empty. IsEmpty() will then return true.
224 */
danno@chromium.orgb6451162011-08-17 14:33:23 +0000225 inline void Clear() { val_ = 0; }
226
227 inline T* operator->() const { return val_; }
228
229 inline T* operator*() const { return val_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230
231 /**
232 * Checks whether two handles are the same.
233 * Returns true if both are empty, or if the objects
234 * to which they refer are identical.
235 * The handles' references are not checked.
236 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000237 template <class S> inline bool operator==(Handle<S> that) const {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238 internal::Object** a = reinterpret_cast<internal::Object**>(**this);
239 internal::Object** b = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 if (a == 0) return b == 0;
241 if (b == 0) return false;
242 return *a == *b;
243 }
244
245 /**
246 * Checks whether two handles are different.
247 * Returns true if only one of the handles is empty, or if
248 * the objects to which they refer are different.
249 * The handles' references are not checked.
250 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000251 template <class S> inline bool operator!=(Handle<S> that) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 return !operator==(that);
253 }
254
255 template <class S> static inline Handle<T> Cast(Handle<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000256#ifdef V8_ENABLE_CHECKS
257 // If we're going to perform the type check then we have to check
258 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 if (that.IsEmpty()) return Handle<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000260#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 return Handle<T>(T::Cast(*that));
262 }
263
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000264 template <class S> inline Handle<S> As() {
265 return Handle<S>::Cast(*this);
266 }
267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 private:
269 T* val_;
270};
271
272
273/**
274 * A light-weight stack-allocated object handle. All operations
275 * that return objects from within v8 return them in local handles. They
276 * are created within HandleScopes, and all local handles allocated within a
277 * handle scope are destroyed when the handle scope is destroyed. Hence it
278 * is not necessary to explicitly deallocate local handles.
279 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000280template <class T> class Local : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 public:
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000282 inline Local();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 template <class S> inline Local(Local<S> that)
284 : Handle<T>(reinterpret_cast<T*>(*that)) {
285 /**
286 * This check fails when trying to convert between incompatible
287 * handles. For example, converting from a Handle<String> to a
288 * Handle<Number>.
289 */
290 TYPE_CHECK(T, S);
291 }
292 template <class S> inline Local(S* that) : Handle<T>(that) { }
293 template <class S> static inline Local<T> Cast(Local<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000294#ifdef V8_ENABLE_CHECKS
295 // If we're going to perform the type check then we have to check
296 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 if (that.IsEmpty()) return Local<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000298#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 return Local<T>(T::Cast(*that));
300 }
301
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000302 template <class S> inline Local<S> As() {
303 return Local<S>::Cast(*this);
304 }
305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 /** Create a local handle for the content of another handle.
307 * The referee is kept alive by the local handle even when
308 * the original handle is destroyed/disposed.
309 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000310 inline static Local<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311};
312
313
314/**
315 * An object reference that is independent of any handle scope. Where
v8.team.kasperl727e9952008-09-02 14:56:44 +0000316 * a Local handle only lives as long as the HandleScope in which it was
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317 * allocated, a Persistent handle remains valid until it is explicitly
318 * disposed.
319 *
320 * A persistent handle contains a reference to a storage cell within
321 * the v8 engine which holds an object value and which is updated by
322 * the garbage collector whenever the object is moved. A new storage
323 * cell can be created using Persistent::New and existing handles can
324 * be disposed using Persistent::Dispose. Since persistent handles
325 * are passed by value you may have many persistent handle objects
326 * that point to the same storage cell. For instance, if you pass a
327 * persistent handle as an argument to a function you will not get two
328 * different storage cells but rather two references to the same
329 * storage cell.
330 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000331template <class T> class Persistent : public Handle<T> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 /**
334 * Creates an empty persistent handle that doesn't point to any
335 * storage cell.
336 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000337 inline Persistent();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338
339 /**
340 * Creates a persistent handle for the same storage cell as the
341 * specified handle. This constructor allows you to pass persistent
342 * handles as arguments by value and to assign between persistent
v8.team.kasperl727e9952008-09-02 14:56:44 +0000343 * handles. However, attempting to assign between incompatible
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344 * persistent handles, for instance from a Persistent<String> to a
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000345 * Persistent<Number> will cause a compile-time error. Assigning
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 * between compatible persistent handles, for instance assigning a
347 * Persistent<String> to a variable declared as Persistent<Value>,
v8.team.kasperl727e9952008-09-02 14:56:44 +0000348 * is allowed as String is a subclass of Value.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 */
350 template <class S> inline Persistent(Persistent<S> that)
351 : Handle<T>(reinterpret_cast<T*>(*that)) {
352 /**
353 * This check fails when trying to convert between incompatible
354 * handles. For example, converting from a Handle<String> to a
355 * Handle<Number>.
356 */
357 TYPE_CHECK(T, S);
358 }
359
360 template <class S> inline Persistent(S* that) : Handle<T>(that) { }
361
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000362 /**
363 * "Casts" a plain handle which is known to be a persistent handle
364 * to a persistent handle.
365 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000366 template <class S> explicit inline Persistent(Handle<S> that)
367 : Handle<T>(*that) { }
368
369 template <class S> static inline Persistent<T> Cast(Persistent<S> that) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000370#ifdef V8_ENABLE_CHECKS
371 // If we're going to perform the type check then we have to check
372 // that the handle isn't empty before doing the checked cast.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000374#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375 return Persistent<T>(T::Cast(*that));
376 }
377
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000378 template <class S> inline Persistent<S> As() {
379 return Persistent<S>::Cast(*this);
380 }
381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000383 * Creates a new persistent handle for an existing local or
384 * persistent handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000386 inline static Persistent<T> New(Handle<T> that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388 /**
389 * Releases the storage cell referenced by this persistent handle.
390 * Does not remove the reference to the cell from any handles.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000391 * This handle's reference, and any other references to the storage
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392 * cell remain and IsEmpty will still return false.
393 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000394 inline void Dispose();
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +0000395 inline void Dispose(Isolate* isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396
397 /**
398 * Make the reference to this object weak. When only weak handles
399 * refer to the object, the garbage collector will perform a
400 * callback to the given V8::WeakReferenceCallback function, passing
401 * it the object reference and the given parameters.
402 */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000403 inline void MakeWeak(void* parameters, WeakReferenceCallback callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000405 /** Clears the weak reference to this object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000406 inline void ClearWeak();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407
408 /**
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000409 * Marks the reference to this object independent. Garbage collector
410 * is free to ignore any object groups containing this object.
411 * Weak callback for an independent handle should not
412 * assume that it will be preceded by a global GC prologue callback
413 * or followed by a global GC epilogue callback.
414 */
415 inline void MarkIndependent();
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000416 inline void MarkIndependent(Isolate* isolate);
417
418 /**
419 * Marks the reference to this object partially dependent. Partially
420 * dependent handles only depend on other partially dependent handles and
421 * these dependencies are provided through object groups. It provides a way
422 * to build smaller object groups for young objects that represent only a
423 * subset of all external dependencies. This mark is automatically cleared
424 * after each garbage collection.
425 */
426 inline void MarkPartiallyDependent();
427 inline void MarkPartiallyDependent(Isolate* isolate);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000428
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000429 /** Returns true if this handle was previously marked as independent. */
430 inline bool IsIndependent() const;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +0000431 inline bool IsIndependent(Isolate* isolate) const;
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000432
433 /** Checks if the handle holds the only reference to an object. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000434 inline bool IsNearDeath() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000436 /** Returns true if the handle's reference is weak. */
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000437 inline bool IsWeak() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000439 /**
440 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo
441 * interface description in v8-profiler.h for details.
442 */
443 inline void SetWrapperClassId(uint16_t class_id);
444
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000445 /**
446 * Returns the class ID previously assigned to this handle or 0 if no class
447 * ID was previously assigned.
448 */
449 inline uint16_t WrapperClassId() const;
450
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 private:
452 friend class ImplementationUtilities;
453 friend class ObjectTemplate;
454};
455
456
v8.team.kasperl727e9952008-09-02 14:56:44 +0000457 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 * A stack-allocated class that governs a number of local handles.
459 * After a handle scope has been created, all local handles will be
460 * allocated within that handle scope until either the handle scope is
461 * deleted or another handle scope is created. If there is already a
462 * handle scope and a new one is created, all allocations will take
v8.team.kasperl727e9952008-09-02 14:56:44 +0000463 * place in the new handle scope until it is deleted. After that,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 * new handles will again be allocated in the original handle scope.
465 *
466 * After the handle scope of a local handle has been deleted the
467 * garbage collector will no longer track the object stored in the
468 * handle and may deallocate it. The behavior of accessing a handle
469 * for which the handle scope has been deleted is undefined.
470 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000471class V8EXPORT HandleScope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000473 HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000475 ~HandleScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
477 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 * Closes the handle scope and returns the value as a handle in the
479 * previous scope, which is the new current scope after the call.
480 */
481 template <class T> Local<T> Close(Handle<T> value);
482
483 /**
484 * Counts the number of allocated handles.
485 */
486 static int NumberOfHandles();
487
488 /**
489 * Creates a new handle with the given value.
490 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000491 static internal::Object** CreateHandle(internal::Object* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000492 // Faster version, uses HeapObject to obtain the current Isolate.
493 static internal::Object** CreateHandle(internal::HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494
495 private:
496 // Make it impossible to create heap-allocated or illegal handle
497 // scopes by disallowing certain operations.
498 HandleScope(const HandleScope&);
499 void operator=(const HandleScope&);
500 void* operator new(size_t size);
501 void operator delete(void*, size_t);
502
ager@chromium.org3811b432009-10-28 14:53:37 +0000503 // This Data class is accessible internally as HandleScopeData through a
504 // typedef in the ImplementationUtilities class.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000505 class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000507 internal::Object** next;
508 internal::Object** limit;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000509 int level;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510 inline void Initialize() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 next = limit = NULL;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000512 level = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000513 }
514 };
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000515
lrn@chromium.org303ada72010-10-27 09:33:13 +0000516 void Leave();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000518 internal::Isolate* isolate_;
lrn@chromium.org303ada72010-10-27 09:33:13 +0000519 internal::Object** prev_next_;
520 internal::Object** prev_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000522 // Allow for the active closing of HandleScopes which allows to pass a handle
523 // from the HandleScope being closed to the next top most HandleScope.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 bool is_closed_;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000525 internal::Object** RawClose(internal::Object** value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 friend class ImplementationUtilities;
528};
529
530
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000531// --- Special objects ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
533
534/**
535 * The superclass of values and API object templates.
536 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000537class V8EXPORT Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538 private:
539 Data();
540};
541
542
543/**
544 * Pre-compilation data that can be associated with a script. This
545 * data can be calculated for a script in advance of actually
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000546 * compiling it, and can be stored between compilations. When script
v8.team.kasperl727e9952008-09-02 14:56:44 +0000547 * data is given to the compile method compilation will be faster.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000549class V8EXPORT ScriptData { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 public:
551 virtual ~ScriptData() { }
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000552
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000553 /**
554 * Pre-compiles the specified script (context-independent).
555 *
556 * \param input Pointer to UTF-8 script source code.
557 * \param length Length of UTF-8 script source code.
558 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 static ScriptData* PreCompile(const char* input, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000561 /**
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000562 * Pre-compiles the specified script (context-independent).
563 *
564 * NOTE: Pre-compilation using this method cannot happen on another thread
565 * without using Lockers.
566 *
567 * \param source Script source code.
568 */
569 static ScriptData* PreCompile(Handle<String> source);
570
571 /**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000572 * Load previous pre-compilation data.
573 *
574 * \param data Pointer to data returned by a call to Data() of a previous
575 * ScriptData. Ownership is not transferred.
576 * \param length Length of data.
577 */
578 static ScriptData* New(const char* data, int length);
579
580 /**
581 * Returns the length of Data().
582 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 virtual int Length() = 0;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000584
585 /**
586 * Returns a serialized representation of this ScriptData that can later be
587 * passed to New(). NOTE: Serialized data is platform-dependent.
588 */
589 virtual const char* Data() = 0;
590
591 /**
592 * Returns true if the source code could not be parsed.
593 */
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000594 virtual bool HasError() = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595};
596
597
598/**
599 * The origin, within a file, of a script.
600 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000601class ScriptOrigin {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000603 inline ScriptOrigin(
604 Handle<Value> resource_name,
605 Handle<Integer> resource_line_offset = Handle<Integer>(),
606 Handle<Integer> resource_column_offset = Handle<Integer>())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 : resource_name_(resource_name),
608 resource_line_offset_(resource_line_offset),
609 resource_column_offset_(resource_column_offset) { }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000610 inline Handle<Value> ResourceName() const;
611 inline Handle<Integer> ResourceLineOffset() const;
612 inline Handle<Integer> ResourceColumnOffset() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 private:
mads.s.agercbaa0602008-08-14 13:41:48 +0000614 Handle<Value> resource_name_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 Handle<Integer> resource_line_offset_;
616 Handle<Integer> resource_column_offset_;
617};
618
619
620/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000621 * A compiled JavaScript script.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000623class V8EXPORT Script {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625 /**
ager@chromium.org5c838252010-02-19 08:53:10 +0000626 * Compiles the specified script (context-independent).
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000627 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000628 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000629 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000630 * when New() returns
631 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
632 * using pre_data speeds compilation if it's done multiple times.
633 * Owned by caller, no references are kept when New() returns.
634 * \param script_data Arbitrary data associated with script. Using
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000635 * this has same effect as calling SetData(), but allows data to be
ager@chromium.org5c838252010-02-19 08:53:10 +0000636 * available to compile event handlers.
637 * \return Compiled script object (context independent; when run it
638 * will use the currently entered context).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 */
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 static Local<Script> New(Handle<String> source,
641 ScriptOrigin* origin = NULL,
642 ScriptData* pre_data = NULL,
643 Handle<String> script_data = Handle<String>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644
mads.s.agercbaa0602008-08-14 13:41:48 +0000645 /**
646 * Compiles the specified script using the specified file name
647 * object (typically a string) as the script's origin.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000648 *
ager@chromium.org5c838252010-02-19 08:53:10 +0000649 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000650 * \param file_name file name object (typically a string) to be used
ager@chromium.org5c838252010-02-19 08:53:10 +0000651 * as the script's origin.
652 * \return Compiled script object (context independent; when run it
653 * will use the currently entered context).
654 */
655 static Local<Script> New(Handle<String> source,
656 Handle<Value> file_name);
657
658 /**
659 * Compiles the specified script (bound to current context).
660 *
661 * \param source Script source code.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000662 * \param origin Script origin, owned by caller, no references are kept
ager@chromium.org5c838252010-02-19 08:53:10 +0000663 * when Compile() returns
664 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
665 * using pre_data speeds compilation if it's done multiple times.
666 * Owned by caller, no references are kept when Compile() returns.
667 * \param script_data Arbitrary data associated with script. Using
668 * this has same effect as calling SetData(), but makes data available
669 * earlier (i.e. to compile event handlers).
670 * \return Compiled script object, bound to the context that was active
671 * when this function was called. When run it will always use this
672 * context.
mads.s.agercbaa0602008-08-14 13:41:48 +0000673 */
674 static Local<Script> Compile(Handle<String> source,
ager@chromium.org5c838252010-02-19 08:53:10 +0000675 ScriptOrigin* origin = NULL,
676 ScriptData* pre_data = NULL,
677 Handle<String> script_data = Handle<String>());
678
679 /**
680 * Compiles the specified script using the specified file name
681 * object (typically a string) as the script's origin.
682 *
683 * \param source Script source code.
684 * \param file_name File name to use as script's origin
685 * \param script_data Arbitrary data associated with script. Using
686 * this has same effect as calling SetData(), but makes data available
687 * earlier (i.e. to compile event handlers).
688 * \return Compiled script object, bound to the context that was active
689 * when this function was called. When run it will always use this
690 * context.
691 */
692 static Local<Script> Compile(Handle<String> source,
693 Handle<Value> file_name,
694 Handle<String> script_data = Handle<String>());
mads.s.agercbaa0602008-08-14 13:41:48 +0000695
v8.team.kasperl727e9952008-09-02 14:56:44 +0000696 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000697 * Runs the script returning the resulting value. If the script is
698 * context independent (created using ::New) it will be run in the
699 * currently entered context. If it is context specific (created
700 * using ::Compile) it will be run in the context in which it was
701 * compiled.
v8.team.kasperl727e9952008-09-02 14:56:44 +0000702 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 Local<Value> Run();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000704
705 /**
706 * Returns the script id value.
707 */
708 Local<Value> Id();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000709
710 /**
711 * Associate an additional data object with the script. This is mainly used
712 * with the debugger as this data object is only available through the
713 * debugger API.
714 */
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000715 void SetData(Handle<String> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716};
717
718
719/**
720 * An error message.
721 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000722class V8EXPORT Message {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 public:
ager@chromium.org32912102009-01-16 10:38:43 +0000724 Local<String> Get() const;
725 Local<String> GetSourceLine() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000727 /**
728 * Returns the resource name for the script from where the function causing
729 * the error originates.
730 */
ager@chromium.org32912102009-01-16 10:38:43 +0000731 Handle<Value> GetScriptResourceName() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000733 /**
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000734 * Returns the resource data for the script from where the function causing
735 * the error originates.
736 */
737 Handle<Value> GetScriptData() const;
738
739 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000740 * Exception stack trace. By default stack traces are not captured for
741 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
742 * to change this option.
743 */
744 Handle<StackTrace> GetStackTrace() const;
745
746 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000747 * Returns the number, 1-based, of the line where the error occurred.
748 */
ager@chromium.org32912102009-01-16 10:38:43 +0000749 int GetLineNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000751 /**
752 * Returns the index within the script of the first character where
753 * the error occurred.
754 */
ager@chromium.org32912102009-01-16 10:38:43 +0000755 int GetStartPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000756
757 /**
758 * Returns the index within the script of the last character where
759 * the error occurred.
760 */
ager@chromium.org32912102009-01-16 10:38:43 +0000761 int GetEndPosition() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000762
763 /**
764 * Returns the index within the line of the first character where
765 * the error occurred.
766 */
ager@chromium.org32912102009-01-16 10:38:43 +0000767 int GetStartColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000768
769 /**
770 * Returns the index within the line of the last character where
771 * the error occurred.
772 */
ager@chromium.org32912102009-01-16 10:38:43 +0000773 int GetEndColumn() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000774
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 // TODO(1245381): Print to a string instead of on a FILE.
776 static void PrintCurrentStackTrace(FILE* out);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000777
778 static const int kNoLineNumberInfo = 0;
779 static const int kNoColumnInfo = 0;
780};
781
782
783/**
784 * Representation of a JavaScript stack trace. The information collected is a
785 * snapshot of the execution stack and the information remains valid after
786 * execution continues.
787 */
788class V8EXPORT StackTrace {
789 public:
790 /**
791 * Flags that determine what information is placed captured for each
792 * StackFrame when grabbing the current stack trace.
793 */
794 enum StackTraceOptions {
795 kLineNumber = 1,
796 kColumnOffset = 1 << 1 | kLineNumber,
797 kScriptName = 1 << 2,
798 kFunctionName = 1 << 3,
799 kIsEval = 1 << 4,
800 kIsConstructor = 1 << 5,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000801 kScriptNameOrSourceURL = 1 << 6,
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000802 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000803 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000804 };
805
806 /**
807 * Returns a StackFrame at a particular index.
808 */
809 Local<StackFrame> GetFrame(uint32_t index) const;
810
811 /**
812 * Returns the number of StackFrames.
813 */
814 int GetFrameCount() const;
815
816 /**
817 * Returns StackTrace as a v8::Array that contains StackFrame objects.
818 */
819 Local<Array> AsArray();
820
821 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000822 * Grab a snapshot of the current JavaScript execution stack.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000823 *
824 * \param frame_limit The maximum number of stack frames we want to capture.
825 * \param options Enumerates the set of things we will capture for each
826 * StackFrame.
827 */
828 static Local<StackTrace> CurrentStackTrace(
829 int frame_limit,
830 StackTraceOptions options = kOverview);
831};
832
833
834/**
835 * A single JavaScript stack frame.
836 */
837class V8EXPORT StackFrame {
838 public:
839 /**
840 * Returns the number, 1-based, of the line for the associate function call.
841 * This method will return Message::kNoLineNumberInfo if it is unable to
842 * retrieve the line number, or if kLineNumber was not passed as an option
843 * when capturing the StackTrace.
844 */
845 int GetLineNumber() const;
846
847 /**
848 * Returns the 1-based column offset on the line for the associated function
849 * call.
850 * This method will return Message::kNoColumnInfo if it is unable to retrieve
851 * the column number, or if kColumnOffset was not passed as an option when
852 * capturing the StackTrace.
853 */
854 int GetColumn() const;
855
856 /**
857 * Returns the name of the resource that contains the script for the
858 * function for this StackFrame.
859 */
860 Local<String> GetScriptName() const;
861
862 /**
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000863 * Returns the name of the resource that contains the script for the
864 * function for this StackFrame or sourceURL value if the script name
865 * is undefined and its source ends with //@ sourceURL=... string.
866 */
867 Local<String> GetScriptNameOrSourceURL() const;
868
869 /**
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000870 * Returns the name of the function associated with this stack frame.
871 */
872 Local<String> GetFunctionName() const;
873
874 /**
875 * Returns whether or not the associated function is compiled via a call to
876 * eval().
877 */
878 bool IsEval() const;
879
880 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000881 * Returns whether or not the associated function is called as a
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000882 * constructor via "new".
883 */
884 bool IsConstructor() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885};
886
887
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000888// --- Value ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
890
891/**
v8.team.kasperl727e9952008-09-02 14:56:44 +0000892 * The superclass of all JavaScript values and objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000894class Value : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 /**
897 * Returns true if this value is the undefined value. See ECMA-262
898 * 4.3.10.
899 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000900 inline bool IsUndefined() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901
902 /**
903 * Returns true if this value is the null value. See ECMA-262
904 * 4.3.11.
905 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +0000906 inline bool IsNull() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907
908 /**
909 * Returns true if this value is true.
910 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000911 V8EXPORT bool IsTrue() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912
913 /**
914 * Returns true if this value is false.
915 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000916 V8EXPORT bool IsFalse() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
918 /**
919 * Returns true if this value is an instance of the String type.
920 * See ECMA-262 8.4.
921 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000922 inline bool IsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
924 /**
925 * Returns true if this value is a function.
926 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000927 V8EXPORT bool IsFunction() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
929 /**
930 * Returns true if this value is an array.
931 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000932 V8EXPORT bool IsArray() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933
v8.team.kasperl727e9952008-09-02 14:56:44 +0000934 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000935 * Returns true if this value is an object.
936 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000937 V8EXPORT bool IsObject() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938
v8.team.kasperl727e9952008-09-02 14:56:44 +0000939 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940 * Returns true if this value is boolean.
941 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000942 V8EXPORT bool IsBoolean() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000943
v8.team.kasperl727e9952008-09-02 14:56:44 +0000944 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945 * Returns true if this value is a number.
946 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000947 V8EXPORT bool IsNumber() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948
v8.team.kasperl727e9952008-09-02 14:56:44 +0000949 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950 * Returns true if this value is external.
951 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000952 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953
v8.team.kasperl727e9952008-09-02 14:56:44 +0000954 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 * Returns true if this value is a 32-bit signed integer.
956 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000957 V8EXPORT bool IsInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000959 /**
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000960 * Returns true if this value is a 32-bit unsigned integer.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000961 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000962 V8EXPORT bool IsUint32() const;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000963
964 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000965 * Returns true if this value is a Date.
966 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000967 V8EXPORT bool IsDate() const;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000968
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000969 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +0000970 * Returns true if this value is a Boolean object.
971 */
972 V8EXPORT bool IsBooleanObject() const;
973
974 /**
975 * Returns true if this value is a Number object.
976 */
977 V8EXPORT bool IsNumberObject() const;
978
979 /**
980 * Returns true if this value is a String object.
981 */
982 V8EXPORT bool IsStringObject() const;
983
984 /**
985 * Returns true if this value is a NativeError.
986 */
987 V8EXPORT bool IsNativeError() const;
988
989 /**
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000990 * Returns true if this value is a RegExp.
991 */
992 V8EXPORT bool IsRegExp() const;
993
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000994 V8EXPORT Local<Boolean> ToBoolean() const;
995 V8EXPORT Local<Number> ToNumber() const;
996 V8EXPORT Local<String> ToString() const;
997 V8EXPORT Local<String> ToDetailString() const;
998 V8EXPORT Local<Object> ToObject() const;
999 V8EXPORT Local<Integer> ToInteger() const;
1000 V8EXPORT Local<Uint32> ToUint32() const;
1001 V8EXPORT Local<Int32> ToInt32() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002
1003 /**
1004 * Attempts to convert a string to an array index.
1005 * Returns an empty handle if the conversion fails.
1006 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001007 V8EXPORT Local<Uint32> ToArrayIndex() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001009 V8EXPORT bool BooleanValue() const;
1010 V8EXPORT double NumberValue() const;
1011 V8EXPORT int64_t IntegerValue() const;
1012 V8EXPORT uint32_t Uint32Value() const;
1013 V8EXPORT int32_t Int32Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001014
1015 /** JS == */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001016 V8EXPORT bool Equals(Handle<Value> that) const;
1017 V8EXPORT bool StrictEquals(Handle<Value> that) const;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001018
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001019 private:
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00001020 inline bool QuickIsUndefined() const;
1021 inline bool QuickIsNull() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001022 inline bool QuickIsString() const;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00001023 V8EXPORT bool FullIsUndefined() const;
1024 V8EXPORT bool FullIsNull() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001025 V8EXPORT bool FullIsString() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001026};
1027
1028
1029/**
1030 * The superclass of primitive values. See ECMA-262 4.3.2.
1031 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001032class Primitive : public Value { };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033
1034
1035/**
1036 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
1037 * or false value.
1038 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001039class Boolean : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001041 V8EXPORT bool Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 static inline Handle<Boolean> New(bool value);
1043};
1044
1045
1046/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001047 * A JavaScript string value (ECMA-262, 4.3.17).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001049class String : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050 public:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001051 enum Encoding {
1052 UNKNOWN_ENCODING = 0x1,
1053 TWO_BYTE_ENCODING = 0x0,
1054 ASCII_ENCODING = 0x4
1055 };
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001056 /**
1057 * Returns the number of characters in this string.
1058 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001059 V8EXPORT int Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060
v8.team.kasperl727e9952008-09-02 14:56:44 +00001061 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001062 * Returns the number of bytes in the UTF-8 encoded
1063 * representation of this string.
1064 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001065 V8EXPORT int Utf8Length() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001066
1067 /**
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001068 * A fast conservative check for non-ASCII characters. May
1069 * return true even for ASCII strings, but if it returns
1070 * false you can be sure that all characters are in the range
1071 * 0-127.
1072 */
1073 V8EXPORT bool MayContainNonAscii() const;
1074
1075 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001076 * Write the contents of the string to an external buffer.
1077 * If no arguments are given, expects the buffer to be large
1078 * enough to hold the entire string and NULL terminator. Copies
1079 * the contents of the string and the NULL terminator into the
1080 * buffer.
1081 *
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001082 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
1083 * before the end of the buffer.
1084 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00001085 * Copies up to length characters into the output buffer.
1086 * Only null-terminates if there is enough space in the buffer.
1087 *
1088 * \param buffer The buffer into which the string will be copied.
1089 * \param start The starting position within the string at which
1090 * copying begins.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001091 * \param length The number of characters to copy from the string. For
1092 * WriteUtf8 the number of bytes in the buffer.
ager@chromium.org357bf652010-04-12 11:30:10 +00001093 * \param nchars_ref The number of characters written, can be NULL.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001094 * \param options Various options that might affect performance of this or
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001095 * subsequent operations.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001096 * \return The number of characters copied to the buffer excluding the null
1097 * terminator. For WriteUtf8: The number of bytes copied to the buffer
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001098 * including the null terminator (if written).
v8.team.kasperl727e9952008-09-02 14:56:44 +00001099 */
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001100 enum WriteOptions {
1101 NO_OPTIONS = 0,
1102 HINT_MANY_WRITES_EXPECTED = 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001103 NO_NULL_TERMINATION = 2,
1104 PRESERVE_ASCII_NULL = 4
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001105 };
1106
lrn@chromium.org34e60782011-09-15 07:25:40 +00001107 // 16-bit character codes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001108 V8EXPORT int Write(uint16_t* buffer,
1109 int start = 0,
1110 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001111 int options = NO_OPTIONS) const;
1112 // ASCII characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001113 V8EXPORT int WriteAscii(char* buffer,
1114 int start = 0,
1115 int length = -1,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001116 int options = NO_OPTIONS) const;
1117 // UTF-8 encoded characters.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001118 V8EXPORT int WriteUtf8(char* buffer,
1119 int length = -1,
1120 int* nchars_ref = NULL,
lrn@chromium.org34e60782011-09-15 07:25:40 +00001121 int options = NO_OPTIONS) const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122
v8.team.kasperl727e9952008-09-02 14:56:44 +00001123 /**
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001124 * A zero length string.
1125 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001126 V8EXPORT static v8::Local<v8::String> Empty();
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001127 inline static v8::Local<v8::String> Empty(Isolate* isolate);
ager@chromium.org563b8dc2009-03-20 14:23:52 +00001128
1129 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001130 * Returns true if the string is external
1131 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001132 V8EXPORT bool IsExternal() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133
v8.team.kasperl727e9952008-09-02 14:56:44 +00001134 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001135 * Returns true if the string is both external and ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001136 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001137 V8EXPORT bool IsExternalAscii() const;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001138
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001139 class V8EXPORT ExternalStringResourceBase { // NOLINT
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001140 public:
1141 virtual ~ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001142
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001143 protected:
1144 ExternalStringResourceBase() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001145
1146 /**
1147 * Internally V8 will call this Dispose method when the external string
1148 * resource is no longer needed. The default implementation will use the
1149 * delete operator. This method can be overridden in subclasses to
1150 * control how allocated external string resources are disposed.
1151 */
1152 virtual void Dispose() { delete this; }
1153
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001154 private:
1155 // Disallow copying and assigning.
1156 ExternalStringResourceBase(const ExternalStringResourceBase&);
1157 void operator=(const ExternalStringResourceBase&);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001158
1159 friend class v8::internal::Heap;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001160 };
1161
v8.team.kasperl727e9952008-09-02 14:56:44 +00001162 /**
1163 * An ExternalStringResource is a wrapper around a two-byte string
1164 * buffer that resides outside V8's heap. Implement an
1165 * ExternalStringResource to manage the life cycle of the underlying
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001166 * buffer. Note that the string data must be immutable.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001167 */
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001168 class V8EXPORT ExternalStringResource
1169 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 public:
1171 /**
1172 * Override the destructor to manage the life cycle of the underlying
1173 * buffer.
1174 */
1175 virtual ~ExternalStringResource() {}
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001176
1177 /**
1178 * The string data from the underlying buffer.
1179 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 virtual const uint16_t* data() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001181
1182 /**
1183 * The length of the string. That is, the number of two-byte characters.
1184 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185 virtual size_t length() const = 0;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 protected:
1188 ExternalStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189 };
1190
1191 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001192 * An ExternalAsciiStringResource is a wrapper around an ASCII
v8.team.kasperl727e9952008-09-02 14:56:44 +00001193 * string buffer that resides outside V8's heap. Implement an
1194 * ExternalAsciiStringResource to manage the life cycle of the
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001195 * underlying buffer. Note that the string data must be immutable
lrn@chromium.org34e60782011-09-15 07:25:40 +00001196 * and that the data must be strict (7-bit) ASCII, not Latin-1 or
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001197 * UTF-8, which would require special treatment internally in the
1198 * engine and, in the case of UTF-8, do not allow efficient indexing.
1199 * Use String::New or convert to 16 bit data for non-ASCII.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001200 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001202 class V8EXPORT ExternalAsciiStringResource
1203 : public ExternalStringResourceBase {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 public:
1205 /**
1206 * Override the destructor to manage the life cycle of the underlying
1207 * buffer.
1208 */
1209 virtual ~ExternalAsciiStringResource() {}
1210 /** The string data from the underlying buffer.*/
1211 virtual const char* data() const = 0;
lrn@chromium.org34e60782011-09-15 07:25:40 +00001212 /** The number of ASCII characters in the string.*/
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 virtual size_t length() const = 0;
1214 protected:
1215 ExternalAsciiStringResource() {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 };
1217
1218 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001219 * If the string is an external string, return the ExternalStringResourceBase
1220 * regardless of the encoding, otherwise return NULL. The encoding of the
1221 * string is returned in encoding_out.
1222 */
1223 inline ExternalStringResourceBase* GetExternalStringResourceBase(
1224 Encoding* encoding_out) const;
1225
1226 /**
ager@chromium.org9085a012009-05-11 19:22:57 +00001227 * Get the ExternalStringResource for an external string. Returns
1228 * NULL if IsExternal() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001230 inline ExternalStringResource* GetExternalStringResource() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231
1232 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001233 * Get the ExternalAsciiStringResource for an external ASCII string.
ager@chromium.org9085a012009-05-11 19:22:57 +00001234 * Returns NULL if IsExternalAscii() doesn't return true.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 */
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001236 V8EXPORT const ExternalAsciiStringResource* GetExternalAsciiStringResource()
1237 const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001239 static inline String* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240
1241 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001242 * Allocates a new string from either UTF-8 encoded or ASCII data.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 * The second parameter 'length' gives the buffer length.
lrn@chromium.org34e60782011-09-15 07:25:40 +00001244 * If the data is UTF-8 encoded, the caller must
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 * be careful to supply the length parameter.
1246 * If it is not given, the function calls
1247 * 'strlen' to determine the buffer length, it might be
kasper.lund7276f142008-07-30 08:49:36 +00001248 * wrong if 'data' contains a null character.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001250 V8EXPORT static Local<String> New(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251
lrn@chromium.org34e60782011-09-15 07:25:40 +00001252 /** Allocates a new string from 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001253 V8EXPORT static Local<String> New(const uint16_t* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254
1255 /** Creates a symbol. Returns one if it exists already.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001256 V8EXPORT static Local<String> NewSymbol(const char* data, int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257
v8.team.kasperl727e9952008-09-02 14:56:44 +00001258 /**
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001259 * Creates a new string by concatenating the left and the right strings
1260 * passed in as parameters.
1261 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001262 V8EXPORT static Local<String> Concat(Handle<String> left,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00001263 Handle<String> right);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001264
1265 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001266 * Creates a new external string using the data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001267 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001268 * resource will be disposed by calling its Dispose method. The caller of
1269 * this function should not otherwise delete or modify the resource. Neither
1270 * should the underlying buffer be deallocated or modified except through the
1271 * destructor of the external string resource.
v8.team.kasperl727e9952008-09-02 14:56:44 +00001272 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001273 V8EXPORT static Local<String> NewExternal(ExternalStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001274
ager@chromium.org6f10e412009-02-13 10:11:16 +00001275 /**
1276 * Associate an external string resource with this string by transforming it
1277 * in place so that existing references to this string in the JavaScript heap
1278 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001279 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001280 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001281 * The string is not modified if the operation fails. See NewExternal for
1282 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001283 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001284 V8EXPORT bool MakeExternal(ExternalStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285
v8.team.kasperl727e9952008-09-02 14:56:44 +00001286 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001287 * Creates a new external string using the ASCII data defined in the given
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001288 * resource. When the external string is no longer live on V8's heap the
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001289 * resource will be disposed by calling its Dispose method. The caller of
1290 * this function should not otherwise delete or modify the resource. Neither
1291 * should the underlying buffer be deallocated or modified except through the
1292 * destructor of the external string resource.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001293 */ V8EXPORT static Local<String> NewExternal(
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001294 ExternalAsciiStringResource* resource);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001295
ager@chromium.org6f10e412009-02-13 10:11:16 +00001296 /**
1297 * Associate an external string resource with this string by transforming it
1298 * in place so that existing references to this string in the JavaScript heap
1299 * will use the external string resource. The external string resource's
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001300 * character contents need to be equivalent to this string.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001301 * Returns true if the string has been changed to be an external string.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001302 * The string is not modified if the operation fails. See NewExternal for
1303 * information on the lifetime of the resource.
ager@chromium.org6f10e412009-02-13 10:11:16 +00001304 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001305 V8EXPORT bool MakeExternal(ExternalAsciiStringResource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001307 /**
1308 * Returns true if this string can be made external.
1309 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001310 V8EXPORT bool CanMakeExternal();
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001311
lrn@chromium.org34e60782011-09-15 07:25:40 +00001312 /** Creates an undetectable string from the supplied ASCII or UTF-8 data.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001313 V8EXPORT static Local<String> NewUndetectable(const char* data,
1314 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315
lrn@chromium.org34e60782011-09-15 07:25:40 +00001316 /** Creates an undetectable string from the supplied 16-bit character codes.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001317 V8EXPORT static Local<String> NewUndetectable(const uint16_t* data,
1318 int length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319
1320 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001321 * Converts an object to a UTF-8-encoded character array. Useful if
ager@chromium.org71daaf62009-04-01 07:22:49 +00001322 * you want to print the object. If conversion to a string fails
lrn@chromium.org34e60782011-09-15 07:25:40 +00001323 * (e.g. due to an exception in the toString() method of the object)
ager@chromium.org71daaf62009-04-01 07:22:49 +00001324 * then the length() method returns 0 and the * operator returns
1325 * NULL.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001326 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001327 class V8EXPORT Utf8Value {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001328 public:
1329 explicit Utf8Value(Handle<v8::Value> obj);
1330 ~Utf8Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001331 char* operator*() { return str_; }
1332 const char* operator*() const { return str_; }
1333 int length() const { return length_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001334 private:
1335 char* str_;
1336 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001337
1338 // Disallow copying and assigning.
1339 Utf8Value(const Utf8Value&);
1340 void operator=(const Utf8Value&);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001341 };
1342
1343 /**
lrn@chromium.org34e60782011-09-15 07:25:40 +00001344 * Converts an object to an ASCII string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 * Useful if you want to print the object.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001346 * If conversion to a string fails (eg. due to an exception in the toString()
1347 * method of the object) then the length() method returns 0 and the * operator
1348 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001350 class V8EXPORT AsciiValue {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 public:
1352 explicit AsciiValue(Handle<v8::Value> obj);
1353 ~AsciiValue();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001354 char* operator*() { return str_; }
1355 const char* operator*() const { return str_; }
1356 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 private:
1358 char* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001359 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001360
1361 // Disallow copying and assigning.
1362 AsciiValue(const AsciiValue&);
1363 void operator=(const AsciiValue&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 };
1365
1366 /**
1367 * Converts an object to a two-byte string.
ager@chromium.org71daaf62009-04-01 07:22:49 +00001368 * If conversion to a string fails (eg. due to an exception in the toString()
1369 * method of the object) then the length() method returns 0 and the * operator
1370 * returns NULL.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001372 class V8EXPORT Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 public:
1374 explicit Value(Handle<v8::Value> obj);
1375 ~Value();
ager@chromium.orga1645e22009-09-09 19:27:10 +00001376 uint16_t* operator*() { return str_; }
1377 const uint16_t* operator*() const { return str_; }
1378 int length() const { return length_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 private:
1380 uint16_t* str_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001381 int length_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001382
1383 // Disallow copying and assigning.
1384 Value(const Value&);
1385 void operator=(const Value&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001387
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001388 private:
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001389 V8EXPORT void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
1390 Encoding encoding) const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001391 V8EXPORT void VerifyExternalStringResource(ExternalStringResource* val) const;
1392 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393};
1394
1395
1396/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001397 * A JavaScript number value (ECMA-262, 4.3.20)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001399class Number : public Primitive {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001401 V8EXPORT double Value() const;
1402 V8EXPORT static Local<Number> New(double value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001403 static inline Number* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001405 V8EXPORT Number();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001406 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407};
1408
1409
1410/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001411 * A JavaScript value representing a signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001413class Integer : public Number {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001415 V8EXPORT static Local<Integer> New(int32_t value);
1416 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00001417 V8EXPORT static Local<Integer> New(int32_t value, Isolate*);
1418 V8EXPORT static Local<Integer> NewFromUnsigned(uint32_t value, Isolate*);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001419 V8EXPORT int64_t Value() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001420 static inline Integer* Cast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001422 V8EXPORT Integer();
1423 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424};
1425
1426
1427/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001428 * A JavaScript value representing a 32-bit signed integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001429 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001430class Int32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001432 V8EXPORT int32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001434 V8EXPORT Int32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435};
1436
1437
1438/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001439 * A JavaScript value representing a 32-bit unsigned integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001441class Uint32 : public Integer {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001443 V8EXPORT uint32_t Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001445 V8EXPORT Uint32();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446};
1447
1448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449enum PropertyAttribute {
1450 None = 0,
1451 ReadOnly = 1 << 0,
1452 DontEnum = 1 << 1,
1453 DontDelete = 1 << 2
1454};
1455
ager@chromium.org3811b432009-10-28 14:53:37 +00001456enum ExternalArrayType {
1457 kExternalByteArray = 1,
1458 kExternalUnsignedByteArray,
1459 kExternalShortArray,
1460 kExternalUnsignedShortArray,
1461 kExternalIntArray,
1462 kExternalUnsignedIntArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001463 kExternalFloatArray,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001464 kExternalDoubleArray,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001465 kExternalPixelArray
ager@chromium.org3811b432009-10-28 14:53:37 +00001466};
1467
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468/**
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001469 * Accessor[Getter|Setter] are used as callback functions when
1470 * setting|getting a particular property. See Object and ObjectTemplate's
1471 * method SetAccessor.
1472 */
1473typedef Handle<Value> (*AccessorGetter)(Local<String> property,
1474 const AccessorInfo& info);
1475
1476
1477typedef void (*AccessorSetter)(Local<String> property,
1478 Local<Value> value,
1479 const AccessorInfo& info);
1480
1481
1482/**
1483 * Access control specifications.
1484 *
1485 * Some accessors should be accessible across contexts. These
1486 * accessors have an explicit access control parameter which specifies
1487 * the kind of cross-context access that should be allowed.
1488 *
1489 * Additionally, for security, accessors can prohibit overwriting by
1490 * accessors defined in JavaScript. For objects that have such
1491 * accessors either locally or in their prototype chain it is not
1492 * possible to overwrite the accessor by using __defineGetter__ or
1493 * __defineSetter__ from JavaScript code.
1494 */
1495enum AccessControl {
1496 DEFAULT = 0,
1497 ALL_CAN_READ = 1,
1498 ALL_CAN_WRITE = 1 << 1,
1499 PROHIBITS_OVERWRITING = 1 << 2
1500};
1501
1502
1503/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001504 * A JavaScript object (ECMA-262, 4.3.3)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001506class Object : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001508 V8EXPORT bool Set(Handle<Value> key,
1509 Handle<Value> value,
1510 PropertyAttribute attribs = None);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001511
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001512 V8EXPORT bool Set(uint32_t index,
1513 Handle<Value> value);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001514
ager@chromium.orge2902be2009-06-08 12:21:35 +00001515 // Sets a local property on this object bypassing interceptors and
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001516 // overriding accessors or read-only properties.
1517 //
1518 // Note that if the object has an interceptor the property will be set
1519 // locally, but since the interceptor takes precedence the local property
1520 // will only be returned if the interceptor doesn't return a value.
1521 //
1522 // Note also that this only works for named properties.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001523 V8EXPORT bool ForceSet(Handle<Value> key,
1524 Handle<Value> value,
1525 PropertyAttribute attribs = None);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001526
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001527 V8EXPORT Local<Value> Get(Handle<Value> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001529 V8EXPORT Local<Value> Get(uint32_t index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001530
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001531 /**
1532 * Gets the property attributes of a property which can be None or
1533 * any combination of ReadOnly, DontEnum and DontDelete. Returns
1534 * None when the property doesn't exist.
1535 */
1536 V8EXPORT PropertyAttribute GetPropertyAttributes(Handle<Value> key);
1537
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 // TODO(1245389): Replace the type-specific versions of these
1539 // functions with generic ones that accept a Handle<Value> key.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001540 V8EXPORT bool Has(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001541
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001542 V8EXPORT bool Delete(Handle<String> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001543
1544 // Delete a property on this object bypassing interceptors and
1545 // ignoring dont-delete attributes.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001546 V8EXPORT bool ForceDelete(Handle<Value> key);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001547
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001548 V8EXPORT bool Has(uint32_t index);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001549
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001550 V8EXPORT bool Delete(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001552 V8EXPORT bool SetAccessor(Handle<String> name,
1553 AccessorGetter getter,
1554 AccessorSetter setter = 0,
1555 Handle<Value> data = Handle<Value>(),
1556 AccessControl settings = DEFAULT,
1557 PropertyAttribute attribute = None);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001558
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 /**
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001560 * Returns an array containing the names of the enumerable properties
1561 * of this object, including properties from prototype objects. The
1562 * array returned by this method contains the same values as would
1563 * be enumerated by a for-in statement over this object.
1564 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001565 V8EXPORT Local<Array> GetPropertyNames();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001566
1567 /**
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001568 * This function has the same functionality as GetPropertyNames but
1569 * the returned array doesn't contain the names of properties from
1570 * prototype objects.
1571 */
1572 V8EXPORT Local<Array> GetOwnPropertyNames();
1573
1574 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575 * Get the prototype object. This does not skip objects marked to
1576 * be skipped by __proto__ and it does not consult the security
1577 * handler.
1578 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001579 V8EXPORT Local<Value> GetPrototype();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580
1581 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001582 * Set the prototype object. This does not skip objects marked to
1583 * be skipped by __proto__ and it does not consult the security
1584 * handler.
1585 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001586 V8EXPORT bool SetPrototype(Handle<Value> prototype);
ager@chromium.org5c838252010-02-19 08:53:10 +00001587
1588 /**
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001589 * Finds an instance of the given function template in the prototype
1590 * chain.
1591 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001592 V8EXPORT Local<Object> FindInstanceInPrototypeChain(
1593 Handle<FunctionTemplate> tmpl);
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +00001594
1595 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 * Call builtin Object.prototype.toString on this object.
1597 * This is different from Value::ToString() that may call
1598 * user-defined toString function. This one does not.
1599 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001600 V8EXPORT Local<String> ObjectProtoToString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001602 /**
yangguo@chromium.orgc74d6742012-06-29 15:15:45 +00001603 * Returns the function invoked as a constructor for this object.
1604 * May be the null value.
1605 */
1606 V8EXPORT Local<Value> GetConstructor();
1607
1608 /**
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001609 * Returns the name of the function invoked as a constructor for this object.
1610 */
1611 V8EXPORT Local<String> GetConstructorName();
1612
kasper.lund212ac232008-07-16 07:07:30 +00001613 /** Gets the number of internal fields for this Object. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001614 V8EXPORT int InternalFieldCount();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001615
1616 /** Gets the value from an internal field. */
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001617 inline Local<Value> GetInternalField(int index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001618
kasper.lund212ac232008-07-16 07:07:30 +00001619 /** Sets the value in an internal field. */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001620 V8EXPORT void SetInternalField(int index, Handle<Value> value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001622 /**
1623 * Gets a native pointer from an internal field. Deprecated. If the pointer is
1624 * always 2-byte-aligned, use GetAlignedPointerFromInternalField instead,
1625 * otherwise use a combination of GetInternalField, External::Cast and
1626 * External::Value.
1627 */
1628 V8EXPORT V8_DEPRECATED(void* GetPointerFromInternalField(int index));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001629
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001630 /**
1631 * Sets a native pointer in an internal field. Deprecated. If the pointer is
1632 * always 2-byte aligned, use SetAlignedPointerInInternalField instead,
1633 * otherwise use a combination of External::New and SetInternalField.
1634 */
1635 inline V8_DEPRECATED(void SetPointerInInternalField(int index, void* value));
1636
1637 /**
1638 * Gets a 2-byte-aligned native pointer from an internal field. This field
1639 * must have been set by SetAlignedPointerInInternalField, everything else
1640 * leads to undefined behavior.
1641 */
1642 inline void* GetAlignedPointerFromInternalField(int index);
1643
1644 /**
1645 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
1646 * a field, GetAlignedPointerFromInternalField must be used, everything else
1647 * leads to undefined behavior.
1648 */
1649 V8EXPORT void SetAlignedPointerInInternalField(int index, void* value);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 // Testers for local properties.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00001652 V8EXPORT bool HasOwnProperty(Handle<String> key);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001653 V8EXPORT bool HasRealNamedProperty(Handle<String> key);
1654 V8EXPORT bool HasRealIndexedProperty(uint32_t index);
1655 V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656
1657 /**
1658 * If result.IsEmpty() no real property was located in the prototype chain.
1659 * This means interceptors in the prototype chain are not called.
1660 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001661 V8EXPORT Local<Value> GetRealNamedPropertyInPrototypeChain(
1662 Handle<String> key);
sgjesse@chromium.org98aff2f2009-09-30 08:27:10 +00001663
1664 /**
1665 * If result.IsEmpty() no real property was located on the object or
1666 * in the prototype chain.
1667 * This means interceptors in the prototype chain are not called.
1668 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001669 V8EXPORT Local<Value> GetRealNamedProperty(Handle<String> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670
1671 /** Tests for a named lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001672 V8EXPORT bool HasNamedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673
kasper.lund212ac232008-07-16 07:07:30 +00001674 /** Tests for an index lookup interceptor.*/
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001675 V8EXPORT bool HasIndexedLookupInterceptor();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001677 /**
1678 * Turns on access check on the object if the object is an instance of
1679 * a template that has access check callbacks. If an object has no
1680 * access check info, the object cannot be accessed by anyone.
1681 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001682 V8EXPORT void TurnOnAccessCheck();
ager@chromium.org41826e72009-03-30 13:30:57 +00001683
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001684 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001685 * Returns the identity hash for this object. The current implementation
1686 * uses a hidden property on the object to store the identity hash.
ager@chromium.org5ec48922009-05-05 07:25:34 +00001687 *
ager@chromium.org9085a012009-05-11 19:22:57 +00001688 * The return value will never be 0. Also, it is not guaranteed to be
ager@chromium.org5ec48922009-05-05 07:25:34 +00001689 * unique.
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001690 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001691 V8EXPORT int GetIdentityHash();
ager@chromium.org41826e72009-03-30 13:30:57 +00001692
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001693 /**
1694 * Access hidden properties on JavaScript objects. These properties are
1695 * hidden from the executing JavaScript and only accessible through the V8
1696 * C++ API. Hidden properties introduced by V8 internally (for example the
1697 * identity hash) are prefixed with "v8::".
1698 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001699 V8EXPORT bool SetHiddenValue(Handle<String> key, Handle<Value> value);
1700 V8EXPORT Local<Value> GetHiddenValue(Handle<String> key);
1701 V8EXPORT bool DeleteHiddenValue(Handle<String> key);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001702
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001703 /**
1704 * Returns true if this is an instance of an api function (one
1705 * created from a function created from a function template) and has
1706 * been modified since it was created. Note that this method is
1707 * conservative and may return true for objects that haven't actually
1708 * been modified.
1709 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001710 V8EXPORT bool IsDirty();
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001711
1712 /**
1713 * Clone this object with a fast but shallow copy. Values will point
1714 * to the same values as the original object.
1715 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001716 V8EXPORT Local<Object> Clone();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001718 /**
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001719 * Returns the context in which the object was created.
1720 */
1721 V8EXPORT Local<Context> CreationContext();
1722
1723 /**
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001724 * Set the backing store of the indexed properties to be managed by the
1725 * embedding layer. Access to the indexed properties will follow the rules
1726 * spelled out in CanvasPixelArray.
1727 * Note: The embedding program still owns the data and needs to ensure that
1728 * the backing store is preserved while V8 has a reference.
1729 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001730 V8EXPORT void SetIndexedPropertiesToPixelData(uint8_t* data, int length);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001731 V8EXPORT bool HasIndexedPropertiesInPixelData();
1732 V8EXPORT uint8_t* GetIndexedPropertiesPixelData();
1733 V8EXPORT int GetIndexedPropertiesPixelDataLength();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001734
ager@chromium.org3811b432009-10-28 14:53:37 +00001735 /**
1736 * Set the backing store of the indexed properties to be managed by the
1737 * embedding layer. Access to the indexed properties will follow the rules
1738 * spelled out for the CanvasArray subtypes in the WebGL specification.
1739 * Note: The embedding program still owns the data and needs to ensure that
1740 * the backing store is preserved while V8 has a reference.
1741 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001742 V8EXPORT void SetIndexedPropertiesToExternalArrayData(
1743 void* data,
1744 ExternalArrayType array_type,
1745 int number_of_elements);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001746 V8EXPORT bool HasIndexedPropertiesInExternalArrayData();
1747 V8EXPORT void* GetIndexedPropertiesExternalArrayData();
1748 V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
1749 V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
ager@chromium.org3811b432009-10-28 14:53:37 +00001750
lrn@chromium.org1c092762011-05-09 09:42:16 +00001751 /**
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001752 * Checks whether a callback is set by the
1753 * ObjectTemplate::SetCallAsFunctionHandler method.
1754 * When an Object is callable this method returns true.
1755 */
1756 V8EXPORT bool IsCallable();
1757
1758 /**
fschneider@chromium.org1805e212011-09-05 10:49:12 +00001759 * Call an Object as a function if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001760 * ObjectTemplate::SetCallAsFunctionHandler method.
1761 */
1762 V8EXPORT Local<Value> CallAsFunction(Handle<Object> recv,
1763 int argc,
1764 Handle<Value> argv[]);
1765
1766 /**
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001767 * Call an Object as a constructor if a callback is set by the
lrn@chromium.org1c092762011-05-09 09:42:16 +00001768 * ObjectTemplate::SetCallAsFunctionHandler method.
1769 * Note: This method behaves like the Function::NewInstance method.
1770 */
1771 V8EXPORT Local<Value> CallAsConstructor(int argc,
1772 Handle<Value> argv[]);
1773
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001774 V8EXPORT static Local<Object> New();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001775 static inline Object* Cast(Value* obj);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001776
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001778 V8EXPORT Object();
1779 V8EXPORT static void CheckCast(Value* obj);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001780 V8EXPORT Local<Value> SlowGetInternalField(int index);
1781 V8EXPORT void* SlowGetAlignedPointerFromInternalField(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782};
1783
1784
1785/**
1786 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
1787 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001788class Array : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001790 V8EXPORT uint32_t Length() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791
ager@chromium.org3e875802009-06-29 08:26:34 +00001792 /**
1793 * Clones an element at index |index|. Returns an empty
1794 * handle if cloning fails (for any reason).
1795 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001796 V8EXPORT Local<Object> CloneElementAt(uint32_t index);
ager@chromium.org3e875802009-06-29 08:26:34 +00001797
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001798 /**
1799 * Creates a JavaScript array with the given length. If the length
1800 * is negative the returned array will have length 0.
1801 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001802 V8EXPORT static Local<Array> New(int length = 0);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00001803
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001804 static inline Array* Cast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001806 V8EXPORT Array();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001807 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808};
1809
1810
1811/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00001812 * A JavaScript function object (ECMA-262, 15.3).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001814class Function : public Object {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 public:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001816 V8EXPORT Local<Object> NewInstance() const;
1817 V8EXPORT Local<Object> NewInstance(int argc, Handle<Value> argv[]) const;
1818 V8EXPORT Local<Value> Call(Handle<Object> recv,
1819 int argc,
1820 Handle<Value> argv[]);
1821 V8EXPORT void SetName(Handle<String> name);
1822 V8EXPORT Handle<Value> GetName() const;
ager@chromium.org5c838252010-02-19 08:53:10 +00001823
1824 /**
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001825 * Name inferred from variable or property assignment of this function.
1826 * Used to facilitate debugging and profiling of JavaScript code written
1827 * in an OO style, where many functions are anonymous but are assigned
1828 * to object properties.
1829 */
1830 V8EXPORT Handle<Value> GetInferredName() const;
1831
1832 /**
ager@chromium.org5c838252010-02-19 08:53:10 +00001833 * Returns zero based line number of function body and
1834 * kLineOffsetNotFound if no information available.
1835 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001836 V8EXPORT int GetScriptLineNumber() const;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001837 /**
1838 * Returns zero based column number of function body and
1839 * kLineOffsetNotFound if no information available.
1840 */
1841 V8EXPORT int GetScriptColumnNumber() const;
1842 V8EXPORT Handle<Value> GetScriptId() const;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001843 V8EXPORT ScriptOrigin GetScriptOrigin() const;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001844 static inline Function* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001845 V8EXPORT static const int kLineOffsetNotFound;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001846
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001848 V8EXPORT Function();
1849 V8EXPORT static void CheckCast(Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850};
1851
1852
1853/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001854 * An instance of the built-in Date constructor (ECMA-262, 15.9).
1855 */
1856class Date : public Object {
1857 public:
1858 V8EXPORT static Local<Value> New(double time);
1859
1860 /**
1861 * A specialization of Value::NumberValue that is more efficient
1862 * because we know the structure of this object.
1863 */
1864 V8EXPORT double NumberValue() const;
1865
1866 static inline Date* Cast(v8::Value* obj);
1867
1868 /**
1869 * Notification that the embedder has changed the time zone,
1870 * daylight savings time, or other date / time configuration
1871 * parameters. V8 keeps a cache of various values used for
1872 * date / time computation. This notification will reset
1873 * those cached values for the current context so that date /
1874 * time configuration changes would be reflected in the Date
1875 * object.
1876 *
1877 * This API should not be called more than needed as it will
1878 * negatively impact the performance of date operations.
1879 */
1880 V8EXPORT static void DateTimeConfigurationChangeNotification();
1881
1882 private:
1883 V8EXPORT static void CheckCast(v8::Value* obj);
1884};
1885
1886
1887/**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001888 * A Number object (ECMA-262, 4.3.21).
1889 */
1890class NumberObject : public Object {
1891 public:
1892 V8EXPORT static Local<Value> New(double value);
1893
1894 /**
1895 * Returns the Number held by the object.
1896 */
1897 V8EXPORT double NumberValue() const;
1898
1899 static inline NumberObject* Cast(v8::Value* obj);
1900
1901 private:
1902 V8EXPORT static void CheckCast(v8::Value* obj);
1903};
1904
1905
1906/**
1907 * A Boolean object (ECMA-262, 4.3.15).
1908 */
1909class BooleanObject : public Object {
1910 public:
1911 V8EXPORT static Local<Value> New(bool value);
1912
1913 /**
1914 * Returns the Boolean held by the object.
1915 */
1916 V8EXPORT bool BooleanValue() const;
1917
1918 static inline BooleanObject* Cast(v8::Value* obj);
1919
1920 private:
1921 V8EXPORT static void CheckCast(v8::Value* obj);
1922};
1923
1924
1925/**
1926 * A String object (ECMA-262, 4.3.18).
1927 */
1928class StringObject : public Object {
1929 public:
1930 V8EXPORT static Local<Value> New(Handle<String> value);
1931
1932 /**
1933 * Returns the String held by the object.
1934 */
1935 V8EXPORT Local<String> StringValue() const;
1936
1937 static inline StringObject* Cast(v8::Value* obj);
1938
1939 private:
1940 V8EXPORT static void CheckCast(v8::Value* obj);
1941};
1942
1943
1944/**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001945 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
1946 */
1947class RegExp : public Object {
1948 public:
1949 /**
1950 * Regular expression flag bits. They can be or'ed to enable a set
1951 * of flags.
1952 */
1953 enum Flags {
1954 kNone = 0,
1955 kGlobal = 1,
1956 kIgnoreCase = 2,
1957 kMultiline = 4
1958 };
1959
1960 /**
1961 * Creates a regular expression from the given pattern string and
1962 * the flags bit field. May throw a JavaScript exception as
1963 * described in ECMA-262, 15.10.4.1.
1964 *
1965 * For example,
1966 * RegExp::New(v8::String::New("foo"),
1967 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
1968 * is equivalent to evaluating "/foo/gm".
1969 */
1970 V8EXPORT static Local<RegExp> New(Handle<String> pattern,
1971 Flags flags);
1972
1973 /**
1974 * Returns the value of the source property: a string representing
1975 * the regular expression.
1976 */
1977 V8EXPORT Local<String> GetSource() const;
1978
1979 /**
1980 * Returns the flags bit field.
1981 */
1982 V8EXPORT Flags GetFlags() const;
1983
1984 static inline RegExp* Cast(v8::Value* obj);
1985
1986 private:
1987 V8EXPORT static void CheckCast(v8::Value* obj);
1988};
1989
1990
1991/**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001992 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
1993 * to associate C++ data structures with JavaScript objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001995class External : public Value {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 public:
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001997 /** Deprecated, use New instead. */
1998 V8_DEPRECATED(static inline Local<Value> Wrap(void* value));
1999
2000 /** Deprecated, use a combination of Cast and Value instead. */
2001 V8_DEPRECATED(static inline void* Unwrap(Handle<Value> obj));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002002
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002003 V8EXPORT static Local<External> New(void* value);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002004 static inline External* Cast(Value* obj);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002005 V8EXPORT void* Value() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 private:
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002007 V8EXPORT static void CheckCast(v8::Value* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008};
2009
2010
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002011// --- Templates ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012
2013
2014/**
2015 * The superclass of object and function templates.
2016 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002017class V8EXPORT Template : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 public:
2019 /** Adds a property to each instance created by this template.*/
2020 void Set(Handle<String> name, Handle<Data> value,
2021 PropertyAttribute attributes = None);
2022 inline void Set(const char* name, Handle<Data> value);
2023 private:
2024 Template();
2025
2026 friend class ObjectTemplate;
2027 friend class FunctionTemplate;
2028};
2029
2030
2031/**
2032 * The argument information given to function call callbacks. This
v8.team.kasperl727e9952008-09-02 14:56:44 +00002033 * class provides access to information about the context of the call,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 * including the receiver, the number and values of arguments, and
2035 * the holder of the function.
2036 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002037class Arguments {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 public:
2039 inline int Length() const;
2040 inline Local<Value> operator[](int i) const;
2041 inline Local<Function> Callee() const;
2042 inline Local<Object> This() const;
2043 inline Local<Object> Holder() const;
2044 inline bool IsConstructCall() const;
2045 inline Local<Value> Data() const;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002046 inline Isolate* GetIsolate() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002047
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 private:
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002049 static const int kIsolateIndex = 0;
2050 static const int kDataIndex = -1;
2051 static const int kCalleeIndex = -2;
2052 static const int kHolderIndex = -3;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 friend class ImplementationUtilities;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002055 inline Arguments(internal::Object** implicit_args,
2056 internal::Object** values,
2057 int length,
2058 bool is_construct_call);
2059 internal::Object** implicit_args_;
2060 internal::Object** values_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 int length_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00002062 bool is_construct_call_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063};
2064
2065
2066/**
2067 * The information passed to an accessor callback about the context
2068 * of the property access.
2069 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002070class V8EXPORT AccessorInfo {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071 public:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002072 inline AccessorInfo(internal::Object** args)
2073 : args_(args) { }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00002074 inline Isolate* GetIsolate() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 inline Local<Value> Data() const;
2076 inline Local<Object> This() const;
2077 inline Local<Object> Holder() const;
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00002078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 private:
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002080 internal::Object** args_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081};
2082
2083
2084typedef Handle<Value> (*InvocationCallback)(const Arguments& args);
2085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 * NamedProperty[Getter|Setter] are used as interceptors on object.
2088 * See ObjectTemplate::SetNamedPropertyHandler.
2089 */
2090typedef Handle<Value> (*NamedPropertyGetter)(Local<String> property,
2091 const AccessorInfo& info);
2092
2093
2094/**
2095 * Returns the value if the setter intercepts the request.
2096 * Otherwise, returns an empty handle.
2097 */
2098typedef Handle<Value> (*NamedPropertySetter)(Local<String> property,
2099 Local<Value> value,
2100 const AccessorInfo& info);
2101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102/**
2103 * Returns a non-empty handle if the interceptor intercepts the request.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002104 * The result is an integer encoding property attributes (like v8::None,
2105 * v8::DontEnum, etc.)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 */
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00002107typedef Handle<Integer> (*NamedPropertyQuery)(Local<String> property,
2108 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109
2110
2111/**
2112 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002113 * The return value is true if the property could be deleted and false
2114 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 */
2116typedef Handle<Boolean> (*NamedPropertyDeleter)(Local<String> property,
2117 const AccessorInfo& info);
2118
2119/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002120 * Returns an array containing the names of the properties the named
2121 * property getter intercepts.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 */
2123typedef Handle<Array> (*NamedPropertyEnumerator)(const AccessorInfo& info);
2124
v8.team.kasperl727e9952008-09-02 14:56:44 +00002125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002127 * Returns the value of the property if the getter intercepts the
2128 * request. Otherwise, returns an empty handle.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 */
2130typedef Handle<Value> (*IndexedPropertyGetter)(uint32_t index,
2131 const AccessorInfo& info);
2132
2133
2134/**
2135 * Returns the value if the setter intercepts the request.
2136 * Otherwise, returns an empty handle.
2137 */
2138typedef Handle<Value> (*IndexedPropertySetter)(uint32_t index,
2139 Local<Value> value,
2140 const AccessorInfo& info);
2141
2142
2143/**
2144 * Returns a non-empty handle if the interceptor intercepts the request.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002145 * The result is an integer encoding property attributes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 */
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002147typedef Handle<Integer> (*IndexedPropertyQuery)(uint32_t index,
2148 const AccessorInfo& info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149
2150/**
2151 * Returns a non-empty handle if the deleter intercepts the request.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002152 * The return value is true if the property could be deleted and false
2153 * otherwise.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 */
2155typedef Handle<Boolean> (*IndexedPropertyDeleter)(uint32_t index,
2156 const AccessorInfo& info);
2157
v8.team.kasperl727e9952008-09-02 14:56:44 +00002158/**
2159 * Returns an array containing the indices of the properties the
2160 * indexed property getter intercepts.
2161 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162typedef Handle<Array> (*IndexedPropertyEnumerator)(const AccessorInfo& info);
2163
2164
2165/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002166 * Access type specification.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 */
2168enum AccessType {
2169 ACCESS_GET,
2170 ACCESS_SET,
2171 ACCESS_HAS,
2172 ACCESS_DELETE,
2173 ACCESS_KEYS
2174};
2175
v8.team.kasperl727e9952008-09-02 14:56:44 +00002176
2177/**
2178 * Returns true if cross-context access should be allowed to the named
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002179 * property with the given key on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002180 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002181typedef bool (*NamedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 Local<Value> key,
2183 AccessType type,
2184 Local<Value> data);
2185
v8.team.kasperl727e9952008-09-02 14:56:44 +00002186
2187/**
2188 * Returns true if cross-context access should be allowed to the indexed
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002189 * property with the given index on the host object.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002190 */
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002191typedef bool (*IndexedSecurityCallback)(Local<Object> host,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 uint32_t index,
2193 AccessType type,
2194 Local<Value> data);
2195
2196
2197/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002198 * A FunctionTemplate is used to create functions at runtime. There
2199 * can only be one function created from a FunctionTemplate in a
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002200 * context. The lifetime of the created function is equal to the
2201 * lifetime of the context. So in case the embedder needs to create
2202 * temporary functions that can be collected using Scripts is
2203 * preferred.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 *
2205 * A FunctionTemplate can have properties, these properties are added to the
v8.team.kasperl727e9952008-09-02 14:56:44 +00002206 * function object when it is created.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002208 * A FunctionTemplate has a corresponding instance template which is
2209 * used to create object instances when the function is used as a
2210 * constructor. Properties added to the instance template are added to
2211 * each object instance.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 *
2213 * A FunctionTemplate can have a prototype template. The prototype template
2214 * is used to create the prototype object of the function.
2215 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002216 * The following example shows how to use a FunctionTemplate:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002218 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
2220 * t->Set("func_property", v8::Number::New(1));
2221 *
2222 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
2223 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
2224 * proto_t->Set("proto_const", v8::Number::New(2));
2225 *
2226 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
2227 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
2228 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
2229 * instance_t->Set("instance_property", Number::New(3));
2230 *
2231 * v8::Local<v8::Function> function = t->GetFunction();
2232 * v8::Local<v8::Object> instance = function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002233 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 *
2235 * Let's use "function" as the JS variable name of the function object
v8.team.kasperl727e9952008-09-02 14:56:44 +00002236 * and "instance" for the instance object created above. The function
2237 * and the instance will have the following properties:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002239 * \code
2240 * func_property in function == true;
2241 * function.func_property == 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002243 * function.prototype.proto_method() invokes 'InvokeCallback'
2244 * function.prototype.proto_const == 2;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002246 * instance instanceof function == true;
2247 * instance.instance_accessor calls 'InstanceAccessorCallback'
2248 * instance.instance_property == 3;
2249 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002251 * A FunctionTemplate can inherit from another one by calling the
2252 * FunctionTemplate::Inherit method. The following graph illustrates
2253 * the semantics of inheritance:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002255 * \code
2256 * FunctionTemplate Parent -> Parent() . prototype -> { }
2257 * ^ ^
2258 * | Inherit(Parent) | .__proto__
2259 * | |
2260 * FunctionTemplate Child -> Child() . prototype -> { }
2261 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002263 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
2264 * object of the Child() function has __proto__ pointing to the
2265 * Parent() function's prototype object. An instance of the Child
2266 * function has all properties on Parent's instance templates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002268 * Let Parent be the FunctionTemplate initialized in the previous
2269 * section and create a Child FunctionTemplate by:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002271 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 * Local<FunctionTemplate> parent = t;
2273 * Local<FunctionTemplate> child = FunctionTemplate::New();
2274 * child->Inherit(parent);
2275 *
2276 * Local<Function> child_function = child->GetFunction();
2277 * Local<Object> child_instance = child_function->NewInstance();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002278 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00002280 * The Child function and Child instance will have the following
2281 * properties:
2282 *
2283 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 * child_func.prototype.__proto__ == function.prototype;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002285 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 * child_instance.instance_property == 3;
v8.team.kasperl727e9952008-09-02 14:56:44 +00002287 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002289class V8EXPORT FunctionTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290 public:
2291 /** Creates a function template.*/
v8.team.kasperl727e9952008-09-02 14:56:44 +00002292 static Local<FunctionTemplate> New(
2293 InvocationCallback callback = 0,
2294 Handle<Value> data = Handle<Value>(),
2295 Handle<Signature> signature = Handle<Signature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 /** Returns the unique function instance in the current execution context.*/
2297 Local<Function> GetFunction();
2298
v8.team.kasperl727e9952008-09-02 14:56:44 +00002299 /**
2300 * Set the call-handler callback for a FunctionTemplate. This
2301 * callback is called whenever the function created from this
2302 * FunctionTemplate is called.
2303 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 void SetCallHandler(InvocationCallback callback,
2305 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306
v8.team.kasperl727e9952008-09-02 14:56:44 +00002307 /** Get the InstanceTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 Local<ObjectTemplate> InstanceTemplate();
2309
2310 /** Causes the function template to inherit from a parent function template.*/
2311 void Inherit(Handle<FunctionTemplate> parent);
2312
2313 /**
2314 * A PrototypeTemplate is the template used to create the prototype object
2315 * of the function created by this template.
2316 */
2317 Local<ObjectTemplate> PrototypeTemplate();
2318
v8.team.kasperl727e9952008-09-02 14:56:44 +00002319
2320 /**
2321 * Set the class name of the FunctionTemplate. This is used for
2322 * printing objects created with the function created from the
2323 * FunctionTemplate as its constructor.
2324 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 void SetClassName(Handle<String> name);
2326
2327 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002328 * Determines whether the __proto__ accessor ignores instances of
2329 * the function template. If instances of the function template are
2330 * ignored, __proto__ skips all instances and instead returns the
2331 * next object in the prototype chain.
2332 *
2333 * Call with a value of true to make the __proto__ accessor ignore
2334 * instances of the function template. Call with a value of false
2335 * to make the __proto__ accessor not ignore instances of the
2336 * function template. By default, instances of a function template
2337 * are not ignored.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338 */
2339 void SetHiddenPrototype(bool value);
2340
2341 /**
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002342 * Sets the ReadOnly flag in the attributes of the 'prototype' property
2343 * of functions created from this FunctionTemplate to true.
ager@chromium.org04921a82011-06-27 13:21:41 +00002344 */
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002345 void ReadOnlyPrototype();
ager@chromium.org04921a82011-06-27 13:21:41 +00002346
2347 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002348 * Returns true if the given object is an instance of this function
2349 * template.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 */
2351 bool HasInstance(Handle<Value> object);
2352
2353 private:
2354 FunctionTemplate();
2355 void AddInstancePropertyAccessor(Handle<String> name,
2356 AccessorGetter getter,
2357 AccessorSetter setter,
2358 Handle<Value> data,
2359 AccessControl settings,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002360 PropertyAttribute attributes,
2361 Handle<AccessorSignature> signature);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 void SetNamedInstancePropertyHandler(NamedPropertyGetter getter,
2363 NamedPropertySetter setter,
2364 NamedPropertyQuery query,
2365 NamedPropertyDeleter remover,
2366 NamedPropertyEnumerator enumerator,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002367 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 void SetIndexedInstancePropertyHandler(IndexedPropertyGetter getter,
2369 IndexedPropertySetter setter,
2370 IndexedPropertyQuery query,
2371 IndexedPropertyDeleter remover,
2372 IndexedPropertyEnumerator enumerator,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002373 Handle<Value> data);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 void SetInstanceCallAsFunctionHandler(InvocationCallback callback,
2375 Handle<Value> data);
2376
2377 friend class Context;
2378 friend class ObjectTemplate;
2379};
2380
2381
2382/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002383 * An ObjectTemplate is used to create objects at runtime.
2384 *
2385 * Properties added to an ObjectTemplate are added to each object
2386 * created from the ObjectTemplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002388class V8EXPORT ObjectTemplate : public Template {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00002390 /** Creates an ObjectTemplate. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391 static Local<ObjectTemplate> New();
v8.team.kasperl727e9952008-09-02 14:56:44 +00002392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 /** Creates a new instance of this template.*/
2394 Local<Object> NewInstance();
2395
2396 /**
2397 * Sets an accessor on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002398 *
2399 * Whenever the property with the given name is accessed on objects
2400 * created from this ObjectTemplate the getter and setter callbacks
2401 * are called instead of getting and setting the property directly
2402 * on the JavaScript object.
2403 *
2404 * \param name The name of the property for which an accessor is added.
2405 * \param getter The callback to invoke when getting the property.
2406 * \param setter The callback to invoke when setting the property.
2407 * \param data A piece of data that will be passed to the getter and setter
2408 * callbacks whenever they are invoked.
2409 * \param settings Access control settings for the accessor. This is a bit
2410 * field consisting of one of more of
2411 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
2412 * The default is to not allow cross-context access.
2413 * ALL_CAN_READ means that all cross-context reads are allowed.
2414 * ALL_CAN_WRITE means that all cross-context writes are allowed.
2415 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
2416 * cross-context access.
2417 * \param attribute The attributes of the property for which an accessor
2418 * is added.
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002419 * \param signature The signature describes valid receivers for the accessor
2420 * and is used to perform implicit instance checks against them. If the
2421 * receiver is incompatible (i.e. is not an instance of the constructor as
2422 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
2423 * thrown and no callback is invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424 */
2425 void SetAccessor(Handle<String> name,
2426 AccessorGetter getter,
2427 AccessorSetter setter = 0,
2428 Handle<Value> data = Handle<Value>(),
2429 AccessControl settings = DEFAULT,
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002430 PropertyAttribute attribute = None,
2431 Handle<AccessorSignature> signature =
2432 Handle<AccessorSignature>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433
2434 /**
2435 * Sets a named property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002436 *
2437 * Whenever a named property is accessed on objects created from
2438 * this object template, the provided callback is invoked instead of
2439 * accessing the property directly on the JavaScript object.
2440 *
2441 * \param getter The callback to invoke when getting a property.
2442 * \param setter The callback to invoke when setting a property.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002443 * \param query The callback to invoke to check if a property is present,
2444 * and if present, get its attributes.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002445 * \param deleter The callback to invoke when deleting a property.
2446 * \param enumerator The callback to invoke to enumerate all the named
2447 * properties of an object.
2448 * \param data A piece of data that will be passed to the callbacks
2449 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 */
2451 void SetNamedPropertyHandler(NamedPropertyGetter getter,
2452 NamedPropertySetter setter = 0,
2453 NamedPropertyQuery query = 0,
2454 NamedPropertyDeleter deleter = 0,
2455 NamedPropertyEnumerator enumerator = 0,
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002456 Handle<Value> data = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457
2458 /**
2459 * Sets an indexed property handler on the object template.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002460 *
2461 * Whenever an indexed property is accessed on objects created from
2462 * this object template, the provided callback is invoked instead of
2463 * accessing the property directly on the JavaScript object.
2464 *
2465 * \param getter The callback to invoke when getting a property.
2466 * \param setter The callback to invoke when setting a property.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002467 * \param query The callback to invoke to check if an object has a property.
v8.team.kasperl727e9952008-09-02 14:56:44 +00002468 * \param deleter The callback to invoke when deleting a property.
2469 * \param enumerator The callback to invoke to enumerate all the indexed
2470 * properties of an object.
2471 * \param data A piece of data that will be passed to the callbacks
2472 * whenever they are invoked.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 */
2474 void SetIndexedPropertyHandler(IndexedPropertyGetter getter,
2475 IndexedPropertySetter setter = 0,
2476 IndexedPropertyQuery query = 0,
2477 IndexedPropertyDeleter deleter = 0,
2478 IndexedPropertyEnumerator enumerator = 0,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002479 Handle<Value> data = Handle<Value>());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002480
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002481 /**
2482 * Sets the callback to be used when calling instances created from
2483 * this template as a function. If no callback is set, instances
v8.team.kasperl727e9952008-09-02 14:56:44 +00002484 * behave like normal JavaScript objects that cannot be called as a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485 * function.
2486 */
2487 void SetCallAsFunctionHandler(InvocationCallback callback,
2488 Handle<Value> data = Handle<Value>());
2489
v8.team.kasperl727e9952008-09-02 14:56:44 +00002490 /**
2491 * Mark object instances of the template as undetectable.
2492 *
2493 * In many ways, undetectable objects behave as though they are not
2494 * there. They behave like 'undefined' in conditionals and when
2495 * printed. However, properties can be accessed and called as on
2496 * normal objects.
2497 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 void MarkAsUndetectable();
2499
v8.team.kasperl727e9952008-09-02 14:56:44 +00002500 /**
2501 * Sets access check callbacks on the object template.
2502 *
2503 * When accessing properties on instances of this object template,
2504 * the access check callback will be called to determine whether or
2505 * not to allow cross-context access to the properties.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002506 * The last parameter specifies whether access checks are turned
2507 * on by default on instances. If access checks are off by default,
2508 * they can be turned on on individual instances by calling
2509 * Object::TurnOnAccessCheck().
v8.team.kasperl727e9952008-09-02 14:56:44 +00002510 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
2512 IndexedSecurityCallback indexed_handler,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002513 Handle<Value> data = Handle<Value>(),
2514 bool turned_on_by_default = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515
kasper.lund212ac232008-07-16 07:07:30 +00002516 /**
2517 * Gets the number of internal fields for objects generated from
2518 * this template.
2519 */
2520 int InternalFieldCount();
2521
2522 /**
2523 * Sets the number of internal fields for objects generated from
2524 * this template.
2525 */
2526 void SetInternalFieldCount(int value);
2527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 private:
2529 ObjectTemplate();
2530 static Local<ObjectTemplate> New(Handle<FunctionTemplate> constructor);
2531 friend class FunctionTemplate;
2532};
2533
2534
2535/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002536 * A Signature specifies which receivers and arguments are valid
2537 * parameters to a function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002539class V8EXPORT Signature : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 public:
2541 static Local<Signature> New(Handle<FunctionTemplate> receiver =
2542 Handle<FunctionTemplate>(),
2543 int argc = 0,
2544 Handle<FunctionTemplate> argv[] = 0);
2545 private:
2546 Signature();
2547};
2548
2549
2550/**
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002551 * An AccessorSignature specifies which receivers are valid parameters
2552 * to an accessor callback.
2553 */
2554class V8EXPORT AccessorSignature : public Data {
2555 public:
2556 static Local<AccessorSignature> New(Handle<FunctionTemplate> receiver =
2557 Handle<FunctionTemplate>());
2558 private:
2559 AccessorSignature();
2560};
2561
2562
2563/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002564 * A utility for determining the type of objects based on the template
2565 * they were constructed from.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002566 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002567class V8EXPORT TypeSwitch : public Data {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568 public:
2569 static Local<TypeSwitch> New(Handle<FunctionTemplate> type);
2570 static Local<TypeSwitch> New(int argc, Handle<FunctionTemplate> types[]);
2571 int match(Handle<Value> value);
2572 private:
2573 TypeSwitch();
2574};
2575
2576
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002577// --- Extensions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002578
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002579class V8EXPORT ExternalAsciiStringResourceImpl
2580 : public String::ExternalAsciiStringResource {
2581 public:
2582 ExternalAsciiStringResourceImpl() : data_(0), length_(0) {}
2583 ExternalAsciiStringResourceImpl(const char* data, size_t length)
2584 : data_(data), length_(length) {}
2585 const char* data() const { return data_; }
2586 size_t length() const { return length_; }
2587
2588 private:
2589 const char* data_;
2590 size_t length_;
2591};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592
2593/**
2594 * Ignore
2595 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002596class V8EXPORT Extension { // NOLINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002598 // Note that the strings passed into this constructor must live as long
2599 // as the Extension itself.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600 Extension(const char* name,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002601 const char* source = 0,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602 int dep_count = 0,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002603 const char** deps = 0,
2604 int source_length = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605 virtual ~Extension() { }
2606 virtual v8::Handle<v8::FunctionTemplate>
2607 GetNativeFunction(v8::Handle<v8::String> name) {
2608 return v8::Handle<v8::FunctionTemplate>();
2609 }
2610
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002611 const char* name() const { return name_; }
2612 size_t source_length() const { return source_length_; }
2613 const String::ExternalAsciiStringResource* source() const {
2614 return &source_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615 int dependency_count() { return dep_count_; }
2616 const char** dependencies() { return deps_; }
2617 void set_auto_enable(bool value) { auto_enable_ = value; }
2618 bool auto_enable() { return auto_enable_; }
2619
2620 private:
2621 const char* name_;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002622 size_t source_length_; // expected to initialize before source_
2623 ExternalAsciiStringResourceImpl source_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 int dep_count_;
2625 const char** deps_;
2626 bool auto_enable_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002627
2628 // Disallow copying and assigning.
2629 Extension(const Extension&);
2630 void operator=(const Extension&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631};
2632
2633
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002634void V8EXPORT RegisterExtension(Extension* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002635
2636
2637/**
2638 * Ignore
2639 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002640class V8EXPORT DeclareExtension {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 public:
2642 inline DeclareExtension(Extension* extension) {
2643 RegisterExtension(extension);
2644 }
2645};
2646
2647
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002648// --- Statics ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002649
2650
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002651Handle<Primitive> V8EXPORT Undefined();
2652Handle<Primitive> V8EXPORT Null();
2653Handle<Boolean> V8EXPORT True();
2654Handle<Boolean> V8EXPORT False();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002656inline Handle<Primitive> Undefined(Isolate* isolate);
2657inline Handle<Primitive> Null(Isolate* isolate);
2658inline Handle<Boolean> True(Isolate* isolate);
2659inline Handle<Boolean> False(Isolate* isolate);
2660
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661
2662/**
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002663 * A set of constraints that specifies the limits of the runtime's memory use.
2664 * You must set the heap size before initializing the VM - the size cannot be
2665 * adjusted after the VM is initialized.
2666 *
2667 * If you are using threads then you should hold the V8::Locker lock while
2668 * setting the stack limit and you must set a non-default stack limit separately
2669 * for each thread.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002671class V8EXPORT ResourceConstraints {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672 public:
2673 ResourceConstraints();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002674 int max_young_space_size() const { return max_young_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675 void set_max_young_space_size(int value) { max_young_space_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002676 int max_old_space_size() const { return max_old_space_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 void set_max_old_space_size(int value) { max_old_space_size_ = value; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002678 int max_executable_size() { return max_executable_size_; }
2679 void set_max_executable_size(int value) { max_executable_size_ = value; }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002680 uint32_t* stack_limit() const { return stack_limit_; }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002681 // Sets an address beyond which the VM's stack may not grow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
2683 private:
2684 int max_young_space_size_;
2685 int max_old_space_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002686 int max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 uint32_t* stack_limit_;
2688};
2689
2690
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002691bool V8EXPORT SetResourceConstraints(ResourceConstraints* constraints);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692
2693
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002694// --- Exceptions ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695
2696
2697typedef void (*FatalErrorCallback)(const char* location, const char* message);
2698
2699
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00002700typedef void (*MessageCallback)(Handle<Message> message, Handle<Value> error);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701
2702
2703/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00002704 * Schedules an exception to be thrown when returning to JavaScript. When an
2705 * exception has been scheduled it is illegal to invoke any JavaScript
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 * operation; the caller must return immediately and only after the exception
v8.team.kasperl727e9952008-09-02 14:56:44 +00002707 * has been handled does it become legal to invoke JavaScript operations.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002708 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002709Handle<Value> V8EXPORT ThrowException(Handle<Value> exception);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710
2711/**
2712 * Create new error objects by calling the corresponding error object
2713 * constructor with the message.
2714 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002715class V8EXPORT Exception {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 public:
2717 static Local<Value> RangeError(Handle<String> message);
2718 static Local<Value> ReferenceError(Handle<String> message);
2719 static Local<Value> SyntaxError(Handle<String> message);
2720 static Local<Value> TypeError(Handle<String> message);
2721 static Local<Value> Error(Handle<String> message);
2722};
2723
2724
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002725// --- Counters Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002727typedef int* (*CounterLookupCallback)(const char* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002729typedef void* (*CreateHistogramCallback)(const char* name,
2730 int min,
2731 int max,
2732 size_t buckets);
2733
2734typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
2735
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002736// --- Memory Allocation Callback ---
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00002737 enum ObjectSpace {
2738 kObjectSpaceNewSpace = 1 << 0,
2739 kObjectSpaceOldPointerSpace = 1 << 1,
2740 kObjectSpaceOldDataSpace = 1 << 2,
2741 kObjectSpaceCodeSpace = 1 << 3,
2742 kObjectSpaceMapSpace = 1 << 4,
2743 kObjectSpaceLoSpace = 1 << 5,
2744
2745 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldPointerSpace |
2746 kObjectSpaceOldDataSpace | kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
2747 kObjectSpaceLoSpace
2748 };
2749
2750 enum AllocationAction {
2751 kAllocationActionAllocate = 1 << 0,
2752 kAllocationActionFree = 1 << 1,
2753 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
2754 };
2755
2756typedef void (*MemoryAllocationCallback)(ObjectSpace space,
2757 AllocationAction action,
2758 int size);
2759
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00002760// --- Leave Script Callback ---
2761typedef void (*CallCompletedCallback)();
2762
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002763// --- Failed Access Check Callback ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002764typedef void (*FailedAccessCheckCallback)(Local<Object> target,
2765 AccessType type,
2766 Local<Value> data);
2767
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002768// --- AllowCodeGenerationFromStrings callbacks ---
2769
2770/**
2771 * Callback to check if code generation from strings is allowed. See
2772 * Context::AllowCodeGenerationFromStrings.
2773 */
2774typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
2775
2776// --- Garbage Collection Callbacks ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777
2778/**
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002779 * Applications can register callback functions which will be called
2780 * before and after a garbage collection. Allocations are not
2781 * allowed in the callback functions, you therefore cannot manipulate
v8.team.kasperl727e9952008-09-02 14:56:44 +00002782 * objects (set or delete properties for example) since it is possible
2783 * such operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 */
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002785enum GCType {
2786 kGCTypeScavenge = 1 << 0,
2787 kGCTypeMarkSweepCompact = 1 << 1,
2788 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact
2789};
2790
2791enum GCCallbackFlags {
2792 kNoGCCallbackFlags = 0,
2793 kGCCallbackFlagCompacted = 1 << 0
2794};
2795
2796typedef void (*GCPrologueCallback)(GCType type, GCCallbackFlags flags);
2797typedef void (*GCEpilogueCallback)(GCType type, GCCallbackFlags flags);
2798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002799typedef void (*GCCallback)();
2800
2801
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802/**
ager@chromium.org3811b432009-10-28 14:53:37 +00002803 * Collection of V8 heap information.
2804 *
2805 * Instances of this class can be passed to v8::V8::HeapStatistics to
2806 * get heap statistics from V8.
2807 */
2808class V8EXPORT HeapStatistics {
2809 public:
2810 HeapStatistics();
2811 size_t total_heap_size() { return total_heap_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002812 size_t total_heap_size_executable() { return total_heap_size_executable_; }
danno@chromium.org72204d52012-10-31 10:02:10 +00002813 size_t total_physical_size() { return total_physical_size_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002814 size_t used_heap_size() { return used_heap_size_; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002815 size_t heap_size_limit() { return heap_size_limit_; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002816
2817 private:
2818 void set_total_heap_size(size_t size) { total_heap_size_ = size; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002819 void set_total_heap_size_executable(size_t size) {
2820 total_heap_size_executable_ = size;
2821 }
danno@chromium.org72204d52012-10-31 10:02:10 +00002822 void set_total_physical_size(size_t size) {
2823 total_physical_size_ = size;
2824 }
ager@chromium.org3811b432009-10-28 14:53:37 +00002825 void set_used_heap_size(size_t size) { used_heap_size_ = size; }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002826 void set_heap_size_limit(size_t size) { heap_size_limit_ = size; }
ager@chromium.org3811b432009-10-28 14:53:37 +00002827
2828 size_t total_heap_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00002829 size_t total_heap_size_executable_;
danno@chromium.org72204d52012-10-31 10:02:10 +00002830 size_t total_physical_size_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002831 size_t used_heap_size_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002832 size_t heap_size_limit_;
ager@chromium.org3811b432009-10-28 14:53:37 +00002833
2834 friend class V8;
2835};
2836
2837
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002838class RetainedObjectInfo;
2839
ager@chromium.org3811b432009-10-28 14:53:37 +00002840/**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002841 * Isolate represents an isolated instance of the V8 engine. V8
2842 * isolates have completely separate states. Objects from one isolate
2843 * must not be used in other isolates. When V8 is initialized a
2844 * default isolate is implicitly created and entered. The embedder
2845 * can create additional isolates and use them in parallel in multiple
2846 * threads. An isolate can be entered by at most one thread at any
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002847 * given time. The Locker/Unlocker API must be used to synchronize.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002848 */
2849class V8EXPORT Isolate {
2850 public:
2851 /**
2852 * Stack-allocated class which sets the isolate for all operations
2853 * executed within a local scope.
2854 */
2855 class V8EXPORT Scope {
2856 public:
2857 explicit Scope(Isolate* isolate) : isolate_(isolate) {
2858 isolate->Enter();
2859 }
2860
2861 ~Scope() { isolate_->Exit(); }
2862
2863 private:
2864 Isolate* const isolate_;
2865
2866 // Prevent copying of Scope objects.
2867 Scope(const Scope&);
2868 Scope& operator=(const Scope&);
2869 };
2870
2871 /**
2872 * Creates a new isolate. Does not change the currently entered
2873 * isolate.
2874 *
2875 * When an isolate is no longer used its resources should be freed
2876 * by calling Dispose(). Using the delete operator is not allowed.
2877 */
2878 static Isolate* New();
2879
2880 /**
2881 * Returns the entered isolate for the current thread or NULL in
2882 * case there is no current isolate.
2883 */
2884 static Isolate* GetCurrent();
2885
2886 /**
2887 * Methods below this point require holding a lock (using Locker) in
2888 * a multi-threaded environment.
2889 */
2890
2891 /**
2892 * Sets this isolate as the entered one for the current thread.
2893 * Saves the previously entered one (if any), so that it can be
2894 * restored when exiting. Re-entering an isolate is allowed.
2895 */
2896 void Enter();
2897
2898 /**
2899 * Exits this isolate by restoring the previously entered one in the
2900 * current thread. The isolate may still stay the same, if it was
2901 * entered more than once.
2902 *
2903 * Requires: this == Isolate::GetCurrent().
2904 */
2905 void Exit();
2906
2907 /**
2908 * Disposes the isolate. The isolate must not be entered by any
2909 * thread to be disposable.
2910 */
2911 void Dispose();
2912
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002913 /**
2914 * Associate embedder-specific data with the isolate
2915 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002916 inline void SetData(void* data);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002917
2918 /**
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002919 * Retrieve embedder-specific data from the isolate.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002920 * Returns NULL if SetData has never been called.
2921 */
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002922 inline void* GetData();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002923
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002924 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002925 Isolate();
2926 Isolate(const Isolate&);
2927 ~Isolate();
2928 Isolate& operator=(const Isolate&);
2929 void* operator new(size_t size);
2930 void operator delete(void*, size_t);
2931};
2932
2933
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00002934class StartupData {
2935 public:
2936 enum CompressionAlgorithm {
2937 kUncompressed,
2938 kBZip2
2939 };
2940
2941 const char* data;
2942 int compressed_size;
2943 int raw_size;
2944};
2945
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002946
2947/**
2948 * A helper class for driving V8 startup data decompression. It is based on
2949 * "CompressedStartupData" API functions from the V8 class. It isn't mandatory
2950 * for an embedder to use this class, instead, API functions can be used
2951 * directly.
2952 *
2953 * For an example of the class usage, see the "shell.cc" sample application.
2954 */
2955class V8EXPORT StartupDataDecompressor { // NOLINT
2956 public:
2957 StartupDataDecompressor();
2958 virtual ~StartupDataDecompressor();
2959 int Decompress();
2960
2961 protected:
2962 virtual int DecompressData(char* raw_data,
2963 int* raw_data_size,
2964 const char* compressed_data,
2965 int compressed_data_size) = 0;
2966
2967 private:
2968 char** raw_data;
2969};
2970
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002971
2972/**
2973 * EntropySource is used as a callback function when v8 needs a source
2974 * of entropy.
2975 */
2976typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
2977
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002978
2979/**
ulan@chromium.org967e2702012-02-28 09:49:15 +00002980 * ReturnAddressLocationResolver is used as a callback function when v8 is
2981 * resolving the location of a return address on the stack. Profilers that
2982 * change the return address on the stack can use this to resolve the stack
2983 * location to whereever the profiler stashed the original return address.
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002984 *
2985 * \param return_addr_location points to a location on stack where a machine
2986 * return address resides.
2987 * \returns either return_addr_location, or else a pointer to the profiler's
2988 * copy of the original return address.
2989 *
2990 * \note the resolver function must not cause garbage collection.
ulan@chromium.org967e2702012-02-28 09:49:15 +00002991 */
2992typedef uintptr_t (*ReturnAddressLocationResolver)(
2993 uintptr_t return_addr_location);
2994
2995
2996/**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002997 * FunctionEntryHook is the type of the profile entry hook called at entry to
2998 * any generated function when function-level profiling is enabled.
2999 *
3000 * \param function the address of the function that's being entered.
3001 * \param return_addr_location points to a location on stack where the machine
3002 * return address resides. This can be used to identify the caller of
3003 * \p function, and/or modified to divert execution when \p function exits.
3004 *
3005 * \note the entry hook must not cause garbage collection.
3006 */
3007typedef void (*FunctionEntryHook)(uintptr_t function,
3008 uintptr_t return_addr_location);
3009
3010
3011/**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003012 * A JIT code event is issued each time code is added, moved or removed.
3013 *
3014 * \note removal events are not currently issued.
3015 */
3016struct JitCodeEvent {
3017 enum EventType {
3018 CODE_ADDED,
3019 CODE_MOVED,
3020 CODE_REMOVED
3021 };
3022
3023 // Type of event.
3024 EventType type;
3025 // Start of the instructions.
3026 void* code_start;
3027 // Size of the instructions.
3028 size_t code_len;
3029
3030 union {
3031 // Only valid for CODE_ADDED.
3032 struct {
3033 // Name of the object associated with the code, note that the string is
3034 // not zero-terminated.
3035 const char* str;
3036 // Number of chars in str.
3037 size_t len;
3038 } name;
3039 // New location of instructions. Only valid for CODE_MOVED.
3040 void* new_code_start;
3041 };
3042};
3043
3044/**
3045 * Option flags passed to the SetJitCodeEventHandler function.
3046 */
3047enum JitCodeEventOptions {
3048 kJitCodeEventDefault = 0,
3049 // Generate callbacks for already existent code.
3050 kJitCodeEventEnumExisting = 1
3051};
3052
3053
3054/**
3055 * Callback function passed to SetJitCodeEventHandler.
3056 *
3057 * \param event code add, move or removal event.
3058 */
3059typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
3060
3061
3062/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003063 * Interface for iterating through all external resources in the heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003064 */
3065class V8EXPORT ExternalResourceVisitor { // NOLINT
3066 public:
3067 virtual ~ExternalResourceVisitor() {}
3068 virtual void VisitExternalString(Handle<String> string) {}
3069};
3070
3071
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003072/**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003073 * Interface for iterating through all the persistent handles in the heap.
3074 */
3075class V8EXPORT PersistentHandleVisitor { // NOLINT
3076 public:
3077 virtual ~PersistentHandleVisitor() {}
3078 virtual void VisitPersistentHandle(Persistent<Value> value,
3079 uint16_t class_id) {}
3080};
3081
3082
3083/**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 * Container class for static utility functions.
3085 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003086class V8EXPORT V8 {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003087 public:
v8.team.kasperl727e9952008-09-02 14:56:44 +00003088 /** Set the callback to invoke in case of fatal errors. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089 static void SetFatalErrorHandler(FatalErrorCallback that);
3090
v8.team.kasperl727e9952008-09-02 14:56:44 +00003091 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003092 * Set the callback to invoke to check if code generation from
3093 * strings should be allowed.
3094 */
3095 static void SetAllowCodeGenerationFromStringsCallback(
3096 AllowCodeGenerationFromStringsCallback that);
3097
3098 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003099 * Ignore out-of-memory exceptions.
3100 *
3101 * V8 running out of memory is treated as a fatal error by default.
3102 * This means that the fatal error handler is called and that V8 is
3103 * terminated.
3104 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003105 * IgnoreOutOfMemoryException can be used to not treat an
v8.team.kasperl727e9952008-09-02 14:56:44 +00003106 * out-of-memory situation as a fatal error. This way, the contexts
3107 * that did not cause the out of memory problem might be able to
3108 * continue execution.
3109 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 static void IgnoreOutOfMemoryException();
3111
v8.team.kasperl727e9952008-09-02 14:56:44 +00003112 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003113 * Check if V8 is dead and therefore unusable. This is the case after
v8.team.kasperl727e9952008-09-02 14:56:44 +00003114 * fatal errors such as out-of-memory situations.
3115 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003116 static bool IsDead();
3117
3118 /**
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003119 * The following 4 functions are to be used when V8 is built with
3120 * the 'compress_startup_data' flag enabled. In this case, the
3121 * embedder must decompress startup data prior to initializing V8.
3122 *
3123 * This is how interaction with V8 should look like:
3124 * int compressed_data_count = v8::V8::GetCompressedStartupDataCount();
3125 * v8::StartupData* compressed_data =
3126 * new v8::StartupData[compressed_data_count];
3127 * v8::V8::GetCompressedStartupData(compressed_data);
3128 * ... decompress data (compressed_data can be updated in-place) ...
3129 * v8::V8::SetDecompressedStartupData(compressed_data);
3130 * ... now V8 can be initialized
3131 * ... make sure the decompressed data stays valid until V8 shutdown
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003132 *
3133 * A helper class StartupDataDecompressor is provided. It implements
3134 * the protocol of the interaction described above, and can be used in
3135 * most cases instead of calling these API functions directly.
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003136 */
3137 static StartupData::CompressionAlgorithm GetCompressedStartupDataAlgorithm();
3138 static int GetCompressedStartupDataCount();
3139 static void GetCompressedStartupData(StartupData* compressed_data);
3140 static void SetDecompressedStartupData(StartupData* decompressed_data);
3141
3142 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003143 * Adds a message listener.
3144 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003145 * The same message listener can be added more than once and in that
v8.team.kasperl727e9952008-09-02 14:56:44 +00003146 * case it will be called more than once for each message.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003147 */
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003148 static bool AddMessageListener(MessageCallback that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149
3150 /**
3151 * Remove all message listeners from the specified callback function.
3152 */
3153 static void RemoveMessageListeners(MessageCallback that);
3154
3155 /**
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003156 * Tells V8 to capture current stack trace when uncaught exception occurs
3157 * and report it to the message listeners. The option is off by default.
3158 */
3159 static void SetCaptureStackTraceForUncaughtExceptions(
3160 bool capture,
3161 int frame_limit = 10,
3162 StackTrace::StackTraceOptions options = StackTrace::kOverview);
3163
3164 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003165 * Sets V8 flags from a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003166 */
3167 static void SetFlagsFromString(const char* str, int length);
3168
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003169 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003170 * Sets V8 flags from the command line.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003171 */
3172 static void SetFlagsFromCommandLine(int* argc,
3173 char** argv,
3174 bool remove_flags);
3175
kasper.lund7276f142008-07-30 08:49:36 +00003176 /** Get the version string. */
3177 static const char* GetVersion();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178
3179 /**
3180 * Enables the host application to provide a mechanism for recording
3181 * statistics counters.
3182 */
3183 static void SetCounterFunction(CounterLookupCallback);
3184
3185 /**
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003186 * Enables the host application to provide a mechanism for recording
3187 * histograms. The CreateHistogram function returns a
3188 * histogram which will later be passed to the AddHistogramSample
3189 * function.
3190 */
3191 static void SetCreateHistogramFunction(CreateHistogramCallback);
3192 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
3193
3194 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003195 * Enables the computation of a sliding window of states. The sliding
3196 * window information is recorded in statistics counters.
3197 */
3198 static void EnableSlidingStateWindow();
3199
3200 /** Callback function for reporting failed access checks.*/
3201 static void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
3202
3203 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003204 * Enables the host application to receive a notification before a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003205 * garbage collection. Allocations are not allowed in the
3206 * callback function, you therefore cannot manipulate objects (set
3207 * or delete properties for example) since it is possible such
3208 * operations will result in the allocation of objects. It is possible
3209 * to specify the GCType filter for your callback. But it is not possible to
3210 * register the same callback function two times with different
3211 * GCType filters.
3212 */
3213 static void AddGCPrologueCallback(
3214 GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll);
3215
3216 /**
3217 * This function removes callback which was installed by
3218 * AddGCPrologueCallback function.
3219 */
3220 static void RemoveGCPrologueCallback(GCPrologueCallback callback);
3221
3222 /**
3223 * The function is deprecated. Please use AddGCPrologueCallback instead.
3224 * Enables the host application to receive a notification before a
3225 * garbage collection. Allocations are not allowed in the
v8.team.kasperl727e9952008-09-02 14:56:44 +00003226 * callback function, you therefore cannot manipulate objects (set
3227 * or delete properties for example) since it is possible such
3228 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229 */
3230 static void SetGlobalGCPrologueCallback(GCCallback);
3231
3232 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003233 * Enables the host application to receive a notification after a
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003234 * garbage collection. Allocations are not allowed in the
3235 * callback function, you therefore cannot manipulate objects (set
3236 * or delete properties for example) since it is possible such
3237 * operations will result in the allocation of objects. It is possible
3238 * to specify the GCType filter for your callback. But it is not possible to
3239 * register the same callback function two times with different
3240 * GCType filters.
3241 */
3242 static void AddGCEpilogueCallback(
3243 GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll);
3244
3245 /**
3246 * This function removes callback which was installed by
3247 * AddGCEpilogueCallback function.
3248 */
3249 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
3250
3251 /**
3252 * The function is deprecated. Please use AddGCEpilogueCallback instead.
3253 * Enables the host application to receive a notification after a
v8.team.kasperl727e9952008-09-02 14:56:44 +00003254 * major garbage collection. Allocations are not allowed in the
3255 * callback function, you therefore cannot manipulate objects (set
3256 * or delete properties for example) since it is possible such
3257 * operations will result in the allocation of objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 */
3259 static void SetGlobalGCEpilogueCallback(GCCallback);
3260
3261 /**
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003262 * Enables the host application to provide a mechanism to be notified
3263 * and perform custom logging when V8 Allocates Executable Memory.
3264 */
3265 static void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
3266 ObjectSpace space,
3267 AllocationAction action);
3268
3269 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003270 * Removes callback that was installed by AddMemoryAllocationCallback.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003271 */
3272 static void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
3273
3274 /**
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003275 * Adds a callback to notify the host application when a script finished
3276 * running. If a script re-enters the runtime during executing, the
3277 * CallCompletedCallback is only invoked when the outer-most script
3278 * execution ends. Executing scripts inside the callback do not trigger
3279 * further callbacks.
3280 */
3281 static void AddCallCompletedCallback(CallCompletedCallback callback);
3282
3283 /**
3284 * Removes callback that was installed by AddCallCompletedCallback.
3285 */
3286 static void RemoveCallCompletedCallback(CallCompletedCallback callback);
3287
3288 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003289 * Allows the host application to group objects together. If one
3290 * object in the group is alive, all objects in the group are alive.
3291 * After each garbage collection, object groups are removed. It is
3292 * intended to be used in the before-garbage-collection callback
ager@chromium.org8bb60582008-12-11 12:02:20 +00003293 * function, for instance to simulate DOM tree connections among JS
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003294 * wrapper objects. Object groups for all dependent handles need to
3295 * be provided for kGCTypeMarkSweepCompact collections, for all other
3296 * garbage collection types it is sufficient to provide object groups
3297 * for partially dependent handles only.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003298 * See v8-profiler.h for RetainedObjectInfo interface description.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003299 */
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003300 static void AddObjectGroup(Persistent<Value>* objects,
3301 size_t length,
3302 RetainedObjectInfo* info = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303
3304 /**
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00003305 * Allows the host application to declare implicit references between
3306 * the objects: if |parent| is alive, all |children| are alive too.
3307 * After each garbage collection, all implicit references
3308 * are removed. It is intended to be used in the before-garbage-collection
3309 * callback function.
3310 */
3311 static void AddImplicitReferences(Persistent<Object> parent,
3312 Persistent<Value>* children,
3313 size_t length);
3314
3315 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003316 * Initializes from snapshot if possible. Otherwise, attempts to
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003317 * initialize from scratch. This function is called implicitly if
3318 * you use the API without calling it first.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003319 */
3320 static bool Initialize();
3321
kasper.lund7276f142008-07-30 08:49:36 +00003322 /**
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00003323 * Allows the host application to provide a callback which can be used
3324 * as a source of entropy for random number generators.
3325 */
3326 static void SetEntropySource(EntropySource source);
3327
3328 /**
ulan@chromium.org967e2702012-02-28 09:49:15 +00003329 * Allows the host application to provide a callback that allows v8 to
3330 * cooperate with a profiler that rewrites return addresses on stack.
3331 */
3332 static void SetReturnAddressLocationResolver(
3333 ReturnAddressLocationResolver return_address_resolver);
3334
3335 /**
verwaest@chromium.org753aee42012-07-17 16:15:42 +00003336 * Allows the host application to provide the address of a function that's
3337 * invoked on entry to every V8-generated function.
3338 * Note that \p entry_hook is invoked at the very start of each
3339 * generated function.
3340 *
3341 * \param entry_hook a function that will be invoked on entry to every
3342 * V8-generated function.
3343 * \returns true on success on supported platforms, false on failure.
3344 * \note Setting a new entry hook function when one is already active will
3345 * fail.
3346 */
3347 static bool SetFunctionEntryHook(FunctionEntryHook entry_hook);
3348
3349 /**
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00003350 * Allows the host application to provide the address of a function that is
3351 * notified each time code is added, moved or removed.
3352 *
3353 * \param options options for the JIT code event handler.
3354 * \param event_handler the JIT code event handler, which will be invoked
3355 * each time code is added, moved or removed.
3356 * \note \p event_handler won't get notified of existent code.
3357 * \note since code removal notifications are not currently issued, the
3358 * \p event_handler may get notifications of code that overlaps earlier
3359 * code notifications. This happens when code areas are reused, and the
3360 * earlier overlapping code areas should therefore be discarded.
3361 * \note the events passed to \p event_handler and the strings they point to
3362 * are not guaranteed to live past each call. The \p event_handler must
3363 * copy strings and other parameters it needs to keep around.
3364 * \note the set of events declared in JitCodeEvent::EventType is expected to
3365 * grow over time, and the JitCodeEvent structure is expected to accrue
3366 * new members. The \p event_handler function must ignore event codes
3367 * it does not recognize to maintain future compatibility.
3368 */
3369 static void SetJitCodeEventHandler(JitCodeEventOptions options,
3370 JitCodeEventHandler event_handler);
3371
3372 /**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003373 * Adjusts the amount of registered external memory. Used to give
3374 * V8 an indication of the amount of externally allocated memory
3375 * that is kept alive by JavaScript objects. V8 uses this to decide
3376 * when to perform global garbage collections. Registering
3377 * externally allocated memory will trigger global garbage
3378 * collections more often than otherwise in an attempt to garbage
3379 * collect the JavaScript objects keeping the externally allocated
3380 * memory alive.
3381 *
3382 * \param change_in_bytes the change in externally allocated memory
3383 * that is kept alive by JavaScript objects.
3384 * \returns the adjusted value.
kasper.lund7276f142008-07-30 08:49:36 +00003385 */
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00003386 static intptr_t AdjustAmountOfExternalAllocatedMemory(
3387 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00003388
iposva@chromium.org245aa852009-02-10 00:49:54 +00003389 /**
3390 * Suspends recording of tick samples in the profiler.
3391 * When the V8 profiling mode is enabled (usually via command line
3392 * switches) this function suspends recording of tick samples.
3393 * Profiling ticks are discarded until ResumeProfiler() is called.
3394 *
3395 * See also the --prof and --prof_auto command line switches to
3396 * enable V8 profiling.
3397 */
3398 static void PauseProfiler();
3399
3400 /**
3401 * Resumes recording of tick samples in the profiler.
3402 * See also PauseProfiler().
3403 */
3404 static void ResumeProfiler();
3405
ager@chromium.org41826e72009-03-30 13:30:57 +00003406 /**
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003407 * Return whether profiler is currently paused.
3408 */
3409 static bool IsProfilerPaused();
3410
3411 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003412 * Retrieve the V8 thread id of the calling thread.
3413 *
3414 * The thread id for a thread should only be retrieved after the V8
3415 * lock has been acquired with a Locker object with that thread.
3416 */
3417 static int GetCurrentThreadId();
3418
3419 /**
3420 * Forcefully terminate execution of a JavaScript thread. This can
3421 * be used to terminate long-running scripts.
3422 *
3423 * TerminateExecution should only be called when then V8 lock has
3424 * been acquired with a Locker object. Therefore, in order to be
3425 * able to terminate long-running threads, preemption must be
3426 * enabled to allow the user of TerminateExecution to acquire the
3427 * lock.
3428 *
3429 * The termination is achieved by throwing an exception that is
3430 * uncatchable by JavaScript exception handlers. Termination
3431 * exceptions act as if they were caught by a C++ TryCatch exception
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003432 * handler. If forceful termination is used, any C++ TryCatch
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003433 * exception handler that catches an exception should check if that
3434 * exception is a termination exception and immediately return if
3435 * that is the case. Returning immediately in that case will
3436 * continue the propagation of the termination exception if needed.
3437 *
3438 * The thread id passed to TerminateExecution must have been
3439 * obtained by calling GetCurrentThreadId on the thread in question.
3440 *
3441 * \param thread_id The thread id of the thread to terminate.
3442 */
3443 static void TerminateExecution(int thread_id);
3444
3445 /**
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003446 * Forcefully terminate the current thread of JavaScript execution
3447 * in the given isolate. If no isolate is provided, the default
3448 * isolate is used.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003449 *
3450 * This method can be used by any thread even if that thread has not
3451 * acquired the V8 lock with a Locker object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003452 *
3453 * \param isolate The isolate in which to terminate the current JS execution.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003454 */
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003455 static void TerminateExecution(Isolate* isolate = NULL);
ager@chromium.org9085a012009-05-11 19:22:57 +00003456
3457 /**
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003458 * Is V8 terminating JavaScript execution.
3459 *
3460 * Returns true if JavaScript execution is currently terminating
3461 * because of a call to TerminateExecution. In that case there are
3462 * still JavaScript frames on the stack and the termination
3463 * exception is still active.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003464 *
3465 * \param isolate The isolate in which to check.
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003466 */
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003467 static bool IsExecutionTerminating(Isolate* isolate = NULL);
sgjesse@chromium.org2ab99522010-03-10 09:03:43 +00003468
3469 /**
ager@chromium.org41826e72009-03-30 13:30:57 +00003470 * Releases any resources used by v8 and stops any utility threads
3471 * that may be running. Note that disposing v8 is permanent, it
3472 * cannot be reinitialized.
3473 *
3474 * It should generally not be necessary to dispose v8 before exiting
3475 * a process, this should happen automatically. It is only necessary
3476 * to use if the process needs the resources taken up by v8.
3477 */
3478 static bool Dispose();
3479
ager@chromium.org3811b432009-10-28 14:53:37 +00003480 /**
3481 * Get statistics about the heap memory usage.
3482 */
3483 static void GetHeapStatistics(HeapStatistics* heap_statistics);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003484
3485 /**
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003486 * Iterates through all external resources referenced from current isolate
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003487 * heap. GC is not invoked prior to iterating, therefore there is no
3488 * guarantee that visited objects are still alive.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003489 */
3490 static void VisitExternalResources(ExternalResourceVisitor* visitor);
3491
3492 /**
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003493 * Iterates through all the persistent handles in the current isolate's heap
3494 * that have class_ids.
3495 */
3496 static void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
3497
3498 /**
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003499 * Optional notification that the embedder is idle.
3500 * V8 uses the notification to reduce memory footprint.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003501 * This call can be used repeatedly if the embedder remains idle.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003502 * Returns true if the embedder should stop calling IdleNotification
3503 * until real work has been done. This indicates that V8 has done
3504 * as much cleanup as it will be able to do.
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003505 *
3506 * The hint argument specifies the amount of work to be done in the function
3507 * on scale from 1 to 1000. There is no guarantee that the actual work will
3508 * match the hint.
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003509 */
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00003510 static bool IdleNotification(int hint = 1000);
ager@chromium.orgadd848f2009-08-13 12:44:13 +00003511
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003512 /**
3513 * Optional notification that the system is running low on memory.
3514 * V8 uses these notifications to attempt to free memory.
3515 */
3516 static void LowMemoryNotification();
3517
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003518 /**
3519 * Optional notification that a context has been disposed. V8 uses
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003520 * these notifications to guide the GC heuristic. Returns the number
3521 * of context disposals - including this one - since the last time
3522 * V8 had a chance to clean up.
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003523 */
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003524 static int ContextDisposedNotification();
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00003525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526 private:
3527 V8();
3528
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003529 static internal::Object** GlobalizeReference(internal::Object** handle);
3530 static void DisposeGlobal(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003531 static void DisposeGlobal(internal::Isolate* isolate,
3532 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003533 static void MakeWeak(internal::Object** global_handle,
3534 void* data,
3535 WeakReferenceCallback);
3536 static void ClearWeak(internal::Object** global_handle);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003537 static void MarkIndependent(internal::Object** global_handle);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003538 static void MarkIndependent(internal::Isolate* isolate,
3539 internal::Object** global_handle);
3540 static void MarkPartiallyDependent(internal::Object** global_handle);
3541 static void MarkPartiallyDependent(internal::Isolate* isolate,
3542 internal::Object** global_handle);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003543 static bool IsGlobalIndependent(internal::Object** global_handle);
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003544 static bool IsGlobalIndependent(internal::Isolate* isolate,
3545 internal::Object** global_handle);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003546 static bool IsGlobalNearDeath(internal::Object** global_handle);
3547 static bool IsGlobalWeak(internal::Object** global_handle);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003548 static void SetWrapperClassId(internal::Object** global_handle,
3549 uint16_t class_id);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00003550 static uint16_t GetWrapperClassId(internal::Object** global_handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551
3552 template <class T> friend class Handle;
3553 template <class T> friend class Local;
3554 template <class T> friend class Persistent;
3555 friend class Context;
3556};
3557
3558
3559/**
3560 * An external exception handler.
3561 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003562class V8EXPORT TryCatch {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003564 /**
3565 * Creates a new try/catch block and registers it with v8.
3566 */
3567 TryCatch();
3568
3569 /**
3570 * Unregisters and deletes this try/catch block.
3571 */
3572 ~TryCatch();
3573
3574 /**
3575 * Returns true if an exception has been caught by this try/catch block.
3576 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003577 bool HasCaught() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003578
3579 /**
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00003580 * For certain types of exceptions, it makes no sense to continue
3581 * execution.
3582 *
3583 * Currently, the only type of exception that can be caught by a
3584 * TryCatch handler and for which it does not make sense to continue
3585 * is termination exception. Such exceptions are thrown when the
3586 * TerminateExecution methods are called to terminate a long-running
3587 * script.
3588 *
3589 * If CanContinue returns false, the correct action is to perform
3590 * any C++ cleanup needed and then return.
3591 */
3592 bool CanContinue() const;
3593
3594 /**
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003595 * Throws the exception caught by this TryCatch in a way that avoids
3596 * it being caught again by this same TryCatch. As with ThrowException
3597 * it is illegal to execute any JavaScript operations after calling
3598 * ReThrow; the caller must return immediately to where the exception
3599 * is caught.
3600 */
3601 Handle<Value> ReThrow();
3602
3603 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003604 * Returns the exception caught by this try/catch block. If no exception has
3605 * been caught an empty handle is returned.
3606 *
3607 * The returned handle is valid until this TryCatch block has been destroyed.
3608 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003609 Local<Value> Exception() const;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003610
3611 /**
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003612 * Returns the .stack property of the thrown object. If no .stack
3613 * property is present an empty handle is returned.
3614 */
3615 Local<Value> StackTrace() const;
3616
3617 /**
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003618 * Returns the message associated with this exception. If there is
3619 * no message associated an empty handle is returned.
3620 *
3621 * The returned handle is valid until this TryCatch block has been
3622 * destroyed.
3623 */
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003624 Local<v8::Message> Message() const;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003625
3626 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627 * Clears any exceptions that may have been caught by this try/catch block.
3628 * After this method has been called, HasCaught() will return false.
3629 *
3630 * It is not necessary to clear a try/catch block before using it again; if
3631 * another exception is thrown the previously caught exception will just be
3632 * overwritten. However, it is often a good idea since it makes it easier
3633 * to determine which operation threw a given exception.
3634 */
3635 void Reset();
3636
v8.team.kasperl727e9952008-09-02 14:56:44 +00003637 /**
3638 * Set verbosity of the external exception handler.
3639 *
3640 * By default, exceptions that are caught by an external exception
3641 * handler are not reported. Call SetVerbose with true on an
3642 * external exception handler to have exceptions caught by the
3643 * handler reported as if they were not caught.
3644 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003645 void SetVerbose(bool value);
3646
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003647 /**
3648 * Set whether or not this TryCatch should capture a Message object
3649 * which holds source information about where the exception
3650 * occurred. True by default.
3651 */
3652 void SetCaptureMessage(bool value);
3653
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003654 private:
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00003655 v8::internal::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003656 void* next_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657 void* exception_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003658 void* message_;
christian.plesner.hansen@gmail.comb9ce6372009-11-03 11:38:18 +00003659 bool is_verbose_ : 1;
3660 bool can_continue_ : 1;
3661 bool capture_message_ : 1;
3662 bool rethrow_ : 1;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003663
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003664 friend class v8::internal::Isolate;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665};
3666
3667
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003668// --- Context ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003669
3670
3671/**
3672 * Ignore
3673 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003674class V8EXPORT ExtensionConfiguration {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675 public:
3676 ExtensionConfiguration(int name_count, const char* names[])
3677 : name_count_(name_count), names_(names) { }
3678 private:
3679 friend class ImplementationUtilities;
3680 int name_count_;
3681 const char** names_;
3682};
3683
3684
3685/**
3686 * A sandboxed execution context with its own set of built-in objects
3687 * and functions.
3688 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003689class V8EXPORT Context {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003690 public:
whesse@chromium.org7a392b32011-01-31 11:30:36 +00003691 /**
3692 * Returns the global proxy object or global object itself for
3693 * detached contexts.
3694 *
3695 * Global proxy object is a thin wrapper whose prototype points to
3696 * actual context's global object with the properties like Object, etc.
3697 * This is done that way for security reasons (for more details see
3698 * https://wiki.mozilla.org/Gecko:SplitWindow).
3699 *
3700 * Please note that changes to global proxy object prototype most probably
3701 * would break VM---v8 expects only global object as a prototype of
3702 * global proxy object.
3703 *
3704 * If DetachGlobal() has been invoked, Global() would return actual global
3705 * object until global is reattached with ReattachGlobal().
3706 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 Local<Object> Global();
3708
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003709 /**
3710 * Detaches the global object from its context before
3711 * the global object can be reused to create a new context.
3712 */
3713 void DetachGlobal();
3714
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00003715 /**
3716 * Reattaches a global object to a context. This can be used to
3717 * restore the connection between a global object and a context
3718 * after DetachGlobal has been called.
3719 *
3720 * \param global_object The global object to reattach to the
3721 * context. For this to work, the global object must be the global
3722 * object that was associated with this context before a call to
3723 * DetachGlobal.
3724 */
3725 void ReattachGlobal(Handle<Object> global_object);
3726
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003727 /** Creates a new context.
3728 *
3729 * Returns a persistent handle to the newly allocated context. This
3730 * persistent handle has to be disposed when the context is no
3731 * longer used so the context can be garbage collected.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003732 *
3733 * \param extensions An optional extension configuration containing
3734 * the extensions to be installed in the newly created context.
3735 *
3736 * \param global_template An optional object template from which the
3737 * global object for the newly created context will be created.
3738 *
3739 * \param global_object An optional global object to be reused for
3740 * the newly created context. This global object must have been
3741 * created by a previous call to Context::New with the same global
3742 * template. The state of the global object will be completely reset
3743 * and only object identify will remain.
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00003744 */
v8.team.kasperl727e9952008-09-02 14:56:44 +00003745 static Persistent<Context> New(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00003746 ExtensionConfiguration* extensions = NULL,
v8.team.kasperl727e9952008-09-02 14:56:44 +00003747 Handle<ObjectTemplate> global_template = Handle<ObjectTemplate>(),
3748 Handle<Value> global_object = Handle<Value>());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003749
kasper.lund44510672008-07-25 07:37:58 +00003750 /** Returns the last entered context. */
3751 static Local<Context> GetEntered();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003752
kasper.lund44510672008-07-25 07:37:58 +00003753 /** Returns the context that is on the top of the stack. */
3754 static Local<Context> GetCurrent();
3755
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756 /**
ager@chromium.org1bf0cd02009-05-20 11:34:19 +00003757 * Returns the context of the calling JavaScript code. That is the
3758 * context of the top-most JavaScript frame. If there are no
3759 * JavaScript frames an empty handle is returned.
3760 */
3761 static Local<Context> GetCalling();
3762
3763 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 * Sets the security token for the context. To access an object in
3765 * another context, the security tokens must match.
3766 */
3767 void SetSecurityToken(Handle<Value> token);
3768
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003769 /** Restores the security token to the default value. */
3770 void UseDefaultSecurityToken();
3771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003772 /** Returns the security token of this context.*/
3773 Handle<Value> GetSecurityToken();
3774
v8.team.kasperl727e9952008-09-02 14:56:44 +00003775 /**
3776 * Enter this context. After entering a context, all code compiled
3777 * and run is compiled and run in this context. If another context
3778 * is already entered, this old context is saved so it can be
3779 * restored when the new context is exited.
3780 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781 void Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003782
3783 /**
3784 * Exit this context. Exiting the current context restores the
3785 * context that was in place when entering the current context.
3786 */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787 void Exit();
3788
v8.team.kasperl727e9952008-09-02 14:56:44 +00003789 /** Returns true if the context has experienced an out of memory situation. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003790 bool HasOutOfMemoryException();
3791
v8.team.kasperl727e9952008-09-02 14:56:44 +00003792 /** Returns true if V8 has a current context. */
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793 static bool InContext();
3794
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795 /**
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003796 * Gets embedder data with index 0. Deprecated, use GetEmbedderData with index
3797 * 0 instead.
ager@chromium.org9085a012009-05-11 19:22:57 +00003798 */
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003799 V8_DEPRECATED(inline Local<Value> GetData());
3800
3801 /**
3802 * Sets embedder data with index 0. Deprecated, use SetEmbedderData with index
3803 * 0 instead.
3804 */
3805 V8_DEPRECATED(inline void SetData(Handle<Value> value));
3806
3807 /**
3808 * Gets the embedder data with the given index, which must have been set by a
3809 * previous call to SetEmbedderData with the same index. Note that index 0
3810 * currently has a special meaning for Chrome's debugger.
3811 */
3812 inline Local<Value> GetEmbedderData(int index);
3813
3814 /**
3815 * Sets the embedder data with the given index, growing the data as
3816 * needed. Note that index 0 currently has a special meaning for Chrome's
3817 * debugger.
3818 */
3819 void SetEmbedderData(int index, Handle<Value> value);
3820
3821 /**
3822 * Gets a 2-byte-aligned native pointer from the embedder data with the given
3823 * index, which must have bees set by a previous call to
3824 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
3825 * currently has a special meaning for Chrome's debugger.
3826 */
3827 inline void* GetAlignedPointerFromEmbedderData(int index);
3828
3829 /**
3830 * Sets a 2-byte-aligned native pointer in the embedder data with the given
3831 * index, growing the data as needed. Note that index 0 currently has a
3832 * special meaning for Chrome's debugger.
3833 */
3834 void SetAlignedPointerInEmbedderData(int index, void* value);
ager@chromium.org9085a012009-05-11 19:22:57 +00003835
3836 /**
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00003837 * Control whether code generation from strings is allowed. Calling
3838 * this method with false will disable 'eval' and the 'Function'
3839 * constructor for code running in this context. If 'eval' or the
3840 * 'Function' constructor are used an exception will be thrown.
3841 *
3842 * If code generation from strings is not allowed the
3843 * V8::AllowCodeGenerationFromStrings callback will be invoked if
3844 * set before blocking the call to 'eval' or the 'Function'
3845 * constructor. If that callback returns true, the call will be
3846 * allowed, otherwise an exception will be thrown. If no callback is
3847 * set an exception will be thrown.
3848 */
3849 void AllowCodeGenerationFromStrings(bool allow);
3850
3851 /**
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00003852 * Returns true if code generation from strings is allowed for the context.
3853 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
3854 */
3855 bool IsCodeGenerationFromStringsAllowed();
3856
3857 /**
ulan@chromium.org56c14af2012-09-20 12:51:09 +00003858 * Sets the error description for the exception that is thrown when
3859 * code generation from strings is not allowed and 'eval' or the 'Function'
3860 * constructor are called.
3861 */
3862 void SetErrorMessageForCodeGenerationFromStrings(Handle<String> message);
3863
3864 /**
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003865 * Stack-allocated class which sets the execution context for all
3866 * operations executed within a local scope.
3867 */
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003868 class Scope {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00003870 explicit inline Scope(Handle<Context> context) : context_(context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871 context_->Enter();
3872 }
3873 inline ~Scope() { context_->Exit(); }
3874 private:
3875 Handle<Context> context_;
3876 };
3877
3878 private:
3879 friend class Value;
3880 friend class Script;
3881 friend class Object;
3882 friend class Function;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00003883
3884 Local<Value> SlowGetEmbedderData(int index);
3885 void* SlowGetAlignedPointerFromEmbedderData(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003886};
3887
3888
3889/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00003890 * Multiple threads in V8 are allowed, but only one thread at a time
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003891 * is allowed to use any given V8 isolate. See Isolate class
3892 * comments. The definition of 'using V8 isolate' includes
3893 * accessing handles or holding onto object pointers obtained
3894 * from V8 handles while in the particular V8 isolate. It is up
3895 * to the user of V8 to ensure (perhaps with locking) that this
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003896 * constraint is not violated. In addition to any other synchronization
3897 * mechanism that may be used, the v8::Locker and v8::Unlocker classes
3898 * must be used to signal thead switches to V8.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003900 * v8::Locker is a scoped lock object. While it's
3901 * active (i.e. between its construction and destruction) the current thread is
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003902 * allowed to use the locked isolate. V8 guarantees that an isolate can be
3903 * locked by at most one thread at any time. In other words, the scope of a
3904 * v8::Locker is a critical section.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003905 *
lrn@chromium.org1c092762011-05-09 09:42:16 +00003906 * Sample usage:
3907* \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908 * ...
3909 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003910 * v8::Locker locker(isolate);
3911 * v8::Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003912 * ...
lrn@chromium.org1c092762011-05-09 09:42:16 +00003913 * // Code using V8 and isolate goes here.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914 * ...
3915 * } // Destructor called here
v8.team.kasperl727e9952008-09-02 14:56:44 +00003916 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003918 * If you wish to stop using V8 in a thread A you can do this either
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 * by destroying the v8::Locker object as above or by constructing a
3920 * v8::Unlocker object:
3921 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003922 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003924 * isolate->Exit();
3925 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926 * ...
3927 * // Code not using V8 goes here while V8 can run in another thread.
3928 * ...
3929 * } // Destructor called here.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003930 * isolate->Enter();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003931 * \endcode
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003932 *
3933 * The Unlocker object is intended for use in a long-running callback
3934 * from V8, where you want to release the V8 lock for other threads to
3935 * use.
3936 *
3937 * The v8::Locker is a recursive lock. That is, you can lock more than
3938 * once in a given thread. This can be useful if you have code that can
3939 * be called either from code that holds the lock or from code that does
3940 * not. The Unlocker is not recursive so you can not have several
3941 * Unlockers on the stack at once, and you can not use an Unlocker in a
3942 * thread that is not inside a Locker's scope.
3943 *
3944 * An unlocker will unlock several lockers if it has to and reinstate
3945 * the correct depth of locking on its destruction. eg.:
3946 *
v8.team.kasperl727e9952008-09-02 14:56:44 +00003947 * \code
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948 * // V8 not locked.
3949 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003950 * v8::Locker locker(isolate);
3951 * Isolate::Scope isolate_scope(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003952 * // V8 locked.
3953 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003954 * v8::Locker another_locker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955 * // V8 still locked (2 levels).
3956 * {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003957 * isolate->Exit();
3958 * v8::Unlocker unlocker(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959 * // V8 not locked.
3960 * }
lrn@chromium.org1c092762011-05-09 09:42:16 +00003961 * isolate->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962 * // V8 locked again (2 levels).
3963 * }
3964 * // V8 still locked (1 level).
3965 * }
3966 * // V8 Now no longer locked.
v8.team.kasperl727e9952008-09-02 14:56:44 +00003967 * \endcode
lrn@chromium.org1c092762011-05-09 09:42:16 +00003968 *
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003969 *
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003970 */
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003971class V8EXPORT Unlocker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003973 /**
3974 * Initialize Unlocker for a given Isolate. NULL means default isolate.
3975 */
3976 explicit Unlocker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977 ~Unlocker();
lrn@chromium.org1c092762011-05-09 09:42:16 +00003978 private:
3979 internal::Isolate* isolate_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980};
3981
3982
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003983class V8EXPORT Locker {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 public:
lrn@chromium.org1c092762011-05-09 09:42:16 +00003985 /**
3986 * Initialize Locker for a given Isolate. NULL means default isolate.
3987 */
3988 explicit Locker(Isolate* isolate = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989 ~Locker();
v8.team.kasperl727e9952008-09-02 14:56:44 +00003990
3991 /**
3992 * Start preemption.
3993 *
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003994 * When preemption is started, a timer is fired every n milliseconds
v8.team.kasperl727e9952008-09-02 14:56:44 +00003995 * that will switch between multiple threads that are in contention
3996 * for the V8 lock.
3997 */
3998 static void StartPreemption(int every_n_ms);
3999
4000 /**
4001 * Stop preemption.
4002 */
4003 static void StopPreemption();
4004
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004005 /**
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004006 * Returns whether or not the locker for a given isolate, or default isolate
4007 * if NULL is given, is locked by the current thread.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004008 */
lrn@chromium.org1c092762011-05-09 09:42:16 +00004009 static bool IsLocked(Isolate* isolate = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004010
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004011 /**
4012 * Returns whether v8::Locker is being used by this V8 instance.
4013 */
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004014 static bool IsActive();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004015
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016 private:
4017 bool has_lock_;
4018 bool top_level_;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004019 internal::Isolate* isolate_;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004020
ager@chromium.orgddb913d2009-01-27 10:01:48 +00004021 static bool active_;
4022
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004023 // Disallow copying and assigning.
4024 Locker(const Locker&);
4025 void operator=(const Locker&);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004026};
4027
4028
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004029/**
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004030 * A struct for exporting HeapStats data from V8, using "push" model.
4031 */
4032struct HeapStatsUpdate;
4033
4034
4035/**
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004036 * An interface for exporting data from V8, using "push" model.
4037 */
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004038class V8EXPORT OutputStream { // NOLINT
4039 public:
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004040 enum OutputEncoding {
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004041 kAscii = 0 // 7-bit ASCII.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004042 };
4043 enum WriteResult {
4044 kContinue = 0,
4045 kAbort = 1
4046 };
4047 virtual ~OutputStream() {}
4048 /** Notify about the end of stream. */
4049 virtual void EndOfStream() = 0;
4050 /** Get preferred output chunk size. Called only once. */
4051 virtual int GetChunkSize() { return 1024; }
4052 /** Get preferred output encoding. Called only once. */
4053 virtual OutputEncoding GetOutputEncoding() { return kAscii; }
4054 /**
4055 * Writes the next chunk of snapshot data into the stream. Writing
4056 * can be stopped by returning kAbort as function result. EndOfStream
4057 * will not be called in case writing was aborted.
4058 */
4059 virtual WriteResult WriteAsciiChunk(char* data, int size) = 0;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004060 /**
4061 * Writes the next chunk of heap stats data into the stream. Writing
4062 * can be stopped by returning kAbort as function result. EndOfStream
4063 * will not be called in case writing was aborted.
4064 */
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004065 virtual WriteResult WriteHeapStatsChunk(HeapStatsUpdate* data, int count) {
jkummerow@chromium.org3ee08a62012-04-13 13:01:33 +00004066 return kAbort;
4067 };
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004068};
4069
4070
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004071/**
4072 * An interface for reporting progress and controlling long-running
4073 * activities.
4074 */
4075class V8EXPORT ActivityControl { // NOLINT
4076 public:
4077 enum ControlOption {
4078 kContinue = 0,
4079 kAbort = 1
4080 };
4081 virtual ~ActivityControl() {}
4082 /**
4083 * Notify about current progress. The activity can be stopped by
4084 * returning kAbort as the callback result.
4085 */
4086 virtual ControlOption ReportProgressValue(int done, int total) = 0;
4087};
4088
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004089
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00004090// --- Implementation ---
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004091
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004092
4093namespace internal {
4094
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00004095const int kApiPointerSize = sizeof(void*); // NOLINT
4096const int kApiIntSize = sizeof(int); // NOLINT
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004097
4098// Tag information for HeapObject.
4099const int kHeapObjectTag = 1;
4100const int kHeapObjectTagSize = 2;
4101const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
4102
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004103// Tag information for Smi.
4104const int kSmiTag = 0;
4105const int kSmiTagSize = 1;
4106const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
4107
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004108template <size_t ptr_size> struct SmiTagging;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004109
4110// Smi constants for 32-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004111template <> struct SmiTagging<4> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004112 static const int kSmiShiftSize = 0;
4113 static const int kSmiValueSize = 31;
4114 static inline int SmiToInt(internal::Object* value) {
4115 int shift_bits = kSmiTagSize + kSmiShiftSize;
4116 // Throw away top 32 bits and shift down (requires >> to be sign extending).
4117 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
4118 }
4119};
4120
4121// Smi constants for 64-bit systems.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004122template <> struct SmiTagging<8> {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004123 static const int kSmiShiftSize = 31;
4124 static const int kSmiValueSize = 32;
4125 static inline int SmiToInt(internal::Object* value) {
4126 int shift_bits = kSmiTagSize + kSmiShiftSize;
4127 // Shift down and throw away top 32 bits.
4128 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
4129 }
4130};
4131
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004132typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
4133const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
4134const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004135
4136/**
4137 * This class exports constants and functionality from within v8 that
4138 * is necessary to implement inline functions in the v8 api. Don't
4139 * depend on functions and constants defined here.
4140 */
4141class Internals {
4142 public:
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004143 // These values match non-compiler-dependent values defined within
4144 // the implementation of v8.
4145 static const int kHeapObjectMapOffset = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004146 static const int kMapInstanceTypeOffset = 1 * kApiPointerSize + kApiIntSize;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004147 static const int kStringResourceOffset = 3 * kApiPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004148
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004149 static const int kOddballKindOffset = 3 * kApiPointerSize;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004150 static const int kForeignAddressOffset = kApiPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004151 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004152 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
4153 static const int kContextHeaderSize = 2 * kApiPointerSize;
4154 static const int kContextEmbedderDataIndex = 54;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004155 static const int kFullStringRepresentationMask = 0x07;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004156 static const int kStringEncodingMask = 0x4;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +00004157 static const int kExternalTwoByteRepresentationTag = 0x02;
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004158 static const int kExternalAsciiRepresentationTag = 0x06;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004159
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004160 static const int kIsolateStateOffset = 0;
4161 static const int kIsolateEmbedderDataOffset = 1 * kApiPointerSize;
4162 static const int kIsolateRootsOffset = 3 * kApiPointerSize;
4163 static const int kUndefinedValueRootIndex = 5;
4164 static const int kNullValueRootIndex = 7;
4165 static const int kTrueValueRootIndex = 8;
4166 static const int kFalseValueRootIndex = 9;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004167 static const int kEmptySymbolRootIndex = 119;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004168
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004169 static const int kJSObjectType = 0xaa;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00004170 static const int kFirstNonstringType = 0x80;
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004171 static const int kOddballType = 0x82;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004172 static const int kForeignType = 0x85;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004173
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004174 static const int kUndefinedOddballKind = 5;
4175 static const int kNullOddballKind = 3;
4176
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004177 static inline bool HasHeapObjectTag(internal::Object* value) {
4178 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
4179 kHeapObjectTag);
4180 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004181
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004182 static inline int SmiValue(internal::Object* value) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00004183 return PlatformSmiTagging::SmiToInt(value);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004184 }
4185
4186 static inline int GetInstanceType(internal::Object* obj) {
4187 typedef internal::Object O;
4188 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
4189 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
4190 }
4191
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004192 static inline int GetOddballKind(internal::Object* obj) {
4193 typedef internal::Object O;
4194 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
4195 }
4196
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004197 static inline bool IsExternalTwoByteString(int instance_type) {
4198 int representation = (instance_type & kFullStringRepresentationMask);
4199 return representation == kExternalTwoByteRepresentationTag;
4200 }
4201
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004202 static inline bool IsInitialized(v8::Isolate* isolate) {
4203 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateStateOffset;
4204 return *reinterpret_cast<int*>(addr) == 1;
4205 }
4206
4207 static inline void SetEmbedderData(v8::Isolate* isolate, void* data) {
4208 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4209 kIsolateEmbedderDataOffset;
4210 *reinterpret_cast<void**>(addr) = data;
4211 }
4212
4213 static inline void* GetEmbedderData(v8::Isolate* isolate) {
4214 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
4215 kIsolateEmbedderDataOffset;
4216 return *reinterpret_cast<void**>(addr);
4217 }
4218
4219 static inline internal::Object** GetRoot(v8::Isolate* isolate, int index) {
4220 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
4221 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
4222 }
4223
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004224 template <typename T>
4225 static inline T ReadField(Object* ptr, int offset) {
4226 uint8_t* addr = reinterpret_cast<uint8_t*>(ptr) + offset - kHeapObjectTag;
4227 return *reinterpret_cast<T*>(addr);
4228 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004229
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004230 template <typename T>
4231 static inline T ReadEmbedderData(Context* context, int index) {
4232 typedef internal::Object O;
4233 typedef internal::Internals I;
4234 O* ctx = *reinterpret_cast<O**>(context);
4235 int embedder_data_offset = I::kContextHeaderSize +
4236 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
4237 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
4238 int value_offset =
4239 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
4240 return I::ReadField<T>(embedder_data, value_offset);
4241 }
4242
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004243 static inline bool CanCastToHeapObject(void* o) { return false; }
4244 static inline bool CanCastToHeapObject(Context* o) { return true; }
4245 static inline bool CanCastToHeapObject(String* o) { return true; }
4246 static inline bool CanCastToHeapObject(Object* o) { return true; }
4247 static inline bool CanCastToHeapObject(Message* o) { return true; }
4248 static inline bool CanCastToHeapObject(StackTrace* o) { return true; }
4249 static inline bool CanCastToHeapObject(StackFrame* o) { return true; }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004250};
4251
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00004252} // namespace internal
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004253
4254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004255template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004256Local<T>::Local() : Handle<T>() { }
4257
4258
4259template <class T>
4260Local<T> Local<T>::New(Handle<T> that) {
4261 if (that.IsEmpty()) return Local<T>();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004262 T* that_ptr = *that;
4263 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
4264 if (internal::Internals::CanCastToHeapObject(that_ptr)) {
4265 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
4266 reinterpret_cast<internal::HeapObject*>(*p))));
4267 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004268 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(*p)));
4269}
4270
4271
4272template <class T>
4273Persistent<T> Persistent<T>::New(Handle<T> that) {
4274 if (that.IsEmpty()) return Persistent<T>();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004275 internal::Object** p = reinterpret_cast<internal::Object**>(*that);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276 return Persistent<T>(reinterpret_cast<T*>(V8::GlobalizeReference(p)));
4277}
4278
4279
4280template <class T>
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004281bool Persistent<T>::IsIndependent() const {
4282 if (this->IsEmpty()) return false;
4283 return V8::IsGlobalIndependent(reinterpret_cast<internal::Object**>(**this));
4284}
4285
4286
4287template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004288bool Persistent<T>::IsIndependent(Isolate* isolate) const {
4289 if (this->IsEmpty()) return false;
4290 return V8::IsGlobalIndependent(reinterpret_cast<internal::Isolate*>(isolate),
4291 reinterpret_cast<internal::Object**>(**this));
4292}
4293
4294
4295template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004296bool Persistent<T>::IsNearDeath() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004297 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004298 return V8::IsGlobalNearDeath(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004299}
4300
4301
4302template <class T>
ager@chromium.org32912102009-01-16 10:38:43 +00004303bool Persistent<T>::IsWeak() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004304 if (this->IsEmpty()) return false;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004305 return V8::IsGlobalWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004306}
4307
4308
4309template <class T>
4310void Persistent<T>::Dispose() {
4311 if (this->IsEmpty()) return;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004312 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004313}
4314
4315
4316template <class T>
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004317void Persistent<T>::Dispose(Isolate* isolate) {
4318 if (this->IsEmpty()) return;
4319 V8::DisposeGlobal(reinterpret_cast<internal::Isolate*>(isolate),
4320 reinterpret_cast<internal::Object**>(**this));
4321}
4322
4323
4324template <class T>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004325Persistent<T>::Persistent() : Handle<T>() { }
4326
4327template <class T>
4328void Persistent<T>::MakeWeak(void* parameters, WeakReferenceCallback callback) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004329 V8::MakeWeak(reinterpret_cast<internal::Object**>(**this),
4330 parameters,
4331 callback);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004332}
4333
4334template <class T>
4335void Persistent<T>::ClearWeak() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004336 V8::ClearWeak(reinterpret_cast<internal::Object**>(**this));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004337}
4338
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004339template <class T>
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004340void Persistent<T>::MarkIndependent() {
4341 V8::MarkIndependent(reinterpret_cast<internal::Object**>(**this));
4342}
4343
4344template <class T>
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00004345void Persistent<T>::MarkIndependent(Isolate* isolate) {
4346 V8::MarkIndependent(reinterpret_cast<internal::Isolate*>(isolate),
4347 reinterpret_cast<internal::Object**>(**this));
4348}
4349
4350template <class T>
4351void Persistent<T>::MarkPartiallyDependent() {
4352 V8::MarkPartiallyDependent(reinterpret_cast<internal::Object**>(**this));
4353}
4354
4355template <class T>
4356void Persistent<T>::MarkPartiallyDependent(Isolate* isolate) {
4357 V8::MarkPartiallyDependent(reinterpret_cast<internal::Isolate*>(isolate),
4358 reinterpret_cast<internal::Object**>(**this));
4359}
4360
4361template <class T>
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00004362void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
4363 V8::SetWrapperClassId(reinterpret_cast<internal::Object**>(**this), class_id);
4364}
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004365
rossberg@chromium.org89e18f52012-10-22 13:09:53 +00004366template <class T>
4367uint16_t Persistent<T>::WrapperClassId() const {
4368 return V8::GetWrapperClassId(reinterpret_cast<internal::Object**>(**this));
4369}
4370
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004371Arguments::Arguments(internal::Object** implicit_args,
4372 internal::Object** values, int length,
4373 bool is_construct_call)
4374 : implicit_args_(implicit_args),
4375 values_(values),
4376 length_(length),
4377 is_construct_call_(is_construct_call) { }
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004378
4379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004380Local<Value> Arguments::operator[](int i) const {
4381 if (i < 0 || length_ <= i) return Local<Value>(*Undefined());
4382 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
4383}
4384
4385
4386Local<Function> Arguments::Callee() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004387 return Local<Function>(reinterpret_cast<Function*>(
4388 &implicit_args_[kCalleeIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004389}
4390
4391
4392Local<Object> Arguments::This() const {
4393 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
4394}
4395
4396
4397Local<Object> Arguments::Holder() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004398 return Local<Object>(reinterpret_cast<Object*>(
4399 &implicit_args_[kHolderIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004400}
4401
4402
4403Local<Value> Arguments::Data() const {
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004404 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004405}
4406
4407
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004408Isolate* Arguments::GetIsolate() const {
4409 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
4410}
4411
4412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004413bool Arguments::IsConstructCall() const {
4414 return is_construct_call_;
4415}
4416
4417
4418int Arguments::Length() const {
4419 return length_;
4420}
4421
4422
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004423template <class T>
4424Local<T> HandleScope::Close(Handle<T> value) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004425 internal::Object** before = reinterpret_cast<internal::Object**>(*value);
4426 internal::Object** after = RawClose(before);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004427 return Local<T>(reinterpret_cast<T*>(after));
4428}
4429
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004430Handle<Value> ScriptOrigin::ResourceName() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004431 return resource_name_;
4432}
4433
4434
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004435Handle<Integer> ScriptOrigin::ResourceLineOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004436 return resource_line_offset_;
4437}
4438
4439
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004440Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004441 return resource_column_offset_;
4442}
4443
4444
4445Handle<Boolean> Boolean::New(bool value) {
4446 return value ? True() : False();
4447}
4448
4449
4450void Template::Set(const char* name, v8::Handle<Data> value) {
4451 Set(v8::String::New(name), value);
4452}
4453
4454
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004455Local<Value> Object::GetInternalField(int index) {
4456#ifndef V8_ENABLE_CHECKS
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004457 typedef internal::Object O;
4458 typedef internal::Internals I;
4459 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004460 // Fast path: If the object is a plain JSObject, which is the common case, we
4461 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004462 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004463 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004464 O* value = I::ReadField<O*>(obj, offset);
4465 O** result = HandleScope::CreateHandle(value);
4466 return Local<Value>(reinterpret_cast<Value*>(result));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004467 }
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004468#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004469 return SlowGetInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004470}
4471
4472
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004473void Object::SetPointerInInternalField(int index, void* value) {
4474 SetInternalField(index, External::New(value));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004475}
4476
4477
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004478void* Object::GetAlignedPointerFromInternalField(int index) {
4479#ifndef V8_ENABLE_CHECKS
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004480 typedef internal::Object O;
4481 typedef internal::Internals I;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004482 O* obj = *reinterpret_cast<O**>(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004483 // Fast path: If the object is a plain JSObject, which is the common case, we
4484 // know where to find the internal fields and can return the value directly.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004485 if (I::GetInstanceType(obj) == I::kJSObjectType) {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00004486 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004487 return I::ReadField<void*>(obj, offset);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004488 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004489#endif
4490 return SlowGetAlignedPointerFromInternalField(index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004491}
4492
4493
4494String* String::Cast(v8::Value* value) {
4495#ifdef V8_ENABLE_CHECKS
4496 CheckCast(value);
4497#endif
4498 return static_cast<String*>(value);
4499}
4500
4501
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004502Local<String> String::Empty(Isolate* isolate) {
4503 typedef internal::Object* S;
4504 typedef internal::Internals I;
4505 if (!I::IsInitialized(isolate)) return Empty();
4506 S* slot = I::GetRoot(isolate, I::kEmptySymbolRootIndex);
4507 return Local<String>(reinterpret_cast<String*>(slot));
4508}
4509
4510
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004511String::ExternalStringResource* String::GetExternalStringResource() const {
4512 typedef internal::Object O;
4513 typedef internal::Internals I;
4514 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004515 String::ExternalStringResource* result;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004516 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004517 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4518 result = reinterpret_cast<String::ExternalStringResource*>(value);
4519 } else {
4520 result = NULL;
4521 }
4522#ifdef V8_ENABLE_CHECKS
4523 VerifyExternalStringResource(result);
4524#endif
4525 return result;
4526}
4527
4528
ulan@chromium.org56c14af2012-09-20 12:51:09 +00004529String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
4530 String::Encoding* encoding_out) const {
4531 typedef internal::Object O;
4532 typedef internal::Internals I;
4533 O* obj = *reinterpret_cast<O**>(const_cast<String*>(this));
4534 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
4535 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
4536 ExternalStringResourceBase* resource = NULL;
4537 if (type == I::kExternalAsciiRepresentationTag ||
4538 type == I::kExternalTwoByteRepresentationTag) {
4539 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
4540 resource = static_cast<ExternalStringResourceBase*>(value);
4541 }
4542#ifdef V8_ENABLE_CHECKS
4543 VerifyExternalStringResourceBase(resource, *encoding_out);
4544#endif
4545 return resource;
4546}
4547
4548
svenpanne@chromium.orgfb046332012-04-19 12:02:44 +00004549bool Value::IsUndefined() const {
4550#ifdef V8_ENABLE_CHECKS
4551 return FullIsUndefined();
4552#else
4553 return QuickIsUndefined();
4554#endif
4555}
4556
4557bool Value::QuickIsUndefined() const {
4558 typedef internal::Object O;
4559 typedef internal::Internals I;
4560 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4561 if (!I::HasHeapObjectTag(obj)) return false;
4562 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4563 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
4564}
4565
4566
4567bool Value::IsNull() const {
4568#ifdef V8_ENABLE_CHECKS
4569 return FullIsNull();
4570#else
4571 return QuickIsNull();
4572#endif
4573}
4574
4575bool Value::QuickIsNull() const {
4576 typedef internal::Object O;
4577 typedef internal::Internals I;
4578 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4579 if (!I::HasHeapObjectTag(obj)) return false;
4580 if (I::GetInstanceType(obj) != I::kOddballType) return false;
4581 return (I::GetOddballKind(obj) == I::kNullOddballKind);
4582}
4583
4584
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004585bool Value::IsString() const {
4586#ifdef V8_ENABLE_CHECKS
4587 return FullIsString();
4588#else
4589 return QuickIsString();
4590#endif
4591}
4592
4593bool Value::QuickIsString() const {
4594 typedef internal::Object O;
4595 typedef internal::Internals I;
4596 O* obj = *reinterpret_cast<O**>(const_cast<Value*>(this));
4597 if (!I::HasHeapObjectTag(obj)) return false;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00004598 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004599}
4600
4601
4602Number* Number::Cast(v8::Value* value) {
4603#ifdef V8_ENABLE_CHECKS
4604 CheckCast(value);
4605#endif
4606 return static_cast<Number*>(value);
4607}
4608
4609
4610Integer* Integer::Cast(v8::Value* value) {
4611#ifdef V8_ENABLE_CHECKS
4612 CheckCast(value);
4613#endif
4614 return static_cast<Integer*>(value);
4615}
4616
4617
4618Date* Date::Cast(v8::Value* value) {
4619#ifdef V8_ENABLE_CHECKS
4620 CheckCast(value);
4621#endif
4622 return static_cast<Date*>(value);
4623}
4624
4625
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004626StringObject* StringObject::Cast(v8::Value* value) {
4627#ifdef V8_ENABLE_CHECKS
4628 CheckCast(value);
4629#endif
4630 return static_cast<StringObject*>(value);
4631}
4632
4633
4634NumberObject* NumberObject::Cast(v8::Value* value) {
4635#ifdef V8_ENABLE_CHECKS
4636 CheckCast(value);
4637#endif
4638 return static_cast<NumberObject*>(value);
4639}
4640
4641
4642BooleanObject* BooleanObject::Cast(v8::Value* value) {
4643#ifdef V8_ENABLE_CHECKS
4644 CheckCast(value);
4645#endif
4646 return static_cast<BooleanObject*>(value);
4647}
4648
4649
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004650RegExp* RegExp::Cast(v8::Value* value) {
4651#ifdef V8_ENABLE_CHECKS
4652 CheckCast(value);
4653#endif
4654 return static_cast<RegExp*>(value);
4655}
4656
4657
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004658Object* Object::Cast(v8::Value* value) {
4659#ifdef V8_ENABLE_CHECKS
4660 CheckCast(value);
4661#endif
4662 return static_cast<Object*>(value);
4663}
4664
4665
4666Array* Array::Cast(v8::Value* value) {
4667#ifdef V8_ENABLE_CHECKS
4668 CheckCast(value);
4669#endif
4670 return static_cast<Array*>(value);
4671}
4672
4673
4674Function* Function::Cast(v8::Value* value) {
4675#ifdef V8_ENABLE_CHECKS
4676 CheckCast(value);
4677#endif
4678 return static_cast<Function*>(value);
4679}
4680
4681
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004682Local<Value> External::Wrap(void* value) {
4683 return External::New(value);
4684}
4685
4686
4687void* External::Unwrap(Handle<v8::Value> obj) {
4688 return External::Cast(*obj)->Value();
4689}
4690
4691
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004692External* External::Cast(v8::Value* value) {
4693#ifdef V8_ENABLE_CHECKS
4694 CheckCast(value);
4695#endif
4696 return static_cast<External*>(value);
4697}
4698
4699
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004700Isolate* AccessorInfo::GetIsolate() const {
4701 return *reinterpret_cast<Isolate**>(&args_[-3]);
4702}
4703
4704
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004705Local<Value> AccessorInfo::Data() const {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004706 return Local<Value>(reinterpret_cast<Value*>(&args_[-2]));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00004707}
4708
4709
4710Local<Object> AccessorInfo::This() const {
4711 return Local<Object>(reinterpret_cast<Object*>(&args_[0]));
4712}
4713
4714
4715Local<Object> AccessorInfo::Holder() const {
4716 return Local<Object>(reinterpret_cast<Object*>(&args_[-1]));
4717}
4718
4719
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00004720Handle<Primitive> Undefined(Isolate* isolate) {
4721 typedef internal::Object* S;
4722 typedef internal::Internals I;
4723 if (!I::IsInitialized(isolate)) return Undefined();
4724 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
4725 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4726}
4727
4728
4729Handle<Primitive> Null(Isolate* isolate) {
4730 typedef internal::Object* S;
4731 typedef internal::Internals I;
4732 if (!I::IsInitialized(isolate)) return Null();
4733 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
4734 return Handle<Primitive>(reinterpret_cast<Primitive*>(slot));
4735}
4736
4737
4738Handle<Boolean> True(Isolate* isolate) {
4739 typedef internal::Object* S;
4740 typedef internal::Internals I;
4741 if (!I::IsInitialized(isolate)) return True();
4742 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
4743 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4744}
4745
4746
4747Handle<Boolean> False(Isolate* isolate) {
4748 typedef internal::Object* S;
4749 typedef internal::Internals I;
4750 if (!I::IsInitialized(isolate)) return False();
4751 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
4752 return Handle<Boolean>(reinterpret_cast<Boolean*>(slot));
4753}
4754
4755
4756void Isolate::SetData(void* data) {
4757 typedef internal::Internals I;
4758 I::SetEmbedderData(this, data);
4759}
4760
4761
4762void* Isolate::GetData() {
4763 typedef internal::Internals I;
4764 return I::GetEmbedderData(this);
4765}
4766
4767
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00004768Local<Value> Context::GetData() {
4769 return GetEmbedderData(0);
4770}
4771
4772void Context::SetData(Handle<Value> data) {
4773 SetEmbedderData(0, data);
4774}
4775
4776
4777Local<Value> Context::GetEmbedderData(int index) {
4778#ifndef V8_ENABLE_CHECKS
4779 typedef internal::Object O;
4780 typedef internal::Internals I;
4781 O** result = HandleScope::CreateHandle(I::ReadEmbedderData<O*>(this, index));
4782 return Local<Value>(reinterpret_cast<Value*>(result));
4783#else
4784 return SlowGetEmbedderData(index);
4785#endif
4786}
4787
4788
4789void* Context::GetAlignedPointerFromEmbedderData(int index) {
4790#ifndef V8_ENABLE_CHECKS
4791 typedef internal::Internals I;
4792 return I::ReadEmbedderData<void*>(this, index);
4793#else
4794 return SlowGetAlignedPointerFromEmbedderData(index);
4795#endif
4796}
4797
4798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004799/**
v8.team.kasperl727e9952008-09-02 14:56:44 +00004800 * \example shell.cc
4801 * A simple shell that takes a list of expressions on the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004802 * command-line and executes them.
4803 */
4804
4805
4806/**
4807 * \example process.cc
4808 */
4809
4810
4811} // namespace v8
4812
4813
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004814#undef V8EXPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004815#undef TYPE_CHECK
4816
4817
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004818#endif // V8_H_